-
-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathUpdateEventEmailTemplateAction.php
More file actions
77 lines (68 loc) · 2.82 KB
/
UpdateEventEmailTemplateAction.php
File metadata and controls
77 lines (68 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace HiEvents\Http\Actions\EmailTemplates;
use HiEvents\DomainObjects\Enums\EmailTemplateType;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\Exceptions\AccountNotVerifiedException;
use HiEvents\Exceptions\EmailTemplateNotFoundException;
use HiEvents\Exceptions\EmailTemplateValidationException;
use HiEvents\Exceptions\InvalidEmailTemplateException;
use HiEvents\Http\Resources\EmailTemplateResource;
use HiEvents\Http\ResponseCodes;
use HiEvents\Services\Application\Handlers\EmailTemplate\DTO\UpsertEmailTemplateDTO;
use HiEvents\Services\Application\Handlers\EmailTemplate\UpdateEmailTemplateHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Response;
class UpdateEventEmailTemplateAction extends BaseEmailTemplateAction
{
public function __construct(
private readonly UpdateEmailTemplateHandler $handler
) {}
/**
* @throws ValidationException
*/
public function __invoke(Request $request, int $eventId, int $templateId): JsonResponse
{
$this->isActionAuthorized($eventId, EventDomainObject::class);
try {
$this->verifyAccountCanModifyEmailTemplates();
} catch (AccountNotVerifiedException $e) {
return $this->errorResponse($e->getMessage(), Response::HTTP_UNAUTHORIZED);
}
$validated = $this->validateUpdateEmailTemplateRequest($request);
try {
$cta = [
'label' => $validated['ctaLabel'],
];
$template = $this->handler->handle(
new UpsertEmailTemplateDTO(
account_id: $this->getAuthenticatedAccountId(),
template_type: EmailTemplateType::ORDER_CONFIRMATION, // This will be ignored in update
subject: $validated['subject'],
body: $validated['body'],
organizer_id: null,
event_id: $eventId,
id: $templateId,
cta: $cta,
is_active: $validated['isActive'] ?? true,
)
);
} catch (EmailTemplateValidationException $e) {
throw ValidationException::withMessages($e->validationErrors ?: ['body' => $e->getMessage()]);
} catch (InvalidEmailTemplateException $e) {
throw ValidationException::withMessages([
'id' => $e->getMessage(),
]);
} catch (EmailTemplateNotFoundException $e) {
return $this->errorResponse(
message: $e->getMessage(),
statusCode: ResponseCodes::HTTP_NOT_FOUND,
);
}
return $this->resourceResponse(
resource: EmailTemplateResource::class,
data: $template,
);
}
}