From 3d70d13f78c792cf748f043b874c7cd414f1c277 Mon Sep 17 00:00:00 2001 From: Sebastian Leidig Date: Thu, 11 Jan 2024 09:44:35 +0100 Subject: [PATCH] fix(core): related-entities can handle records where the mapped field is undefined fixes #2172 --- .../related-entities.component.spec.ts | 24 +++++++++++++++++-- .../related-entities.component.ts | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/app/core/entity-details/related-entities/related-entities.component.spec.ts b/src/app/core/entity-details/related-entities/related-entities.component.spec.ts index 49a1a1f2f5..384bfc306a 100644 --- a/src/app/core/entity-details/related-entities/related-entities.component.spec.ts +++ b/src/app/core/entity-details/related-entities/related-entities.component.spec.ts @@ -5,10 +5,13 @@ import { MockedTestingModule } from "../../../utils/mocked-testing.module"; import { EntityMapperService } from "../../entity/entity-mapper/entity-mapper.service"; import { Child } from "../../../child-dev-project/children/model/child"; import { ChildSchoolRelation } from "../../../child-dev-project/children/model/childSchoolRelation"; +import { Note } from "../../../child-dev-project/notes/model/note"; describe("RelatedEntitiesComponent", () => { - let component: RelatedEntitiesComponent; - let fixture: ComponentFixture>; + let component: RelatedEntitiesComponent; + let fixture: ComponentFixture< + RelatedEntitiesComponent + >; beforeEach(async () => { await TestBed.configureTestingModule({ @@ -56,6 +59,23 @@ describe("RelatedEntitiesComponent", () => { expect(component.filter).toEqual({ ...filter, childId: c1.getId() }); }); + it("should ignore entities of the related type where the matching field is undefined instead of array", async () => { + const c1 = new Child(); + const r1 = new Note(); + r1.children = [c1.getId()]; + const rEmpty = new Note(); + delete rEmpty.children; // some entity types will not have a default empty array + const entityMapper = TestBed.inject(EntityMapperService); + await entityMapper.saveAll([c1, r1, rEmpty]); + + component.entity = c1; + component.entityType = Note.ENTITY_TYPE; + component.property = "children"; + await component.ngOnInit(); + + expect(component.data).toEqual([r1]); + }); + it("should create a new entity that references the related one", async () => { const related = new Child(); component.entity = related; diff --git a/src/app/core/entity-details/related-entities/related-entities.component.ts b/src/app/core/entity-details/related-entities/related-entities.component.ts index ddefb12814..76684c739f 100644 --- a/src/app/core/entity-details/related-entities/related-entities.component.ts +++ b/src/app/core/entity-details/related-entities/related-entities.component.ts @@ -69,7 +69,7 @@ export class RelatedEntitiesComponent implements OnInit { this.data = (await this.entityMapper.loadType(this.entityType)).filter( (e) => this.isArray - ? e[this.property].includes(this.entity.getId()) + ? e[this.property]?.includes(this.entity.getId()) : e[this.property] === this.entity.getId(), ); this.filter = {