Skip to content

Commit

Permalink
feat: add update workitem API
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed May 22, 2023
1 parent eb69a59 commit 6d921bb
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 6 deletions.
132 changes: 132 additions & 0 deletions api/dicom-web/controller/UPS-RS/service/update-workItem.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const _ = require("lodash");
const workItemModel = require("@models/mongodb/models/workItems");
const patientModel = require("@models/mongodb/models/patient");
const { UIDUtils } = require("@dcm4che/util/UIDUtils");
const {
DicomWebServiceError,
DicomWebStatusCodes
} = require("@error/dicom-web-service");
const { DicomJsonModel } = require("@models/DICOM/dicom-json-model");


const notAllowedAttributes = [
"00080016",
"00080018",
"00100010",
"00100020",
"00100030",
"00100040",
"00380010",
"00380014",
"00081080",
"00081084",
"0040A370",
"00741224",
"00741000"
];

class UpdateWorkItemService {
/**
*
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
constructor(req, res) {
this.request = req;
this.response = res;
this.requestWorkItem = /** @type {Object[]} */(this.request.body).pop();
/** @type {DicomJsonModel} */
this.requestWorkItem = new DicomJsonModel(this.requestWorkItem);
this.workItem = null;
this.transactionUID = null;
}

async updateUps() {
this.transactionUID = this.requestWorkItem.getString("00081195");
await this.findOneWorkItem();
await this.checkRequestUpsIsValid();
this.adjustRequestWorkItem();

await workItemModel.findOneAndUpdate({
upsInstanceUID: this.workItem.dicomJson.upsInstanceUID
}, {
...this.requestWorkItem.dicomJson
});
}

async findOneWorkItem() {

let workItem = await workItemModel.findOne({
upsInstanceUID: this.request.params.workItem
});

if (!workItem) {
throw new DicomWebServiceError(
DicomWebStatusCodes.UPSDoesNotExist,
"The UPS instance not exist",
404
);
}

this.workItem = new DicomJsonModel(workItem);

}

checkRequestUpsIsValid() {
let procedureState = this.workItem.getString("00741000");

const mappingMethod = {
"SCHEDULED": () => {
if (this.transactionUID) {
throw new DicomWebServiceError(
DicomWebStatusCodes.UPSNotYetInProgress,
"The request should not contain transaction UID of the UPS instance state is SCHEDULED",
400
);
}
},
"IN PROGRESS": () => {
let foundUpsTransactionUID = this.workItem.getString("00741000");
if (!this.transactionUID) {
throw new DicomWebServiceError(
DicomWebStatusCodes.UPSTransactionUIDNotCorrect,
"The transaction UID is missing when request UPS instance state is IN_PROGRESS",
400
);
} else if (foundUpsTransactionUID != this.transactionUID) {
throw new DicomWebServiceError(
DicomWebStatusCodes.UPSTransactionUIDNotCorrect,
"The request transaction UID is inconsistent with found UPS instance's transaction UID",
400
);
}
},
"CANCELED": () => {
throw new DicomWebServiceError(
DicomWebStatusCodes.UPSMayNoLongerBeUpdated,
"Shall not modify UPS instance with state CANCELED"
);
},
"COMPLETED": () => {
throw new DicomWebServiceError(
DicomWebStatusCodes.UPSMayNoLongerBeUpdated,
"Shall not modify UPS instance with state COMPLETED"
);
}
};

return mappingMethod[procedureState]() || true;
}

/**
* remove not allowed updating attribute in request work item
*/
adjustRequestWorkItem() {
for(let i = 0 ; i < notAllowedAttributes.length ; i++) {
let notAllowedAttr = notAllowedAttributes[i];
_.unset(this.requestWorkItem.dicomJson, notAllowedAttr);
}
}
}

module.exports.UpdateWorkItemService = UpdateWorkItemService;
58 changes: 58 additions & 0 deletions api/dicom-web/controller/UPS-RS/update-workItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const {
UpdateWorkItemService
} = require("./service/update-workItem.service");
const { ApiLogger } = require("../../../../utils/logs/api-logger");
const { Controller } = require("../../../controller.class");
const { DicomWebServiceError } = require("@error/dicom-web-service");

class UpdateWorkItemController extends Controller {
constructor(req, res) {
super(req, res);
}

async mainProcess() {
let apiLogger = new ApiLogger(this.request, "UPS-RS");

apiLogger.addTokenValue();
apiLogger.logger.info(`Update workItem, params: ${this.paramsToString()}`);

try {
let service = new UpdateWorkItemService(this.request, this.response);
let workItems = await service.updateUps();
return this.response
.set("Content-Type", "application/dicom+json")
.set("Warning", "299 Raccoon: The Workitem was updated with modifications.")
.status(200)
.end();
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
apiLogger.logger.error(errorStr);

if (e instanceof DicomWebServiceError) {
return this.response.status(e.code).json({
status: e.status,
message: e.message
});
}

this.response.writeHead(500, {
"Content-Type": "application/dicom+json"
});
this.response.end(JSON.stringify({
code: 500,
message: "An Server Exception Occurred"
}));
}
}
}

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

await controller.doPipeline();
};
8 changes: 2 additions & 6 deletions api/dicom-web/ups-rs.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,15 @@ router.get("/workitems/:workItem",
/**
* @openapi
* /dicom-web/workitems/{workitemUID}:
* get:
* post:
* tags:
* - UPS-RS
* description: >
* This transaction modifies Attributes of an existing Workitem. It corresponds to the UPS DIMSE N-SET operation.
* See [Update Workitem Transaction](https://dicom.nema.org/medical/dicom/current/output/html/part18.html#sect_11.6)
* responses:
* "200":
* description: Query successfully
* content:
* "application/dicom+json":
* schema:
* type: array
* description: modify successfully
*/
router.post("/workitems/:workItem",
require("./controller/UPS-RS/update-workItem")
Expand Down

0 comments on commit 6d921bb

Please sign in to comment.