Skip to content

Commit

Permalink
feat: add dicom convert to fhir API
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Jan 3, 2024
1 parent 432ce6b commit 8d9adb3
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
38 changes: 38 additions & 0 deletions api/fhir-convert/controller/dicom-to-fhir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { ApiErrorArrayHandler } = require("@error/api-errors.handler");
const { Controller } = require("@root/api/controller.class");
const { FhirConvertService } = require("./service/fhir-convert.service");
const { ApiLogger } = require("@root/utils/logs/api-logger");


class FhirConvertController extends Controller {
constructor(req, res) {
super(req, res);
this.apiLogger = new ApiLogger(req, "fhir-convert");
this.apiLogger.addTokenValue();
}

async mainProcess() {
let fhirConvertService = new FhirConvertService(this.request, this.response);
try {
let fhirJson = await fhirConvertService.convert();
return this.response
.set("content-type", "application/json")
.status(200)
.json(fhirJson);
} catch (e) {
let apiErrorArrayHandler = new ApiErrorArrayHandler(this.response, this.apiLogger, e);
return apiErrorArrayHandler.doErrorResponse();
}
}
}

/**
*
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
module.exports = async function (req, res) {
let controller = new FhirConvertController(req, res);

await controller.doPipeline();
};
38 changes: 38 additions & 0 deletions api/fhir-convert/controller/service/fhir-convert.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { DicomJsonToFhir } = require("dicomjson-to-fhir");
const dicomToJson = require("../dicom-to-fhir");
const { dcm2jsonV8 } = require("@models/DICOM/dcmtk");
const { raccoonConfig } = require("@root/config-class");
const Joi = require("joi");
const { DicomWebServiceError, DicomWebStatusCodes } = require("@error/dicom-web-service");

const fileSchema = Joi.object({
files: Joi.object({
file: Joi.object({
filepath: Joi.string().required()
}).required()
}).required()
});

class FhirConvertService {
constructor(req, res) {
this.request = req;
this.response = res;
}

async convert() {
let { value, error } = fileSchema.validate(this.request, { allowUnknown : true});
if (error) {
throw new DicomWebServiceError(DicomWebStatusCodes.InvalidArgumentValue, error.details[0].message, 400);
}
let dicomJson = await dcm2jsonV8.exec(this.request.files.file.filepath);
let protocol = this.request.secure ? "https" : "http";
let dicomJsonToFhir = new DicomJsonToFhir(
dicomJson,
"raccoon-dicom-web-server",
`${protocol}://${this.request.headers.host}/${raccoonConfig.dicomWebConfig.apiPath}/studies`
);
return dicomJsonToFhir.getFhirJson();
}
}

module.exports.FhirConvertService = FhirConvertService;
54 changes: 54 additions & 0 deletions api/fhir-convert/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Route /fhir-convert
* Implement `DICOM convert to FHIR ImagingStudy, Patient, Endpoint`
*
* @author Chin-Lin Lee <[email protected]>
*/

const Joi = require("joi");
const { validateByJoi } = require("../validator");
const express = require("express");
const router = express.Router();
const formidable = require("formidable");

const formMiddleWare = async (req, res, next) => {
const form = formidable({});

form.parse(req, (err, fields, files) => {
if (err) {
next(err);
return;
}
req.fields = fields;
req.files = files;
next();
});
};

/**
* @openapi
* /fhir-convert:
* post:
* tags:
* - fhir-convert
* description: Convert DICOM to FHIR ImagingStudy, Patient, Endpoint
* requestBody:
* content:
* multipart/form-data:
* schema:
* type: object
* properties:
* file:
* type: string
* format: binary
* encoding:
* file:
* contentType: application/dicom;
* responses:
* "200":
* description: The DICOM instance store successfully
*/
router.post("/", formMiddleWare, require("./controller/dicom-to-fhir"));


module.exports = router;
40 changes: 39 additions & 1 deletion docs/swagger/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@
"content": {
"application/dicom+json": {
"schema": {
"$ref": "#/components/schemas/PatientRequiredMatchingAttributes"
"type": "object",
"properties": {
"patientID": {
"type": "string"
}
}
}
}
}
Expand Down Expand Up @@ -1298,6 +1303,39 @@
}
}
},
"/fhir-convert": {
"post": {
"tags": [
"fhir-convert"
],
"description": "Convert DICOM to FHIR ImagingStudy, Patient, Endpoint",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
}
},
"encoding": {
"file": {
"contentType": "application/dicom;"
}
}
}
}
},
"responses": {
"200": {
"description": "The DICOM instance store successfully"
}
}
}
},
"/wado": {
"get": {
"tags": [
Expand Down
2 changes: 2 additions & 0 deletions routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ module.exports = function (app) {
app.use("/dicom-web", require("./api/dicom-web/pam-rs.route"));

app.use("/wado", require("./api/WADO-URI"));

app.use("/fhir-convert", require("./api/fhir-convert"));
};

0 comments on commit 8d9adb3

Please sign in to comment.