Skip to content

Commit

Permalink
feat: add first version of creating work item service
Browse files Browse the repository at this point in the history
- The service extends
the `CreateWorkItemService` class and includes methods for creating UPS
(Unified Procedure Step) work items
  • Loading branch information
Chinlinlee committed Nov 19, 2023
1 parent c517f41 commit ed58e54
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { PatientModel } = require("@dbModels/patient.model");
const { UIDUtils } = require("@dcm4che/util/UIDUtils");
const { WorkItemModel } = require("@models/sql/models/workItems.model");
const { CreateWorkItemService } = require("@root/api/dicom-web/controller/UPS-RS/service/create-workItem.service");
const { get, set } = require("lodash");
const { UpsWorkItemPersistentObject } = require("@models/sql/po/upsWorkItem.po");
const { PatientPersistentObject } = require("@models/sql/po/patient.po");

class SqlCreateWorkItemService extends CreateWorkItemService {
constructor(req, res) {
super(req, res);
}

async createUps() {
let uid = get(this.request, "query.workitem",
await UIDUtils.createUID()
);
await this.dataAdjustBeforeCreatingUps(uid);
await this.validateWorkItem(uid);

let patientId = this.requestWorkItem.getString("00100020");
let patient = await this.findOneOrCreatePatient(patientId);
let workItem = new UpsWorkItemPersistentObject(this.requestWorkItem.dicomJson, patient);
let savedWorkItem = await workItem.save();

//TODO: subscription
//this.triggerCreateEvent(savedWorkItem);

return workItem;
}

async findOneOrCreatePatient(patientId) {
/** @type {PatientModel | null} */
let patientPersistent = new PatientPersistentObject(this.requestWorkItem.dicomJson);
let patient = await patientPersistent.createPatient();

return patient;
}

async isUpsExist(uid) {
return await WorkItemModel.findOne({
where: {
upsInstanceUID: uid
}
});
}
}

