diff --git a/models/mongodb/models/dicom.js b/models/mongodb/models/dicom.js index a2cf0df7..af9feed8 100644 --- a/models/mongodb/models/dicom.js +++ b/models/mongodb/models/dicom.js @@ -144,7 +144,7 @@ let dicomModelSchema = new mongoose.Schema( await fsP.unlink( path.join( - raccoonConfig.dicomWebConfig.storeRootPath, + raccoonConfig.dicomWebConfig.storeRootPath, instancePath ) ); @@ -152,7 +152,7 @@ let dicomModelSchema = new mongoose.Schema( console.error(e); } }, - getAttributes: async function() { + getAttributes: async function () { let study = this.toObject(); delete study._id; delete study.id; @@ -166,10 +166,10 @@ let dicomModelSchema = new mongoose.Schema( return mongoose.model("dicom").find(query, keys).setOptions({ strictQuery: false }) - .cursor(); + .cursor(); }, getAuditInstancesInfoFromStudyUID: async (studyUID) => { - let instances = await mongoose.model("dicom").find({studyUID}).exec(); + let instances = await mongoose.model("dicom").find({ studyUID }).exec(); let instanceInfos = { sopClassUIDs: [], @@ -188,11 +188,149 @@ let dicomModelSchema = new mongoose.Schema( patientID ? instanceInfos.patientID = patientID : null; patientName ? instanceInfos.patientName = patientName : null; } - + instanceInfos.sopClassUIDs = _.uniq(instanceInfos.sopClassUIDs); instanceInfos.accessionNumbers = _.uniq(instanceInfos.accessionNumbers); return instanceInfos; + }, + /** + * + * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions + * @returns + */ + getDicomJson: async function (queryOptions) { + + let includeFieldsFactory = new IncludeFieldsFactory(queryOptions.includeFields); + let instanceFields = includeFieldsFactory.getInstanceLevelFields(); + + try { + + let docs = await mongoose + .model("dicom") + .find({ + ...queryOptions.query, + deleteStatus: { + $eq: 0 + } + }, { + ...instanceFields + }) + .setOptions({ + strictQuery: false + }) + .limit(queryOptions.limit) + .skip(queryOptions.skip) + .exec(); + + let instanceDicomJson = docs.map(v => { + let obj = v.toObject(); + delete obj._id; + delete obj.id; + obj["00081190"] = { + vr: "UR", + Value: [ + `${queryOptions.retrieveBaseUrl}/${obj["0020000D"]["Value"][0]}/series/${obj["0020000E"]["Value"][0]}/instances/${obj["00080018"]["Value"][0]}` + ] + }; + + _.set(obj, dictionary.keyword.RetrieveAETitle, { + ...dictionary.tagVR[dictionary.keyword.RetrieveAETitle], + Value: [raccoonConfig.aeTitle] + }); + + return obj; + }); + + return instanceDicomJson; + + } catch (e) { + throw e; + } + + }, + /** + * + * @param {object} iParam + * @param {string} iParam.studyUID + * @param {string} iParam.seriesUID + * @param {string} iParam.instanceUID + */ + getPathOfInstance: async function (iParam) { + let { studyUID, seriesUID, instanceUID } = iParam; + + try { + let query = { + $and: [ + { + studyUID: studyUID + }, + { + seriesUID: seriesUID + }, + { + instanceUID: instanceUID + }, + { + deleteStatus: { + $eq: 0 + } + } + ] + }; + + let doc = await mongoose.model("dicom").findOne(query, { + studyUID: 1, + seriesUID: 1, + instanceUID: 1, + instancePath: 1 + }).exec(); + + if (doc) { + let docObj = doc.toObject(); + docObj.instancePath = getStoreDicomFullPath(docObj); + + return docObj; + } + + return undefined; + + } catch (e) { + throw e; + } + }, + /** + * + * @param {string} studyUID + * @param {string} seriesUID + */ + getInstanceOfMedianIndex: async function (query) { + let instanceCountOfStudy = await mongoose.model("dicom").countDocuments({ + $and: [ + { + studyUID: query.studyUID + }, + { + deleteStatus: { + $eq: 0 + } + } + ] + }); + + return await mongoose.model("dicom").findOne(query, { + studyUID: 1, + seriesUID: 1, + instanceUID: 1, + instancePath: 1 + }) + .sort({ + studyUID: 1, + seriesUID: 1 + }) + .skip(instanceCountOfStudy >> 1) + .limit(1) + .exec(); } }, timestamps: true @@ -405,163 +543,8 @@ async function updateStudyNumberOfStudyRelatedInstance(doc) { } } -/** - * - * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions - * @returns - */ -dicomModelSchema.statics.getDicomJson = async function (queryOptions) { - - let includeFieldsFactory = new IncludeFieldsFactory(queryOptions.includeFields); - let instanceFields = includeFieldsFactory.getInstanceLevelFields(); - - try { - - let docs = await mongoose - .model("dicom") - .find({ - ...queryOptions.query, - deleteStatus: { - $eq: 0 - } - }, { - ...instanceFields - }) - .setOptions({ - strictQuery: false - }) - .limit(queryOptions.limit) - .skip(queryOptions.skip) - .exec(); - - let instanceDicomJson = docs.map(v => { - let obj = v.toObject(); - delete obj._id; - delete obj.id; - obj["00081190"] = { - vr: "UR", - Value: [ - `${queryOptions.retrieveBaseUrl}/${obj["0020000D"]["Value"][0]}/series/${obj["0020000E"]["Value"][0]}/instances/${obj["00080018"]["Value"][0]}` - ] - }; - - _.set(obj, dictionary.keyword.RetrieveAETitle, { - ...dictionary.tagVR[dictionary.keyword.RetrieveAETitle], - Value: [raccoonConfig.aeTitle] - }); - - return obj; - }); - - return instanceDicomJson; - - } catch (e) { - throw e; - } - -}; - -/** - * - * @param {object} iParam - * @param {string} iParam.studyUID - * @param {string} iParam.seriesUID - * @param {string} iParam.instanceUID - */ -dicomModelSchema.statics.getPathOfInstance = async function (iParam) { - let { studyUID, seriesUID, instanceUID } = iParam; - - try { - let query = { - $and: [ - { - studyUID: studyUID - }, - { - seriesUID: seriesUID - }, - { - instanceUID: instanceUID - }, - { - deleteStatus: { - $eq: 0 - } - } - ] - }; - - let doc = await mongoose.model("dicom").findOne(query, { - studyUID: 1, - seriesUID: 1, - instanceUID: 1, - instancePath: 1 - }).exec(); - - if (doc) { - let docObj = doc.toObject(); - docObj.instancePath = getStoreDicomFullPath(docObj); - - return docObj; - } - - return undefined; - - } catch (e) { - throw e; - } -}; - -/** - * - * @param {string} studyUID - * @param {string} seriesUID - */ -dicomModelSchema.statics.getInstanceOfMedianIndex = async function (query) { - let instanceCountOfStudy = await mongoose.model("dicom").countDocuments({ - $and: [ - { - studyUID: query.studyUID - }, - { - deleteStatus: { - $eq: 0 - } - } - ] - }); - - return await mongoose.model("dicom").findOne(query, { - studyUID: 1, - seriesUID: 1, - instanceUID: 1, - instancePath: 1 - }) - .sort({ - studyUID: 1, - seriesUID: 1 - }) - .skip(instanceCountOfStudy >> 1) - .limit(1) - .exec(); -}; - -/** - * @typedef {import("mongoose").Model & { - * getPathOfInstance: (iParam: { - * studyUID: string, - * seriesUID: string, - * instanceUID: string - * }) => Promise; - * getDicomJson: (queryOptions: import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions) => Promise; - * getInstanceOfMedianIndex: (studyUID: string, seriesUID: string) => Promise; - * }} DicomModelSchema - * - */ - let dicomModel = mongoose.model("dicom", dicomModelSchema, "dicom"); -/** @type {DicomModelSchema} */ module.exports = dicomModel; module.exports.getModalitiesInStudy = getModalitiesInStudy; diff --git a/models/mongodb/models/dicomSeries.js b/models/mongodb/models/dicomSeries.js index 39f81f60..06224f69 100644 --- a/models/mongodb/models/dicomSeries.js +++ b/models/mongodb/models/dicomSeries.js @@ -57,7 +57,7 @@ let dicomSeriesSchema = new mongoose.Schema( recursive: true }); }, - getAttributes: async function() { + getAttributes: async function () { let series = this.toObject(); delete series._id; delete series.id; @@ -71,7 +71,106 @@ let dicomSeriesSchema = new mongoose.Schema( return mongoose.model("dicomSeries").find(query, keys).setOptions({ strictQuery: false }) - .cursor(); + .cursor(); + }, + /** + * + * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions + * @returns + */ + getDicomJson: async function (queryOptions) { + let includeFieldsFactory = new IncludeFieldsFactory(queryOptions.includeFields); + let seriesFields = includeFieldsFactory.getSeriesLevelFields(); + + try { + let docs = await mongoose + .model("dicomSeries") + .find({ + ...queryOptions.query, + deleteStatus: { + $eq: 0 + } + }, { + ...seriesFields + }) + .setOptions({ + strictQuery: false + }) + .limit(queryOptions.limit) + .skip(queryOptions.skip) + .exec(); + + + let seriesDicomJson = docs.map((v) => { + let obj = v.toObject(); + delete obj._id; + delete obj.id; + obj["00081190"] = { + vr: "UR", + Value: [ + `${queryOptions.retrieveBaseUrl}/${obj["0020000D"]["Value"][0]}/series/${obj["0020000E"]["Value"][0]}` + ] + }; + + _.set(obj, dictionary.keyword.RetrieveAETitle, { + ...dictionary.tagVR[dictionary.keyword.RetrieveAETitle], + Value: [raccoonConfig.aeTitle] + }); + + return obj; + }); + + return seriesDicomJson; + + } catch (e) { + throw e; + } + }, + /** + * + * @param {object} iParam + * @param {string} iParam.studyUID + * @param {string} iParam.seriesUID + */ + getPathGroupOfInstances: async function (iParam) { + let { studyUID, seriesUID } = iParam; + try { + let query = [ + { + $match: { + $and: [ + { + seriesUID: seriesUID + }, + { + studyUID: studyUID + } + ] + } + }, + { + $group: { + _id: "$seriesUID", + pathList: { + $addToSet: { + studyUID: "$studyUID", + seriesUID: "$seriesUID", + instanceUID: "$instanceUID", + instancePath: "$instancePath" + } + } + } + } + ]; + let docs = await mongoose.model("dicom").aggregate(query); + let pathGroup = _.get(docs, "0.pathList", []); + + let fullPathGroup = getStoreDicomFullPathGroup(pathGroup); + + return fullPathGroup; + } catch (e) { + throw e; + } } }, timestamps: true @@ -107,120 +206,6 @@ dicomSeriesSchema.index({ "0020000E": 1 }); - -/** - * - * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions - * @returns - */ -dicomSeriesSchema.statics.getDicomJson = async function (queryOptions) { - let includeFieldsFactory = new IncludeFieldsFactory(queryOptions.includeFields); - let seriesFields = includeFieldsFactory.getSeriesLevelFields(); - - try { - let docs = await mongoose - .model("dicomSeries") - .find({ - ...queryOptions.query, - deleteStatus: { - $eq: 0 - } - }, { - ...seriesFields - }) - .setOptions({ - strictQuery: false - }) - .limit(queryOptions.limit) - .skip(queryOptions.skip) - .exec(); - - - let seriesDicomJson = docs.map((v) => { - let obj = v.toObject(); - delete obj._id; - delete obj.id; - obj["00081190"] = { - vr: "UR", - Value: [ - `${queryOptions.retrieveBaseUrl}/${obj["0020000D"]["Value"][0]}/series/${obj["0020000E"]["Value"][0]}` - ] - }; - - _.set(obj, dictionary.keyword.RetrieveAETitle, { - ...dictionary.tagVR[dictionary.keyword.RetrieveAETitle], - Value: [raccoonConfig.aeTitle] - }); - - return obj; - }); - - return seriesDicomJson; - - } catch (e) { - throw e; - } -}; - -/** - * - * @param {object} iParam - * @param {string} iParam.studyUID - * @param {string} iParam.seriesUID - */ -dicomSeriesSchema.statics.getPathGroupOfInstances = async function (iParam) { - let { studyUID, seriesUID } = iParam; - try { - let query = [ - { - $match: { - $and: [ - { - seriesUID: seriesUID - }, - { - studyUID: studyUID - } - ] - } - }, - { - $group: { - _id: "$seriesUID", - pathList: { - $addToSet: { - studyUID: "$studyUID", - seriesUID: "$seriesUID", - instanceUID: "$instanceUID", - instancePath: "$instancePath" - } - } - } - } - ]; - let docs = await mongoose.model("dicom").aggregate(query); - let pathGroup = _.get(docs, "0.pathList", []); - - let fullPathGroup = getStoreDicomFullPathGroup(pathGroup); - - return fullPathGroup; - } catch (e) { - throw e; - } -}; - - -/** - * @typedef { mongoose.Model & { - * getPathGroupOfInstances: function(iParam: { - * studyUID: string, - * seriesUID: string - * }): Promise; - * getDicomJson: function(queryOptions: import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions): Promise; - * }} DicomSeriesModel -*/ - -/** @type { DicomSeriesModel } */ let dicomSeriesModel = mongoose.model( "dicomSeries", dicomSeriesSchema, diff --git a/models/mongodb/models/dicomStudy.js b/models/mongodb/models/dicomStudy.js index e4e4deb3..d4788c77 100644 --- a/models/mongodb/models/dicomStudy.js +++ b/models/mongodb/models/dicomStudy.js @@ -57,7 +57,7 @@ let dicomStudySchema = new mongoose.Schema( recursive: true }); }, - getAttributes: async function() { + getAttributes: async function () { let study = this.toObject(); delete study._id; delete study.id; @@ -72,6 +72,91 @@ let dicomStudySchema = new mongoose.Schema( strictQuery: false }) .cursor(); + }, + /** + * + * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions + * @returns + */ + getDicomJson: async function (queryOptions) { + let includeFieldsFactory = new IncludeFieldsFactory(queryOptions.includeFields); + let studyFields = includeFieldsFactory.getStudyLevelFields(); + + try { + let docs = await mongoose.model("dicomStudy").find({ + ...queryOptions.query, + deleteStatus: { + $eq: 0 + } + }, studyFields) + .limit(queryOptions.limit) + .skip(queryOptions.skip) + .setOptions({ + strictQuery: false + }) + .exec(); + + let studyDicomJson = docs.map((v) => { + let obj = v.toObject(); + delete obj._id; + delete obj.id; + obj["00081190"] = { + vr: "UR", + Value: [`${queryOptions.retrieveBaseUrl}/${obj["0020000D"]["Value"][0]}`] + }; + + _.set(obj, dictionary.keyword.RetrieveAETitle, { + ...dictionary.tagVR[dictionary.keyword.RetrieveAETitle], + Value: [raccoonConfig.aeTitle] + }); + + return obj; + }); + + return studyDicomJson; + + } catch (e) { + throw e; + } + }, + /** + * + * @param {Object} iParam + * @param {string} iParam.studyUID + */ + getPathGroupOfInstances: async function (iParam) { + let { studyUID } = iParam; + try { + let query = [ + { + $match: { + studyUID: studyUID + } + }, + { + $group: { + _id: "$studyUID", + pathList: { + $addToSet: { + studyUID: "$studyUID", + seriesUID: "$seriesUID", + instanceUID: "$instanceUID", + instancePath: "$instancePath" + } + } + } + } + ]; + let docs = await mongoose.model("dicom").aggregate(query).exec(); + let pathGroup = _.get(docs, "0.pathList", []); + + let fullPathGroup = getStoreDicomFullPathGroup(pathGroup); + + return fullPathGroup; + + } catch (e) { + throw e; + } } }, timestamps: true @@ -93,103 +178,6 @@ dicomStudySchema.index({ "0020000D": 1 }); -/** - * - * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions - * @returns - */ -dicomStudySchema.statics.getDicomJson = async function (queryOptions) { - let includeFieldsFactory = new IncludeFieldsFactory(queryOptions.includeFields); - let studyFields = includeFieldsFactory.getStudyLevelFields(); - - try { - let docs = await mongoose.model("dicomStudy").find({ - ...queryOptions.query, - deleteStatus: { - $eq: 0 - } - }, studyFields) - .limit(queryOptions.limit) - .skip(queryOptions.skip) - .setOptions({ - strictQuery: false - }) - .exec(); - - let studyDicomJson = docs.map((v) => { - let obj = v.toObject(); - delete obj._id; - delete obj.id; - obj["00081190"] = { - vr: "UR", - Value: [`${queryOptions.retrieveBaseUrl}/${obj["0020000D"]["Value"][0]}`] - }; - - _.set(obj, dictionary.keyword.RetrieveAETitle, { - ...dictionary.tagVR[dictionary.keyword.RetrieveAETitle], - Value: [raccoonConfig.aeTitle] - }); - - return obj; - }); - - return studyDicomJson; - - } catch (e) { - throw e; - } -}; - -/** - * - * @param {Object} iParam - * @param {string} iParam.studyUID - */ -dicomStudySchema.statics.getPathGroupOfInstances = async function (iParam) { - let { studyUID } = iParam; - try { - let query = [ - { - $match: { - studyUID: studyUID - } - }, - { - $group: { - _id: "$studyUID", - pathList: { - $addToSet: { - studyUID: "$studyUID", - seriesUID: "$seriesUID", - instanceUID: "$instanceUID", - instancePath: "$instancePath" - } - } - } - } - ]; - let docs = await mongoose.model("dicom").aggregate(query).exec(); - let pathGroup = _.get(docs, "0.pathList", []); - - let fullPathGroup = getStoreDicomFullPathGroup(pathGroup); - - return fullPathGroup; - - } catch (e) { - throw e; - } -}; - -/** - * @typedef { mongoose.Model & { - * getPathGroupOfInstances: function(iParam: { - * studyUID: string, - * }): Promise; - * getDicomJson: function(queryOptions: import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions): Promise - * }} DicomStudyModel -*/ - -/** @type { DicomStudyModel } */ let dicomStudyModel = mongoose.model( "dicomStudy", dicomStudySchema, diff --git a/models/mongodb/models/patient.js b/models/mongodb/models/patient.js index 5dc47b09..4c945b5f 100644 --- a/models/mongodb/models/patient.js +++ b/models/mongodb/models/patient.js @@ -35,16 +35,8 @@ let patientSchema = new mongoose.Schema( toObject: { getters: true }, - statics: { - getDimseResultCursor : async function (query, keys) { - return mongoose.model("patient").find(query, keys).setOptions({ - strictQuery: false - }) - .cursor(); - } - }, methods: { - getAttributes: async function() { + getAttributes: async function () { let patient = this.toObject(); delete patient._id; delete patient.id; @@ -52,6 +44,84 @@ let patientSchema = new mongoose.Schema( let jsonStr = JSON.stringify(patient); return await Common.getAttributesFromJsonString(jsonStr); } + }, + statics: { + getDimseResultCursor: async function (query, keys) { + return mongoose.model("patient").find(query, keys).setOptions({ + strictQuery: false + }) + .cursor(); + }, + /** + * + * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions + * @returns + */ + getDicomJson: async function (queryOptions) { + let patientFields = getPatientLevelFields(); + + try { + let docs = await mongoose.model("patient").find(queryOptions.query, patientFields) + .limit(queryOptions.limit) + .skip(queryOptions.skip) + .setOptions({ + strictQuery: false + }) + .exec(); + + + let patientDicomJson = docs.map((v) => { + let obj = v.toObject(); + delete obj._id; + delete obj.id; + return obj; + }); + + return patientDicomJson; + + } catch (e) { + throw e; + } + }, + /** + * + * @param {Object} iParam + * @param {string} iParam.studyUID + */ + getPathGroupOfInstances: async function (iParam) { + let { patientID } = iParam; + try { + let query = [ + { + $match: { + "00100020.Value": patientID + } + }, + { + $group: { + _id: "$studyUID", + pathList: { + $addToSet: { + studyUID: "$studyUID", + seriesUID: "$seriesUID", + instanceUID: "$instanceUID", + instancePath: "$instancePath" + } + } + } + } + ]; + let docs = await mongoose.model("dicom").aggregate(query).exec(); + let pathGroup = _.get(docs, "0.pathList", []); + + let fullPathGroup = getStoreDicomFullPathGroup(pathGroup); + + return fullPathGroup; + + } catch (e) { + throw e; + } + } } } ); @@ -72,37 +142,6 @@ patientSchema.index({ "00100020": 1 }); -/** - * - * @param {import("../../../utils/typeDef/dicom").DicomJsonMongoQueryOptions} queryOptions - * @returns - */ -patientSchema.statics.getDicomJson = async function (queryOptions) { - let patientFields = getPatientLevelFields(); - - try { - let docs = await mongoose.model("patient").find(queryOptions.query, patientFields) - .limit(queryOptions.limit) - .skip(queryOptions.skip) - .setOptions({ - strictQuery: false - }) - .exec(); - - - let patientDicomJson = docs.map((v) => { - let obj = v.toObject(); - delete obj._id; - delete obj.id; - return obj; - }); - - return patientDicomJson; - - } catch (e) { - throw e; - } -}; function getPatientLevelFields() { let fields = {}; @@ -112,63 +151,12 @@ function getPatientLevelFields() { return fields; } -/** - * - * @param {Object} iParam - * @param {string} iParam.studyUID - */ -patientSchema.statics.getPathGroupOfInstances = async function (iParam) { - let { patientID } = iParam; - try { - let query = [ - { - $match: { - "00100020.Value": patientID - } - }, - { - $group: { - _id: "$studyUID", - pathList: { - $addToSet: { - studyUID: "$studyUID", - seriesUID: "$seriesUID", - instanceUID: "$instanceUID", - instancePath: "$instancePath" - } - } - } - } - ]; - let docs = await mongoose.model("dicom").aggregate(query).exec(); - let pathGroup = _.get(docs, "0.pathList", []); - - let fullPathGroup = getStoreDicomFullPathGroup(pathGroup); - - return fullPathGroup; - - } catch (e) { - throw e; - } -}; - -/** - * @typedef { mongoose.Model & { - * getPathGroupOfInstances: function(iParam: { - * patientID: string - * }): Promise; - * getDicomJson: function(queryOptions: DicomJsonMongoQueryOptions): Promise - * }} PatientModel -*/ - -/** @type {PatientModel} */ let patientModel = mongoose.model( "patient", patientSchema, "patient" ); -/** @type {PatientModel} */ module.exports = patientModel; module.exports.getPatientLevelFields = getPatientLevelFields;