Skip to content

Commit

Permalink
feat(sql): implement create mwl item
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Dec 20, 2023
1 parent 50429e6 commit 6f345e3
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { DicomWebServiceError, DicomWebStatusCodes } = require("@error/dicom-web-service");
const { PatientModel } = require("@models/sql/models/patient.model");
const { CreateMwlItemService } = require("@root/api/dicom-web/controller/MWL-RS/service/create-mwlitem.service");
const { dictionary } = require("@models/DICOM/dicom-tags-dic");
const { MwlItemPersistentObject } = require("@models/sql/po/mwlItem.po");
const { DicomJsonModel } = require("@models/DICOM/dicom-json-model");

class SqlCreateMwlItemService extends CreateMwlItemService {
constructor(req, res) {
super(req, res);
this.requestMwlItemDicomJsonModel = new DicomJsonModel(this.requestMwlItem[0]);
}

async checkPatientExist() {
let patientID = this.requestMwlItemDicomJsonModel.getString("00100020");
let patientCount = await PatientModel.count({
where: {
x00100020: patientID
}
});
if (patientCount <= 0) {
throw new DicomWebServiceError(
DicomWebStatusCodes.MissingAttribute,
`Patient[id=${patientID}] does not exists`,
404
);
}
}

async createOrUpdateMwl(mwlDicomJson) {
let studyInstanceUID = mwlDicomJson.getValue(dictionary.keyword.StudyInstanceUID);
let patientID = this.requestMwlItemDicomJsonModel.getString("00100020");
let mwlItemPO = new MwlItemPersistentObject(mwlDicomJson.dicomJson, await PatientModel.findOne({ where: { x00100020: patientID } }));
let mwlItem = await mwlItemPO.save();
this.apiLogger.logger.info(`create mwl item: ${studyInstanceUID}`);
return mwlItem.json;
}
}

module.exports.CreateMwlItemService = SqlCreateMwlItemService;
2 changes: 1 addition & 1 deletion api/dicom-web/controller/MWL-RS/create-mwlItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ApiErrorArrayHandler } = require("@error/api-errors.handler");
const { Controller } = require("@root/api/controller.class");
const { ApiLogger } = require("@root/utils/logs/api-logger");
const { CreateMwlItemService } = require("./service/create-mwlitem.service");
const { CreateMwlItemService } = require("@mwl-service/create-mwlitem.service");

