Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): change EntityConfig to flattened structure with EntitySchemaField including .id #2102

Merged
merged 11 commits into from
Nov 30, 2023
1 change: 1 addition & 0 deletions src/app/core/basic-datatypes/array/array.datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ConfigurableEnumService } from "../configurable-enum/configurable-enum.

describe("Schema data type: array", () => {
const schema: EntitySchemaField = {
id: null,
dataType: "array",
innerDataType: "configurable-enum",
additional: "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ describe("Schema data type: configurable-enum", () => {
const actualMapped = dataType.importMapFunction(
input,
{
id: null,
dataType: "configurable-enum",
additional: "genders",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe("EditConfigurableEnumComponent", () => {
expect(component.multi).toBeTrue();
});

function initWithSchema(schema: EntitySchemaField) {
function initWithSchema(schema: Omit<EntitySchemaField, "id">) {
const fromGroup = new FormGroup({ test: new FormControl() });
component.formControl = fromGroup.get("test") as FormControl;
component.formFieldConfig = { id: "test" };
component.propertySchema = schema;
component.propertySchema = { id: "test", ...schema };
component.entity = new Entity();
component.ngOnInit();
}
Expand Down
6 changes: 5 additions & 1 deletion src/app/core/basic-datatypes/entity/entity.datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ describe("Schema data type: entity", () => {
const dataType = new EntityDatatype(entityMapper, mockRemoveService);

const testValue = referencedEntity.getId();
const testSchemaField = { additional: "Child", dataType: "entity" };
const testSchemaField = {
id: null,
additional: "Child",
dataType: "entity",
};

const anonymizedValue = await dataType.anonymize(
testValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe("EntityFormService", () => {
});

it("should assign default values", () => {
const schema: EntitySchemaField = { defaultValue: 1 };
const schema: EntitySchemaField = { id: "test", defaultValue: 1 };
Entity.schema.set("test", schema);

let form = service.createFormGroup([{ id: "test" }], new Entity());
Expand All @@ -191,7 +191,7 @@ describe("EntityFormService", () => {
});

it("should not assign default values to existing entities", () => {
Entity.schema.set("test", { defaultValue: 1 });
Entity.schema.set("test", { id: "test", defaultValue: 1 });

const entity = new Entity();
entity._rev = "1-existing_entity";
Expand All @@ -202,7 +202,7 @@ describe("EntityFormService", () => {
});

it("should not overwrite existing values with default value", () => {
Entity.schema.set("test", { defaultValue: 1 });
Entity.schema.set("test", { id: "test", defaultValue: 1 });

const entity = new Entity();
entity["test"] = 2;
Expand Down
174 changes: 68 additions & 106 deletions src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,160 +946,122 @@ export const defaultJsonConfig = {
"labelPlural": $localize`:Plural label for child:Children`,
"attributes": [
{
"name": "address",
"schema": {
"dataType": "location",
"label": $localize`:Label for the address of a child:Address`
}
"id": "address",
"dataType": "location",
"label": $localize`:Label for the address of a child:Address`
},
{
"name": "health_bloodGroup",
"schema": {
"dataType": "string",
"label": $localize`:Label for a child attribute:Blood Group`
}
"id": "health_bloodGroup",
"dataType": "string",
"label": $localize`:Label for a child attribute:Blood Group`
},
{
"name": "religion",
"schema": {
"dataType": "string",
"label": $localize`:Label for the religion of a child:Religion`
}
"id": "religion",
"dataType": "string",
"label": $localize`:Label for the religion of a child:Religion`
},
{
"name": "motherTongue",
"schema": {
"dataType": "string",
"label": $localize`:Label for the mother tongue of a child:Mother Tongue`,
description: $localize`:Tooltip description for the mother tongue of a child:The primary language spoken at home`,
}
"id": "motherTongue",
"dataType": "string",
"label": $localize`:Label for the mother tongue of a child:Mother Tongue`,
description: $localize`:Tooltip description for the mother tongue of a child:The primary language spoken at home`,
},
{
"name": "health_lastDentalCheckup",
"schema": {
"dataType": "date",
"label": $localize`:Label for a child attribute:Last Dental Check-Up`
}
"id": "health_lastDentalCheckup",
"dataType": "date",
"label": $localize`:Label for a child attribute:Last Dental Check-Up`
},
{
"name": "birth_certificate",
"schema": {
"dataType": "file",
"label": $localize`:Label for a child attribute:Birth certificate`
}
"id": "birth_certificate",
"dataType": "file",
"label": $localize`:Label for a child attribute:Birth certificate`
}
]
},
"entity:School": {
"attributes": [
{
"name": "name",
"schema": {
"dataType": "string",
"label": $localize`:Label for the name of a school:Name`
}
"id": "name",
"dataType": "string",
"label": $localize`:Label for the name of a school:Name`
},
{
"name": "privateSchool",
"schema": {
"dataType": "boolean",
"label": $localize`:Label for if a school is a private school:Private School`
}
"id": "privateSchool",
"dataType": "boolean",
"label": $localize`:Label for if a school is a private school:Private School`
},
{
"name": "language",
"schema": {
"dataType": "string",
"label": $localize`:Label for the language of a school:Language`
}
"id": "language",
"dataType": "string",
"label": $localize`:Label for the language of a school:Language`
},
{
"name": "address",
"schema": {
"dataType": "location",
"label": $localize`:Label for the address of a school:Address`
}
"id": "address",
"dataType": "location",
"label": $localize`:Label for the address of a school:Address`
},
{
"name": "phone",
"schema": {
"dataType": "string",
"label": $localize`:Label for the phone number of a school:Phone Number`
}
"id": "phone",
"dataType": "string",
"label": $localize`:Label for the phone number of a school:Phone Number`
},
{
"name": "timing",
"schema": {
"dataType": "string",
"label": $localize`:Label for the timing of a school:School Timing`
}
"id": "timing",
"dataType": "string",
"label": $localize`:Label for the timing of a school:School Timing`
},
{
"name": "remarks",
"schema": {
"dataType": "string",
"label": $localize`:Label for the remarks for a school:Remarks`
}
"id": "remarks",
"dataType": "string",
"label": $localize`:Label for the remarks for a school:Remarks`
}
]
},
"entity:HistoricalEntityData": {
"attributes": [
{
"name": "isMotivatedDuringClass",
"schema": {
"dataType": "configurable-enum",
"innerDataType": "rating-answer",
"label": $localize`:Label for a child attribute:Motivated`,
description: $localize`:Description for a child attribute:The child is motivated during the class.`
}
"id": "isMotivatedDuringClass",
"dataType": "configurable-enum",
"additional": "rating-answer",
"label": $localize`:Label for a child attribute:Motivated`,
description: $localize`:Description for a child attribute:The child is motivated during the class.`
},
{
"name": "isParticipatingInClass",
"schema": {
"dataType": "configurable-enum",
"innerDataType": "rating-answer",
"label": $localize`:Label for a child attribute:Participating`,
description: $localize`:Description for a child attribute:The child is actively participating in the class.`
}
"id": "isParticipatingInClass",
"dataType": "configurable-enum",
"additional": "rating-answer",
"label": $localize`:Label for a child attribute:Participating`,
description: $localize`:Description for a child attribute:The child is actively participating in the class.`
},
{
"name": "isInteractingWithOthers",
"schema": {
"dataType": "configurable-enum",
"innerDataType": "rating-answer",
"label": $localize`:Label for a child attribute:Interacting`,
description: $localize`:Description for a child attribute:The child interacts with other students during the class.`
}
"id": "isInteractingWithOthers",
"dataType": "configurable-enum",
"additional": "rating-answer",
"label": $localize`:Label for a child attribute:Interacting`,
description: $localize`:Description for a child attribute:The child interacts with other students during the class.`
},
{
"name": "doesHomework",
"schema": {
"dataType": "configurable-enum",
"innerDataType": "rating-answer",
"label": $localize`:Label for a child attribute:Homework`,
description: $localize`:Description for a child attribute:The child does its homework.`
}
"id": "doesHomework",
"dataType": "configurable-enum",
"additional": "rating-answer",
"label": $localize`:Label for a child attribute:Homework`,
description: $localize`:Description for a child attribute:The child does its homework.`
},
{
"name": "asksQuestions",
"schema": {
"dataType": "configurable-enum",
"innerDataType": "rating-answer",
"label": $localize`:Label for a child attribute:Asking Questions`,
description: $localize`:Description for a child attribute:The child is asking questions during the class.`
}
"id": "asksQuestions",
"dataType": "configurable-enum",
"additional": "rating-answer",
"label": $localize`:Label for a child attribute:Asking Questions`,
description: $localize`:Description for a child attribute:The child is asking questions during the class.`
},
]
},
"entity:User": {
"attributes": [
{
"name": "phone",
"schema": {
"dataType": "string",
"label": $localize`:Label of user phone:Contact`
}
"id": "phone",
"dataType": "string",
"label": $localize`:Label of user phone:Contact`
},
]
},
Expand Down
40 changes: 40 additions & 0 deletions src/app/core/config/config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EntityMapperService } from "../entity/entity-mapper/entity-mapper.servi
import { Config } from "./config";
import { firstValueFrom, Subject } from "rxjs";
import { UpdatedEntity } from "../entity/model/entity-update";
import { EntityConfig } from "../entity/entity-config";

describe("ConfigService", () => {
let service: ConfigService;
Expand Down Expand Up @@ -98,4 +99,43 @@ describe("ConfigService", () => {
const result = service.exportConfig();
expect(result).toEqual(expected);
}));

it("should migrate entity attributes config to flattened format with id", fakeAsync(() => {
const testConfigId = "entity:Test";
const config = new Config();
config.data = {
[testConfigId]: {
attributes: [
{
name: "count",
schema: {
dataType: "number",
},
},
],
},
};
updateSubject.next({ entity: config, type: "update" });
tick();

const result = service.getConfig<EntityConfig>("entity:Test");

expect(result.attributes).toEqual([{ id: "count", dataType: "number" }]);
}));
it("should not migrate / change entity attributes if already in new format", fakeAsync(() => {
const testConfigId = "entity:Test";
const entityAttributes = [{ id: "count", dataType: "number" }];
const config = new Config();
config.data = {
[testConfigId]: {
attributes: entityAttributes,
},
};
updateSubject.next({ entity: config, type: "update" });
tick();

const result = service.getConfig<EntityConfig>("entity:Test");

expect(result.attributes).toEqual(entityAttributes);
}));
});
Loading