diff --git a/modules/api/php/endpoints/candidates.class.inc b/modules/api/php/endpoints/candidates.class.inc index 64ae7d6c7e2..ed56e9a7242 100644 --- a/modules/api/php/endpoints/candidates.class.inc +++ b/modules/api/php/endpoints/candidates.class.inc @@ -241,8 +241,14 @@ class Candidates extends Endpoint implements \LORIS\Middleware\ETagCalculator $pscid, $project->getId() ); - } catch (\LorisException | \InvalidArgumentException $e) { - return new \LORIS\Http\Response\JSON\BadRequest($e->getMessage()); + } catch (\ConflictException $e) { + return new \LORIS\Http\Response\JSON\Conflict( + $e->getMessage() + ); + } catch (\Exception | \LorisException | \InvalidArgumentException $e) { + return new \LORIS\Http\Response\JSON\BadRequest( + $e->getMessage() + ); } $candidate = \NDB_Factory::singleton()->candidate($candid); diff --git a/php/exceptions/ConflictException.class.inc b/php/exceptions/ConflictException.class.inc new file mode 100644 index 00000000000..0c5ef1b0bfa --- /dev/null +++ b/php/exceptions/ConflictException.class.inc @@ -0,0 +1,11 @@ +database(); $site = \Site::singleton($centerID); @@ -264,6 +265,25 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource, ); } + // check pscid uniqueness + $existing = $db->pselectOne( + 'SELECT + COUNT(*) + FROM candidate + WHERE PSCID = :v_pscid + GROUP BY + PSCID + ', + ['v_pscid' => $PSCID] + ); + + if ($existing > 0) { + throw new \ConflictException( + "PSCID must be unique", + PSCID_NOT_UNIQUE + ); + } + // check pscid structure if (!Candidate::validatePSCID( $PSCID,