class CreateMwlItemController extends Controller {
constructor(req, res) {
Expand Down
2 changes: 2 additions & 0 deletions config/jsconfig.mongodb.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@delete-service": ["./api/dicom-web/controller/WADO-RS/deletion/service/delete.js"],
"@rendered-service": ["./api/dicom-web/controller/WADO-RS/service/rendered.service.js"],
"@thumbnail-service": ["./api/dicom-web/controller/WADO-RS/service/thumbnail.service.js"],
"@ups-service/*": ["./api/dicom-web/controller/UPS-RS/service/*"],
"@mwl-service/*": ["./api/dicom-web/controller/MWL-RS/service/*"],
"@dimse-query-builder": ["./dimse/queryBuilder.js"],
"@dimse-patient-query-task": ["./dimse/patientQueryTask.js"],
"@dimse-study-query-task": ["./dimse/studyQueryTask.js"],
Expand Down
1 change: 1 addition & 0 deletions config/jsconfig.sql.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@rendered-service": ["./api-sql/dicom-web/controller/WADO-RS/service/rendered.service.js"],
"@thumbnail-service": ["./api-sql/dicom-web/controller/WADO-RS/service/thumbnail.service.js"],
"@ups-service/*": ["./api-sql/dicom-web/controller/UPS-RS/service/*"],
"@mwl-service/*": ["./api-sql/dicom-web/controller/MWL-RS/service/*"],
"@dimse-query-builder": ["./dimse-sql/queryBuilder.js"],
"@dimse-patient-query-task": ["./dimse-sql/patientQueryTask.js"],
"@dimse-study-query-task": ["./dimse-sql/studyQueryTask.js"],
Expand Down
1 change: 1 addition & 0 deletions config/modula-alias/mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@rendered-service": "../../../api/dicom-web/controller/WADO-RS/service/rendered.service.js",
"@thumbnail-service": "../../../api/dicom-web/controller/WADO-RS/service/thumbnail.service.js",
"@ups-service": "../../../api/dicom-web/controller/UPS-RS/service",
"@mwl-service": "../../../api/dicom-web/controller/MWL-RS/service",
"@dimse-query-builder": "../../../dimse/queryBuilder.js",
"@dimse-patient-query-task": "../../../dimse/patientQueryTask.js",
"@dimse-study-query-task": "../../../dimse/studyQueryTask.js",
Expand Down
1 change: 1 addition & 0 deletions config/modula-alias/sql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@rendered-service": "../../../api-sql/dicom-web/controller/WADO-RS/service/rendered.service.js",
"@thumbnail-service": "../../../api-sql/dicom-web/controller/WADO-RS/service/thumbnail.service.js",
"@ups-service": "../../../api-sql/dicom-web/controller/UPS-RS/service",
"@mwl-service": "../../../api-sql/dicom-web/controller/MWL-RS/service",
"@dimse-query-builder": "../../../dimse-sql/queryBuilder.js",
"@dimse-patient-query-task": "../../../dimse-sql/patientQueryTask.js",
"@dimse-study-query-task": "../../../dimse-sql/studyQueryTask.js",
Expand Down
162 changes: 162 additions & 0 deletions models/sql/po/mwlItem.po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
const { get, set, cloneDeep, unset } = require("lodash");
const { PersonNameModel } = require("../models/personName.model");
const { tagsNeedStore } = require("@models/DICOM/dicom-tags-mapping");
const { BaseDicomJson } = require("@models/DICOM/dicom-json-model");
const { dictionary } = require("@models/DICOM/dicom-tags-dic");
const { vrValueTransform } = require("./utils");
const { DicomCodeModel } = require("../models/dicomCode.model");
const { MwlItemModel } = require("../models/mwlItems.model");
const { Op } = require("sequelize");


class MwlItemPersistentObject {
constructor(dicomJson, patient) {

this.json = {};
this.initJsonProperties(dicomJson);

this.patient = patient;

let dicomJsonObj = new BaseDicomJson(dicomJson);
this.study_instance_uid = dicomJsonObj.getValue("0020000D");
this.accession_number = dicomJsonObj.getValue(dictionary.keyword.AccessionNumber);
this.accno_local_id = dicomJsonObj.getValue(`${dictionary.keyword.IssuerOfAccessionNumberSequence}.Value.0.${dictionary.keyword.LocalNamespaceEntityID}`);
this.accno_universal_id = dicomJsonObj.getValue(`${dictionary.keyword.IssuerOfAccessionNumberSequence}.Value.0.${dictionary.keyword.UniversalEntityID}`);
this.accno_universal_id_type = dicomJsonObj.getValue(`${dictionary.keyword.IssuerOfAccessionNumberSequence}.Value.0.${dictionary.keyword.UniversalEntityIDType}`);
this.requested_procedure_id = dicomJsonObj.getValue(dictionary.keyword.RequestedProcedureID);
this.admission_id = dicomJsonObj.getValue(dictionary.keyword.AdmissionID);
this.issuer_admission_local_id = dicomJsonObj.getValue(`${dictionary.keyword.IssuerOfAdmissionIDSequence}.Value.0.${dictionary.keyword.LocalNamespaceEntityID}`);
this.issuer_admission_universal_id = dicomJsonObj.getValue(`${dictionary.keyword.IssuerOfAdmissionIDSequence}.Value.0.${dictionary.keyword.UniversalEntityID}`);
this.issuer_admission_universal_id_type = dicomJsonObj.getValue(`${dictionary.keyword.IssuerOfAdmissionIDSequence}.Value.0.${dictionary.keyword.UniversalEntityIDType}`);
this.station_ae_title = dicomJsonObj.getValue(dictionary.keyword.StationAETitle);
this.station_name = dicomJsonObj.getValue(dictionary.keyword.StationName);
this.start_date = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepStartDate);
this.end_date = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepEndDate);
this.start_time = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepStartTime);
this.end_time = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepEndTime);
this.physician_name = dicomJsonObj.getValue(dictionary.keyword.ScheduledPerformingPhysicianName);
this.procedure_step_location = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepLocation);
this.description = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepDescription);
this.protocol_code = dicomJsonObj.getValue(dictionary.keyword.ScheduledProtocolCodeSequence);
this.institution_name = dicomJsonObj.getValue(dictionary.keyword.InstitutionName);
this.institution_department_name = dicomJsonObj.getValue(dictionary.keyword.InstitutionalDepartmentName);
this.institution_department_type_code = dicomJsonObj.getValue(dictionary.keyword.InstitutionalDepartmentTypeCodeSequence);
this.institution_code = dicomJsonObj.getValue(dictionary.keyword.InstitutionCodeSequence);
this.sps_id = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepID);
this.sps_status = dicomJsonObj.getValue(dictionary.keyword.ScheduledProcedureStepStatus);
this.modality = dicomJsonObj.getValue(dictionary.keyword.Modality);
}

