From b619fe05d6ac9dddf3ead790f5332d4d130c98be Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Wed, 3 Oct 2018 15:40:33 +0200 Subject: [PATCH] FEAT #8264 Contact update + address Rest --- rest/index.php | 3 + .../contact/controllers/ContactController.php | 100 ++++++++++++++++++ .../contact/models/ContactModelAbstract.php | 19 +++- .../resource/controllers/ResController.php | 2 +- 4 files changed, 121 insertions(+), 3 deletions(-) diff --git a/rest/index.php b/rest/index.php index 744a10834e7..d382129c760 100755 --- a/rest/index.php +++ b/rest/index.php @@ -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'); diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php index e3cc212bb85..3608fecebc5 100644 --- a/src/app/contact/controllers/ContactController.php +++ b/src/app/contact/controllers/ContactController.php @@ -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([ diff --git a/src/app/contact/models/ContactModelAbstract.php b/src/app/contact/models/ContactModelAbstract.php index 122ff9a27f6..d0b6e58f2da 100644 --- a/src/app/contact/models/ContactModelAbstract.php +++ b/src/app/contact/models/ContactModelAbstract.php @@ -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' - ] ]); diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php index ee5f8af67a8..d0c0261d915 100755 --- a/src/app/resource/controllers/ResController.php +++ b/src/app/resource/controllers/ResController.php @@ -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) { -- GitLab