Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[api/instruments] Add endpoint that allows you to add instruments to a battery #8868

Merged
merged 10 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions modules/api/docs/LorisRESTAPI_v0.0.4-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,10 @@ Loris, or Approval has not occurred)
### 3.3 Candidate Instruments
```
GET /candidates/$CandID/$VisitLabel/instruments
POST /candidates/$CandID/$VisitLabel/instruments
```

Will return a JSON object of the form:
GET will return a JSON object of the form:

```js
{
Expand All @@ -480,7 +481,10 @@ Where the instruments array represents the instruments that were administered fo
candidate at that visit. InstrumentNames are the short names and the forms for them
SHOULD all be retrievable through the `project` portion of the API.

PUT / PATCH / POST are not currently supported for candidate instruments.
POST accepts data of the same format. Any instruments in the Instrument key not currently in the visit test battery will be added to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ask for the Meta object? Just the instruments would be enough no?

{
  "Instruments": [...]
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Meta object is generally required in all our endpoints for 1. symmetric 2. as a double check to ensure a bug didn't send it to the wrong endpoint, isn't it?

the battery.

PUT / PATCH are not currently supported for candidate instruments.

#### 3.3.1 The Candidate Instrument Data

Expand Down
65 changes: 63 additions & 2 deletions modules/api/php/endpoints/candidate/visit/instruments.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator
*/
protected function allowedMethods() : array
{
return ['GET'];
return ['GET', 'POST'];
}

/**
Expand Down Expand Up @@ -85,12 +85,18 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator
{
$pathparts = $request->getAttribute('pathparts');
$loris = $request->getAttribute("loris");
$version = $request->getAttribute("LORIS-API-Version");
if (count($pathparts) === 0) {
switch ($request->getMethod()) {
case 'GET':
return $this->_handleGET($request);

case 'POST':
return $this->_handlePOST($request);
case 'OPTIONS':
if ($version == 'v0.0.3') {
return (new \LORIS\Http\Response())
->withHeader('Allow', ['GET']);
}
return (new \LORIS\Http\Response())
->withHeader('Allow', $this->allowedMethods());

Expand Down Expand Up @@ -166,6 +172,61 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator
return $this->_cache;
}

/**
* Handle an incoming post request.
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
private function _handlePOST(ServerRequestInterface $request): ResponseInterface
{
$version = $request->getAttribute("LORIS-API-Version");
$user = $request->getAttribute("user");
$loris = $request->getAttribute("loris");

if ($version == 'v0.0.3') {
return new \LORIS\Http\Response\JSON\NotFound();
}

$requestdata = json_decode($request->getBody()->__toString(), true);
if (empty($requestdata)) {
return new \LORIS\Http\Response\JSON\BadRequest();
}
foreach ($requestdata['Instruments'] as $instrument) {
try {
$instrument = \NDB_BVL_Instrument::factory(
$loris,
$instrument,
'',
'',
true
);
if (!$instrument->_hasAccess($user)) {
return new \LORIS\Http\Response\JSON\Forbidden();
}
} catch (\NotFound $e) {
return new \LORIS\Http\Response\JSON\NotFound("Instrument does not exist");
} catch (\Exception $e) {
return new \LORIS\Http\Response\JSON\InternalServerError();
ridz1208 marked this conversation as resolved.
Show resolved Hide resolved
}
}
$battery = new \NDB_BVL_Battery();
$battery->selectBattery($this->_visit->getSessionID());
$existingBattery = $battery->getBattery();
foreach ($requestdata['Instruments'] as $instrument) {
if (in_array($instrument, $existingBattery, true)) {
continue;
}
$battery->addInstrument($loris, $instrument);
}

$view = (new \LORIS\api\Views\Visit\Instruments(
$this->_visit
))->toArray();
return new \LORIS\Http\Response\JsonResponse($view);
}

/**
* Implements the ETagCalculator interface
*
Expand Down
33 changes: 33 additions & 0 deletions modules/api/static/schema-v0.0.4-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,39 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/inline_response_200_4'
post:
tags:
- Instruments
summary: Add instruments the the visit test battery
parameters:
- name: id
in: path
description: The candidate identifier. Either a PSCID or CandID.
required: true
style: simple
explode: false
schema:
type: string
- name: visit
in: path
description: The visit label
required: true
style: simple
explode: false
schema:
type: string
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/inline_response_200_4'
responses:
'200':
description: The battery was successfully updated. Returns the new battery
content:
application/json:
schema:
$ref: '#/components/schemas/inline_response_200_4'
'/candidates/{id}/{visit}/instruments/{instrument}':
get:
tags:
Expand Down
89 changes: 89 additions & 0 deletions raisinbread/test/api/LorisApiInstrumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,95 @@ public function testGetCandidatesCandidVisitInstruments(): void
);
}

/**
* Tests the HTTP POST request for the
* endpoint /candidates/{candid}/{visit}/instruments
*
* @return void
*/
public function testPostCandidatesCandidVisitInstruments(): void
{
// Remove all instruments from this CandID.
$SessionID = $this->DB->pselectOne(
"SELECT ID FROM session WHERE Visit_label=:VL AND CandID=:Candidate",
[
'VL' => $this->visitTest,
'Candidate' => $this->candidTest
]
);
$this->DB->delete("flag", ['SessionID' => $SessionID]);

// Insert one
$json_data = [
'Meta' => [
'CandID' => $this->candidTest,
'Visit' => $this->visitTest,
],
"Instruments" => [ 'bmi' ],
];
$response = $this->client->request(
'POST',
"candidates/$this->candidTest/$this->visitTest/instruments",
[
'http_errors' => false,
'headers' => $this->headers,
'json' => $json_data,
]
);
$this->assertEquals(200, $response->getStatusCode());
// Verify the endpoint has a body
$body = $response->getBody();
$this->assertNotEmpty($body);

$instrArray = json_decode(
(string) utf8_encode(
$response->getBody()->getContents()
),
true
);

$this->assertSame(gettype($instrArray), 'array');

$this->assertArrayHasKey(
'CandID',
$instrArray['Meta']
);
$this->assertArrayHasKey(
'Visit',
$instrArray['Meta']
);
$this->assertArrayHasKey(
'0',
$instrArray['Instruments']
);
$this->assertEquals($instrArray['Instruments'], ['bmi']);

// Insert another and make sure they are both there now.
$json_data = [
'Meta' => [
'CandID' => $this->candidTest,
'Visit' => $this->visitTest,
],
"Instruments" => [ 'testtest' ],
];
$response = $this->client->request(
'POST',
"candidates/$this->candidTest/$this->visitTest/instruments",
[
'http_errors' => false,
'headers' => $this->headers,
'json' => $json_data,
]
);
$instrArray = json_decode(
(string) utf8_encode(
$response->getBody()->getContents()
),
true
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals($instrArray['Instruments'], ['bmi', 'testtest']);
}
/**
* Tests the HTTP GET request for the
* endpoint /candidates/{candid}/{visit}/instruments{instruments}
Expand Down
Loading