diff --git a/api/FHIR/Patient/controller/getPatient.js b/api/FHIR/Patient/controller/getPatient.js index 9b3b9cb9..a0820f1a 100644 --- a/api/FHIR/Patient/controller/getPatient.js +++ b/api/FHIR/Patient/controller/getPatient.js @@ -3,7 +3,7 @@ const mongodb = require('models/mongodb'); const { createBundle } = require('models/FHIR/func'); -const queryBuild = require('../../../../models/FHIR/queryBuild.js'); +const queryBuild = require('models/FHIR/queryBuild.js'); const { handleError } = require('models/FHIR/httpMessage'); @@ -22,6 +22,7 @@ const paramsSearch = { delete query["_id"]; } } + paramsSearch["_lastUpdated"] = (query) => { if (!_.isArray(query["_lastUpdated"])) { query["_lastUpdated"] = [query["_lastUpdated"]] @@ -35,7 +36,6 @@ paramsSearch["_lastUpdated"] = (query) => { } delete query["_lastUpdated"]; } - paramsSearchFields["address"] = ["address"]; paramsSearch["address"] = (query) => { if (!_.isArray(query["address"])) { @@ -164,7 +164,7 @@ paramsSearch["birthdate"] = (query) => { } delete query["birthdate"]; } -paramsSearchFields["email"] = ["telecom.where(system='email')"]; +paramsSearchFields["email"] = ["telecom"]; paramsSearch["email"] = (query) => { if (!_.isArray(query["email"])) { query["email"] = [query["email"]] @@ -240,7 +240,7 @@ paramsSearch["given"] = (query) => { } delete query['given']; } -paramsSearchFields["phone"] = ["telecom.where(system='phone')"]; +paramsSearchFields["phone"] = ["telecom"]; paramsSearch["phone"] = (query) => { if (!_.isArray(query["phone"])) { query["phone"] = [query["phone"]] @@ -314,7 +314,7 @@ paramsSearch["active"] = (query) => { } delete query['active']; } -paramsSearchFields["deceased"] = ["deceased.exists() and Patient.deceased != false"]; +paramsSearchFields["deceased"] = "deceased"; paramsSearch["deceased"] = (query) => { if (!_.isArray(query["deceased"])) { query["deceased"] = [query["deceased"]] diff --git a/api/FHIR/Patient/index.js b/api/FHIR/Patient/index.js index 6b0cf3a9..cfb42b5a 100644 --- a/api/FHIR/Patient/index.js +++ b/api/FHIR/Patient/index.js @@ -29,7 +29,7 @@ router.use((req, res, next) => { if (req.headers["content-type"]) { if (req.headers["content-type"].includes("xml")) { res.set('Content-Type', 'application/fhir+xml'); - if (req.method == "POST") { + if (req.method == "POST" || req.method == "PUT") { let Fhir = new FHIR(); req.body = Fhir.xmlToObj(req.body); } @@ -50,7 +50,9 @@ router.use((req, res, next) => { } }); -router.use(user.tokenAuthentication); +if (process.env.ENABLE_TOKEN_AUTH == "true") { + router.use(user.tokenAuthentication); +} if (_.get(config, "Patient.interaction.search", true)) { router.get('/', FHIRValidateParams({ diff --git a/models/mongodb/FHIRDataTypesSchema/Patient_Communication.js b/models/mongodb/FHIRDataTypesSchema/Patient_Communication.js index 2e98dd8c..7648cf4c 100644 --- a/models/mongodb/FHIRDataTypesSchema/Patient_Communication.js +++ b/models/mongodb/FHIRDataTypesSchema/Patient_Communication.js @@ -18,5 +18,9 @@ module.exports = new mongoose.Schema({ }, preferred: boolean }, { - _id: false + _id: false, + id: false, + toObject: { + getters: true + } }); \ No newline at end of file diff --git a/models/mongodb/FHIRDataTypesSchema/Patient_Contact.js b/models/mongodb/FHIRDataTypesSchema/Patient_Contact.js index a44c082c..23328d5f 100644 --- a/models/mongodb/FHIRDataTypesSchema/Patient_Contact.js +++ b/models/mongodb/FHIRDataTypesSchema/Patient_Contact.js @@ -45,5 +45,9 @@ module.exports = new mongoose.Schema({ default: void 0 } }, { - _id: false + _id: false, + id: false, + toObject: { + getters: true + } }); \ No newline at end of file diff --git a/models/mongodb/FHIRDataTypesSchema/Patient_Link.js b/models/mongodb/FHIRDataTypesSchema/Patient_Link.js index a31b3c26..4b05d941 100644 --- a/models/mongodb/FHIRDataTypesSchema/Patient_Link.js +++ b/models/mongodb/FHIRDataTypesSchema/Patient_Link.js @@ -21,5 +21,9 @@ module.exports = new mongoose.Schema({ default: void 0 } }, { - _id: false + _id: false, + id: false, + toObject: { + getters: true + } }); \ No newline at end of file diff --git a/models/mongodb/model/Patient.js b/models/mongodb/model/Patient.js index d5920c3b..367a3afa 100644 --- a/models/mongodb/model/Patient.js +++ b/models/mongodb/model/Patient.js @@ -11,7 +11,7 @@ const boolean = require('../FHIRDataTypesSchema/boolean'); const HumanName = require('../FHIRDataTypesSchema/HumanName'); const ContactPoint = require('../FHIRDataTypesSchema/ContactPoint'); const date = require('../FHIRDataTypesSchema/date'); -const string = require('../FHIRDataTypesSchema/string'); +const dateTime = require('../FHIRDataTypesSchema/dateTime'); const Address = require('../FHIRDataTypesSchema/Address'); const CodeableConcept = require('../FHIRDataTypesSchema/CodeableConcept'); const Attachment = require('../FHIRDataTypesSchema/Attachment'); @@ -61,7 +61,7 @@ module.exports = function() { }, birthDate: date, deceasedBoolean: boolean, - deceasedDateTime: string, + deceasedDateTime: dateTime, address: { type: [Address], default: void 0 @@ -112,8 +112,12 @@ module.exports = function() { ...id, index: true } + Patient.contained = { + type: [Object], + default: void 0 + } module.exports.schema = Patient; - const PatientSchema = new mongoose.Schema(Patient, { + let schemaConfig = { toObject: { getters: true }, @@ -121,13 +125,23 @@ module.exports = function() { getters: true }, versionKey: false - }); + }; + if (process.env.MONGODB_IS_SHARDING_MODE == "true") { + schemaConfig["shardKey"] = { + id: 1 + }; + } + const PatientSchema = new mongoose.Schema(Patient, schemaConfig); PatientSchema.methods.getFHIRField = function() { let result = this.toObject(); delete result._id; delete result.__v; + if (_.get(result, "myCollection")) { + let tempCollectionField = _.cloneDeep(result["myCollection"]); + _.set(result, "collection", tempCollectionField); + } return result; } @@ -136,7 +150,7 @@ module.exports = function() { let storedID = await mongodb.FHIRStoredID.findOne({ id: this.id }); - if (storedID) { + if (storedID && process.env.ENABLE_CHECK_ALL_RESOURCE_ID == "true") { if (storedID.resourceType == "Patient") { const docInHistory = await mongodb.Patient_history.findOne({ id: this.id @@ -164,8 +178,8 @@ module.exports = function() { let item = result.toObject(); delete item._id; let version = item.meta.versionId; + let port = (process.env.FHIRSERVER_PORT == "80" || process.env.FHIRSERVER_PORT == "443") ? "" : `:${process.env.FHIRSERVER_PORT}`; if (version == "1") { - let port = (process.env.FHIRSERVER_PORT == "80" || process.env.FHIRSERVER_PORT == "443") ? "" : `:${process.env.FHIRSERVER_PORT}`; _.set(item, "request", { "method": "POST", url: `http://${process.env.FHIRSERVER_HOST}${port}/${process.env.FHIRSERVER_APIPATH}/Patient/${item.id}/_history/${version}` @@ -174,6 +188,15 @@ module.exports = function() { status: "201" }); let createdDocs = await mongodb['Patient_history'].create(item); + } else { + _.set(item, "request", { + "method": "PUT", + url: `http://${process.env.FHIRSERVER_HOST}${port}/${process.env.FHIRSERVER_APIPATH}/Patient/${item.id}/_history/${version}` + }); + _.set(item, "response", { + status: "200" + }); + let createdDocs = await mongodb['Patient_history'].create(item); } await mongodb.FHIRStoredID.findOneAndUpdate({ id: result.id @@ -188,7 +211,7 @@ module.exports = function() { PatientSchema.pre('findOneAndUpdate', async function(next) { const docToUpdate = await this.model.findOne(this.getFilter()); let version = Number(docToUpdate.meta.versionId); - this._update.$set.meta = {}; + this._update.$set.meta = docToUpdate.meta; this._update.$set.meta.versionId = String(version + 1); this._update.$set.meta.lastUpdated = new Date(); return next(); diff --git a/models/mongodb/model/Patient_history.js b/models/mongodb/model/Patient_history.js index 2be61f0e..48ec0d43 100644 --- a/models/mongodb/model/Patient_history.js +++ b/models/mongodb/model/Patient_history.js @@ -34,10 +34,6 @@ module.exports = function() { PatientHistorySchema.methods.getFHIRField = function() { let result = this.toObject(); delete result._id; - let version = result.__v; - if (version) { - _.set(result, 'meta.versionId', version.toString()); - } delete result.__v; delete result['name._id']; delete result['request']; @@ -47,10 +43,6 @@ module.exports = function() { PatientHistorySchema.methods.getFHIRBundleField = function() { let result = this.toObject(); delete result._id; - let version = result.__v; - if (version) { - _.set(result, 'meta.versionId', version.toString()); - } delete result.__v; delete result['name._id']; return result;