Skip to content

Commit c521659

Browse files
authored
[Core] PSCID uniqueness (#8411)
Add PSCID uniqueness check to Candidate::createNew.
1 parent 12cbd16 commit c521659

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

modules/api/php/endpoints/candidates.class.inc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,14 @@ class Candidates extends Endpoint implements \LORIS\Middleware\ETagCalculator
241241
$pscid,
242242
$project->getId()
243243
);
244-
} catch (\LorisException | \InvalidArgumentException $e) {
245-
return new \LORIS\Http\Response\JSON\BadRequest($e->getMessage());
244+
} catch (\ConflictException $e) {
245+
return new \LORIS\Http\Response\JSON\Conflict(
246+
$e->getMessage()
247+
);
248+
} catch (\Exception | \LorisException | \InvalidArgumentException $e) {
249+
return new \LORIS\Http\Response\JSON\BadRequest(
250+
$e->getMessage()
251+
);
246252
}
247253

248254
$candidate = \NDB_Factory::singleton()->candidate($candid);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* This file contains the Conflict exception type.
4+
*
5+
* PHP Version 7
6+
*
7+
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
8+
*/
9+
class ConflictException extends LorisException
10+
{
11+
}

php/libraries/Candidate.class.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource,
223223
ProjectID $registrationProjectID = null
224224
): CandID {
225225
$factory = NDB_Factory::singleton();
226+
$db = $factory->database();
226227

227228
$site = \Site::singleton($centerID);
228229

@@ -264,6 +265,25 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource,
264265
);
265266
}
266267

268+
// check pscid uniqueness
269+
$existing = $db->pselectOne(
270+
'SELECT
271+
COUNT(*)
272+
FROM candidate
273+
WHERE PSCID = :v_pscid
274+
GROUP BY
275+
PSCID
276+
',
277+
['v_pscid' => $PSCID]
278+
);
279+
280+
if ($existing > 0) {
281+
throw new \ConflictException(
282+
"PSCID must be unique",
283+
PSCID_NOT_UNIQUE
284+
);
285+
}
286+
267287
// check pscid structure
268288
if (!Candidate::validatePSCID(
269289
$PSCID,

0 commit comments

Comments
 (0)