Skip to content

Commit

Permalink
Merge pull request #14 from RyanNerd/fix-dupe-resident-record
Browse files Browse the repository at this point in the history
Reactivation and dupe detection for Residents
  • Loading branch information
RyanNerd committed May 21, 2021
2 parents 2dbe5d5 + e5a8f2a commit ce8b395
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/Controllers/ApiValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __invoke(Request $request, RequestHandler $handler): ResponseInt
if (!$responseBody->getIsAuthenticated()) {
// Short circuit the request by returning a response with status of 401;
$responseBody = $responseBody
->setStatus(401)
->setStatus(ResponseBody::HTTP_UNAUTHORIZED)
->setMessage('Invalid or missing API Key');
return $responseBody();
}
Expand Down
6 changes: 3 additions & 3 deletions app/Controllers/Authenticate/AuthenticatePostAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
// Is the user not found or is the password not valid?
if ($user === null || !password_verify($body['password'], $user->PasswordHash)) {
$responseBody = $responseBody
->setStatus(401)
->setStatus(ResponseBody::HTTP_UNAUTHORIZED)
->setData(null)
->setMessage('Not authorized');
return $responseBody();
Expand All @@ -69,7 +69,7 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
if (!$user->save()) {
// Save failed for some reason, so reject the request.
$responseBody = $responseBody
->setStatus(500)
->setStatus(ResponseBody::HTTP_INTERNAL_SERVER_ERROR)
->setData(null)
->setMessage('Unable to set new API_KEY');
return $responseBody();
Expand All @@ -78,7 +78,7 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
// Request is valid and authenticated!
$responseBody = $responseBody
->setIsAuthenticated()
->setStatus(200)
->setStatus(ResponseBody::HTTP_OK)
->setData([
'apiKey' => $user->API_KEY,
'organization' => $user->Organization
Expand Down
2 changes: 1 addition & 1 deletion app/Controllers/Authenticate/AuthenticatePostValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __invoke(Request $request, RequestHandler $handler): ResponseInt
// If there are any missing required, or invalid data points then we short circuit and return invalid request.
if ($responseBody->hasMissingRequiredOrInvalid()) {
$responseBody = $responseBody
->setStatus(400)
->setStatus(ResponseBody::HTTP_BAD_REQUEST)
->setMessage('Missing or invalid request');
return $responseBody();
}
Expand Down
5 changes: 2 additions & 3 deletions app/Controllers/GetActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ public function __invoke(Request $request, Response $response, array $args): Res
// If the record is not found then 404 error, otherwise status is 200.
if ($model === null) {
$data = null;
$status = 404;
$status = ResponseBody::HTTP_NOT_FOUND;
} else {
// Remove any protected fields from the response
$data = $model->toArray();
$this->sanitize($data, $model::FIELDS);

$status = 200;
$status = ResponseBody::HTTP_OK;
}

// Set the status and data of the ResponseBody
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/MedHistory/MedHistoryDeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __invoke(Request $request, Response $response, array $args): Res

// Destroy the model given the id.
if ($model->destroy($args['id']) === 1) {
$status = 200;
$status = ResponseBody::HTTP_OK;
} else {
$status = 404;
$status = ResponseBody::HTTP_NOT_FOUND;
}

// Set the status and data of the ResponseBody
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/Medicine/MedicineDeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __invoke(Request $request, Response $response, array $args): Res

// Destroy the model given the id.
if ($model->destroy($args['id']) === 1) {
$status = 200;
$status = ResponseBody::HTTP_OK;
} else {
$status = 404;
$status = ResponseBody::HTTP_NOT_FOUND;
}

// Set the status and data of the ResponseBody
Expand Down
6 changes: 3 additions & 3 deletions app/Controllers/PasswordReset/PasswordResetPostAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
// Is the user not found or is the old_password not valid or the api_key is invalid?
if ($user === null || $user->API_KEY != $body['api_key'] || !password_verify($body['old_password'], $user->PasswordHash)) {
$responseBody = $responseBody
->setStatus(401)
->setStatus(ResponseBody::HTTP_UNAUTHORIZED)
->setData(null)
->setMessage('Not authorized');
return $responseBody();
Expand All @@ -59,7 +59,7 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
if (!$user->save()) {
// Save failed for some reason, so reject the request.
$responseBody = $responseBody
->setStatus(500)
->setStatus(ResponseBody::HTTP_INTERNAL_SERVER_ERROR)
->setData(null)
->setMessage('Unable to set new password');
return $responseBody();
Expand All @@ -68,7 +68,7 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
// Request is valid and authenticated with new API_KEY
$responseBody = $responseBody
->setIsAuthenticated()
->setStatus(200)
->setStatus(ResponseBody::HTTP_OK)
->setData(['apiKey' => $user->API_KEY])
->setMessage('Password reset');
return $responseBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __invoke(Request $request, RequestHandler $handler): ResponseInt
// If there are any missing required, or invalid data points then we short circuit and return invalid request.
if ($responseBody->hasMissingRequiredOrInvalid()) {
$responseBody = $responseBody
->setStatus(400)
->setStatus(ResponseBody::HTTP_BAD_REQUEST)
->setMessage('Missing or invalid request');
return $responseBody();
}
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/Resident/ResidentDeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __invoke(Request $request, Response $response, array $args): Res

// Destroy the model given the id.
if ($model->destroy($args['id']) === 1) {
$status = 200;
$status = ResponseBody::HTTP_OK;
} else {
$status = 404;
$status = ResponseBody::HTTP_NOT_FOUND;
}

// Set the status and data of the ResponseBody
Expand Down
64 changes: 64 additions & 0 deletions app/Controllers/Resident/ResidentPostAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

namespace Willow\Controllers\Resident;

use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Willow\Controllers\WriteActionBase;
use Willow\Middleware\ResponseBody;
use Willow\Models\Resident;
use Willow\Models\ModelBase;

Expand All @@ -15,4 +19,64 @@ public function __construct(Resident $model)
{
$this->model = $model;
}

/**
* We override this checking for trashed clients to restore or to prevent duplicates
* @param Request $request
* @param Response $response
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response): ResponseInterface {
/** @var ResponseBody $responseBody */
$responseBody = $request->getAttribute('response_body');
$residentModel = clone $this->model;
$modelColumns = $residentModel::FIELDS;

// Get the request body
$parsedBody = $responseBody->getParsedRequest();

// Get the id/Id from the request
$id = $parsedBody['Id'] ?? null;

// Are we inserting a new record?
if ($id === null) {
// Force UserScope and look for existing records including trashed records
$residentModel = $residentModel->
where('UserId', '=', $responseBody->getUserId())->
where('FirstName', '=', $parsedBody['FirstName'])->
where('LastName', '=', $parsedBody['LastName'])->
where('DOB_YEAR', '=', $parsedBody['DOB_YEAR'])->
where('DOB_MONTH', '=', $parsedBody['DOB_MONTH'])->
where('DOB_DAY', '=', $parsedBody['DOB_DAY'])->
withTrashed()->
first();

// Did we get any results?
if ($residentModel !== null) {
// Is the client deactivated (trashed)?
if ($residentModel->trashed()) {
// Undelete the record
if ($residentModel->restore()) {
$data = $residentModel->toArray();
$this->sanitize($data, $modelColumns);

// Return the response as the restored record.
$responseBody = $responseBody
->setData($data)
->setStatus(ResponseBody::HTTP_OK);
return $responseBody();
}
} else {
// Prevent inserting duplicate clients
$responseBody = $responseBody
->setData(null)
->setStatus(ResponseBody::HTTP_CONFLICT)
->setMessage('Duplicates not allowed');
return $responseBody();
}
}
}

return parent::__invoke($request, $response);
}
}
6 changes: 3 additions & 3 deletions app/Controllers/RestoreActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
// Was the record successfully restored? Return the record and status of 200, otherwise return status 500;
if ($record->restore()) {
$data = $record->toArray();
$status = 200;
$status = ResponseBody::HTTP_OK;
} else {
$data = null;
$status = 500;
$status = ResponseBody::HTTP_INTERNAL_SERVER_ERROR;
$message = 'Unable to restore.';
}
} else {
$status = 404;
$status = ResponseBody::HTTP_NOT_FOUND;
$data = null;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Controllers/RestoreValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(Request $request, RequestHandler $handler): ResponseInt
// Is there an invalid or missing parameter in the request? Respond with status 400.
if ($responseBody->hasMissingRequiredOrInvalid()) {
$responseBody = $responseBody
->setStatus(400)
->setStatus(ResponseBody::HTTP_BAD_REQUEST)
->setData(null);
return $responseBody();
}
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/SearchActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
$this->sanitize($datum, $modelColumns);
}

$status = 200;
$status = ResponseBody::HTTP_OK;
} else {
$data = null;
$status = 404;
$status = ResponseBody::HTTP_NOT_FOUND;
}

$responseBody = $responseBody
Expand Down
5 changes: 3 additions & 2 deletions app/Controllers/SearchValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class SearchValidatorBase

/**
* @param Request $request
* @param RequestHandler $handler
* @param RequestHandler $handlernamespace Willow\Controllers;
* @return ResponseInterface
*/
public function __invoke(Request $request, RequestHandler $handler): ResponseInterface
Expand Down Expand Up @@ -100,7 +101,7 @@ public function __invoke(Request $request, RequestHandler $handler): ResponseInt
if ($responseBody->hasMissingRequiredOrInvalid()) {
$responseBody = $responseBody
->setData(null)
->setStatus(400);
->setStatus(ResponseBody::HTTP_BAD_REQUEST);
return $responseBody();
}

Expand Down
6 changes: 3 additions & 3 deletions app/Controllers/WriteActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __invoke(Request $request, Response $response): ResponseInterfac
if ($model === null) {
$responseBody = $responseBody
->setData(null)
->setStatus(404);
->setStatus(ResponseBody::HTTP_NOT_FOUND);
return $responseBody();
}
}
Expand Down Expand Up @@ -71,12 +71,12 @@ public function __invoke(Request $request, Response $response): ResponseInterfac

$responseBody = $responseBody
->setData($modelArray)
->setStatus(200);
->setStatus(ResponseBody::HTTP_OK);
} else {
// Unable to save for some reason so we return error status.
$responseBody = $responseBody
->setData(null)
->setStatus(500)
->setStatus(ResponseBody::HTTP_INTERNAL_SERVER_ERROR)
->setMessage('Unable to save changes to ' . $model->getTableName());
}

