Skip to content

Commit

Permalink
avniproject/avni-client#1365 |Init Individual.lowestAddressLevel with…
Browse files Browse the repository at this point in the history
… Dummy value only if the Individual SubjectType is User
  • Loading branch information
himeshr committed May 1, 2024
1 parent a5f7a92 commit 66b3232
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
29 changes: 21 additions & 8 deletions src/Individual.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const mergeMap = new Map([
[SchemaNames.Comment, "comments"]
]);

const ADDRESS_LEVEL_DUMMY_UUID = 'f71b2f45-2c11-427f-aa99-be6161a6b413';

class Individual extends BaseEntity {
static schema = {
name: SchemaNames.Individual,
Expand Down Expand Up @@ -298,8 +300,15 @@ class Individual extends BaseEntity {
individual.groupSubjects = [];
individual.groups = [];
individual.approvalStatuses = [];
individual.lowestAddressLevel = AddressLevel.create({
uuid: "",
individual.lowestAddressLevel = this.getPlaceholderAddressLevel();
individual.voided = false;
individual.comments = [];
return individual;
}

static getPlaceholderAddressLevel(entityService) {
return AddressLevel.create({
uuid: ADDRESS_LEVEL_DUMMY_UUID,
title: "",
level: 0,
typeString: "",
Expand All @@ -308,10 +317,7 @@ class Individual extends BaseEntity {
parentUuid: "",
typeUuid: "",
locationProperties: []
});
individual.voided = false;
individual.comments = [];
return individual;
}, entityService);
}

static createEmptySubjectInstance() {
Expand Down Expand Up @@ -381,10 +387,17 @@ class Individual extends BaseEntity {
individual.dateOfBirth = dateOfBirth;
individual.dateOfBirthVerified = dateOfBirthVerified;
individual.gender = gender;
individual.lowestAddressLevel = lowestAddressLevel;
individual.lowestAddressLevel = this.initLowestAddressLevel(lowestAddressLevel, subjectType, null);
return individual;
}

/**
* Init Lowest AddressLevel with Placeholder only if the Individual SubjectType is User
*/
static initLowestAddressLevel(lowestAddressLevel, subjectType, entityService) {
return lowestAddressLevel || (subjectType && subjectType.isUser() && this.getPlaceholderAddressLevel(entityService));
}

static directCopyFields = ["uuid", "firstName", "middleName", "lastName", "profilePicture", "dateOfBirthVerified", "voided"];
static dateFields = ["dateOfBirth", "registrationDate"];

Expand Down Expand Up @@ -413,7 +426,7 @@ class Individual extends BaseEntity {
entityService
);
individual.gender = gender;
individual.lowestAddressLevel = addressLevel;
individual.lowestAddressLevel = this.initLowestAddressLevel(addressLevel, subjectType, entityService);
individual.name = individual.nameString;
if (!_.isNil(individualResource.registrationLocation))
individual.registrationLocation = Point.fromResource(individualResource.registrationLocation);
Expand Down
7 changes: 5 additions & 2 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ import CustomDashboardCache from './CustomDashboardCache';
import DefinedObjectSchema from "./framework/DefinedObjectSchema";
import MigrationsHelper from "./MigrationsHelper";
import MetaDataService from "./service/MetaDataService";
import moment from "moment";

const entities = [
DashboardFilter,
Expand Down Expand Up @@ -253,7 +252,7 @@ function createRealmConfig() {
return doCompact;
},
//order is important, should be arranged according to the dependency
schemaVersion: 188,
schemaVersion: 189,
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 === VersionWithEmbeddedMigrationProblem)
Expand Down Expand Up @@ -904,6 +903,10 @@ function createRealmConfig() {
// newDB.deleteModel("Decision");
// newDB.deleteModel("ProgramOutcome");
}
if (oldDB.schemaVersion < 189) {
// PlaceHolder for SubjectType.User changes, so that people with previous version of client
// are not able to use fastSync of version 189 and above
}
},
};
}
Expand Down
5 changes: 5 additions & 0 deletions src/SubjectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class SubjectType extends ReferenceEntity {
Individual: 'Individual',
Group: 'Group',
Household: 'Household',
User: 'User'
};

static settingKeys = {
Expand Down Expand Up @@ -285,6 +286,10 @@ class SubjectType extends ReferenceEntity {
return this.type === SubjectType.types.Individual;
}

isUser() {
return this.type === SubjectType.types.User;
}

registerIcon() {
return this.isPerson() ? "account-plus" : "plus-box";
}
Expand Down
3 changes: 3 additions & 0 deletions src/utility/General.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class General {
}

static assignObsFields(source, dest, observationFields = [], entityService) {
if(!entityService) {
return dest;
}
observationFields.forEach((observationField) => {
const observations = [];
if (!_.isNil(source[observationField])) {
Expand Down

0 comments on commit 66b3232

Please sign in to comment.