Skip to content
Snippets Groups Projects
Commit be05c941 authored by Pegane Nestor's avatar Pegane Nestor
Browse files

FEAT #7345 ExternalInfos webservice

parent a29b68ee
No related branches found
No related tags found
No related merge requests found
......@@ -117,6 +117,109 @@ class ResControllerTest extends TestCase
$this->assertSame('COU', $res['status']);
}
public function testUpdateExternalInfos(){
$resController = new \Resource\controllers\ResController();
// UPDATE STATUS
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
//ALL OK
$aArgs = [
'externalInfos' => [
[
'res_id' => self::$id,
'external_id' => "BB981212IIYZ",
'external_link' => "https://publik.nancy.fr/res/BB981212BB65"
]
],
'status' => "GRCSENT"
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string) $response->getBody());
$this->assertSame('success', $responseBody->success);
// EXTERNAL INFOS EMPTY AND RES ID IS NOT INTEGER
$aArgs = [
'externalInfos' => [
[
'res_id' => "res_id",
'external_id' => "",
'external_link' => ""
]
],
'status' => "GRCSENT"
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string) $response->getBody());
$this->assertSame('Bad Request', $responseBody->errors);
// DOCUMENT DOES NOT EXIST
$aArgs = [
'externalInfos' => [
[
'res_id' => 123456789,
'external_id' => "BB981212IIYZ",
'external_link' => "https://publik.nancy.fr/res/BB981212BB65"
]
],
'status' => 'GRCSENT'
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string) $response->getBody());
$this->assertSame(_DOCUMENT_NOT_FOUND, $responseBody->errors);
//MISSING EXTERNAL INFO
$aArgs = [
'externalInfos' => [
[
'res_id' => 123456789,
'external_id' => "BB981212IIYZ",
'external_link' => "https://publik.nancy.fr/res/BB981212BB65"
]
],
'status' => NULL
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string) $response->getBody());
$this->assertSame('Bad Request', $responseBody->errors);
//MISSING STATUS
$aArgs = [
'externalInfos' => NULL,
'status' => "GRCSENT"
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string) $response->getBody());
$this->assertSame('Bad Request', $responseBody->errors);
}
public function testDelete()
{
// DELETE
......
......@@ -243,6 +243,7 @@ $app->put('/res/resource/status', \Resource\controllers\ResController::class . '
$app->post('/res/list', \Resource\controllers\ResController::class . ':getList');
$app->get('/res/{resId}/lock', \Resource\controllers\ResController::class . ':isLock');
$app->get('/res/{resId}/notes/count', \Resource\controllers\ResController::class . ':getNotesCountForCurrentUserById');
$app->put('/res/externalInfos', \Resource\controllers\ResController::class . ':updateExternalInfos');
//statuses
$app->get('/statuses', \Status\controllers\StatusController::class . ':get');
......
......@@ -162,6 +162,36 @@ class ResController
}
//EXTERNAL INFOS
public function updateExternalInfos(Request $request, Response $response){
$data = $request->getParams();
if(empty($data['externalInfos'])){
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
if(empty($data['status'])){
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$externalInfos = $data['externalInfos'];
foreach($externalInfos as $mail){
$check = Validator::intType()->validate($mail['res_id']);
$check = $check && Validator::StringType()->notEmpty()->validate($mail['external_id']);
$check = $check && Validator::StringType()->notEmpty()->validate($mail['external_link']);
if(!$check){
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$document = ResModel::getById(['resId' => $mail['res_id'], 'select' => ['res_id']]);
if (empty($document)) {
return $response->withStatus(400)->withJson(['errors' => _DOCUMENT_NOT_FOUND]);
}
if (!ResController::hasRightByResId(['resId' => $document['res_id'], 'userId' => $GLOBALS['userId']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
ResModel::update(['set' => ['external_id' => $mail['external_id'] , 'external_link' => $mail['external_link'], 'status' => $data['status']], 'where' => ['res_id = ?'], 'data' => [$document['res_id']]]);
}
return $response->withJson(['success' => 'success']);
}
public function isLock(Request $request, Response $response, array $aArgs)
{
......
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