From d34e57e0a5c22935e1b17a19d2f3d2870b7596a6 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 20 Dec 2024 14:16:31 +0100 Subject: [PATCH 1/2] Send email on creating a task or updating the 'medewerker' field --- lib/Controller/TakenController.php | 274 +++++++++++++++-------------- lib/Service/MailService.php | 47 +++++ 2 files changed, 189 insertions(+), 132 deletions(-) create mode 100644 lib/Service/MailService.php diff --git a/lib/Controller/TakenController.php b/lib/Controller/TakenController.php index 3308b31..fed3e4d 100644 --- a/lib/Controller/TakenController.php +++ b/lib/Controller/TakenController.php @@ -1,132 +1,142 @@ -request->getParams(); - - // Fetch catalog objects based on filters and order - $data = $this->objectService->getResultArrayForRequest('taken', $requestParams); - - // Return JSON response - return new JSONResponse($data); - } - - /** - * Read a single object - * - * @NoAdminRequired - * @NoCSRFRequired - * - * @return JSONResponse - */ - public function show(string $id): JSONResponse - { - // Fetch the catalog object by its ID - $object = $this->objectService->getObject('taken', $id); - - // Return the catalog as a JSON response - return new JSONResponse($object); - } - - - /** - * Creatue an object - * - * @NoAdminRequired - * @NoCSRFRequired - * - * @return JSONResponse - */ - public function create(): JSONResponse - { - // Get all parameters from the request - $data = $this->request->getParams(); - - // Remove the 'id' field if it exists, as we're creating a new object - unset($data['id']); - - // Save the new catalog object - $object = $this->objectService->saveObject('taken', $data); - - // Return the created object as a JSON response - return new JSONResponse($object); - } - - /** - * Update an object - * - * @NoAdminRequired - * @NoCSRFRequired - * - * @return JSONResponse - */ - public function update(string $id): JSONResponse - { - // Get all parameters from the request - $data = $this->request->getParams(); - - // Save the new catalog object - $object = $this->objectService->saveObject('taken', $data); - - // Return the created object as a JSON response - return new JSONResponse($object); - } - - /** - * Delate an object - * - * @NoAdminRequired - * @NoCSRFRequired - * - * @return JSONResponse - */ - public function destroy(string $id): JSONResponse - { - // Delete the catalog object - $result = $this->objectService->deleteObject('taken', $id); - - // Return the result as a JSON response - return new JSONResponse(['success' => $result], $result === true ? '200' : '404'); - } - - /** - * Get audit trail for a specific klant - * - * @NoAdminRequired - * @NoCSRFRequired - * - * @return JSONResponse - */ - public function getAuditTrail(string $id): JSONResponse - { - $auditTrail = $this->objectService->getAuditTrail('taken', $id); - return new JSONResponse($auditTrail); - } -} +request->getParams(); + + // Fetch catalog objects based on filters and order + $data = $this->objectService->getResultArrayForRequest('taken', $requestParams); + + // Return JSON response + return new JSONResponse($data); + } + + /** + * Read a single object + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function show(string $id): JSONResponse + { + // Fetch the catalog object by its ID + $object = $this->objectService->getObject('taken', $id); + + // Return the catalog as a JSON response + return new JSONResponse($object); + } + + + /** + * Creatue an object + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function create(): JSONResponse + { + // Get all parameters from the request + $data = $this->request->getParams(); + + // Remove the 'id' field if it exists, as we're creating a new object + unset($data['id']); + + // Save the new catalog object + $object = $this->objectService->saveObject('taken', $data); + + $this->mailService->sendMail([], is_array($object) === true ? $object : $object->jsonSerialize()); + + // Return the created object as a JSON response + return new JSONResponse($object); + } + + /** + * Update an object + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function update(string $id): JSONResponse + { + // Get all parameters from the request + $data = $this->request->getParams(); + + $oldObject = $this->objectService->getObject('taken', $id); + + $data['id'] = $id; + + // Save the new catalog object + $object = $this->objectService->saveObject('taken', $data); + + $this->mailService->sendMail(is_array($oldObject) === true ? $oldObject : $oldObject->jsonSerialize(), is_array($object) === true ? $object : $object->jsonSerialize()); + + // Return the created object as a JSON response + return new JSONResponse($object); + } + + /** + * Delate an object + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function destroy(string $id): JSONResponse + { + // Delete the catalog object + $result = $this->objectService->deleteObject('taken', $id); + + // Return the result as a JSON response + return new JSONResponse(['success' => $result], $result === true ? '200' : '404'); + } + + /** + * Get audit trail for a specific klant + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function getAuditTrail(string $id): JSONResponse + { + $auditTrail = $this->objectService->getAuditTrail('taken', $id); + return new JSONResponse($auditTrail); + } +} diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php new file mode 100644 index 0000000..03386fd --- /dev/null +++ b/lib/Service/MailService.php @@ -0,0 +1,47 @@ +mailer->createMessage(); + $message->setSubject('KISS: Er is een taak aan u toegewezen'); + $message->setTo([$email]); + $message->setHtmlBody(body: " + + + + Er is een taak aan u toegewezen. Klik + + hier + + om naar de taak te gaan. + + " + ); + + $this->mailer->send($message); + + return $newObject; + } + +} From c73ff1636c4f14cb2bb8436dbf5f0642dc00d210 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 20 Dec 2024 14:59:03 +0100 Subject: [PATCH 2/2] Add docblocks --- lib/Service/MailService.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index 03386fd..f990a22 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -5,14 +5,31 @@ use OCP\IURLGenerator; use OCP\Mail\IMailer; +/** + * Service class for sending e-mails + * + * This service sends e-mails when an 'medewerker' field is filled on an object. + */ class MailService { + /** + * Constructor for MailService. + */ public function __construct( private readonly IMailer $mailer, private readonly IURLGenerator $urlGenerator, ) { } + /** + * Sends an e-mail when a task is connected to an employee. + * + * @param array $oldObject The previous version of the object (to check if the field changes) + * @param array $newObject The current version of the object. + * + * @return array The current version of the object. + * @throws \Exception + */ public function sendMail(array $oldObject, array $newObject): array { if(isset($newObject['medewerker']) === false) {