From c84fd4f744dd13b8645a5c67c380d3c01a41aa80 Mon Sep 17 00:00:00 2001 From: Epsilonn Date: Mon, 2 May 2016 20:18:21 +0300 Subject: [PATCH 1/2] fix issue #391 --- .gitignore | 3 +++ app/services/config.js | 14 +++++++---- app/services/database.js | 49 ++++++++++++++++++++++++++++++-------- app/services/filesystem.js | 2 +- config/environment.js | 2 +- tests/fixtures/default.txt | 7 ------ 6 files changed, 54 insertions(+), 23 deletions(-) delete mode 100644 tests/fixtures/default.txt diff --git a/.gitignore b/.gitignore index 71ac584da7..feed91778f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # See http://help.github.com/ignore-files/ for more about ignoring files. +/.idea # compiled output /dist @@ -29,3 +30,5 @@ server/config.js newrelic_agent.log newrelic.js + +/async-disk-cache \ No newline at end of file diff --git a/app/services/config.js b/app/services/config.js index 6c2e51622a..b083433b90 100644 --- a/app/services/config.js +++ b/app/services/config.js @@ -15,7 +15,13 @@ export default Ember.Service.extend({ this.set('configDB', db); this.setCurrentUser(); return db; - }).then(replicateConfigDB).then(loadConfig); + }) + .then(replicateConfigDB) + .catch((err) => { + console.log('replicate db error', err); + }) + .finally(loadConfig) + .catch((err)=>console.log(err)); }, createDB() { @@ -51,13 +57,13 @@ export default Ember.Service.extend({ console.log('Could not get configDB configs:', err); reject(err); } - const config = {}; + const configObj = {}; for (var i = 0; i < response.rows.length; i++) { if (!response.rows[i].error && response.rows[i].doc) { - config[response.rows[i].id] = response.rows[i].doc.value; + configObj[response.rows[i].id] = response.rows[i].doc.value; } } - resolve(config); + resolve(configObj); }); }, 'getting configuration from the database'); }, diff --git a/app/services/database.js b/app/services/database.js index 273eb3a645..244b238fb5 100644 --- a/app/services/database.js +++ b/app/services/database.js @@ -21,7 +21,7 @@ export default Ember.Service.extend(PouchAdapterUtils, { createDB(configs) { return new Ember.RSVP.Promise((resolve, reject) => { let pouchOptions = {}; - if (configs.config_use_google_auth) { + if (configs && configs.config_use_google_auth) { pouchOptions.ajax = { timeout: 30000 }; @@ -43,15 +43,13 @@ export default Ember.Service.extend(PouchAdapterUtils, { } } const url = `${document.location.protocol}//${document.location.host}/db/main`; - new PouchDB(url, pouchOptions, (err, db) => { - if (err) { - reject(err); - return; - } - createPouchViews(db); - resolve(db); - }); - }); + + this._createRemoteDB(url, pouchOptions) + .catch(() => this._createLocalDB('localMainDB', pouchOptions)) + .then((db) => resolve(db)) + .catch((err) => reject(err)); + + }, 'initialize application db'); }, queryMainDB(queryParams, mapReduce) { @@ -154,5 +152,36 @@ export default Ember.Service.extend(PouchAdapterUtils, { }); } return mappedRows; + }, + + _createRemoteDB(remoteUrl, pouchOptions) { + return new Ember.RSVP.Promise(function(resolve, reject) { + new PouchDB(remoteUrl, pouchOptions, (errRemote, remoteDB) => { + if (errRemote) { + reject(errRemote); + return; + } + + // remote db lazy created, check if db created correctly + remoteDB.info().then(() => { + createPouchViews(remoteDB); + resolve(remoteDB); + }).catch((err) => reject(err)); + }); + }); + }, + + _createLocalDB(localDBName, pouchOptions) { + return new Ember.RSVP.Promise(function(resolve, reject) { + new PouchDB(localDBName, pouchOptions, (errLocal, localDB) => { + if (errLocal) { + reject(errLocal); + return; + } + + createPouchViews(localDB); + resolve(localDB); + }); + }); } }); diff --git a/app/services/filesystem.js b/app/services/filesystem.js index 33062ae61e..e7e2e627cf 100644 --- a/app/services/filesystem.js +++ b/app/services/filesystem.js @@ -191,7 +191,7 @@ export default Ember.Service.extend({ }, /** - * Property to to determine if file system API is available. + * Property to determine if file system API is available. */ isFileSystemEnabled: function() { var filer = this.get('filer'); diff --git a/config/environment.js b/config/environment.js index 0ce4b8512c..eaf4482546 100644 --- a/config/environment.js +++ b/config/environment.js @@ -62,7 +62,7 @@ module.exports = function(environment) { if (environment === 'production') { ENV.serviceWorker.debug = false; } - + return ENV; }; diff --git a/tests/fixtures/default.txt b/tests/fixtures/default.txt deleted file mode 100644 index 5042fe5fe8..0000000000 --- a/tests/fixtures/default.txt +++ /dev/null @@ -1,7 +0,0 @@ -{"version":"1.2.4","db_type":"http","start_time":"2015-09-28T17:32:33.881Z","db_info":{"db_name":"main","doc_count":21,"doc_del_count":0,"update_seq":22,"purge_seq":0,"compact_running":false,"disk_size":36964,"data_size":12859,"instance_start_time":"1443461538466714","disk_format_version":6,"committed_update_seq":22,"host":"http://localhost:4200/db/main/","auto_compaction":false}} -{"docs":[{"_id":"_design/appointments_by_date","_rev":"1-4bdc8957d4338cde0bdce05a2ecfe7b5","version":4,"lists":{"sort":"function(head, req) {function keysEqual(keyA, keyB) {for (var i= 0; i < keyA.length; i++) {if (keyA[i] !== keyB[i]) {return false;}}return true;}function getCompareDate(dateString) {if (!dateString || dateString === \"\") {return 0;}return new Date(dateString).getTime();}function compareStrings(aString, bString) {if (!aString) {aString = \"\";}if (!bString) {bString = \"\";}if (aString < bString) {return -1;} else if (aString > bString) {return 1;} else {return 0;}}var row,rows=[],startingPosition = 0;while(row = getRow()) {rows.push(row);}rows = rows.filter(function (row) {\n var i,\n filterBy = null,\n includeRow = true;\n if (req.query && req.query.filterBy) {\n filterBy = JSON.parse(req.query.filterBy);\n }\n if (!filterBy) {\n return true;\n }\n for (i = 0; i < filterBy.length; i++) {\n var currentValue = row.doc.data[filterBy[i].name];\n if (filterBy[i].name === 'status' && (!currentValue || currentValue === '')) {\n currentValue = 'Scheduled';\n }\n if (currentValue !== filterBy[i].value) {\n includeRow = false;\n break;\n }\n }\n return includeRow;\n });rows.sort(function (a, b) {\n function defaultStatus(value) {\n if (!value || value === '') {\n return 'Scheduled';\n } else {\n return value;\n }\n }\n var sortBy = '';\n if (req.query && req.query.sortKey) {\n sortBy = req.query.sortKey;\n }\n switch (sortBy) {\n case 'appointmentType':\n case 'location':\n case 'provider':\n return compareStrings(a.doc.data[sortBy], b.doc.data[sortBy]);\n case 'date':\n {\n var startDiff = getCompareDate(a.doc.data.startDate) - getCompareDate(b.doc.data.startDate);\n if (startDiff === 0) {\n return getCompareDate(a.doc.data.endDate) - getCompareDate(b.doc.data.endDate);\n } else {\n return startDiff;\n }\n break;\n }\n case 'status':\n {\n var aStatus = defaultStatus(a.doc.data[sortBy]),\n bStatus = defaultStatus(b.doc.data[sortBy]);\n return compareStrings(aStatus, bStatus);\n }\n default:\n {\n return 0; //Don't sort\n }\n }\n });if (req.query.sortStartKey) {var startKey = JSON.parse(req.query.sortStartKey);for (var i=0; i 0) {rows = rows.slice(startingPosition);}send(JSON.stringify({\"rows\" : rows}));}"},"views":{"appointments_by_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"appointment\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([startDate, endDate, doc._id]);}}}"}}}]} -{"seq":2} -{"docs":[{"_id":"_design/appointments_by_patient","_rev":"1-9868b6586898a48c3fd68722ca3fe1d4","version":3,"views":{"appointments_by_patient":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"appointment\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([doc.data.patient, startDate, endDate,doc._id]);}}}"}}}]} -{"seq":3} -{"docs":[{"_id":"_design/imaging_by_status","_rev":"1-9236042e1fb9f567d1d527bbb429cd8e","version":3,"views":{"imaging_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"imaging\") {var imagingDate = doc.data.imagingDate;if (imagingDate && imagingDate !== \"\") {imagingDate = new Date(imagingDate);if (imagingDate.getTime) {imagingDate = imagingDate.getTime();}}var requestedDate = doc.data.requestedDate;if (requestedDate && requestedDate !== \"\") {requestedDate = new Date(requestedDate);if (requestedDate.getTime) {requestedDate = requestedDate.getTime();}}emit([doc.data.status, requestedDate, imagingDate, doc._id]);}}}"}}},{"_id":"_design/inventory_by_name","_rev":"1-a1cd6b8922ed0f24a090ba9fd96f5c05","version":3,"lists":{"sort":"function(head, req) {function keysEqual(keyA, keyB) {for (var i= 0; i < keyA.length; i++) {if (keyA[i] !== keyB[i]) {return false;}}return true;}function compareStrings(aString, bString) {if (!aString) {aString = \"\";}if (!bString) {bString = \"\";}if (aString < bString) {return -1;} else if (aString > bString) {return 1;} else {return 0;}}var row,rows=[],startingPosition = 0;while(row = getRow()) {rows.push(row);}rows.sort(function (a, b) {\n var sortBy = '';\n if (req.query && req.query.sortKey) {\n sortBy = req.query.sortKey;\n }\n switch (sortBy) {\n case 'crossReference':\n case 'description':\n case 'friendlyId':\n case 'name':\n case 'price':\n case 'quantity':\n case 'inventoryType':\n {\n return compareStrings(a.doc.data[sortBy], b.doc.data[sortBy]);\n }\n default:\n {\n return 0; //Don't sort\n }\n }\n });if (req.query.sortStartKey) {var startKey = JSON.parse(req.query.sortStartKey);for (var i=0; i 0) {rows = rows.slice(startingPosition);}send(JSON.stringify({\"rows\" : rows}));}"},"views":{"inventory_by_name":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"inventory\") {emit([doc.data.name, doc._id]);}}}"}}},{"_id":"_design/inventory_by_type","_rev":"1-003ef699ab0f2b8628c0bf613fabf7a2","version":4,"views":{"inventory_by_type":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"inventory\") {emit(doc.data.inventoryType);}}}"}}},{"_id":"_design/inventory_purchase_by_date_received","_rev":"1-78939ec45bef330bbfa87763dbbfce37","version":4,"views":{"inventory_purchase_by_date_received":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invPurchase\") {var dateReceived = doc.data.dateReceived;if (dateReceived && dateReceived !== \"\") {dateReceived = new Date(dateReceived);if (dateReceived.getTime) {dateReceived = dateReceived.getTime();}}emit([dateReceived, doc._id]);}}}"}}},{"_id":"_design/inventory_purchase_by_expiration_date","_rev":"1-17bd9d3619ae03155d9929d3ebcd5023","version":4,"views":{"inventory_purchase_by_expiration_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invPurchase\") {var expirationDate = doc.data.expirationDate;if (expirationDate && expirationDate !== \"\") {expirationDate = new Date(expirationDate);if (expirationDate.getTime) {expirationDate = expirationDate.getTime();}}emit([expirationDate, doc._id]);}}}"}}},{"_id":"_design/inventory_request_by_item","_rev":"1-7e7ef275a9fc15f2b5f328b1479c6097","version":4,"views":{"inventory_request_by_item":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invRequest\") {var dateCompleted = doc.data.dateCompleted;if (dateCompleted && dateCompleted !== \"\") {dateCompleted = new Date(dateCompleted);if (dateCompleted.getTime) {dateCompleted = dateCompleted.getTime();}}emit([doc.data.inventoryItem, doc.data.status, dateCompleted]);}}}"}}},{"_id":"_design/inventory_request_by_status","_rev":"1-187394c79d3206e11e112891f0d63550","version":4,"views":{"inventory_request_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invRequest\") {var dateCompleted = doc.data.dateCompleted;if (dateCompleted && dateCompleted !== \"\") {dateCompleted = new Date(dateCompleted);if (dateCompleted.getTime) {dateCompleted = dateCompleted.getTime();}}emit([doc.data.status, dateCompleted, doc._id]);}}}"}}},{"_id":"_design/invoice_by_status","_rev":"1-73e3e45673ed0c4f4ae1cee855f216f0","version":3,"views":{"invoice_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invoice\") {var billDate = doc.data.billDate;if (billDate && billDate !== \"\") {billDate = new Date(billDate);if (billDate.getTime) {billDate = billDate.getTime();}}emit([doc.data.status, billDate, doc._id]);}}}"}}},{"_id":"_design/lab_by_status","_rev":"1-db4393c6856b98a40afd6ceef31db2a7","version":3,"views":{"lab_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"lab\") {var labDate = doc.data.labDate;if (labDate && labDate !== \"\") {labDate = new Date(labDate);if (labDate.getTime) {labDate = labDate.getTime();}}var requestedDate = doc.data.requestedDate;if (requestedDate && requestedDate !== \"\") {requestedDate = new Date(requestedDate);if (requestedDate.getTime) {requestedDate = requestedDate.getTime();}}emit([doc.data.status, requestedDate, labDate, doc._id]);}}}"}}},{"_id":"_design/medication_by_status","_rev":"1-36ce9e9853552f5eb646ed26de7184dc","version":3,"views":{"medication_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"medication\") {var prescriptionDate = doc.data.prescriptionDate;if (prescriptionDate && prescriptionDate !== \"\") {prescriptionDate = new Date(prescriptionDate);if (prescriptionDate.getTime) {prescriptionDate = prescriptionDate.getTime();}}var requestedDate = doc.data.requestedDate;if (requestedDate && requestedDate !== \"\") {requestedDate = new Date(requestedDate);if (requestedDate.getTime) {requestedDate = requestedDate.getTime();}}emit([doc.data.status, requestedDate, prescriptionDate, doc._id]);}}}"}}},{"_id":"_design/patient_by_display_id","_rev":"1-e6d44a1448953362319c910bf33a8668","version":4,"lists":{"sort":"function(head, req) {function keysEqual(keyA, keyB) {for (var i= 0; i < keyA.length; i++) {if (keyA[i] !== keyB[i]) {return false;}}return true;}function getCompareDate(dateString) {if (!dateString || dateString === \"\") {return 0;}return new Date(dateString).getTime();}function compareStrings(aString, bString) {if (!aString) {aString = \"\";}if (!bString) {bString = \"\";}if (aString < bString) {return -1;} else if (aString > bString) {return 1;} else {return 0;}}var row,rows=[],startingPosition = 0;while(row = getRow()) {rows.push(row);}rows.sort(function (a, b) {\n var sortBy = '';\n if (req.query && req.query.sortKey) {\n sortBy = req.query.sortKey;\n }\n switch (sortBy) {\n case 'firstName':\n case 'gender':\n case 'lastName':\n case 'status':\n {\n return compareStrings(a.doc.data[sortBy], b.doc.data[sortBy]);\n }\n case 'dateOfBirth':\n {\n return getCompareDate(a.doc.data.dateOfBirth) - getCompareDate(b.doc.data.dateOfBirth);\n }\n default:\n {\n return 0; //Don't sort\n }\n }\n });if (req.query.sortStartKey) {var startKey = JSON.parse(req.query.sortStartKey);for (var i=0; i 0) {rows = rows.slice(startingPosition);}send(JSON.stringify({\"rows\" : rows}));}"},"views":{"patient_by_display_id":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"patient\") {if (doc.data.friendlyId) {emit([doc.data.friendlyId, doc._id]);} else if (doc.data.externalPatientId) {emit([doc.data.externalPatientId, doc._id]);} else {emit([doc._id, doc._id]);}}}}"}}},{"_id":"_design/patient_by_status","_rev":"1-ff26a12cdb8747aae5bf26f9af3a1953","version":2,"views":{"patient_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"patient\") {emit(doc.data.status);}}}"}}},{"_id":"_design/photo_by_patient","_rev":"1-e3dc5506f7f370535b85be0adc59c7cc","version":3,"views":{"photo_by_patient":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"photo\") {emit(doc.data.patient);}}}"}}},{"_id":"_design/pricing_by_category","_rev":"1-23d44e44019170eadd086734c87e3976","version":4,"views":{"pricing_by_category":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"pricing\") {emit([doc.data.category, doc.data.name, doc.data.pricingType, doc._id]);}}}"}}},{"_id":"_design/procedure_by_date","_rev":"1-25ccef2a9dff3addd8c0d15cc79c363c","version":3,"views":{"procedure_by_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"procedure\") {var procedureDate = doc.data.procedureDate;if (procedureDate && procedureDate !== \"\") {procedureDate = new Date(procedureDate);if (procedureDate.getTime) {procedureDate = procedureDate.getTime();}}emit([procedureDate, doc._id]);}}}"}}},{"_id":"_design/sequence_by_prefix","_rev":"1-8ac3a62b4c0b624ebed3b68b27cd197e","version":3,"views":{"sequence_by_prefix":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"sequence\") {emit(doc.data.prefix);}}}"}}},{"_id":"_design/visit_by_date","_rev":"1-367a7fa42f1067f645190d560f83ef5e","version":3,"views":{"visit_by_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"visit\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([startDate, endDate, doc._id]);}}}"}}},{"_id":"_design/visit_by_discharge_date","_rev":"1-e53c95160dacaf007fe01c01215729ce","version":1,"views":{"visit_by_discharge_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"visit\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}emit([endDate, doc._id]);}}}"}}},{"_id":"_design/visit_by_patient","_rev":"1-412252178488867e2350dcd13da779ea","version":3,"views":{"visit_by_patient":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"visit\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([doc.data.patient, startDate, endDate, doc.data.visitType, doc._id]);}}}"}}}]} -{"seq":22} From 38de1f6bef2ca1cabf6a85829ce8a48f667cb02a Mon Sep 17 00:00:00 2001 From: Epsilonn Date: Mon, 2 May 2016 20:32:15 +0300 Subject: [PATCH 2/2] return default fixture --- tests/fixtures/default.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/fixtures/default.txt diff --git a/tests/fixtures/default.txt b/tests/fixtures/default.txt new file mode 100644 index 0000000000..5042fe5fe8 --- /dev/null +++ b/tests/fixtures/default.txt @@ -0,0 +1,7 @@ +{"version":"1.2.4","db_type":"http","start_time":"2015-09-28T17:32:33.881Z","db_info":{"db_name":"main","doc_count":21,"doc_del_count":0,"update_seq":22,"purge_seq":0,"compact_running":false,"disk_size":36964,"data_size":12859,"instance_start_time":"1443461538466714","disk_format_version":6,"committed_update_seq":22,"host":"http://localhost:4200/db/main/","auto_compaction":false}} +{"docs":[{"_id":"_design/appointments_by_date","_rev":"1-4bdc8957d4338cde0bdce05a2ecfe7b5","version":4,"lists":{"sort":"function(head, req) {function keysEqual(keyA, keyB) {for (var i= 0; i < keyA.length; i++) {if (keyA[i] !== keyB[i]) {return false;}}return true;}function getCompareDate(dateString) {if (!dateString || dateString === \"\") {return 0;}return new Date(dateString).getTime();}function compareStrings(aString, bString) {if (!aString) {aString = \"\";}if (!bString) {bString = \"\";}if (aString < bString) {return -1;} else if (aString > bString) {return 1;} else {return 0;}}var row,rows=[],startingPosition = 0;while(row = getRow()) {rows.push(row);}rows = rows.filter(function (row) {\n var i,\n filterBy = null,\n includeRow = true;\n if (req.query && req.query.filterBy) {\n filterBy = JSON.parse(req.query.filterBy);\n }\n if (!filterBy) {\n return true;\n }\n for (i = 0; i < filterBy.length; i++) {\n var currentValue = row.doc.data[filterBy[i].name];\n if (filterBy[i].name === 'status' && (!currentValue || currentValue === '')) {\n currentValue = 'Scheduled';\n }\n if (currentValue !== filterBy[i].value) {\n includeRow = false;\n break;\n }\n }\n return includeRow;\n });rows.sort(function (a, b) {\n function defaultStatus(value) {\n if (!value || value === '') {\n return 'Scheduled';\n } else {\n return value;\n }\n }\n var sortBy = '';\n if (req.query && req.query.sortKey) {\n sortBy = req.query.sortKey;\n }\n switch (sortBy) {\n case 'appointmentType':\n case 'location':\n case 'provider':\n return compareStrings(a.doc.data[sortBy], b.doc.data[sortBy]);\n case 'date':\n {\n var startDiff = getCompareDate(a.doc.data.startDate) - getCompareDate(b.doc.data.startDate);\n if (startDiff === 0) {\n return getCompareDate(a.doc.data.endDate) - getCompareDate(b.doc.data.endDate);\n } else {\n return startDiff;\n }\n break;\n }\n case 'status':\n {\n var aStatus = defaultStatus(a.doc.data[sortBy]),\n bStatus = defaultStatus(b.doc.data[sortBy]);\n return compareStrings(aStatus, bStatus);\n }\n default:\n {\n return 0; //Don't sort\n }\n }\n });if (req.query.sortStartKey) {var startKey = JSON.parse(req.query.sortStartKey);for (var i=0; i 0) {rows = rows.slice(startingPosition);}send(JSON.stringify({\"rows\" : rows}));}"},"views":{"appointments_by_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"appointment\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([startDate, endDate, doc._id]);}}}"}}}]} +{"seq":2} +{"docs":[{"_id":"_design/appointments_by_patient","_rev":"1-9868b6586898a48c3fd68722ca3fe1d4","version":3,"views":{"appointments_by_patient":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"appointment\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([doc.data.patient, startDate, endDate,doc._id]);}}}"}}}]} +{"seq":3} +{"docs":[{"_id":"_design/imaging_by_status","_rev":"1-9236042e1fb9f567d1d527bbb429cd8e","version":3,"views":{"imaging_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"imaging\") {var imagingDate = doc.data.imagingDate;if (imagingDate && imagingDate !== \"\") {imagingDate = new Date(imagingDate);if (imagingDate.getTime) {imagingDate = imagingDate.getTime();}}var requestedDate = doc.data.requestedDate;if (requestedDate && requestedDate !== \"\") {requestedDate = new Date(requestedDate);if (requestedDate.getTime) {requestedDate = requestedDate.getTime();}}emit([doc.data.status, requestedDate, imagingDate, doc._id]);}}}"}}},{"_id":"_design/inventory_by_name","_rev":"1-a1cd6b8922ed0f24a090ba9fd96f5c05","version":3,"lists":{"sort":"function(head, req) {function keysEqual(keyA, keyB) {for (var i= 0; i < keyA.length; i++) {if (keyA[i] !== keyB[i]) {return false;}}return true;}function compareStrings(aString, bString) {if (!aString) {aString = \"\";}if (!bString) {bString = \"\";}if (aString < bString) {return -1;} else if (aString > bString) {return 1;} else {return 0;}}var row,rows=[],startingPosition = 0;while(row = getRow()) {rows.push(row);}rows.sort(function (a, b) {\n var sortBy = '';\n if (req.query && req.query.sortKey) {\n sortBy = req.query.sortKey;\n }\n switch (sortBy) {\n case 'crossReference':\n case 'description':\n case 'friendlyId':\n case 'name':\n case 'price':\n case 'quantity':\n case 'inventoryType':\n {\n return compareStrings(a.doc.data[sortBy], b.doc.data[sortBy]);\n }\n default:\n {\n return 0; //Don't sort\n }\n }\n });if (req.query.sortStartKey) {var startKey = JSON.parse(req.query.sortStartKey);for (var i=0; i 0) {rows = rows.slice(startingPosition);}send(JSON.stringify({\"rows\" : rows}));}"},"views":{"inventory_by_name":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"inventory\") {emit([doc.data.name, doc._id]);}}}"}}},{"_id":"_design/inventory_by_type","_rev":"1-003ef699ab0f2b8628c0bf613fabf7a2","version":4,"views":{"inventory_by_type":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"inventory\") {emit(doc.data.inventoryType);}}}"}}},{"_id":"_design/inventory_purchase_by_date_received","_rev":"1-78939ec45bef330bbfa87763dbbfce37","version":4,"views":{"inventory_purchase_by_date_received":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invPurchase\") {var dateReceived = doc.data.dateReceived;if (dateReceived && dateReceived !== \"\") {dateReceived = new Date(dateReceived);if (dateReceived.getTime) {dateReceived = dateReceived.getTime();}}emit([dateReceived, doc._id]);}}}"}}},{"_id":"_design/inventory_purchase_by_expiration_date","_rev":"1-17bd9d3619ae03155d9929d3ebcd5023","version":4,"views":{"inventory_purchase_by_expiration_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invPurchase\") {var expirationDate = doc.data.expirationDate;if (expirationDate && expirationDate !== \"\") {expirationDate = new Date(expirationDate);if (expirationDate.getTime) {expirationDate = expirationDate.getTime();}}emit([expirationDate, doc._id]);}}}"}}},{"_id":"_design/inventory_request_by_item","_rev":"1-7e7ef275a9fc15f2b5f328b1479c6097","version":4,"views":{"inventory_request_by_item":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invRequest\") {var dateCompleted = doc.data.dateCompleted;if (dateCompleted && dateCompleted !== \"\") {dateCompleted = new Date(dateCompleted);if (dateCompleted.getTime) {dateCompleted = dateCompleted.getTime();}}emit([doc.data.inventoryItem, doc.data.status, dateCompleted]);}}}"}}},{"_id":"_design/inventory_request_by_status","_rev":"1-187394c79d3206e11e112891f0d63550","version":4,"views":{"inventory_request_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invRequest\") {var dateCompleted = doc.data.dateCompleted;if (dateCompleted && dateCompleted !== \"\") {dateCompleted = new Date(dateCompleted);if (dateCompleted.getTime) {dateCompleted = dateCompleted.getTime();}}emit([doc.data.status, dateCompleted, doc._id]);}}}"}}},{"_id":"_design/invoice_by_status","_rev":"1-73e3e45673ed0c4f4ae1cee855f216f0","version":3,"views":{"invoice_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"invoice\") {var billDate = doc.data.billDate;if (billDate && billDate !== \"\") {billDate = new Date(billDate);if (billDate.getTime) {billDate = billDate.getTime();}}emit([doc.data.status, billDate, doc._id]);}}}"}}},{"_id":"_design/lab_by_status","_rev":"1-db4393c6856b98a40afd6ceef31db2a7","version":3,"views":{"lab_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"lab\") {var labDate = doc.data.labDate;if (labDate && labDate !== \"\") {labDate = new Date(labDate);if (labDate.getTime) {labDate = labDate.getTime();}}var requestedDate = doc.data.requestedDate;if (requestedDate && requestedDate !== \"\") {requestedDate = new Date(requestedDate);if (requestedDate.getTime) {requestedDate = requestedDate.getTime();}}emit([doc.data.status, requestedDate, labDate, doc._id]);}}}"}}},{"_id":"_design/medication_by_status","_rev":"1-36ce9e9853552f5eb646ed26de7184dc","version":3,"views":{"medication_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"medication\") {var prescriptionDate = doc.data.prescriptionDate;if (prescriptionDate && prescriptionDate !== \"\") {prescriptionDate = new Date(prescriptionDate);if (prescriptionDate.getTime) {prescriptionDate = prescriptionDate.getTime();}}var requestedDate = doc.data.requestedDate;if (requestedDate && requestedDate !== \"\") {requestedDate = new Date(requestedDate);if (requestedDate.getTime) {requestedDate = requestedDate.getTime();}}emit([doc.data.status, requestedDate, prescriptionDate, doc._id]);}}}"}}},{"_id":"_design/patient_by_display_id","_rev":"1-e6d44a1448953362319c910bf33a8668","version":4,"lists":{"sort":"function(head, req) {function keysEqual(keyA, keyB) {for (var i= 0; i < keyA.length; i++) {if (keyA[i] !== keyB[i]) {return false;}}return true;}function getCompareDate(dateString) {if (!dateString || dateString === \"\") {return 0;}return new Date(dateString).getTime();}function compareStrings(aString, bString) {if (!aString) {aString = \"\";}if (!bString) {bString = \"\";}if (aString < bString) {return -1;} else if (aString > bString) {return 1;} else {return 0;}}var row,rows=[],startingPosition = 0;while(row = getRow()) {rows.push(row);}rows.sort(function (a, b) {\n var sortBy = '';\n if (req.query && req.query.sortKey) {\n sortBy = req.query.sortKey;\n }\n switch (sortBy) {\n case 'firstName':\n case 'gender':\n case 'lastName':\n case 'status':\n {\n return compareStrings(a.doc.data[sortBy], b.doc.data[sortBy]);\n }\n case 'dateOfBirth':\n {\n return getCompareDate(a.doc.data.dateOfBirth) - getCompareDate(b.doc.data.dateOfBirth);\n }\n default:\n {\n return 0; //Don't sort\n }\n }\n });if (req.query.sortStartKey) {var startKey = JSON.parse(req.query.sortStartKey);for (var i=0; i 0) {rows = rows.slice(startingPosition);}send(JSON.stringify({\"rows\" : rows}));}"},"views":{"patient_by_display_id":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"patient\") {if (doc.data.friendlyId) {emit([doc.data.friendlyId, doc._id]);} else if (doc.data.externalPatientId) {emit([doc.data.externalPatientId, doc._id]);} else {emit([doc._id, doc._id]);}}}}"}}},{"_id":"_design/patient_by_status","_rev":"1-ff26a12cdb8747aae5bf26f9af3a1953","version":2,"views":{"patient_by_status":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"patient\") {emit(doc.data.status);}}}"}}},{"_id":"_design/photo_by_patient","_rev":"1-e3dc5506f7f370535b85be0adc59c7cc","version":3,"views":{"photo_by_patient":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"photo\") {emit(doc.data.patient);}}}"}}},{"_id":"_design/pricing_by_category","_rev":"1-23d44e44019170eadd086734c87e3976","version":4,"views":{"pricing_by_category":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"pricing\") {emit([doc.data.category, doc.data.name, doc.data.pricingType, doc._id]);}}}"}}},{"_id":"_design/procedure_by_date","_rev":"1-25ccef2a9dff3addd8c0d15cc79c363c","version":3,"views":{"procedure_by_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"procedure\") {var procedureDate = doc.data.procedureDate;if (procedureDate && procedureDate !== \"\") {procedureDate = new Date(procedureDate);if (procedureDate.getTime) {procedureDate = procedureDate.getTime();}}emit([procedureDate, doc._id]);}}}"}}},{"_id":"_design/sequence_by_prefix","_rev":"1-8ac3a62b4c0b624ebed3b68b27cd197e","version":3,"views":{"sequence_by_prefix":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"sequence\") {emit(doc.data.prefix);}}}"}}},{"_id":"_design/visit_by_date","_rev":"1-367a7fa42f1067f645190d560f83ef5e","version":3,"views":{"visit_by_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"visit\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([startDate, endDate, doc._id]);}}}"}}},{"_id":"_design/visit_by_discharge_date","_rev":"1-e53c95160dacaf007fe01c01215729ce","version":1,"views":{"visit_by_discharge_date":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"visit\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}emit([endDate, doc._id]);}}}"}}},{"_id":"_design/visit_by_patient","_rev":"1-412252178488867e2350dcd13da779ea","version":3,"views":{"visit_by_patient":{"map":"function(doc) {var doctype,uidx;if (doc._id && (uidx = doc._id.indexOf(\"_\")) > 0) {doctype = doc._id.substring(0, uidx);if(doctype === \"visit\") {var endDate = doc.data.endDate;if (endDate && endDate !== \"\") {endDate = new Date(endDate);if (endDate.getTime) {endDate = endDate.getTime();}}var startDate = doc.data.startDate;if (startDate && startDate !== \"\") {startDate = new Date(startDate);if (startDate.getTime) {startDate = startDate.getTime();}}emit([doc.data.patient, startDate, endDate, doc.data.visitType, doc._id]);}}}"}}}]} +{"seq":22}