Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Upgraded Medication Resource #3790

Merged
merged 7 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added Standard Drug Endpoint
Signed-off-by: Yash Bothra <yashrajbothra786@gmail.com>
  • Loading branch information
yashrajbothra committed Aug 3, 2020
commit ab307c45d3575b418d0b971cd6681a1c033c0428
9 changes: 9 additions & 0 deletions _rest_routes.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OpenEMR\RestControllers\ConditionRestController;
use OpenEMR\RestControllers\ONoteRestController;
use OpenEMR\RestControllers\DocumentRestController;
use OpenEMR\RestControllers\DrugRestController;
use OpenEMR\RestControllers\ImmunizationRestController;
use OpenEMR\RestControllers\InsuranceRestController;
use OpenEMR\RestControllers\MessageRestController;
Expand Down Expand Up @@ -383,6 +384,14 @@
RestConfig::authorization_check("patients", "med");
return (new ProcedureRestController())->getOne($uuid);
},
"GET /api/drug" => function () {
RestConfig::authorization_check("patients", "med");
return (new DrugRestController())->getAll();
},
"GET /api/drug/:uuid" => function ($uuid) {
RestConfig::authorization_check("patients", "med");
return (new DrugRestController())->getOne($uuid);
},

);

Expand Down
51 changes: 51 additions & 0 deletions src/RestControllers/DrugRestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* DrugRestController
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <matthewvita48@gmail.com>
* @author Yash Bothra <yashrajbothra786gmail.com>
* @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

namespace OpenEMR\RestControllers;

use OpenEMR\Services\DrugService;
use OpenEMR\RestControllers\RestControllerHelper;

class DrugRestController
{
private $drugService;

public function __construct()
{
$this->drugService = new DrugService();
}

/**
* Fetches a single drug resource by id.
* @param $uuid- The drug uuid identifier in string format.
*/
public function getOne($uuid)
{
$processingResult = $this->drugService->getOne($uuid);

if (!$processingResult->hasErrors() && count($processingResult->getData()) == 0) {
return RestControllerHelper::handleProcessingResult($processingResult, 404);
}

return RestControllerHelper::handleProcessingResult($processingResult, 200);
}

/**
* Returns drug resources which match an optional search criteria.
*/
public function getAll($search = array())
{
$processingResult = $this->drugService->getAll($search);
return RestControllerHelper::handleProcessingResult($processingResult, 200, true);
}
}
147 changes: 147 additions & 0 deletions src/Services/DrugService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

/**
* DrugService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Yash Bothra <yashrajbothra786gmail.com>
* @copyright Copyright (c) 2020 Yash Bothra <yashrajbothra786gmail.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

namespace OpenEMR\Services;

use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Validators\BaseValidator;
use OpenEMR\Validators\ProcessingResult;

class DrugService extends BaseService
{

private const DRUG_TABLE = "drugs";
private $uuidRegistry;

/**
* Default constructor.
*/
public function __construct()
{
parent::__construct(self::DRUG_TABLE);
$this->uuidRegistry = new UuidRegistry([
'table_name' => self::DRUG_TABLE,
'table_id' => 'drug_id'
]);
$this->uuidRegistry->createMissingUuids();
}

/**
* Returns a list of drugs matching optional search criteria.
* Search criteria is conveyed by array where key = field/column name, value = field value.
* If no search criteria is provided, all records are returned.
*
* @param $search search array parameters
* @param $isAndCondition specifies if AND condition is used for multiple criteria. Defaults to true.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getAll($search = array(), $isAndCondition = true, $codeRequired = false)
{
$sqlBindArray = array();

$sql = "SELECT drugs.drug_id,
uuid,
name,
ndc_number,
form,
size,
unit,
route,
related_code,
active,
drug_code,
drug_inventory.manufacturer,
drug_inventory.lot_number,
drug_inventory.expiration
FROM drugs
LEFT JOIN drug_inventory
ON drugs.drug_id = drug_inventory.drug_id";

if (!empty($search)) {
$sql .= ' WHERE ';
$whereClauses = array();
foreach ($search as $fieldName => $fieldValue) {
array_push($whereClauses, $fieldName . ' = ?');
array_push($sqlBindArray, $fieldValue);
}
$sqlCondition = ($isAndCondition == true) ? 'AND' : 'OR';
$sql .= implode(' ' . $sqlCondition . ' ', $whereClauses);
}

$statementResults = sqlStatement($sql, $sqlBindArray);

$processingResult = new ProcessingResult();
while ($row = sqlFetchArray($statementResults)) {
if (!$codeRequired || $row['related_code'] != "") {
$row['uuid'] = UuidRegistry::uuidToString($row['uuid']);
if ($row['related_code'] != "") {
$row['related_code'] = $this->addCoding($row['related_code']);
}
$processingResult->addData($row);
}
}

return $processingResult;
}

/**
* Returns a single drug record by id.
* @param $uuid - The drug uuid identifier in string format.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getOne($uuid, $codeRequired = false)
{
$processingResult = new ProcessingResult();

$isValid = BaseValidator::validateId("uuid", self::DRUG_TABLE, $uuid, true);
if ($isValid !== true) {
$validationMessages = [
'uuid' => ["invalid or nonexisting value" => " value " . $uuid]
];
$processingResult->setValidationMessages($validationMessages);
return $processingResult;
}

$sql = "SELECT drugs.drug_id,
uuid,
name,
ndc_number,
form,
size,
unit,
route,
related_code,
active,
drug_code,
drug_inventory.manufacturer,
drug_inventory.lot_number,
drug_inventory.expiration
FROM drugs
LEFT JOIN drug_inventory
ON drugs.drug_id = drug_inventory.drug_id
WHERE drugs.uuid = ?";

$uuidBinary = UuidRegistry::uuidToBytes($uuid);
$sqlResult = sqlQuery($sql, [$uuidBinary]);
if (!$codeRequired || $sqlResult['related_code'] != "") {
$sqlResult['uuid'] = UuidRegistry::uuidToString($sqlResult['uuid']);
if ($sqlResult['related_code'] != "") {
$sqlResult['related_code'] = $this->addCoding($sqlResult['related_code']);
}
$processingResult->addData($sqlResult);
}

return $processingResult;
}
}