From 3399b2cc017203e89e453cdda654b9abd7a2c231 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 21 Feb 2023 13:12:45 +0200 Subject: [PATCH] email template api endpoint --- .../Espo/Controllers/EmailTemplate.php | 43 +----------- application/Espo/Resources/routes.json | 5 ++ .../Tools/EmailTemplate/Api/PostPrepare.php | 68 +++++++++++++++++++ .../Espo/Tools/EmailTemplate/Service.php | 21 ++---- .../src/views/email/fields/select-template.js | 3 +- 5 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 application/Espo/Tools/EmailTemplate/Api/PostPrepare.php diff --git a/application/Espo/Controllers/EmailTemplate.php b/application/Espo/Controllers/EmailTemplate.php index 7f2b29815a..24f1cd5c06 100644 --- a/application/Espo/Controllers/EmailTemplate.php +++ b/application/Espo/Controllers/EmailTemplate.php @@ -29,48 +29,7 @@ namespace Espo\Controllers; -use Espo\Core\Exceptions\BadRequest; - -use Espo\Core\Exceptions\ForbiddenSilent; -use Espo\Core\Exceptions\NotFound; -use Espo\Tools\EmailTemplate\Data; -use Espo\Tools\EmailTemplate\Service; - -use Espo\Core\Api\Request; use Espo\Core\Controllers\Record; -use stdClass; - class EmailTemplate extends Record -{ - /** - * @throws BadRequest - * @throws NotFound - * @throws ForbiddenSilent - */ - public function actionParse(Request $request): stdClass - { - $id = $request->getQueryParam('id'); - - if ($id === null) { - throw new BadRequest("No `id`."); - } - - $data = Data::create() - ->withRelatedType($request->getQueryParam('relatedType')) - ->withRelatedId($request->getQueryParam('relatedId')) - ->withParentType($request->getQueryParam('parentType')) - ->withParentId($request->getQueryParam('parentId')) - ->withEmailAddress($request->getQueryParam('emailAddress')); - - $result = $this->getEmailTemplateService()->process($id, $data); - - return $result->getValueMap(); - - } - - private function getEmailTemplateService(): Service - { - return $this->injectableFactory->create(Service::class); - } -} +{} diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json index f8e607e40c..aaf7819230 100644 --- a/application/Espo/Resources/routes.json +++ b/application/Espo/Resources/routes.json @@ -314,6 +314,11 @@ "method": "post", "actionClassName": "Espo\\Tools\\Attachment\\Api\\PostCopy" }, + { + "route": "/EmailTemplate/:id/prepare", + "method": "post", + "actionClassName": "Espo\\Tools\\EmailTemplate\\Api\\PostPrepare" + }, { "route": "/Email/:id/attachments/copy", "method": "post", diff --git a/application/Espo/Tools/EmailTemplate/Api/PostPrepare.php b/application/Espo/Tools/EmailTemplate/Api/PostPrepare.php new file mode 100644 index 0000000000..b0393a82f4 --- /dev/null +++ b/application/Espo/Tools/EmailTemplate/Api/PostPrepare.php @@ -0,0 +1,68 @@ +getRouteParam('id'); + + if ($id === null) { + throw new BadRequest(); + } + + $body = $request->getParsedBody(); + + $data = Data::create() + ->withRelatedType($body->relatedType ?? null) + ->withRelatedId($body->relatedId ?? null) + ->withParentType($body->parentType ?? null) + ->withParentId($body->parentId ?? null) + ->withEmailAddress($body->emailAddress ?? null); + + $result = $this->service->process($id, $data); + + return ResponseComposer::json($result->getValueMap()); + } +} diff --git a/application/Espo/Tools/EmailTemplate/Service.php b/application/Espo/Tools/EmailTemplate/Service.php index 0afe6125fb..c3304132ef 100644 --- a/application/Espo/Tools/EmailTemplate/Service.php +++ b/application/Espo/Tools/EmailTemplate/Service.php @@ -32,29 +32,18 @@ namespace Espo\Tools\EmailTemplate; use Espo\Core\Acl; use Espo\Core\Exceptions\ForbiddenSilent; use Espo\Core\Exceptions\NotFound; -use Espo\Core\Record\ServiceContainer; use Espo\Entities\EmailTemplate; use Espo\Entities\User; use Espo\ORM\EntityManager; class Service { - private Processor $processor; - private User $user; - private Acl $acl; - private EntityManager $entityManager; - public function __construct( - Processor $processor, - User $user, - Acl $acl, - EntityManager $entityManager - ) { - $this->processor = $processor; - $this->user = $user; - $this->acl = $acl; - $this->entityManager = $entityManager; - } + private Processor $processor, + private User $user, + private Acl $acl, + private EntityManager $entityManager + ) {} /** * Prepare an email data with an applied template. diff --git a/client/src/views/email/fields/select-template.js b/client/src/views/email/fields/select-template.js index 67004a2e02..84715ef26b 100644 --- a/client/src/views/email/fields/select-template.js +++ b/client/src/views/email/fields/select-template.js @@ -63,8 +63,7 @@ define('views/email/fields/select-template', ['views/fields/link'], function (De } Espo.Ajax - .getRequest('EmailTemplate/action/parse', { - id: id, + .postRequest(`EmailTemplate/${id}/prepare`, { emailAddress: emailAddress, parentType: this.model.get('parentType'), parentId: this.model.get('parentId'),