From dbb0d179201a191f97c5a21d590e4ccfd59b761a Mon Sep 17 00:00:00 2001 From: Andy Wilkins <49269487+andywilkinshmcts@users.noreply.github.com> Date: Mon, 16 Oct 2023 14:08:01 +0100 Subject: [PATCH] Exui 837 cya fix (#1581) * Revert "Revert "fix for exui-837 (#1559)" (#1576)" This reverts commit bb05e82356d0831c8e8444b2c58926a452b71959. * Candidate fix for issues with 837 fix * updated fix and unit tests for exui-837 * lint --------- Co-authored-by: RiteshHMCTS <74713687+RiteshHMCTS@users.noreply.github.com> Co-authored-by: Ritesh Dsouza --- package.json | 2 +- projects/ccd-case-ui-toolkit/package.json | 2 +- .../case-edit-page.component.spec.ts | 78 ++++++++++++++----- .../case-edit-page.component.ts | 12 ++- .../case-edit/case-edit.component.spec.ts | 10 ++- .../case-edit/case-edit.component.ts | 15 +++- .../case-editor/case-editor.module.ts | 4 +- ...valid-page-list-caseFields.service.spec.ts | 71 +++++++++++++++++ .../valid-page-list-caseFields.service.ts | 35 +++++++++ ...write-linked-cases-field.component.spec.ts | 7 +- 10 files changed, 205 insertions(+), 31 deletions(-) create mode 100644 projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.spec.ts create mode 100644 projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.ts diff --git a/package.json b/package.json index 461fd5c9e4..ef4f10f45a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hmcts/ccd-case-ui-toolkit", - "version": "6.19.5", + "version": "6.19.8", "engines": { "yarn": "^3.5.0", "npm": "^8.10.0" diff --git a/projects/ccd-case-ui-toolkit/package.json b/projects/ccd-case-ui-toolkit/package.json index ee81bf880c..b3f0f13570 100644 --- a/projects/ccd-case-ui-toolkit/package.json +++ b/projects/ccd-case-ui-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@hmcts/ccd-case-ui-toolkit", - "version": "6.19.5", + "version": "6.19.8", "engines": { "yarn": "^3.5.0", "npm": "^8.10.0" diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts index c3ddb26b0f..a9ce79c1fc 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts @@ -41,6 +41,7 @@ import { FormValueService, LoadingService, } from '../../../services'; +import { ValidPageListCaseFieldsService } from '../services/valid-page-list-caseFields.service'; import { FieldsUtils } from '../../../services/fields/fields.utils'; import { text } from '../../../test/helpers'; import { MockRpxTranslatePipe } from '../../../test/mock-rpx-translate.pipe'; @@ -52,7 +53,7 @@ import { Wizard, WizardPage } from '../domain'; import { PageValidationService } from '../services'; import { CaseEditPageText } from './case-edit-page-text.enum'; import { CaseEditPageComponent } from './case-edit-page.component'; - +import { ShowCondition } from '../../../directives'; import createSpyObj = jasmine.createSpyObj; describe('CaseEditPageComponent - creation and update event trigger tests', () => { @@ -76,6 +77,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = caseFieldService = {}, caseEditDataService = {}, loadingService = {}, + validPageListCaseFieldsService = {} }) => new CaseEditPageComponent( caseEdit as CaseEditComponent, @@ -87,7 +89,8 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = dialog as MatDialog, caseFieldService as CaseFieldService, caseEditDataService as CaseEditDataService, - loadingService as LoadingService + loadingService as LoadingService, + validPageListCaseFieldsService as ValidPageListCaseFieldsService ); it('should create', () => { @@ -294,8 +297,24 @@ describe('CaseEditPageComponent - all other tests', () => { let wizardPage = createWizardPage( [createCaseField('field1', 'field1Value')], false, - 0 + 0, + 1 + ); + const wizardPage1 = createWizardPage( + [createCaseField('field1', 'field1Value')], + false, + 0, + 2 ); + const wizardPage2 = createWizardPage( + [createCaseField('field3', 'field3Value')], + false, + 0, + 3 + ); + wizardPage1.parsedShowCondition = ShowCondition.getInstance(null); + wizardPage2.parsedShowCondition = ShowCondition.getInstance(null); + const pageList = [wizardPage1, wizardPage2]; const readOnly = new CaseField(); const fieldTypeSanitiser = new FieldTypeSanitiser(); const formValueService = new FormValueService(fieldTypeSanitiser); @@ -303,6 +322,8 @@ describe('CaseEditPageComponent - all other tests', () => { const firstPage = new WizardPage(); const caseFieldService = new CaseFieldService(); const pageValidationService = new PageValidationService(caseFieldService); + const fieldsUtils = new FieldsUtils(); + const validPageListCaseFieldsService = new ValidPageListCaseFieldsService(fieldsUtils); let route: any; let snapshot: any; const FORM_GROUP_NO_JUDICIAL_USERS = new FormGroup({ @@ -352,6 +373,7 @@ describe('CaseEditPageComponent - all other tests', () => { firstPage.case_fields = [ createCaseField('field1', 'SOME_VALUE') ]; + firstPage.parsedShowCondition = ShowCondition.getInstance(null); cancelled = createSpyObj('cancelled', ['emit']); caseEditComponentStub = { form: FORM_GROUP, @@ -373,7 +395,8 @@ describe('CaseEditPageComponent - all other tests', () => { metadataFields: [caseField2], }, getNextPage: () => null, - callbackErrorsSubject: new Subject() + callbackErrorsSubject: new Subject(), + validPageList: pageList }; snapshot = { queryParamMap: createSpyObj('queryParamMap', ['get']), @@ -448,9 +471,10 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: MatDialog, useValue: dialog }, { provide: CaseFieldService, useValue: caseFieldService }, { provide: CaseEditDataService, useValue: caseEditDataService }, - FieldsUtils, + { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, - { provide: LoadingService, useValue: loadingServiceMock } + { provide: LoadingService, useValue: loadingServiceMock }, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ], }).compileComponents(); fixture = TestBed.createComponent(CaseEditPageComponent); @@ -766,7 +790,8 @@ describe('CaseEditPageComponent - all other tests', () => { metadataFields: [], }, getNextPage: () => null, - callbackErrorsSubject: new Subject() + callbackErrorsSubject: new Subject(), + validPageList: pageList }; snapshot = { queryParamMap: createSpyObj('queryParamMap', ['get']), @@ -841,9 +866,10 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: MatDialog, useValue: dialog }, { provide: CaseFieldService, useValue: caseFieldService }, { provide: CaseEditDataService, useValue: caseEditDataService }, - FieldsUtils, + { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ], }).compileComponents(); }) @@ -941,7 +967,8 @@ describe('CaseEditPageComponent - all other tests', () => { title_display: '# 1234567812345678: test', }, }, - getNextPage: () => null + getNextPage: () => null, + validPageList: pageList }; route = { @@ -1000,9 +1027,10 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: MatDialog, useValue: dialog }, { provide: CaseFieldService, useValue: caseFieldService }, { provide: CaseEditDataService, useValue: caseEditDataService }, - FieldsUtils, + { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ], }).compileComponents(); }) @@ -1097,7 +1125,8 @@ describe('CaseEditPageComponent - all other tests', () => { }, getNextPage: () => null, callbackErrorsSubject: new Subject(), - ignoreWarning: true + ignoreWarning: true, + validPageList: pageList }; snapshot = { queryParamMap: createSpyObj('queryParamMap', ['get']), @@ -1125,6 +1154,7 @@ describe('CaseEditPageComponent - all other tests', () => { eventData ); spyOn(formValueService, 'removeUnnecessaryFields'); + spyOn(validPageListCaseFieldsService, 'deleteNonValidatedFields'); caseEditDataService = createSpyObj('caseEditDataService', [ @@ -1166,9 +1196,10 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: MatDialog, useValue: dialog }, { provide: CaseFieldService, useValue: caseFieldService }, { provide: CaseEditDataService, useValue: caseEditDataService }, - FieldsUtils, + { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ], }).compileComponents(); }) @@ -1185,7 +1216,8 @@ describe('CaseEditPageComponent - all other tests', () => { aCaseField('judicialUserField2', 'judicialUser2', 'JudicialUser', 'OPTIONAL', null), ], false, - 0 + 0, + 2 ); comp.wizard = new Wizard([wizardPage]); // Rebuild the FORM_GROUP object before use because it gets modified by the "should call validate" test @@ -1219,6 +1251,7 @@ describe('CaseEditPageComponent - all other tests', () => { expect(formValueService.sanitise).toHaveBeenCalled(); expect(formValueService.removeUnnecessaryFields).toHaveBeenCalled(); expect(loadingServiceMock.register).toHaveBeenCalled(); + expect(validPageListCaseFieldsService.deleteNonValidatedFields).toHaveBeenCalled(); fixture.whenStable().then(() => { expect(comp.caseEdit.eventTrigger.case_id).toEqual(DRAFT_PREFIX + draft.id); @@ -1422,7 +1455,8 @@ describe('CaseEditPageComponent - all other tests', () => { metadataFields: [caseField2], }, getNextPage: () => null, - callbackErrorsSubject: new Subject() + callbackErrorsSubject: new Subject(), + validPageList: pageList }; snapshot = { queryParamMap: createSpyObj('queryParamMap', ['get']), @@ -1496,9 +1530,10 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: MatDialog, useValue: dialog }, { provide: CaseFieldService, useValue: caseFieldService }, { provide: CaseEditDataService, useValue: caseEditDataService }, - FieldsUtils, + { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, - { provide: LoadingService, useValue: loadingServiceMock } + { provide: LoadingService, useValue: loadingServiceMock }, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ], }).compileComponents(); }) @@ -1621,7 +1656,8 @@ describe('CaseEditPageComponent - all other tests', () => { tabs: [], metadataFields: [], }, - getNextPage: () => null + getNextPage: () => null, + validPageList: pageList }; snapshot = { queryParamMap: createSpyObj('queryParamMap', ['get']), @@ -1690,9 +1726,10 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: MatDialog, useValue: dialog }, { provide: CaseFieldService, useValue: caseFieldService }, { provide: CaseEditDataService, useValue: caseEditDataService }, - FieldsUtils, + { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, - { provide: LoadingService, useValue: loadingServiceMock } + { provide: LoadingService, useValue: loadingServiceMock }, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ], }).compileComponents(); }) @@ -1939,7 +1976,8 @@ describe('CaseEditPageComponent - all other tests', () => { function createWizardPage( fields: CaseField[] = [], isMultiColumn = false, - order = 0 + order = 0, + id = 1 ): WizardPage { const wp: WizardPage = new WizardPage(); wp.case_fields = fields; diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts index 5fe2ca5a95..5300b5ecc0 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts @@ -21,6 +21,7 @@ import { CaseEditComponent } from '../case-edit/case-edit.component'; import { WizardPage } from '../domain/wizard-page.model'; import { Wizard } from '../domain/wizard.model'; import { PageValidationService } from '../services/page-validation.service'; +import { ValidPageListCaseFieldsService } from '../services/valid-page-list-caseFields.service'; @Component({ selector: 'ccd-case-edit-page', @@ -72,7 +73,7 @@ export class CaseEditPageComponent implements OnInit, AfterViewChecked, OnDestro } constructor( - public readonly caseEdit: CaseEditComponent, + public caseEdit: CaseEditComponent, private readonly route: ActivatedRoute, private readonly formValueService: FormValueService, private readonly formErrorService: FormErrorService, @@ -81,7 +82,8 @@ export class CaseEditPageComponent implements OnInit, AfterViewChecked, OnDestro private readonly dialog: MatDialog, private readonly caseFieldService: CaseFieldService, private readonly caseEditDataService: CaseEditDataService, - private readonly loadingService: LoadingService + private readonly loadingService: LoadingService, + private readonly validPageListCaseFieldsService: ValidPageListCaseFieldsService ) { } @@ -276,6 +278,9 @@ export class CaseEditPageComponent implements OnInit, AfterViewChecked, OnDestro } if (!this.caseEdit.isSubmitting && !this.currentPageIsNotValid()) { + if (this.caseEdit.validPageList.findIndex(page=> page.id === this.currentPage.id) === -1) { + this.caseEdit.validPageList.push(this.currentPage); + } this.caseEdit.isSubmitting = true; this.caseEdit.error = null; const caseEventData: CaseEventData = this.buildCaseEventData(); @@ -566,6 +571,9 @@ export class CaseEditPageComponent implements OnInit, AfterViewChecked, OnDestro // Get hold of the CaseEventData. const caseEventData: CaseEventData = this.formValueService.sanitise(formFields) as CaseEventData; + // delete fields which are not part of the case event journey wizard pages case fields + this.validPageListCaseFieldsService.deleteNonValidatedFields(this.caseEdit.validPageList, caseEventData.data, this.eventTrigger.case_fields, fromPreviousPage); + // Tidy it up before we return it. this.formValueService.removeUnnecessaryFields(caseEventData.data, caseFields, clearEmpty, clearNonCase, fromPreviousPage, this.currentPage.case_fields); diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.spec.ts index 5f83511045..46dff680ad 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.spec.ts @@ -17,6 +17,7 @@ import { PaletteUtilsModule } from '../../palette'; import { Confirmation, Wizard, WizardPage, WizardPageField } from '../domain'; import { CaseNotifier } from '../services'; import { WizardFactoryService } from '../services/wizard-factory.service'; +import { ValidPageListCaseFieldsService } from '../services/valid-page-list-caseFields.service'; import { CaseEditComponent } from './case-edit.component'; import createSpyObj = jasmine.createSpyObj; @@ -211,6 +212,7 @@ describe('CaseEditComponent', () => { const registrarService = new ConditionalShowRegistrarService(); let route: any; let mockSessionStorageService: jasmine.SpyObj; + const validPageListCaseFieldsService = new ValidPageListCaseFieldsService(fieldsUtils); describe('profile available in route', () => { routerStub = { @@ -268,6 +270,7 @@ describe('CaseEditComponent', () => { 'populateLinkedCasesDetailsFromCaseFields' ]); mockSessionStorageService = createSpyObj('SessionStorageService', ['getItem', 'removeItem', 'setItem']); + spyOn(validPageListCaseFieldsService, 'deleteNonValidatedFields'); route = { queryParams: of({Origin: 'viewDraft'}), @@ -317,7 +320,8 @@ describe('CaseEditComponent', () => { {provide: ActivatedRoute, useValue: route}, SessionStorageService, WindowService, - { provide: LoadingService, loadingServiceMock } + { provide: LoadingService, loadingServiceMock }, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ] }) .compileComponents(); @@ -1219,6 +1223,7 @@ describe('CaseEditComponent', () => { expect(formValueService.removeCaseFieldsOfType) .toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Array), ['FlagLauncher', 'ComponentLauncher']); expect(formValueService.repopulateFormDataFromCaseFieldValues).toHaveBeenCalled(); + expect(validPageListCaseFieldsService.deleteNonValidatedFields).toHaveBeenCalled(); }); it('should NOT submit the case due to error', () => { @@ -1395,7 +1400,8 @@ describe('CaseEditComponent', () => { {provide: Router, useValue: routerStub}, {provide: ActivatedRoute, useValue: mockRouteNoProfile}, SessionStorageService, - WindowService + WindowService, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, ] }) .compileComponents(); diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.ts index b269f929bd..49ed0961d6 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit/case-edit.component.ts @@ -16,9 +16,11 @@ import { FieldsPurger, FieldsUtils, FormErrorService, FormValueService, LoadingService, SessionStorageService, WindowService } from '../../../services'; +import { ShowCondition } from '../../../directives/conditional-show/domain/conditional-show.model'; import { Confirmation, Wizard, WizardPage } from '../domain'; import { EventCompletionParams } from '../domain/event-completion-params.model'; import { CaseNotifier, WizardFactoryService } from '../services'; +import { ValidPageListCaseFieldsService } from '../services/valid-page-list-caseFields.service'; @Component({ selector: 'ccd-case-edit', @@ -81,6 +83,8 @@ export class CaseEditComponent implements OnInit, OnDestroy { public callbackErrorsSubject: Subject = new Subject(); + public validPageList: WizardPage[] = []; + constructor( private readonly fb: FormBuilder, private readonly caseNotifier: CaseNotifier, @@ -94,7 +98,8 @@ export class CaseEditComponent implements OnInit, OnDestroy { private readonly windowsService: WindowService, private readonly formValueService: FormValueService, private readonly formErrorService: FormErrorService, - private readonly loadingService: LoadingService + private readonly loadingService: LoadingService, + private readonly validPageListCaseFieldsService: ValidPageListCaseFieldsService ) {} public ngOnInit(): void { @@ -286,6 +291,10 @@ export class CaseEditComponent implements OnInit, OnDestroy { this.formValueService.populateLinkedCasesDetailsFromCaseFields(caseEventData.data, eventTrigger.case_fields); // Remove "Launcher"-type fields (these have no values and are not intended to be persisted) this.formValueService.removeCaseFieldsOfType(caseEventData.data, eventTrigger.case_fields, ['FlagLauncher', 'ComponentLauncher']); + + // delete fields which are not part of the case event journey wizard pages case fields + this.validPageListCaseFieldsService.deleteNonValidatedFields(this.validPageList, caseEventData.data, eventTrigger.case_fields, false); + caseEventData.event_token = eventTrigger.event_token; caseEventData.ignore_warning = this.ignoreWarning; if (this.confirmation) { @@ -391,7 +400,9 @@ private replaceHiddenFormValuesWithOriginalCaseData(formGroup: FormGroup, caseFi if (parentField && parentField.formatted_value) { rawFormValueData[key] = parentField.formatted_value[caseField.id]; } else { - rawFormValueData[key] = caseField.formatted_value; + if (!(caseField.hidden && caseField.retain_hidden_value)) { + rawFormValueData[key] = caseField.formatted_value; + } } } } diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-editor.module.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-editor.module.ts index f75215f283..659bfacfdd 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-editor.module.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-editor.module.ts @@ -54,6 +54,7 @@ import { } from './services'; import { CaseEditWizardGuard } from './services/case-edit-wizard.guard'; import { CaseworkerService } from './services/case-worker.service'; +import { ValidPageListCaseFieldsService } from './services/valid-page-list-caseFields.service'; @NgModule({ imports: [ @@ -121,7 +122,8 @@ import { CaseworkerService } from './services/case-worker.service'; JudicialworkerService, CaseworkerService, SessionStorageService, - EventCompletionStateMachineService + EventCompletionStateMachineService, + ValidPageListCaseFieldsService ] }) export class CaseEditorModule { } diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.spec.ts new file mode 100644 index 0000000000..8fa29ddcbe --- /dev/null +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.spec.ts @@ -0,0 +1,71 @@ +import { CaseField } from '../../../domain/definition'; +import { ValidPageListCaseFieldsService } from './valid-page-list-caseFields.service'; +import { WizardPage } from '../../case-editor/domain/wizard-page.model'; +import { FieldsUtils } from '../../../services'; +import { ShowCondition } from '../../../directives/conditional-show/domain'; + +describe('ValidPageListCaseFieldsService', () => { + let validPageListCaseFieldsService: ValidPageListCaseFieldsService; + const caseField1 = buildCaseField('caseField1', 'someValue1'); + const caseField2 = buildCaseField('caseField2', 'someValue2'); + const caseField3 = buildCaseField('caseField3', 'someValue3'); + const caseField4 = buildCaseField('caseField4', 'someValue4'); + const caseField5 = buildCaseField('caseField5', 'someValue5'); + const caseField6 = buildCaseField('caseField6', 'someValue6'); + const caseField7 = buildCaseField('caseField7', 'someValue7'); + const caseField8 = buildCaseField('caseField8', 'someValue8'); + const caseField9 = buildCaseField('caseField9', 'someValue9'); + const caseField10 = buildCaseField('caseField10', 'someValue10'); + + const PAGE_1: WizardPage = buildPage('page1', 'Page 1', 1, [caseField8, caseField9, caseField10]); + const PAGE_2: WizardPage = buildPage('page2', 'Page 2', 2, [caseField1, caseField2, caseField3, caseField4, caseField5], 'caseField1=\"someValue1\"'); + const validPageList = [PAGE_1, PAGE_2]; + PAGE_1.parsedShowCondition = ShowCondition.getInstance(null); + PAGE_2.parsedShowCondition = ShowCondition.getInstance('caseField1=\"someValue1\"'); + const fieldsUtils = new FieldsUtils(); + const eventTriggerFields = [caseField1, caseField2, caseField3, caseField4, + caseField5, caseField6, caseField7, caseField8, caseField9, caseField10]; + let caseEventData; + beforeEach(() => { + caseEventData = { + caseField1: 'someValue1', + caseField4: 'someValue4', + caseField10: 'someValue10', + caseField12: 'someValue12', + caseField14: 'someValue14', + }; + validPageListCaseFieldsService = new ValidPageListCaseFieldsService(fieldsUtils); + }); + + it('should return valid data from case submition after deleting non-valid fields', () => { + validPageListCaseFieldsService.deleteNonValidatedFields(validPageList, caseEventData, eventTriggerFields); + expect(Object.keys(caseEventData).length).toBe(3); + }); + + it('should return valid data from EventJourney after deleting non-valid fields', () => { + validPageListCaseFieldsService.deleteNonValidatedFields(validPageList, caseEventData, eventTriggerFields); + expect(Object.keys(caseEventData).length).toBe(3); + }); + + it('should return same data if fromPrevious argument set to true', () => { + validPageListCaseFieldsService.deleteNonValidatedFields(validPageList, caseEventData, eventTriggerFields, true); + expect(Object.keys(caseEventData).length).toBe(5); + }); + + function buildPage(pageId: string, label: string, order: number, caseFields?: CaseField[], condition?: string): WizardPage { + const wp = new WizardPage(); + wp.id = pageId; + wp.label = label; + wp.order = order; + wp.case_fields = caseFields; + wp.show_condition = condition; + return wp; + } + + function buildCaseField(caseFieldId: string, caseFieldValue: any) { + const cField = new CaseField(); + cField.id = caseFieldId; + cField.value = caseFieldValue; + return cField; + } +}); \ No newline at end of file diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.ts new file mode 100644 index 0000000000..9950d6d177 --- /dev/null +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/services/valid-page-list-caseFields.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { CaseField } from '../../../domain/definition'; +import { WizardPage } from '../../case-editor/domain/wizard-page.model'; +import { FieldsUtils } from '../../../services'; + +@Injectable() +export class ValidPageListCaseFieldsService { + constructor( + private readonly fieldsUtils: FieldsUtils + ) {} + + private isShown(page: WizardPage, eventTriggerFields: CaseField[], data: object): boolean { + const fields = this.fieldsUtils + .mergeCaseFieldsAndFormFields(eventTriggerFields, data); + return page.parsedShowCondition.match(fields); + } + + public deleteNonValidatedFields(validPageList: WizardPage[], data: object, eventTriggerFields: CaseField[], + fromPreviousPage: boolean = false): void { + const validPageListCaseFields: CaseField[] = []; + validPageList.forEach(page => { + if (this.isShown(page, eventTriggerFields, data)) { + page.case_fields.forEach(field => validPageListCaseFields.push(field)); + } + }); + + if (!fromPreviousPage && validPageListCaseFields.length > 0) { + Object.keys(data).forEach(key => { + if (validPageListCaseFields.findIndex((element) => element.id === key) < 0) { + delete data[key]; + } + }); + } + } +} diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/write-linked-cases-field.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/write-linked-cases-field.component.spec.ts index 5847494b07..949d081b2f 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/write-linked-cases-field.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/write-linked-cases-field.component.spec.ts @@ -10,7 +10,7 @@ import { CaseEventData } from '../../../domain/case-event-data.model'; import { CaseView } from '../../../domain/case-view'; import { CaseField } from '../../../domain/definition/case-field.model'; import { Draft } from '../../../domain/draft.model'; -import { LoadingService } from '../../../services'; +import { FieldsUtils, LoadingService } from '../../../services'; import { CaseFieldService } from '../../../services/case-fields/case-field.service'; import { CommonDataService, LovRefDataByServiceModel } from '../../../services/common-data-service/common-data-service'; import { FieldTypeSanitiser } from '../../../services/form/field-type-sanitiser'; @@ -28,6 +28,7 @@ import { LinkedCasesService } from './services'; import { WriteLinkedCasesFieldComponent } from './write-linked-cases-field.component'; import createSpyObj = jasmine.createSpyObj; +import { ValidPageListCaseFieldsService } from '../../case-editor/services/valid-page-list-caseFields.service'; describe('WriteLinkedCasesFieldComponent', () => { let component: WriteLinkedCasesFieldComponent; @@ -84,8 +85,10 @@ describe('WriteLinkedCasesFieldComponent', () => { const formErrorService = new FormErrorService(); const caseFieldService = new CaseFieldService(); const pageValidationService = new PageValidationService(caseFieldService); + const fieldUtils = new FieldsUtils(); + const validPageListCaseFieldsService = new ValidPageListCaseFieldsService(fieldUtils); caseEditPageComponent = new CaseEditPageComponent(caseEditComponentStub, - route, formValueService, formErrorService, null, pageValidationService, dialog, caseFieldService, new CaseEditDataService(), new LoadingService()); + route, formValueService, formErrorService, null, pageValidationService, dialog, caseFieldService, new CaseEditDataService(), new LoadingService(), validPageListCaseFieldsService); const caseInfo = { case_id: '1682374819203471',