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

fix: fix errors when creating note on group relation record #2425

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/child-dev-project/notes/model/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ export class Note extends Entity {
* @param child The child or the id of the child to add to the notes
*/
addChild(child: Child | string) {
const childId = typeof child === "string" ? child : child.getId();
if (this.children.includes(childId)) {
const childId = typeof child === "string" ? child : child?.getId();
if (!childId || this.children.includes(childId)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import { ChildSchoolRelation } from "../../children/model/childSchoolRelation";
describe("NotesRelatedToEntityComponent", () => {
let component: NotesRelatedToEntityComponent;
let fixture: ComponentFixture<NotesRelatedToEntityComponent>;
const originalNoteSchema_relatedEntities =
Note.schema.get("relatedEntities").additional;

beforeEach(waitForAsync(() => {
Note.schema.get("relatedEntities").additional = [
ChildSchoolRelation.ENTITY_TYPE,
];

TestBed.configureTestingModule({
imports: [NotesRelatedToEntityComponent, MockedTestingModule.withState()],
}).compileComponents();
Expand All @@ -23,10 +29,13 @@ describe("NotesRelatedToEntityComponent", () => {
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(NotesRelatedToEntityComponent);
component = fixture.componentInstance;
component.entity = new Child("1");
fixture.detectChanges();
}));

afterEach(() => {
Note.schema.get("relatedEntities").additional =
originalNoteSchema_relatedEntities;
});

it("should create", () => {
expect(component).toBeTruthy();
});
Expand Down Expand Up @@ -81,6 +90,21 @@ describe("NotesRelatedToEntityComponent", () => {
expect(note.schools).toEqual([`${Child.ENTITY_TYPE}:someSchool`]);
});

it("should handle ChildSchoolRelation links also if they are arrays", async () => {
const relation = new ChildSchoolRelation();
relation.schoolId = ["School:1"] as any; // assume entity config was overwritten to hold array
relation.childId = ["Child:1", "Child:2"] as any; // assume entity config was overwritten to hold array

component.entity = relation;
await component.ngOnInit();

const newNote = component.createNewRecordFactory()();

expect(newNote.relatedEntities).toContain(relation.getId());
expect(newNote.children).toEqual(relation.childId);
expect(newNote.schools).toEqual(relation.schoolId);
});

it("should create a new note and fill it with indirectly related references (2-hop) of the types allowed for note.relatedEntities", () => {
@DatabaseEntity("EntityWithRelations")
class EntityWithRelations extends Entity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,31 @@ export class NotesRelatedToEntityComponent
const newNote = super.createNewRecordFactory()();
//TODO: generalize this code - possibly by only using relatedEntities to link other records here? see #1501
if (this.entity.getType() === ChildSchoolRelation.ENTITY_TYPE) {
newNote.addChild((this.entity as ChildSchoolRelation).childId);
newNote.addSchool((this.entity as ChildSchoolRelation).schoolId);
for (const childId of asArray(
(this.entity as ChildSchoolRelation).childId,
)) {
if (childId) {
newNote.addChild(childId);
}
}

for (const schooldId of asArray(
(this.entity as ChildSchoolRelation).schoolId,
)) {
if (schooldId) {
newNote.addSchool(schooldId);
}
}
}

newNote.relatedEntities.push(this.entity.getId());
this.getIndirectlyRelatedEntityIds(this.entity).forEach((e) =>
newNote.relatedEntities.push(e),
);
for (const e of [
this.entity.getId(),
...this.getIndirectlyRelatedEntityIds(this.entity),
]) {
if (!newNote.relatedEntities.includes(e)) {
newNote.relatedEntities.push(e);
}
}

return newNote;
};
Expand Down
Loading