Skip to content

Commit

Permalink
avniproject/avni-client#1272 - DashboardCacheFilter class for all the…
Browse files Browse the repository at this point in the history
… fields support by Dashboard Cache. Print UUID if possible when throwing mandatory save error. Moved UI code to avni client. Deleted previous cache.
  • Loading branch information
petmongrels committed Jan 19, 2024
1 parent d45e4f5 commit d6eff43
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 39 deletions.
57 changes: 21 additions & 36 deletions src/DashboardCache.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -818,6 +818,10 @@ function createRealmConfig() {
rc.initCountOfCards = 1;
});
}
if (newDB.schemaVersion < 183) {
const newObjects = newDB.objects("DashboardCache");
newDB.delete(newObjects);
}
},
};
}
Expand Down
53 changes: 53 additions & 0 deletions src/application/DashboardCacheFilter.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion src/framework/RealmProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -263,5 +264,6 @@ export {
ArrayUtil,
DateTimeUtil,
EntityApprovalStatusMetaData,
AgeUtil
AgeUtil,
DashboardCacheFilter
};

0 comments on commit d6eff43

Please sign in to comment.