Skip to content
Snippets Groups Projects
Verified Commit 9a449c25 authored by Damien's avatar Damien
Browse files

FEAT #11693 TIME 0:35 Refactor email controller

parent f7a60951
No related branches found
No related tags found
No related merge requests found
...@@ -50,8 +50,7 @@ class EmailController ...@@ -50,8 +50,7 @@ class EmailController
$body = $request->getParsedBody(); $body = $request->getParsedBody();
$user = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); $isSent = EmailController::createEmail(['userId' => $GLOBALS['id'], 'data' => $body]);
$isSent = EmailController::createEmail(['userId' => $user['id'], 'data' => $body]);
if (!empty($isSent['errors'])) { if (!empty($isSent['errors'])) {
$httpCode = empty($isSent['code']) ? 400 : $isSent['code']; $httpCode = empty($isSent['code']) ? 400 : $isSent['code'];
...@@ -117,16 +116,13 @@ class EmailController ...@@ -117,16 +116,13 @@ class EmailController
return $isSent; return $isSent;
} }
public static function getById(array $args) public function getById(Request $request, Response $response, array $args)
{ {
ValidatorModel::notEmpty($args, ['id', 'userId']);
ValidatorModel::intVal($args, ['id', 'userId']);
$emailArray = EmailModel::getById(['id' => $args['id']]); $emailArray = EmailModel::getById(['id' => $args['id']]);
$document = (array)json_decode($emailArray['document']); $document = (array)json_decode($emailArray['document']);
if (!ResController::hasRightByResId(['resId' => [$document['id']], 'userId' => $args['userId']])) { if (!ResController::hasRightByResId(['resId' => [$document['id']], 'userId' => $GLOBALS['id']])) {
return ['errors' => 'Document out of perimeter', 'code' => 403]; return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
} }
$sender = (array)json_decode($emailArray['sender']); $sender = (array)json_decode($emailArray['sender']);
...@@ -137,7 +133,7 @@ class EmailController ...@@ -137,7 +133,7 @@ class EmailController
$email['resId'] = $document['id']; $email['resId'] = $document['id'];
$user = UserModel::getById(['id' => $emailArray['user_id'], 'select' => ['user_id']]); $user = UserModel::getById(['id' => $emailArray['user_id'], 'select' => ['user_id']]);
$email['userId'] = $user['user_id']; $email['login'] = $user['user_id'];
$email['attachments'] = []; $email['attachments'] = [];
$email['attachments_version'] = []; $email['attachments_version'] = [];
...@@ -171,55 +167,89 @@ class EmailController ...@@ -171,55 +167,89 @@ class EmailController
$email['sender_email'] = $sender['email']; $email['sender_email'] = $sender['email'];
} }
return $email; return $response->withJson($email);
} }
public static function update(array $args) public function update(Request $request, Response $response, array $args)
{ {
ValidatorModel::notEmpty($args, ['userId', 'data', 'emailId']); $body = $request->getParsedBody();
ValidatorModel::intVal($args, ['userId', 'emailId']);
ValidatorModel::arrayType($args, ['data', 'options']);
$check = EmailController::controlCreateEmail(['userId' => $args['userId'], 'data' => $args['data']]); $check = EmailController::controlCreateEmail(['userId' => $GLOBALS['id'], 'data' => $body]);
if (!empty($check['errors'])) { if (!empty($check['errors'])) {
return ['errors' => $check['errors'], 'code' => $check['code']]; return $response->withStatus($check['code'])->withJson(['errors' => $check['errors']]);
} }
EmailModel::update([ EmailModel::update([
'set' => [ 'set' => [
'sender' => json_encode($args['data']['sender']), 'sender' => json_encode($body['sender']),
'recipients' => json_encode($args['data']['recipients']), 'recipients' => json_encode($body['recipients']),
'cc' => empty($args['data']['cc']) ? '[]' : json_encode($args['data']['cc']), 'cc' => empty($body['cc']) ? '[]' : json_encode($body['cc']),
'cci' => empty($args['data']['cci']) ? '[]' : json_encode($args['data']['cci']), 'cci' => empty($body['cci']) ? '[]' : json_encode($body['cci']),
'object' => empty($args['data']['object']) ? null : $args['data']['object'], 'object' => empty($body['object']) ? null : $body['object'],
'body' => empty($args['data']['body']) ? null : $args['data']['body'], 'body' => empty($body['body']) ? null : $body['body'],
'document' => empty($args['data']['document']) ? null : json_encode($args['data']['document']), 'document' => empty($body['document']) ? null : json_encode($body['document']),
'is_html' => $args['data']['isHtml'] ? 'true' : 'false', 'is_html' => $body['isHtml'] ? 'true' : 'false',
'status' => $args['data']['status'] == 'DRAFT' ? 'DRAFT' : 'WAITING' 'status' => $body['status'] == 'DRAFT' ? 'DRAFT' : 'WAITING'
], ],
'where' => ['id = ?'], 'where' => ['id = ?'],
'data' => [$args['emailId']] 'data' => [$args['id']]
]); ]);
HistoryController::add([ HistoryController::add([
'tableName' => 'emails', 'tableName' => 'emails',
'recordId' => $args['emailId'], 'recordId' => $args['id'],
'eventType' => 'UP', 'eventType' => 'UP',
'eventId' => 'emailModification', 'eventId' => 'emailModification',
'info' => _EMAIL_UPDATED 'info' => _EMAIL_UPDATED
]); ]);
if ($args['data']['status'] != 'DRAFT') { if ($body['status'] != 'DRAFT') {
$customId = CoreConfigModel::getCustomId(); $customId = CoreConfigModel::getCustomId();
if (empty($customId)) { if (empty($customId)) {
$customId = 'null'; $customId = 'null';
} }
$encryptKey = CoreConfigModel::getEncryptKey(); $encryptKey = CoreConfigModel::getEncryptKey();
$options = empty($args['options']) ? '' : serialize($args['options']); exec("php src/app/email/scripts/sendEmail.php {$customId} {$args['id']} {$GLOBALS['id']} '{$encryptKey}' > /dev/null &");
exec("php src/app/email/scripts/sendEmail.php {$customId} {$args['emailId']} {$args['userId']} '{$encryptKey}' '{$options}' > /dev/null &"); }
return $response->withStatus(204);
}
public function delete(Request $request, Response $response, array $args)
{
$email = EmailModel::getById(['select' => ['user_id'], 'id' => $args['id']]);
if (empty($email)) {
return $response->withStatus(400)->withJson(['errors' => 'Email does not exist']);
}
if ($email['user_id'] != $GLOBALS['id']) {
return $response->withStatus(403)->withJson(['errors' => 'Email out of perimeter']);
}
EmailModel::delete([
'where' => ['id = ?'],
'data' => [$args['id']]
]);
HistoryController::add([
'tableName' => 'emails',
'recordId' => $args['id'],
'eventType' => 'DEL',
'eventId' => 'emailDeletion',
'info' => _EMAIL_REMOVED
]);
return $response->withStatus(204);
}
public function getByResId(Request $request, Response $response, array $args)
{
if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
} }
return true; $emails = EmailModel::get(['select' => ['*'], 'where' => ['document->>\'id\' = ?'], 'data' => [$args['resId']]]);
return $response->withJson(['emails' => $emails]);
} }
public static function sendEmail(array $args) public static function sendEmail(array $args)
...@@ -488,32 +518,4 @@ class EmailController ...@@ -488,32 +518,4 @@ class EmailController
return ['success' => 'success']; return ['success' => 'success'];
} }
public static function delete(Request $request, Response $response, array $args)
{
$user = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
$email = EmailModel::getById(['select' => ['user_id'], 'id' => $args['id']]);
if (empty($email)) {
return $response->withStatus(400)->withJson(['errors' => 'Email does not exist']);
}
if ($email['user_id'] != $user['id']) {
return $response->withStatus(403)->withJson(['errors' => 'Email out of perimeter']);
}
EmailModel::delete([
'where' => ['id = ?'],
'data' => [$args['id']]
]);
HistoryController::add([
'tableName' => 'emails',
'recordId' => $args['id'],
'eventType' => 'DEL',
'eventId' => 'emailDeletion',
'info' => _EMAIL_REMOVED
]);
return $response->withStatus(204);
}
} }
...@@ -19,6 +19,24 @@ use SrcCore\models\ValidatorModel; ...@@ -19,6 +19,24 @@ use SrcCore\models\ValidatorModel;
class EmailModel class EmailModel
{ {
public static function get(array $args = [])
{
ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy']);
ValidatorModel::intType($args, ['limit', 'offset']);
$emails = DatabaseModel::select([
'select' => empty($args['select']) ? ['*'] : $args['select'],
'table' => ['emails'],
'where' => empty($args['where']) ? [] : $args['where'],
'data' => empty($args['data']) ? [] : $args['data'],
'order_by' => empty($args['orderBy']) ? [] : $args['orderBy'],
'offset' => empty($args['offset']) ? 0 : $args['offset'],
'limit' => empty($args['limit']) ? 0 : $args['limit']
]);
return $emails;
}
public static function getById(array $aArgs) public static function getById(array $aArgs)
{ {
ValidatorModel::notEmpty($aArgs, ['id']); ValidatorModel::notEmpty($aArgs, ['id']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment