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

FEAT #12509 TIME 0:30 Enable/suspend contact

parent 3d64b21d
No related branches found
No related tags found
No related merge requests found
...@@ -117,6 +117,7 @@ $app->post('/contacts', \Contact\controllers\ContactController::class . ':create ...@@ -117,6 +117,7 @@ $app->post('/contacts', \Contact\controllers\ContactController::class . ':create
$app->get('/contacts/{id}', \Contact\controllers\ContactController::class . ':getById'); $app->get('/contacts/{id}', \Contact\controllers\ContactController::class . ':getById');
$app->put('/contacts/{id}', \Contact\controllers\ContactController::class . ':update'); $app->put('/contacts/{id}', \Contact\controllers\ContactController::class . ':update');
$app->delete('/contacts/{id}', \Contact\controllers\ContactController::class . ':delete'); $app->delete('/contacts/{id}', \Contact\controllers\ContactController::class . ':delete');
$app->put('/contacts/{id}/activation', \Contact\controllers\ContactController::class . ':updateActivation');
$app->get('/contacts/{contactId}/communication', \Contact\controllers\ContactController::class . ':getCommunicationByContactId'); $app->get('/contacts/{contactId}/communication', \Contact\controllers\ContactController::class . ':getCommunicationByContactId');
$app->get('/contactsGroups', \Contact\controllers\ContactGroupController::class . ':get'); $app->get('/contactsGroups', \Contact\controllers\ContactGroupController::class . ':get');
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
/** /**
* @brief Contact Controller * @brief Contact Controller
*
* @author dev@maarch.org * @author dev@maarch.org
*/ */
...@@ -43,11 +42,12 @@ class ContactController ...@@ -43,11 +42,12 @@ class ContactController
//TODO privileges //TODO privileges
$body = $request->getParsedBody(); $body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['lastname']) && !Validator::stringType()->notEmpty()->validate($body['company'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body lastname or company is mandatory']); $control = ContactController::controlContact(['body' => $body]);
if (!empty($control['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
} }
$body['email'] = filter_var($body['email'], FILTER_VALIDATE_EMAIL) ? $body['email'] : null;
if (!empty($body['email'])) { if (!empty($body['email'])) {
$contact = ContactModel::get(['select' => ['id'], 'where' => ['email = ?'], 'data' => [$body['email']]]); $contact = ContactModel::get(['select' => ['id'], 'where' => ['email = ?'], 'data' => [$body['email']]]);
if (!empty($contact[0]['id'])) { if (!empty($contact[0]['id'])) {
...@@ -133,18 +133,18 @@ class ContactController ...@@ -133,18 +133,18 @@ class ContactController
{ {
//TODO privileges //TODO privileges
$body = $request->getParsedBody();
$control = ContactController::controlContact(['body' => $body]);
if (!empty($control['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
}
$contact = ContactModel::getById(['id' => $args['id'], 'select' => [1]]); $contact = ContactModel::getById(['id' => $args['id'], 'select' => [1]]);
if (empty($contact)) { if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']); return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
} }
$body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['lastname']) && !Validator::stringType()->notEmpty()->validate($body['company'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body lastname or company is mandatory']);
}
$body['email'] = filter_var($body['email'], FILTER_VALIDATE_EMAIL) ? $body['email'] : null;
if (!empty($body['communicationMeans'])) { if (!empty($body['communicationMeans'])) {
if (filter_var($body['communicationMeans'], FILTER_VALIDATE_EMAIL)) { if (filter_var($body['communicationMeans'], FILTER_VALIDATE_EMAIL)) {
$body['communicationMeans'] = ['email' => $body['communicationMeans']]; $body['communicationMeans'] = ['email' => $body['communicationMeans']];
...@@ -166,11 +166,11 @@ class ContactController ...@@ -166,11 +166,11 @@ class ContactController
'company' => $body['company'] ?? null, 'company' => $body['company'] ?? null,
'department' => $body['department'] ?? null, 'department' => $body['department'] ?? null,
'function' => $body['function'] ?? null, 'function' => $body['function'] ?? null,
'address_number' => $body['addressNumber'] ?? null, 'address_number' => $body['addressNumber'] ?? null,
'address_street' => $body['addressStreet'] ?? null, 'address_street' => $body['addressStreet'] ?? null,
'address_postcode' => $body['addressPostcode'] ?? null, 'address_postcode' => $body['addressPostcode'] ?? null,
'address_town' => $body['addressTown'] ?? null, 'address_town' => $body['addressTown'] ?? null,
'address_country' => $body['addressCountry'] ?? null, 'address_country' => $body['addressCountry'] ?? null,
'email' => $body['email'] ?? null, 'email' => $body['email'] ?? null,
'phone' => $body['phone'] ?? null, 'phone' => $body['phone'] ?? null,
'communication_means' => !empty($body['communicationMeans']) ? json_encode($body['communicationMeans']) : null, 'communication_means' => !empty($body['communicationMeans']) ? json_encode($body['communicationMeans']) : null,
...@@ -185,6 +185,26 @@ class ContactController ...@@ -185,6 +185,26 @@ class ContactController
return $response->withStatus(204); return $response->withStatus(204);
} }
public function updateActivation(Request $request, Response $response, array $args)
{
//TODO privileges
$contact = ContactModel::getById(['id' => $args['id'], 'select' => [1]]);
if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
$body = $request->getParsedBody();
ContactModel::update([
'set' => ['enabled' => empty($body['enabled']) ? 'false' : 'true'],
'where' => ['id = ?'],
'data' => [$args['id']]
]);
return $response->withStatus(204);
}
public function delete(Request $request, Response $response, array $args) public function delete(Request $request, Response $response, array $args)
{ {
//TODO privileges //TODO privileges
...@@ -662,4 +682,43 @@ class ContactController ...@@ -662,4 +682,43 @@ class ContactController
return $contacts; return $contacts;
} }
private static function controlContact(array $args)
{
$body = $args['body'];
if (empty($body)) {
return ['errors' => 'Body is not set or empty'];
} elseif (!Validator::stringType()->notEmpty()->validate($body['lastname']) && !Validator::stringType()->notEmpty()->validate($body['company'])) {
return ['errors' => 'Body lastname or company is mandatory'];
} elseif (!empty($body['email']) && !filter_var($body['email'], FILTER_VALIDATE_EMAIL)) {
return ['errors' => 'Body email is not valid'];
} elseif (!empty($body['phone']) && !preg_match("/\+?((|\ |\.|\(|\)|\-)?(\d)*)*\d$/", $body['phone'])) {
return ['errors' => 'Body phone is not valid'];
}
$lengthFields = [
'civility',
'firstname',
'lastname',
'company',
'department',
'function',
'addressNumber',
'addressStreet',
'addressPostcode',
'addressTown',
'addressCountry',
'email',
'phone'
];
foreach ($lengthFields as $field) {
if (!empty($body[$field]) && !Validator::stringType()->length(1, 256)->validate($body[$field])) {
return ['errors' => "Body {$field} length is not valid (1..256)"];
}
}
return true;
}
} }
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