Skip to content

Commit

Permalink
Merge pull request #118 from ConductionNL/feature/PC108-140/e-mail
Browse files Browse the repository at this point in the history
Send email on creating a task or updating the 'medewerker' field
  • Loading branch information
rjzondervan authored Dec 20, 2024
2 parents cda0a93 + c73ff16 commit 3fe9a60
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 132 deletions.
274 changes: 142 additions & 132 deletions lib/Controller/TakenController.php
Original file line number Diff line number Diff line change
@@ -1,132 +1,142 @@
<?php

namespace OCA\ZaakAfhandelApp\Controller;

use OCA\ZaakAfhandelApp\Service\ObjectService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

class TakenController extends Controller
{
public function __construct(
$appName,
IRequest $request,
private readonly ObjectService $objectService,
) {
parent::__construct($appName, $request);
}


/**
* Return (and serach) all objects
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function index(): JSONResponse
{
// Retrieve all request parameters
$requestParams = $this->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);
}
}
<?php

namespace OCA\ZaakAfhandelApp\Controller;

use OCA\ZaakAfhandelApp\Service\MailService;
use OCA\ZaakAfhandelApp\Service\ObjectService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

class TakenController extends Controller
{
public function __construct(
$appName,
IRequest $request,
private readonly MailService $mailService,
private readonly ObjectService $objectService,
) {
parent::__construct($appName, $request);
}


/**
* Return (and serach) all objects
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function index(): JSONResponse
{
// Retrieve all request parameters
$requestParams = $this->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);
}
}
64 changes: 64 additions & 0 deletions lib/Service/MailService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace OCA\ZaakAfhandelApp\Service;

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) {
return $newObject;
} else if (isset($oldObject['medewerker']) === true && $oldObject === $newObject) {
return $newObject;
}

$email = $newObject['medewerker'];

$message = $this->mailer->createMessage();
$message->setSubject('KISS: Er is een taak aan u toegewezen');
$message->setTo([$email]);
$message->setHtmlBody(body: "
<!doctype html>
<html lang='nl'>
<body>
Er is een taak aan u toegewezen. Klik
<a href='".$this->urlGenerator->getBaseUrl()."/apps/zaakafhandelapp/taken/{$newObject["id"]}'>
hier
</a>
om naar de taak te gaan.
</body>
</html>"
);

$this->mailer->send($message);

return $newObject;
}

}

0 comments on commit 3fe9a60

Please sign in to comment.