Skip to content

Commit

Permalink
Upgraded Medication Resource (#3790)
Browse files Browse the repository at this point in the history
* Added Support to UUID

* Changed addDiagnosis to addCode

Signed-off-by: Yash Bothra <yashrajbothra786@gmail.com>

* Added Standard Drug Endpoint

Signed-off-by: Yash Bothra <yashrajbothra786@gmail.com>

* Updated FHIR Medication Endpoints

Signed-off-by: Yash Bothra <yashrajbothra786@gmail.com>

* Update API Docs

Signed-off-by: Yash Bothra <yashrajbothra786@gmail.com>

* Added UUID to database.sql

Signed-off-by: Yash Bothra <yashrajbothra786@gmail.com>

* Fixes

Signed-off-by: Yash Bothra <yashrajbothra786@gmail.com>
  • Loading branch information
yashrajbothra authored Aug 5, 2020
1 parent 4185fec commit bf4d7bc
Show file tree
Hide file tree
Showing 12 changed files with 413 additions and 149 deletions.
18 changes: 18 additions & 0 deletions API_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Finally, APIs which are integrated with the new `handleProcessingResult` method
- [Immunization API](API_README.md#get-apiimmunization)
- [Allergy API](API_README.md#get-apiallergy)
- [Procedure API](API_README.md#get-apiprocedure)
- [Drug API](API_README.md#get-apidrug)
- [Insurance API](API_README.md#get-apipatientpidinsurance)
- [Appointment API](API_README.md#get-apiappointment)
- [Document API](API_README.md#get-apipatientpiddocument)
Expand All @@ -78,6 +79,7 @@ Finally, APIs which are integrated with the new `handleProcessingResult` method
- [FHIR Condition API](FHIR_README.md#get-fhircondition)
- [FHIR MedicationStatement API](FHIR_README.md#get-fhirmedicationstatement)
- [FHIR Medication API](FHIR_README.md#get-fhirmedication)
- [FHIR MedicationStatement API](FHIR_README.md#get-fhirmedicationstatement)
- [Portal FHIR API Endpoints](FHIR_README.md#portalfhir-endpoints)
- [Patient API](FHIR_README.md#get-portalfhirpatient)
- [Dev notes](API_README.md#dev-notes)
Expand Down Expand Up @@ -929,6 +931,22 @@ Request:
curl -X GET 'http://localhost:8300/apis/api/procedure/90c196f2-51cc-4655-8858-3a80aebff3ef'
```

#### GET /api/drug

Request:

```sh
curl -X GET 'http://localhost:8300/apis/api/drug'
```

#### GET /api/drug/:uuid

Request:

```sh
curl -X GET 'http://localhost:8300/apis/api/drug/90c196f2-51cc-4655-8858-3a80aebff3ef'
```

#### POST /api/patient/:pid/medication

Request:
Expand Down
5 changes: 3 additions & 2 deletions FHIR_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Database Result -> Service Component -> FHIR Service Component -> Parse OpenEMR
- [FHIR Condition API](FHIR_README.md#get-fhircondition)
- [FHIR MedicationStatement API](FHIR_README.md#get-fhirmedicationstatement)
- [FHIR Medication API](FHIR_README.md#get-fhirmedication)
- [FHIR MedicationStatement API](FHIR_README.md#get-fhirmedicationstatement)
- [Portal FHIR API Endpoints](FHIR_README.md#portalfhir-endpoints)
- [Patient API](FHIR_README.md#get-portalfhirpatient)
- [Todos](FHIR_README.md#project-management)
Expand Down Expand Up @@ -477,12 +478,12 @@ Request:
curl -X GET 'http://localhost:8300/apis/fhir/Medication'
```

#### GET /fhir/Medication/:id
#### GET /fhir/Medication/:uuid

Request:

```sh
curl -X GET 'http://localhost:8300/apis/fhir/Medication/1'
curl -X GET 'http://localhost:8300/apis/fhir/Medication/9109890a-6756-44c1-a82d-bdfac91c7424'
```

### /portalfhir/ Endpoints
Expand Down
11 changes: 10 additions & 1 deletion _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 Expand Up @@ -535,7 +544,7 @@
},
"GET /fhir/Medication" => function () {
RestConfig::authorization_check("patients", "med");
return (new FhirMedicationRestController(null))->getAll();
return (new FhirMedicationRestController())->getAll($_GET);
},
"GET /fhir/Medication/:id" => function ($id) {
RestConfig::authorization_check("patients", "med");
Expand Down
11 changes: 11 additions & 0 deletions sql/5_0_2-to-6_0_0_upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1973,3 +1973,14 @@ UPDATE `openemr_postcalendar_categories` SET `pc_catcolor`='#ced4da' WHERE `pc_c
UPDATE `openemr_postcalendar_categories` SET `pc_catcolor`='#d3c6ec' WHERE `pc_constant_id`='preventive_care_services' AND `pc_catcolor`='#CCCCFF';
UPDATE `openemr_postcalendar_categories` SET `pc_catcolor`='#febe89' WHERE `pc_constant_id`='ophthalmological_services' AND `pc_catcolor`='#F89219';
UPDATE `openemr_postcalendar_categories` SET `pc_catcolor`='#adb5bd' WHERE `pc_constant_id`='group_therapy' AND `pc_catcolor`='#BFBFBF';

#IfMissingColumn drugs uuid
ALTER TABLE `drugs` ADD `uuid` binary(16) DEFAULT NULL;
#EndIf

#IfUuidNeedUpdateId drugs drug_id
#EndIf

#IfNotIndex drugs uuid
CREATE UNIQUE INDEX `uuid` ON `drugs` (`uuid`);
#EndIf
2 changes: 2 additions & 0 deletions sql/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ CREATE TABLE `drug_templates` (
DROP TABLE IF EXISTS `drugs`;
CREATE TABLE `drugs` (
`drug_id` int(11) NOT NULL auto_increment,
`drugs` ADD `uuid` binary(16) DEFAULT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
`ndc_number` varchar(20) NOT NULL DEFAULT '',
`on_order` int(11) NOT NULL default '0',
Expand All @@ -1357,6 +1358,7 @@ CREATE TABLE `drugs` (
`consumable` tinyint(1) NOT NULL DEFAULT 0 COMMENT '1 = will not show on the fee sheet',
`dispensable` tinyint(1) NOT NULL DEFAULT 1 COMMENT '0 = pharmacy elsewhere, 1 = dispensed here',
PRIMARY KEY (`drug_id`)
UNIQUE INDEX `uuid` ON `drugs` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=1;

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

/**
* DrugRestController
*
* @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\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);
}
}
90 changes: 41 additions & 49 deletions src/RestControllers/FHIR/FhirMedicationRestController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?php

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

namespace OpenEMR\RestControllers\FHIR;

use OpenEMR\Services\FHIR\FhirValidationService;
use OpenEMR\Services\FHIR\FhirMedicationService;
use OpenEMR\Services\FHIR\FhirResourcesService;
use OpenEMR\RestControllers\RestControllerHelper;
Expand All @@ -12,61 +21,44 @@ class FhirMedicationRestController
{
private $fhirMedicationService;
private $fhirService;
private $fhirValidationService;

public function __construct($id)

public function __construct()
{
$this->fhirMedicationService = new FhirMedicationService();
$this->fhirMedicationService->setId($id);
$this->fhirService = new FhirResourcesService();
$this->fhirValidationService = new FhirValidationService();
}

public function getAll()

/**
* Queries for a single FHIR medication resource by FHIR id
* @param $fhirId The FHIR medication resource id (uuid)
* @returns 200 if the operation completes successfully
*/
public function getOne($fhirId)
{
$result = $this->fhirMedicationService->getAll();
if ($result === false) {
http_response_code(404);
exit;
}
$entries = array();
$resourceURL = \RestConfig::$REST_FULL_URL;
foreach ($result as $condition) {
$entryResource = $this->fhirMedicationService->createMedicationResource(
$condition['id'],
$condition,
false
);
$entry = array(
'fullUrl' => $resourceURL . "/" . $condition['id'],
'resource' => $entryResource
);
$entries[] = new FHIRBundleEntry($entry);
}
$result = $this->fhirService->createBundle('Medication', $entries, false);
return RestControllerHelper::responseHandler($result, null, 200);
$processingResult = $this->fhirMedicationService->getOne($fhirId);
return RestControllerHelper::handleProcessingResult($processingResult, 200);
}

public function getOne($id)

/**
* Queries for FHIR medication resources using various search parameters.
* Search parameters include:
* - patient (puuid)
* @return FHIR bundle with query results, if found
*/
public function getAll($searchParams)
{
$result = $this->fhirMedicationService->getOne($id);
if ($result) {
$resource = $this->fhirMedicationService->createMedicationResource(
$result['id'],
$result,
false
);
$statusCode = 200;
} else {
$statusCode = 404;
$resource = $this->fhirValidationService->operationOutcomeResourceService(
'error',
'invalid',
false,
"Resource Id $id does not exist"
);
$processingResult = $this->fhirMedicationService->getAll($searchParams);
$bundleEntries = array();
foreach ($processingResult->getData() as $index => $searchResult) {
$bundleEntry = [
'fullUrl' => \RestConfig::$REST_FULL_URL . '/' . $searchResult->getId(),
'resource' => $searchResult
];
$fhirBundleEntry = new FHIRBundleEntry($bundleEntry);
array_push($bundleEntries, $fhirBundleEntry);
}

return RestControllerHelper::responseHandler($resource, null, $statusCode);
$bundleSearchResult = $this->fhirService->createBundle('Medication', $bundleEntries, false);
$searchResponseBody = RestControllerHelper::responseHandler($bundleSearchResult, null, 200);
return $searchResponseBody;
}
}
4 changes: 2 additions & 2 deletions src/Services/AllergyIntoleranceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getAll($search = array(), $isAndCondition = true)
UuidRegistry::uuidToString($row['practitioner']) :
$row['practitioner'];
if ($row['diagnosis'] != "") {
$row['diagnosis'] = $this->addDiagnosis($row['diagnosis']);
$row['diagnosis'] = $this->addCoding($row['diagnosis']);
}
$processingResult->addData($row);
}
Expand Down Expand Up @@ -160,7 +160,7 @@ public function getOne($uuid)
UuidRegistry::uuidToString($sqlResult['practitioner']) :
$sqlResult['practitioner'];
if ($sqlResult['diagnosis'] != "") {
$row['diagnosis'] = $this->addDiagnosis($sqlResult['diagnosis']);
$row['diagnosis'] = $this->addCoding($sqlResult['diagnosis']);
}
$processingResult->addData($sqlResult);
return $processingResult;
Expand Down
2 changes: 1 addition & 1 deletion src/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function ($key) use ($whitelistedFields) {
* @param string $diagnosis - All Diagnosis Codes
* @return array Array of Code as Key and Description as Value
*/
protected function addDiagnosis($diagnosis)
protected function addCoding($diagnosis)
{
$diags = explode(";", $diagnosis);
$diagnosis = array();
Expand Down
4 changes: 2 additions & 2 deletions src/Services/ConditionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getAll($search = array(), $isAndCondition = true)
$row['uuid'] = UuidRegistry::uuidToString($row['uuid']);
$row['puuid'] = UuidRegistry::uuidToString($row['puuid']);
if ($row['diagnosis'] != "") {
$row['diagnosis'] = $this->addDiagnosis($row['diagnosis']);
$row['diagnosis'] = $this->addCoding($row['diagnosis']);
}
$processingResult->addData($row);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public function getOne($uuid)
$sqlResult['uuid'] = UuidRegistry::uuidToString($sqlResult['uuid']);
$sqlResult['puuid'] = UuidRegistry::uuidToString($sqlResult['puuid']);
if ($sqlResult['diagnosis'] != "") {
$row['diagnosis'] = $this->addDiagnosis($sqlResult['diagnosis']);
$row['diagnosis'] = $this->addCoding($sqlResult['diagnosis']);
}
$processingResult->addData($sqlResult);
return $processingResult;
Expand Down
Loading

0 comments on commit bf4d7bc

Please sign in to comment.