emailService->getAllTemplates($organisation); return $this->success($templates); } public function show(Organisation $organisation, string $type): JsonResponse { Gate::authorize('update', $organisation); $templateType = $this->resolveType($type); $template = $this->emailService->resolveTemplate($templateType, $organisation); $template['type'] = $templateType->value; $template['label'] = $templateType->label(); $template['defaults'] = $templateType->defaults(); return $this->success($template); } public function update(UpdateEmailTemplateRequest $request, Organisation $organisation, string $type): JsonResponse { Gate::authorize('update', $organisation); $templateType = $this->resolveType($type); $template = OrganisationEmailTemplate::updateOrCreate( [ 'organisation_id' => $organisation->id, 'type' => $templateType->value, ], $request->validated(), ); activity('email_template') ->performedOn($template) ->causedBy($request->user()) ->withProperties(['type' => $templateType->value]) ->log('email_template.updated'); $result = $this->emailService->resolveTemplate($templateType, $organisation); $result['type'] = $templateType->value; $result['label'] = $templateType->label(); $result['defaults'] = $templateType->defaults(); return $this->success($result); } public function destroy(Organisation $organisation, string $type): JsonResponse { Gate::authorize('update', $organisation); $templateType = $this->resolveType($type); OrganisationEmailTemplate::where('organisation_id', $organisation->id) ->where('type', $templateType->value) ->delete(); activity('email_template') ->causedBy(request()->user()) ->withProperties(['type' => $templateType->value]) ->log('email_template.reset_to_default'); return $this->success(message: 'Template reset naar standaard.'); } public function preview(Organisation $organisation, string $type): JsonResponse { Gate::authorize('update', $organisation); $templateType = $this->resolveType($type); $sampleVariables = [ 'organisation_name' => $organisation->name, 'event_name' => 'Voorbeeldevenement', 'shift_title' => 'Bar medewerker', 'shift_date' => '15 juni 2026', 'shift_start' => '14:00', 'shift_end' => '22:00', 'section_name' => 'Hoofdpodium Bar', ]; $template = $this->emailService->resolveTemplate($templateType, $organisation); // Substitute sample variables foreach ($template as $key => $value) { if (is_string($value)) { foreach ($sampleVariables as $var => $replacement) { $value = str_replace('{' . $var . '}', $replacement, $value); } $template[$key] = $value; } } $branding = $this->emailService->resolveBranding($organisation); $html = View::make('emails.transactional', [ 'heading' => $template['heading'], 'bodyText' => $template['body_text'], 'buttonText' => $template['button_text'], 'actionUrl' => 'https://crewli.app/example', 'logoUrl' => $branding['logo_url'], 'primaryColor' => $branding['primary_color'], 'secondaryColor' => $branding['secondary_color'], 'footerText' => $branding['footer_text'], ])->render(); return $this->success(['html' => $html]); } public function sendTest(Request $request, Organisation $organisation, string $type): JsonResponse { Gate::authorize('update', $organisation); $request->validate([ 'email' => ['required', 'email'], ]); $templateType = $this->resolveType($type); $sampleVariables = [ 'organisation_name' => $organisation->name, 'event_name' => 'Voorbeeldevenement', 'shift_title' => 'Bar medewerker', 'shift_date' => '15 juni 2026', 'shift_start' => '14:00', 'shift_end' => '22:00', 'section_name' => 'Hoofdpodium Bar', ]; $this->emailService->send( type: $templateType, recipientEmail: $request->input('email'), recipientName: 'Test Ontvanger', variables: $sampleVariables, actionUrl: 'https://crewli.app/example', organisation: $organisation, triggeredByUserId: $request->user()->id, ); activity('email_template') ->causedBy($request->user()) ->withProperties([ 'type' => $templateType->value, 'test_email' => $request->input('email'), ]) ->log('email.test_sent'); return $this->success(message: 'Testmail verzonden naar ' . $request->input('email') . '.'); } private function resolveType(string $type): EmailTemplateType { $templateType = EmailTemplateType::tryFrom($type); if (! $templateType) { abort(404, 'Onbekend template type.'); } return $templateType; } }