Skip to content

Commit

Permalink
fix: cannot query with comma of PN attributes
Browse files Browse the repository at this point in the history
# Problems
- Use spread syntax would be overwrite value

# Solutions
- Combine value with [Op.or] to support comma or search
  • Loading branch information
Chinlinlee committed Aug 10, 2023
1 parent 5c9b223 commit 45a8fcd
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { VerifyIngObserverSqModel } = require("@models/sql/models/verifyingObserv
const { PersonNameModel } = require("@models/sql/models/personName.model");
const sequelize = require("@models/sql/instance");
const { SeriesQueryBuilder } = require("./seriesQueryBuilder");
const { Op } = require("sequelize");

class InstanceQueryBuilder extends BaseQueryBuilder {
constructor(queryOptions) {
Expand Down Expand Up @@ -35,7 +36,7 @@ class InstanceQueryBuilder extends BaseQueryBuilder {
this["0040A073.0040A075"] = VerifyingObserverQueryBuilder.prototype.getName.bind(verifyingObserverQueryBuilder);
this["0040A073.0040A030"] = VerifyingObserverQueryBuilder.prototype.getDateTime.bind(verifyingObserverQueryBuilder);
this["0040A073.0040A027"] = VerifyingObserverQueryBuilder.prototype.getOrganization.bind(verifyingObserverQueryBuilder);


let seriesQueryBuilder = new SeriesQueryBuilder(queryOptions);
let seriesQuery = seriesQueryBuilder.build();
Expand Down Expand Up @@ -122,7 +123,7 @@ class ConceptNameCodeSqQueryBuilder {
}

isModelIncluded() {
return this.instanceQueryBuilder.includeQueries.find(v=> v.model.getTableName() === "ConceptNameCodeSQ");
return this.instanceQueryBuilder.includeQueries.find(v => v.model.getTableName() === "ConceptNameCodeSQ");
}

getCodeValue(value) {
Expand All @@ -136,7 +137,7 @@ class ConceptNameCodeSqQueryBuilder {
}

getCodingSchemeVersion(value) {
let q = this.instanceQueryBuilder.getStringQuery(dictionary.keyword.CodingSchemeVersion ,value);
let q = this.instanceQueryBuilder.getStringQuery(dictionary.keyword.CodingSchemeVersion, value);
this.addQuery(q);
}

Expand Down Expand Up @@ -172,21 +173,21 @@ class ContentSqQueryBuilder {
}

isModelIncluded() {
return this.instanceQueryBuilder.includeQueries.find(v=> v.model.getTableName() === "DicomContentSQ");
return this.instanceQueryBuilder.includeQueries.find(v => v.model.getTableName() === "DicomContentSQ");
}

isConceptNameCodeInclude() {
let currentModel = this.isModelIncluded();
if (currentModel && currentModel.include) {
return currentModel.include.find( v => v.model.getTableName() === "ConceptNameCode");
return currentModel.include.find(v => v.model.getTableName() === "ConceptNameCode");
}
return false;
}

isConceptCodeInclude() {
let currentModel = this.isModelIncluded();
if (currentModel && currentModel.include) {
return currentModel.include.find( v=> v.model.getTableName() === "ConceptCode");
return currentModel.include.find(v => v.model.getTableName() === "ConceptCode");
}
return false;
}
Expand Down Expand Up @@ -226,7 +227,7 @@ class ContentSqQueryBuilder {
this.addConceptNameCodeQuery(q);
}

getConceptCodeValue (value) {
getConceptCodeValue(value) {
let q = this.instanceQueryBuilder.getStringQuery(dictionary.keyword.CodeValue, value);
this.addConceptCodeQuery(q);
}
Expand Down Expand Up @@ -266,7 +267,7 @@ class ContentSqQueryBuilder {

addConceptNameCodeQuery(q) {
if (!this.conceptNameCodeInclude) {
this.conceptNameCodeInclude = {
this.conceptNameCodeInclude = {
model: DicomCodeModel,
as: "ConceptNameCode",
where: {
Expand Down Expand Up @@ -296,7 +297,7 @@ class ContentSqQueryBuilder {

addConceptCodeQuery(q) {
if (!this.conceptCodeInclude) {
this.conceptCodeInclude = {
this.conceptCodeInclude = {
model: DicomCodeModel,
as: "ConceptCode",
where: {
Expand Down Expand Up @@ -332,36 +333,53 @@ class VerifyingObserverQueryBuilder {
}

isModelIncluded() {
return this.instanceQueryBuilder.includeQueries.find(v=> v.model.getTableName() === "VerifyingObserverSQ");
return this.instanceQueryBuilder.includeQueries.find(v => v.model.getTableName() === "VerifyingObserverSQ");
}

isPersonNameIncluded() {
let currentModel = this.isModelIncluded();
if (currentModel && currentModel.include) {
return currentModel.include.find( v => v.model.getTableName() === "PersonName");
return currentModel.include.find(v => v.model.getTableName() === "PersonName");
}
return false;
}

getName(value) {
let q = this.instanceQueryBuilder.getPersonNameQuery(dictionary.keyword.VerifyingObserverName, value);
let { query } = this.instanceQueryBuilder.getPersonNameQuery(dictionary.keyword.VerifyingObserverName, value);
this.addQuery({});
let currentModel = this.isModelIncluded();
currentModel.include = currentModel.include ? currentModel.include : [];
let personNameIncluded = this.isPersonNameIncluded();
if (personNameIncluded) {
personNameIncluded.where = {
...personNameIncluded.where,
...q.query
};
} else {
if (!personNameIncluded) {
currentModel.include.push({
model: PersonNameModel,
where: {
...q.query
[Op.or]: [
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
]
},
attributes: []
});
} else {
personNameIncluded.where[Op.or].push(...[
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,58 @@ const { dictionary } = require("@models/DICOM/dicom-tags-dic");
const { BaseQueryBuilder } = require("./querybuilder");
const sequelize = require("@models/sql/instance");
const { PersonNameModel } = require("@models/sql/models/personName.model");
const { Op } = require("sequelize");

class PatientQueryBuilder extends BaseQueryBuilder {
constructor(queryOptions) {
super(queryOptions);
this.query = {};
}

getIncludedPersonNameModel() {
if (this.includeQueries.length > 0) {
return this.includeQueries.find(v => v.model.getTableName() === "PersonName");
}
return undefined;
}

getPatientName(value) {
let { query } = this.getPersonNameQuery(dictionary.keyword.PatientName, value);
this.includeQueries.push({
model: PersonNameModel,
required: true,
where: {
...query
}
});

let includedPersonNameModel = this.getIncludedPersonNameModel();
if (!includedPersonNameModel) {
this.includeQueries.push({
model: PersonNameModel,
required: true,
where: {
[Op.or]: [
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
]
}
});
} else {
includedPersonNameModel.where[Op.or] = [
...includedPersonNameModel.where[Op.or],
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
];
}

}

getPatientID(value) {
Expand Down
111 changes: 94 additions & 17 deletions api-sql/dicom-web/controller/QIDO-RS/service/querybuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { dictionary } = require("@models/DICOM/dicom-tags-dic");
const { Op, Sequelize, cast, col } = require("sequelize");
const { raccoonConfig } = require("@root/config-class");
const { PersonNameModel } = require("@models/sql/models/personName.model");
const { PatientModel } = require("@models/sql/models/patient.model");
const sequelize = require("@models/sql/instance");

class BaseQueryBuilder {
Expand Down Expand Up @@ -41,7 +40,7 @@ class BaseQueryBuilder {
*/
getQueryByParam_(key) {
let value = this.queryOptions.query[key];
let values = Array.isArray(value) ? value: [value];
let values = Array.isArray(value) ? value : [value];

for (let i = 0; i < values.length; i++) {
let paramValue = values[i];
Expand Down Expand Up @@ -310,6 +309,28 @@ class StudyQueryBuilder extends BaseQueryBuilder {
}
}

getIncludedPatientModel() {
if (this.includeQueries.length > 0) {
return this.includeQueries.find(v => v.model.getTableName() === "Patient");
}
return undefined;
}

getIncludedPersonNameModelInPatient() {
let includedPatientModel = this.getIncludedPatientModel();
if (includedPatientModel) {
return includedPatientModel.include.find(v => v.model.getTableName() === "PersonName");
}
return undefined;
}

getIncludedPersonNameModel() {
if (this.includeQueries.length > 0) {
return this.includeQueries.find(v => v.model.getTableName() === "PersonName");
}
return undefined;
}


getStudyInstanceUID(value) {
let q = this.getStringQuery(dictionary.keyword.StudyInstanceUID, value);
Expand All @@ -320,16 +341,45 @@ class StudyQueryBuilder extends BaseQueryBuilder {
}

getPatientName(value) {
let q = this.getPersonNameQuery(dictionary.keyword.PatientName, value);
this.includeQueries.push({
model: PatientModel,
include: [{
model: PersonNameModel,
where: q.query,
let { query } = this.getPersonNameQuery(dictionary.keyword.PatientName, value);
let includedPatientModel = this.getIncludedPatientModel();
if (!includedPatientModel) {
this.includeQueries.push({
model: sequelize.model("Patient"),
include: [{
model: sequelize.model("PersonName"),
where: {
[Op.or]: [
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
]
},
required: true
}],
required: true
}],
required: true
});
});
} else {
let personNameModel = this.getIncludedPersonNameModelInPatient();
personNameModel.where[Op.or].push(...[
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
]);
}

}

getPatientID(value) {
Expand Down Expand Up @@ -376,12 +426,39 @@ class StudyQueryBuilder extends BaseQueryBuilder {
}

getReferringPhysicianName(value) {
let q = this.getPersonNameQuery(dictionary.keyword.ReferringPhysicianName, value);
this.includeQueries.push({
model: PersonNameModel,
where: q.where,
required: true
});
let { query } = this.getPersonNameQuery(dictionary.keyword.ReferringPhysicianName, value);
let includedPersonNameModel = this.getIncludedPersonNameModel();
if (!includedPersonNameModel) {
this.includeQueries.push({
model: PersonNameModel,
where: {
[Op.or]: [
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
]
},
required: true
});
} else {
includedPersonNameModel.where[Op.or].push(...[
{
alphabetic: query[Op.or].alphabetic
},
{
ideographic: query[Op.or].ideographic
},
{
phonetic: query[Op.or].phonetic
}
]);
}
}

getStudyID(value) {
Expand Down
Loading

0 comments on commit 45a8fcd

Please sign in to comment.