initJsonProperties(dicomJson) {
Object.keys({
...tagsNeedStore.MWL,
...tagsNeedStore.Patient
}).forEach(key => {
let value = get(dicomJson, key);
value ? set(this.json, key, value) : undefined;
});
}

async save() {
let mwlItemObj = MwlItemModel.build(this.getPersistentObject());
mwlItemObj.patient_id = this.patient.x00100020;
let [mwlItem, created] = await MwlItemModel.findOrCreate({
where: {
[Op.and]: [
{
sps_id: this.sps_id
},
{
study_instance_uid: this.study_instance_uid
}
]
},
defaults: mwlItemObj.toJSON()
});

await this.setGeneralCode(mwlItem, dictionary.keyword.InstitutionalDepartmentTypeCodeSequence);
await this.setGeneralCode(mwlItem, dictionary.keyword.InstitutionCodeSequence);
await this.setGeneralCode(mwlItem, dictionary.keyword.ScheduledProtocolCodeSequence);
await this.setPhysicianName(mwlItem);

if (!created) {
await this.removeAllAssociationItems(mwlItem);
mwlItem.json = {
...mwlItem.json,
...this.json
};
mwlItem.changed("json", true);
}
await mwlItem.save();

return mwlItem;
}

async setGeneralCode(item, tag) {
if (this[`x${tag}`]) {
let code = await DicomCodeModel.create({
"x00080100": get(this[`x${tag}`], "00080100.Value.0", undefined),
"x00080102": get(this[`x${tag}`], "00080102.Value.0", undefined),
"x00080103": get(this[`x${tag}`], "00080103.Value.0", undefined),
"x00080104": get(this[`x${tag}`], "00080104.Value.0", undefined)
});
let keyword = dictionary.tag[tag];
await item[`set${keyword}`](code);
}
}

async setPhysicianName(mwlItem) {
if (this.physician_name) {
let nameOfPhysician = await PersonNameModel.createPersonName(this.physician_name);
await mwlItem[`set${dictionary.tag["00400006"]}`](nameOfPhysician);
}
}

async removeAllAssociationItems(upsWorkItem) {
const associationItemsNames = [
"InstitutionalDepartmentTypeCodeSequence",
"InstitutionCodeSequence",
"ScheduledProtocolCodeSequence",
"ScheduledPerformingPhysicianName"
];

for (let i = 0; i < associationItemsNames.length; i++) {
let associationItemName = associationItemsNames[i];
let associationItem = await upsWorkItem[`get${associationItemName}`]();
if (associationItem) {
await associationItem.destroy();
}
}
}

getPersistentObject() {
return {
json: this.json,
study_instance_uid: this.study_instance_uid,
accession_number: this.accession_number,
accno_local_id: this.accno_local_id,
accno_universal_id: this.accno_universal_id,
accno_universal_id_type: this.accno_universal_id_type,
requested_procedure_id: this.requested_procedure_id,
admission_id: this.admission_id,
issuer_admission_local_id: this.issuer_admission_local_id,
issuer_admission_universal_id: this.issuer_admission_universal_id,
issuer_admission_universal_id_type: this.issuer_admission_universal_id_type,
station_ae_title: this.station_ae_title,
station_name: this.station_name,
start_date: this.start_date,
end_date: this.end_date,
start_time: vrValueTransform.DT(this.start_time),
end_time: vrValueTransform.DT(this.end_time),
procedure_step_location: this.procedure_step_location,
description: this.description,
institution_name: this.institution_name,
institution_department_name: this.institution_department_name,
sps_id: this.sps_id,
sps_status: this.sps_status,
modality: this.modality
};
}
}

module.exports.MwlItemPersistentObject = MwlItemPersistentObject;

0 comments on commit 6f345e3

Please sign in to comment.