module.exports.CreateWorkItemService = SqlCreateWorkItemService;
6 changes: 6 additions & 0 deletions models/sql/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { SeriesRequestAttributesModel } = require("./models/seriesRequestAttribut
const { DicomCodeModel } = require("./models/dicomCode.model");
const { DicomContentSqModel } = require("./models/dicomContentSQ.model");
const { VerifyIngObserverSqModel } = require("./models/verifyingObserverSQ.model");
const { WorkItemModel } = require("./models/workItems.model");

async function initDatabasePostgres() {
const { Client } = require("pg");
Expand Down Expand Up @@ -128,6 +129,11 @@ async function init() {
DicomContentSqModel.hasOne(DicomCodeModel, {
as: "ConceptCode"
});

WorkItemModel.belongsTo(PatientModel, {
foreignKey: "x00100020",
targetKey: "x00100020"
});

//TODO: 設計完畢後要將 force 刪除
await sequelizeInstance.sync({
Expand Down
24 changes: 10 additions & 14 deletions models/sql/models/workitems.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class WorkItemModel extends Model {
}
};

WorkItemModel.init({
/** @type { import("sequelize").ModelAttributes } */
const WorkItemSchema = {
upsInstanceUID: {
type: DataTypes.STRING,
allowNull: false
Expand All @@ -33,21 +34,13 @@ WorkItemModel.init({
type: DataTypes.STRING
},
subscribed: {
type: DataTypes.INTEGER,
default: SUBSCRIPTION_STATE.NOT_SUBSCRIBED
type: DataTypes.INTEGER,
defaultValue: SUBSCRIPTION_STATE.NOT_SUBSCRIBED
},
//#region patient level
"x00100010": {
type: DataTypes.INTEGER
},
"x00100020": {
type: vrTypeMapping.LO
},
"x00100021": {
type: vrTypeMapping.LO
},
"x00100040": {
type: vrTypeMapping.CS
type: vrTypeMapping.LO,
allowNull: false
},
//#endregion
"x00080018": {
Expand Down Expand Up @@ -99,11 +92,14 @@ WorkItemModel.init({
"json": {
type: vrTypeMapping.JSON
}
}, {
};

WorkItemModel.init(WorkItemSchema, {
sequelize: sequelizeInstance,
modelName: "UPSWorkItem",
tableName: "UPSWorkItem",
freezeTableName: true
});

module.exports.WorkItemModel = WorkItemModel;
module.exports.WorkItemSchema = WorkItemSchema;
100 changes: 100 additions & 0 deletions models/sql/po/upsWorkItem.po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const moment = require("moment");
const { get, set } = require("lodash");
const { PersonNameModel } = require("../models/personName.model");
const { StudyModel } = require("../models/study.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 { WorkItemModel } = require("../models/workItems.model");


class UpsWorkItemPersistentObject {
constructor(dicomJson, patient) {

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

this.patient = patient;

let dicomJsonObj = new BaseDicomJson(dicomJson);
this.upsInstanceUID = get(dicomJson, "upsInstanceUID", "");
this.patientID = get(dicomJson, "patientID", "");
this.transactionUID = get(dicomJson, "transactionUID", "");

this.x00100020 = dicomJsonObj.getValue("00100020");
this.x00080018 = dicomJsonObj.getValue("00080018");
this.x00741200 = dicomJsonObj.getValue("00741200");
this.x00404010 = dicomJsonObj.getValue("00404010");
this.x00741204 = dicomJsonObj.getValue("00741204");
this.x00741202 = dicomJsonObj.getValue("00741202");
this.x00404025 = dicomJsonObj.getValue("00404025");
this.x00404026 = dicomJsonObj.getValue("00404026");
this.x00404027 = dicomJsonObj.getValue("00404027");
this.x00404034 = dicomJsonObj.getValue("00404034");
this.x00404005 = dicomJsonObj.getValue("00404005");
this.x00404011 = dicomJsonObj.getValue("00404011");
this.x00380010 = dicomJsonObj.getValue("00380010");
this.x00741000 = dicomJsonObj.getValue("00741000");
this.x00080082 = dicomJsonObj.getValue("00080082");
}

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

async save() {
let item = {
json: this.json,
upsInstanceUID : this.upsInstanceUID,
patientID : this.patientID,
transactionUID : this.transactionUID,
x00100020: this.x00100020,
x00080018: this.x00080018,
x00741200: this.x00741200,
x00404010: vrValueTransform.DT(this.x00404010),
x00741204: this.x00741204,
x00741202: this.x00741202,
//TODO: dicom code x00404025: this.x00404025,
//TODO: dicom code x00404026: this.x00404026,
//TODO: dicom code x00404027: this.x00404027,
//TODO: dicom code x00404034: this.x00404034,
x00404005: vrValueTransform.DT(this.x00404005),
x00404011: vrValueTransform.DT(this.x00404011),
x00380010: this.x00380010,
x00741000: this.x00741000
//TODO dicom code x00080082: this.x00080082
};

let upsWorkItemObj = WorkItemModel.build(item);
let [upsWorkItem, created] = await WorkItemModel.findOrCreate({
where: {
upsInstanceUID: this.upsInstanceUID
},
defaults: upsWorkItemObj.toJSON()
});

if (created) {
let patientName = await PersonNameModel.createPersonName(this.x00100010);
upsWorkItem.x00100010 = patientName ? patientName.id : undefined;
await upsWorkItem.save();
} else {
await WorkItemModel.update(item, {
where: {
upsInstanceUID: upsWorkItem.dataValues.upsInstanceUID
}
});
await PersonNameModel.updatePersonNameById(this.x00100010, upsWorkItem.getDataValue("x00100010"));
}

return upsWorkItem;
}
}

module.exports.UpsWorkItemPersistentObject = UpsWorkItemPersistentObject;
2 changes: 1 addition & 1 deletion routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function (app) {
app.use("/dicom-web", require("./api/dicom-web/wado-rs-bulkdata.route"));
app.use("/dicom-web", require("./api/dicom-web/wado-rs-thumbnail.route"));
app.use("/dicom-web", require("./api/dicom-web/delete.route"));
// app.use("/dicom-web", require("./api/dicom-web/ups-rs.route"));
app.use("/dicom-web", require("./api/dicom-web/ups-rs.route"));

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

0 comments on commit ed58e54

Please sign in to comment.