From bb1014855527a571087b6e1b9d5a04b6be9762b5 Mon Sep 17 00:00:00 2001 From: Ben Kiarie Date: Sat, 19 Oct 2024 05:38:57 +0300 Subject: [PATCH] chore: validate contact parent --- .../contacts/contacts-edit.component.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/webapp/src/ts/modules/contacts/contacts-edit.component.ts b/webapp/src/ts/modules/contacts/contacts-edit.component.ts index ad7823bfcc1..b5d026b11f1 100644 --- a/webapp/src/ts/modules/contacts/contacts-edit.component.ts +++ b/webapp/src/ts/modules/contacts/contacts-edit.component.ts @@ -162,6 +162,13 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit { throw new Error(`Unknown contact type "${contactTypeId}"`); } + const parentId = this.routeSnapshot.params?.parent_id; + const newContactType = this.routeSnapshot.params?.type; + const validateParents = await this.validateParent(parentId, newContactType); + if (!validateParents) { + throw new Error(`"{newContactType}" is not a child of "${parentId}"`); + } + const formId = this.getForm(contact, contactType); if (!formId) { throw new Error('Unknown form'); @@ -221,6 +228,17 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit { return formId; } + private async validateParent(parentId, newContactType) { + const parentContact = await this.lineageModelGeneratorService.contact(parentId, {merge: true}); + const parentType = this.contactTypesService.getTypeId(parentContact.doc); + const validChildTypes = await this.contactTypesService.getChildren(parentType); + + if (validChildTypes.some(childType => childType.id === newContactType)) { + return true; + } + return false; + } + private setTitle(titleKey: string) { this.translationsLoadedSubscription?.unsubscribe(); this.translationsLoadedSubscription = this.store