From d6eff43b24e68ecf85139b04cdc0539ad4deaf2d Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 19 Jan 2024 16:03:18 +0530 Subject: [PATCH] avniproject/avni-client#1272 - DashboardCacheFilter class for all the fields support by Dashboard Cache. Print UUID if possible when throwing mandatory save error. Moved UI code to avni client. Deleted previous cache. --- src/DashboardCache.js | 57 +++++++++---------------- src/Schema.js | 6 ++- src/application/DashboardCacheFilter.js | 53 +++++++++++++++++++++++ src/framework/RealmProxy.js | 2 +- src/index.js | 4 +- 5 files changed, 83 insertions(+), 39 deletions(-) create mode 100644 src/application/DashboardCacheFilter.js diff --git a/src/DashboardCache.js b/src/DashboardCache.js index 19314981..e963ceb7 100644 --- a/src/DashboardCache.js +++ b/src/DashboardCache.js @@ -1,8 +1,7 @@ -import _ from 'lodash'; import BaseEntity from "./BaseEntity"; +import DashboardCacheFilter from "./application/DashboardCacheFilter"; -class DashboardCache extends BaseEntity{ - +class DashboardCache extends BaseEntity { static rowUUID = '176d5284-7927-422e-909a-a546f5001c84'; static schema = { @@ -16,71 +15,57 @@ class DashboardCache extends BaseEntity{ }, }; - constructor(that = null) { + constructor(that = null) { super(that); } get updatedAt() { - return this.that.updatedAt; + return this.that.updatedAt; } set updatedAt(x) { - this.that.updatedAt = x; + this.that.updatedAt = x; } get cardJSON() { - return this.that.cardJSON; + return this.that.cardJSON; } set cardJSON(x) { - this.that.cardJSON = x; + this.that.cardJSON = x; } get filterJSON() { - return this.that.filterJSON; + return this.that.filterJSON; } set filterJSON(x) { - this.that.filterJSON = x; + this.that.filterJSON = x; } - static create(updatedAt, cardJSON, filterJSON) { + static createEmptyInstance() { const dashboardCache = new DashboardCache(); dashboardCache.uuid = this.rowUUID; - dashboardCache.updatedAt = updatedAt; - dashboardCache.cardJSON = cardJSON; - dashboardCache.filterJSON = filterJSON; + dashboardCache.filterJSON = JSON.stringify(DashboardCacheFilter.createEmptyInstance()); + dashboardCache.cardJSON = "{}"; + dashboardCache.updatedAt = new Date(); return dashboardCache; } - static getFilterJSONFromState(state) { - const filterCache = { - date: state.date, - selectedPrograms: state.selectedPrograms, - selectedEncounterTypes: state.selectedEncounterTypes, - selectedGeneralEncounterTypes: state.selectedGeneralEncounterTypes, - selectedCustomFilters: state.selectedCustomFilters, - selectedGenders: state.selectedGenders, - programs: state.programs, - individualFilters: state.individualFilters, - encountersFilters: state.encountersFilters, - enrolmentFilters: state.enrolmentFilters, - generalEncountersFilters: state.generalEncountersFilters, - selectedSubjectType: state.subjectType - }; - return state.subjectType && _.isEmpty(state.subjectType.name) ? _.omit(filterCache, ['selectedSubjectType']) : filterCache; + getCard() { + return this.cardJSON && JSON.parse(this.cardJSON) || {}; } - static createEmptyInstance() { - return new DashboardCache(); + getFilter() { + return this.filterJSON && JSON.parse(this.filterJSON) || {}; } - getCardJSON() { - return this.cardJSON && JSON.parse(this.cardJSON) || {}; + setFilter(value) { + this.filterJSON = JSON.stringify(value); } - getFilterJSON() { - return this.filterJSON && JSON.parse(this.filterJSON) || {}; + setCard(value) { + this.cardJSON = JSON.stringify(value); } } diff --git a/src/Schema.js b/src/Schema.js index 6a59935b..36d86079 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -186,7 +186,7 @@ const entities = [ function createRealmConfig() { return { //order is important, should be arranged according to the dependency - schemaVersion: 182, + schemaVersion: 183, onMigration: function (oldDB, newDB) { console.log("[AvniModels.Schema]", `Running migration with old schema version: ${oldDB.schemaVersion} and new schema version: ${newDB.schemaVersion}`); if (oldDB.schemaVersion < 10) { @@ -818,6 +818,10 @@ function createRealmConfig() { rc.initCountOfCards = 1; }); } + if (newDB.schemaVersion < 183) { + const newObjects = newDB.objects("DashboardCache"); + newDB.delete(newObjects); + } }, }; } diff --git a/src/application/DashboardCacheFilter.js b/src/application/DashboardCacheFilter.js new file mode 100644 index 00000000..86246d18 --- /dev/null +++ b/src/application/DashboardCacheFilter.js @@ -0,0 +1,53 @@ +import _ from "lodash"; + +function overwriteField(field, source, destination, force) { + if (_.isNil(destination[field]) || force) + destination[field] = source[field]; +} + +class DashboardCacheFilter { + date; + selectedPrograms; + selectedEncounterTypes; + selectedGeneralEncounterTypes; + selectedCustomFilters; + selectedGenders; + individualFilters; + encountersFilters; + enrolmentFilters; + dueChecklistFilter; + generalEncountersFilters; + selectedSubjectTypeUUID; + selectedLocations; + selectedAddressesInfo; + + static overwriteFields(from, dashboardCacheFilter, force) { + overwriteField("date", from, dashboardCacheFilter, force); + overwriteField("selectedPrograms", from, dashboardCacheFilter, force); + overwriteField("selectedEncounterTypes", from, dashboardCacheFilter, force); + overwriteField("selectedGeneralEncounterTypes", from, dashboardCacheFilter, force); + overwriteField("selectedCustomFilters", from, dashboardCacheFilter, force); + overwriteField("selectedGenders", from, dashboardCacheFilter, force); + overwriteField("individualFilters", from, dashboardCacheFilter, force); + overwriteField("encountersFilters", from, dashboardCacheFilter, force); + overwriteField("enrolmentFilters", from, dashboardCacheFilter, force); + overwriteField("generalEncountersFilters", from, dashboardCacheFilter, force); + overwriteField("selectedSubjectTypeUUID", from, dashboardCacheFilter, force); + overwriteField("dueChecklistFilter", from, dashboardCacheFilter, force); + overwriteField("selectedLocations", from, dashboardCacheFilter, force); + overwriteField("selectedAddressesInfo", from, dashboardCacheFilter, force); + } + + static createEmptyInstance() { + const dashboardCacheFilter = new DashboardCacheFilter(); + dashboardCacheFilter.selectedPrograms = []; + dashboardCacheFilter.selectedEncounterTypes = []; + dashboardCacheFilter.selectedGeneralEncounterTypes = []; + dashboardCacheFilter.selectedCustomFilters = []; + dashboardCacheFilter.selectedGenders = []; + dashboardCacheFilter.selectedLocations = []; + return dashboardCacheFilter; + } +} + +export default DashboardCacheFilter; diff --git a/src/framework/RealmProxy.js b/src/framework/RealmProxy.js index 5459ce46..6268ba3f 100644 --- a/src/framework/RealmProxy.js +++ b/src/framework/RealmProxy.js @@ -57,7 +57,7 @@ class RealmProxy { if (_.isNil(propertyValue) && _.some(mandatoryObjectSchemaProperties, (y) => y === x)) emptyMandatoryProperties.push(x); }); if (emptyMandatoryProperties.length > 0) { - throw new Error(`${emptyMandatoryProperties.join(",")} are mandatory for ${schemaName}, Keys being saved - ${saveObjectKeys}`); + throw new Error(`${emptyMandatoryProperties.join(",")} are mandatory for ${schemaName}, Keys being saved - ${saveObjectKeys}. UUID: ${underlyingObject.uuid}`); } } const dbEntity = this.realmDb.create(schemaName, underlyingObject, updateMode); diff --git a/src/index.js b/src/index.js index 82527a41..b6c827ec 100644 --- a/src/index.js +++ b/src/index.js @@ -128,6 +128,7 @@ import ArrayUtil from './utility/ArrayUtil'; import DateTimeUtil from "./utility/DateTimeUtil"; import CustomDashboardCache from "./CustomDashboardCache"; import AgeUtil from "./utility/AgeUtil"; +import DashboardCacheFilter from './application/DashboardCacheFilter'; export { AbstractEncounter, @@ -263,5 +264,6 @@ export { ArrayUtil, DateTimeUtil, EntityApprovalStatusMetaData, - AgeUtil + AgeUtil, + DashboardCacheFilter };