Skip to content

Commit

Permalink
fix: enums can have a logic ordering (#1349)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon <simon@aam-digital.com>
  • Loading branch information
Schottkyc137 and TheSlimvReal authored Aug 17, 2022
1 parent 0e162ec commit be13fd9
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Injectable } from "@angular/core";
import { Child } from "../model/child";
import { faker } from "../../../core/demo-data/faker";
import { Aser } from "./model/aser";
import { ConfigurableEnumValue } from "../../../core/configurable-enum/configurable-enum.interface";
import { mathLevels, readingLevels } from "./model/skill-levels";
import { WarningLevel } from "../../../core/entity/model/warning-level";

Expand Down Expand Up @@ -78,10 +77,7 @@ export class DemoAserGeneratorService extends DemoDataGenerator<Aser> {
* @param skillRange The array of skill levels for the desired subject (mathLevels or readingLevels)
* @param previousSkillLevel The string indicating the level from the previous test for this subject
*/
private selectNextSkillLevel(
skillRange: ConfigurableEnumValue[],
previousSkillLevel: ConfigurableEnumValue
): ConfigurableEnumValue {
private selectNextSkillLevel<T>(skillRange: T[], previousSkillLevel: T): T {
const previousSkillLevelIndex = skillRange.indexOf(previousSkillLevel);

let nextSkillLevelIndex;
Expand Down
102 changes: 52 additions & 50 deletions src/app/child-dev-project/children/aser/model/skill-levels.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
import {
ConfigurableEnumConfig,
ConfigurableEnumValue,
EMPTY,
} from "../../../../core/configurable-enum/configurable-enum.interface";
import { Ordering } from "../../../../core/configurable-enum/configurable-enum-ordering";

export type SkillLevel = ConfigurableEnumValue & { passed?: boolean };
export type SkillLevel = Ordering.EnumValue & { passed?: boolean };

export const readingLevels: ConfigurableEnumConfig<SkillLevel> = [
EMPTY,
{
id: "Nothing",
label: $localize`:Label reading level:Nothing`,
},
{
id: "Read Letters",
label: $localize`:Label reading level:Read Letters`,
},
{
id: "Read Words",
label: $localize`:Label reading level:Read Words`,
},
{
id: "Read Sentence",
label: $localize`:Label reading level:Read Sentence`,
},
{
id: "Read Paragraph",
label: $localize`:Label reading level:Read Paragraph`,
passed: true,
},
];
export const readingLevels: ConfigurableEnumConfig<SkillLevel> =
Ordering.imposeTotalOrdering([
EMPTY,
{
id: "Nothing",
label: $localize`:Label reading level:Nothing`,
},
{
id: "Read Letters",
label: $localize`:Label reading level:Read Letters`,
},
{
id: "Read Words",
label: $localize`:Label reading level:Read Words`,
},
{
id: "Read Sentence",
label: $localize`:Label reading level:Read Sentence`,
},
{
id: "Read Paragraph",
label: $localize`:Label reading level:Read Paragraph`,
passed: true,
},
]);

export const mathLevels: ConfigurableEnumConfig<SkillLevel> = [
EMPTY,
{
id: "Nothing",
label: $localize`:Label math level:Nothing`,
},
{
id: "Numbers 1-9",
label: $localize`:Label math level:Numbers 1-9`,
},
{
id: "Numbers 10-99",
label: $localize`:Label math level:Numbers 10-99`,
},
{
id: "Subtraction",
label: $localize`:Label math level:Subtraction`,
},
{
id: "Division",
label: $localize`:Label math level:Division`,
passed: true,
},
];
export const mathLevels: ConfigurableEnumConfig<SkillLevel> =
Ordering.imposeTotalOrdering([
EMPTY,
{
id: "Nothing",
label: $localize`:Label math level:Nothing`,
},
{
id: "Numbers 1-9",
label: $localize`:Label math level:Numbers 1-9`,
},
{
id: "Numbers 10-99",
label: $localize`:Label math level:Numbers 10-99`,
},
{
id: "Subtraction",
label: $localize`:Label math level:Subtraction`,
},
{
id: "Division",
label: $localize`:Label math level:Division`,
passed: true,
},
]);
37 changes: 17 additions & 20 deletions src/app/child-dev-project/notes/model/note.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "../../../core/entity/model/warning-level";
import { testEntitySubclass } from "../../../core/entity/model/entity.spec";
import { defaultInteractionTypes } from "../../../core/config/default-config/default-interaction-types";
import { Ordering } from "../../../core/configurable-enum/configurable-enum-ordering";

const testStatusTypes: ConfigurableEnumConfig<AttendanceStatusType> = [
{
Expand Down Expand Up @@ -61,7 +62,7 @@ describe("Note", () => {
const ENTITY_TYPE = "Note";
let entitySchemaService: EntitySchemaService;

const testInteractionTypes: InteractionType[] = [
const testInteractionTypes: InteractionType[] = Ordering.imposeTotalOrdering([
{
id: "",
label: "",
Expand All @@ -74,24 +75,20 @@ describe("Note", () => {
id: "GUARDIAN_TALK",
label: "Talk with Guardians",
},
];

beforeEach(
waitForAsync(() => {
const testConfigs = {};
testConfigs[
CONFIGURABLE_ENUM_CONFIG_PREFIX + INTERACTION_TYPE_CONFIG_ID
] = testInteractionTypes;
testConfigs[
CONFIGURABLE_ENUM_CONFIG_PREFIX + ATTENDANCE_STATUS_CONFIG_ID
] = testStatusTypes;

entitySchemaService = new EntitySchemaService();
entitySchemaService.registerSchemaDatatype(
new ConfigurableEnumDatatype(createTestingConfigService(testConfigs))
);
})
);
]);

beforeEach(waitForAsync(() => {
const testConfigs = {};
testConfigs[CONFIGURABLE_ENUM_CONFIG_PREFIX + INTERACTION_TYPE_CONFIG_ID] =
testInteractionTypes;
testConfigs[CONFIGURABLE_ENUM_CONFIG_PREFIX + ATTENDANCE_STATUS_CONFIG_ID] =
testStatusTypes;

entitySchemaService = new EntitySchemaService();
entitySchemaService.registerSchemaDatatype(
new ConfigurableEnumDatatype(createTestingConfigService(testConfigs))
);
}));

testEntitySubclass("Note", Note, {
_id: "Note:some-id",
Expand Down Expand Up @@ -139,7 +136,7 @@ describe("Note", () => {
it("should return colors", function () {
const note = new Note("1");

note.category = { id: "", label: "test", color: "#FFFFFF" };
note.category = { id: "", label: "test", color: "#FFFFFF", _ordinal: -1 };
expect(note.getColor()).toBe("#FFFFFF");

note.warningLevel = warningLevels.find((level) => level.id === "URGENT");
Expand Down
2 changes: 1 addition & 1 deletion src/app/child-dev-project/notes/model/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Note extends Entity {
dataType: "configurable-enum",
innerDataType: INTERACTION_TYPE_CONFIG_ID,
})
category: InteractionType = { id: "", label: "" };
category: InteractionType = { id: "", label: "", _ordinal: 0 };

/**
* id referencing a different entity (e.g. a recurring activity) this note is related to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LoginState } from "../../../core/session/session-states/login-state.enu
function generateTestNote(forChildren: Child[]) {
const testNote = Note.create(new Date(), "test note");
testNote.category = {
_ordinal: 0,
id: "CHILDREN_MEETING",
label: "Children's Meeting",
color: "#E1F5FE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { UpdatedEntity } from "../../../core/entity/model/entity-update";
import { ExportService } from "../../../core/export/export-service/export.service";
import { MockedTestingModule } from "../../../utils/mocked-testing.module";
import { FontAwesomeTestingModule } from "@fortawesome/angular-fontawesome/testing";
import { Ordering } from "../../../core/configurable-enum/configurable-enum-ordering";

describe("NotesManagerComponent", () => {
let component: NotesManagerComponent;
Expand Down Expand Up @@ -78,7 +79,7 @@ describe("NotesManagerComponent", () => {
snapshot: { queryParams: {} },
};

const testInteractionTypes: InteractionType[] = [
const testInteractionTypes: InteractionType[] = Ordering.imposeTotalOrdering([
{
id: "HOME_VISIT",
label: "Home Visit",
Expand All @@ -87,7 +88,7 @@ describe("NotesManagerComponent", () => {
id: "GUARDIAN_TALK",
label: "Talk with Guardians",
},
];
]);

beforeEach(() => {
const mockConfigService = jasmine.createSpyObj("mockConfigService", [
Expand Down Expand Up @@ -169,9 +170,9 @@ describe("NotesManagerComponent", () => {
flush();

const list = fixture.debugElement.query(By.css("app-entity-list"));
const filterSettings = (list.componentInstance as EntityListComponent<Note>).filterSelections.find(
(f) => f.filterSettings.name === "category"
);
const filterSettings = (
list.componentInstance as EntityListComponent<Note>
).filterSelections.find((f) => f.filterSettings.name === "category");

expect(filterSettings.filterSettings.options).toHaveSize(
testInteractionTypes.length + 1
Expand Down
38 changes: 20 additions & 18 deletions src/app/child-dev-project/warning-levels.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { ConfigurableEnumValue } from "../core/configurable-enum/configurable-enum.interface";
import { Ordering } from "../core/configurable-enum/configurable-enum-ordering";

export const warningLevels: ConfigurableEnumValue[] = [
{
id: "",
label: "",
},
{
id: "OK",
label: $localize`:Label warning level:Solved`,
},
{
id: "WARNING",
label: $localize`:Label warning level:Needs Follow-Up`,
},
{
id: "URGENT",
label: $localize`:Label warning level:Urgent Follow-Up`,
},
];
export const warningLevels: ConfigurableEnumValue[] =
Ordering.imposeTotalOrdering([
{
id: "",
label: "",
},
{
id: "OK",
label: $localize`:Label warning level:Solved`,
},
{
id: "WARNING",
label: $localize`:Label warning level:Needs Follow-Up`,
},
{
id: "URGENT",
label: $localize`:Label warning level:Urgent Follow-Up`,
},
]);
21 changes: 14 additions & 7 deletions src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,38 @@ export const defaultJsonConfig = {
"enum:document-status": [
{
"id": "",
"label": ""
"label": "",
"_ordinal": 0,
},
{
"id": "OK (copy with us)",
"label": $localize`:Document status:OK (copy with us)`
"label": $localize`:Document status:OK (copy with us)`,
"_ordinal": 1,
},
{
"id": "OK (copy needed for us)",
"label": $localize`:Document status:OK (copy needed for us)`
"label": $localize`:Document status:OK (copy needed for us)`,
"_ordinal": 2,
},
{
"id": "needs correction",
"label": $localize`:Document status:needs correction`
"label": $localize`:Document status:needs correction`,
"_ordinal": 3,
},
{
"id": "applied",
"label": $localize`:Document status:applied`
"label": $localize`:Document status:applied`,
"_ordinal": 4,
},
{
"id": "doesn't have",
"label": $localize`:Document status:doesn't have`
"label": $localize`:Document status:doesn't have`,
"_ordinal": 5,
},
{
"id": "not eligible",
"label": $localize`:Document status:not eligible`
"label": $localize`:Document status:not eligible`,
"_ordinal": 6,
}
],
"enum:center": [
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class ConfigService {
if (!id.startsWith(CONFIGURABLE_ENUM_CONFIG_PREFIX)) {
id = CONFIGURABLE_ENUM_CONFIG_PREFIX + id;
}
return this.getConfig(id);
return this.getConfig<any>(id);
}

public getAllConfigs<T>(prefix: string): T[] {
Expand Down
Loading

0 comments on commit be13fd9

Please sign in to comment.