Expand Down
2 changes: 1 addition & 1 deletion app/Controllers/WriteValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __invoke(Request $request, RequestHandler $handler): ResponseInt
// If there are any missing or required data points then we short circuit and return invalid request.
if ($responseBody->hasMissingRequiredOrInvalid()) {
$responseBody = $responseBody
->setStatus(400)
->setStatus(ResponseBody::HTTP_BAD_REQUEST)
->setMessage('Missing or invalid request');
return $responseBody();
}
Expand Down
8 changes: 6 additions & 2 deletions app/Middleware/JsonBodyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function process(Request $request, RequestHandler $handler): Response
} else {
// Short circuit the request by returning a response with status of 400 (invalid request).
$responseBody = new ResponseBody();
$responseBody = $responseBody->setStatus(400)->setMessage('Invalid JSON');
$responseBody = $responseBody->
setStatus(ResponseBody::HTTP_BAD_REQUEST)->
setMessage('Invalid JSON');
return $responseBody();
}
} else {
Expand All @@ -40,7 +42,9 @@ public function process(Request $request, RequestHandler $handler): Response
if ($method === 'POST' || $method === 'PATCH') {
// Short circuit the request by returning a response with status of 400 (invalid request).
$responseBody = new ResponseBody();
$responseBody = $responseBody->setStatus(400)->setMessage("Invalid Content-Type: $contentType");
$responseBody = $responseBody->
setStatus(ResponseBody::HTTP_BAD_REQUEST)->
setMessage("Invalid Content-Type: $contentType");
return $responseBody();
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Middleware/ResponseBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Response;

class ResponseBody
class ResponseBody extends ResponseCodes
{
protected ?array $parsedRequest = null;
protected bool $isAuthenticated = false;
Expand Down Expand Up @@ -132,7 +132,7 @@ public function getUserId(): int
{
return $this->userId;
}
/**
/** trashed records
* Returns true if there are missing or required datapoints in the request
*
* @return bool
Expand Down

0 comments on commit ce8b395

Please sign in to comment.