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

FEAT #8264 Contact update + address Rest

parent 281ce868
No related branches found
No related tags found
No related merge requests found
......@@ -92,6 +92,9 @@ $app->get('/batchHistories', \History\controllers\BatchHistoryController::class
//Contacts
$app->post('/contacts', \Contact\controllers\ContactController::class . ':create');
$app->put('/contacts/{id}', \Contact\controllers\ContactController::class . ':update');
$app->post('/contacts/{id}/addresses', \Contact\controllers\ContactController::class . ':createAddress');
$app->put('/contacts/{id}/addresses/{addressId}', \Contact\controllers\ContactController::class . ':updateAddress');
$app->get('/contacts/{contactId}/communication', \Contact\controllers\ContactController::class . ':getCommunicationByContactId');
$app->get('/contactsGroups', \Contact\controllers\ContactGroupController::class . ':get');
$app->post('/contactsGroups', \Contact\controllers\ContactGroupController::class . ':create');
......
......@@ -15,6 +15,7 @@
namespace Contact\controllers;
use Contact\models\ContactModel;
use Group\models\ServiceModel;
use SrcCore\models\CoreConfigModel;
use Respect\Validation\Validator;
use Slim\Http\Request;
......@@ -25,6 +26,13 @@ class ContactController
{
public function create(Request $request, Response $response)
{
if (!ServiceModel::hasService(['id' => 'admin_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin']) &&
!ServiceModel::hasService(['id' => 'my_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'use']) &&
!ServiceModel::hasService(['id' => 'my_contacts_menu', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu']) &&
!ServiceModel::hasService(['id' => 'create_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$data = $request->getParams();
$check = Validator::notEmpty()->validate($data['firstname']);
......@@ -76,6 +84,98 @@ class ContactController
return $response->withJson(['contactId' => $contactId, 'addressId' => $addressId]);
}
public function createAddress(Request $request, Response $response, array $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin']) &&
!ServiceModel::hasService(['id' => 'my_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'use']) &&
!ServiceModel::hasService(['id' => 'update_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'use']) &&
!ServiceModel::hasService(['id' => 'my_contacts_menu', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu']) &&
!ServiceModel::hasService(['id' => 'create_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$contact = ContactModel::getById(['id' => $aArgs['id'], 'select' => [1]]);
if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
$data = $request->getParams();
$check = Validator::intVal()->notEmpty()->validate($data['contactPurposeId']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['email']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
if (empty($data['userId'])) {
$data['userId'] = 'superadmin';
}
if (empty($data['entityId'])) {
$data['entityId'] = 'SUPERADMIN';
}
$data['addressFirstname'] = $data['firstname'];
$data['addressLastname'] = $data['lastname'];
$data['addressTitle'] = $data['title'];
$data['addressFunction'] = $data['function'];
unset($data['firstname'], $data['lastname'], $data['title'], $data['function']);
if (empty($data['isPrivate'])) {
$data['isPrivate'] = 'N';
} elseif ($data['isPrivate'] != 'N') {
$data['isPrivate'] = 'Y';
}
$data['contactId'] = $aArgs['id'];
$addressId = ContactModel::createAddress($data);
return $response->withJson(['addressId' => $addressId]);
}
public function update(Request $request, Response $response, array $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin']) &&
!ServiceModel::hasService(['id' => 'update_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'use']) &&
!ServiceModel::hasService(['id' => 'my_contacts_menu', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu']) &&
!ServiceModel::hasService(['id' => 'create_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$contact = ContactModel::getById(['id' => $aArgs['id'], 'select' => [1]]);
if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
$data = $request->getParams();
unset($data['contact_id'], $data['user_id']);
ContactModel::update(['set' => $data, 'where' => ['contact_id = ?'], 'data' => [$aArgs['id']]]);
return $response->withJson(['success' => 'success']);
}
public function updateAddress(Request $request, Response $response, array $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin']) &&
!ServiceModel::hasService(['id' => 'update_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'use']) &&
!ServiceModel::hasService(['id' => 'my_contacts_menu', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu']) &&
!ServiceModel::hasService(['id' => 'create_contacts', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$contact = ContactModel::getById(['id' => $aArgs['id'], 'select' => [1]]);
$address = ContactModel::getByAddressId(['addressId' => $aArgs['addressId'], 'select' => [1]]);
if (empty($contact) || empty($address)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact or address do not exist']);
}
$data = $request->getParams();
unset($data['contact_id'], $data['id'], $data['user_id']);
ContactModel::updateAddress(['set' => $data, 'where' => ['contact_id = ?', 'id = ?'], 'data' => [$aArgs['id'], $aArgs['addressId']]]);
return $response->withJson(['success' => 'success']);
}
public function getCommunicationByContactId(Request $request, Response $response, array $aArgs)
{
$contact = ContactModel::getCommunicationByContactId([
......
......@@ -92,6 +92,21 @@ abstract class ContactModelAbstract
return $nextSequenceId;
}
public static function update(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']);
ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']);
DatabaseModel::update([
'table' => 'contacts_v2',
'set' => $aArgs['set'],
'where' => $aArgs['where'],
'data' => $aArgs['data']
]);
return true;
}
public static function createAddress(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['contactId', 'contactPurposeId', 'userId', 'entityId', 'isPrivate', 'email']);
......@@ -99,7 +114,7 @@ abstract class ContactModelAbstract
ValidatorModel::stringType($aArgs, [
'departement', 'addressFirstname', 'addressLastname', 'addressTitle', 'addressFunction', 'occupancy', 'addressNum', 'addressStreet', 'addressComplement',
'addressTown', 'addressZip', 'addressCountry', 'phone', 'email', 'website', 'salutationHeader', 'salutationFooter', 'addressOtherData',
'userId', 'entityId', 'isPrivate'
'userId', 'entityId', 'isPrivate', 'external_contact_id'
]);
$nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'contact_addresses_id_seq']);
......@@ -130,9 +145,9 @@ abstract class ContactModelAbstract
'other_data' => $aArgs['otherData'],
'user_id' => $aArgs['userId'],
'entity_id' => $aArgs['entityId'],
'external_contact_id' => $aArgs['external_contact_id'],
'is_private' => $aArgs['isPrivate'],
'enabled' => 'Y'
]
]);
......
......@@ -79,7 +79,7 @@ class ResController
$mandatoryColumns = [];
if ($data['table'] == 'res_letterbox') {
array_push($mandatoryColumns, 'type_id');
$mandatoryColumns[] = 'type_id';
}
foreach ($data['data'] as $value) {
......
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