From 2f28ec9b6f8e04e73c1cb0297adecafec466bbbc Mon Sep 17 00:00:00 2001 From: Aayush Rohila <112551534+arohilaGL@users.noreply.github.com> Date: Tue, 10 Oct 2023 21:42:44 +0530 Subject: [PATCH] [ACS-5877] Migrate widget-visibility service do date-fns (#8870) * [ACS-5877] Migrate from moment to date-fns for widget-visibility service * [ACS-5877] Migration from moment to date-fns in widget-visiblity.service * migrate widget visibility service to date-fns * restore clean method --------- Co-authored-by: Denys Vuika --- .../form/models/widget-visibility.model.ts | 4 +-- .../widget-visibility-cloud.service.spec.ts | 22 +++++++--------- .../widget-visibility.service.spec.ts | 22 +++++++--------- .../services/widget-visibility.service.ts | 25 +++++++++---------- 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/lib/core/src/lib/form/models/widget-visibility.model.ts b/lib/core/src/lib/form/models/widget-visibility.model.ts index e6904967c91..fce444e152b 100644 --- a/lib/core/src/lib/form/models/widget-visibility.model.ts +++ b/lib/core/src/lib/form/models/widget-visibility.model.ts @@ -21,7 +21,7 @@ export class WidgetVisibilityModel { leftRestResponseId?: string; leftFormFieldId?: string; operator: string; - nextCondition: WidgetVisibilityModel; + nextCondition?: WidgetVisibilityModel; nextConditionOperator: string; constructor(private json?: any) { @@ -81,7 +81,7 @@ export class WidgetVisibilityModel { return null; } - set rightType(rightType: string) { + set rightType(rightType: string | null) { this.json.rightType = rightType; } diff --git a/lib/core/src/lib/form/services/widget-visibility-cloud.service.spec.ts b/lib/core/src/lib/form/services/widget-visibility-cloud.service.spec.ts index ec3f03a26f4..1326519f879 100644 --- a/lib/core/src/lib/form/services/widget-visibility-cloud.service.spec.ts +++ b/lib/core/src/lib/form/services/widget-visibility-cloud.service.spec.ts @@ -38,9 +38,9 @@ import { TranslateModule } from '@ngx-translate/core'; declare let jasmine: any; describe('WidgetVisibilityCloudService', () => { - let service: WidgetVisibilityService; - let booleanResult: boolean; + let booleanResult: boolean | undefined; + const stubFormWithFields = new FormModel(fakeFormJson); beforeEach(() => { @@ -131,7 +131,7 @@ describe('WidgetVisibilityCloudService', () => { }); it('should return undefined for invalid operation', () => { - booleanResult = service.evaluateCondition(null, null, undefined); + booleanResult = service.evaluateCondition(null, null, ''); expect(booleanResult).toBeUndefined(); }); @@ -156,7 +156,7 @@ describe('WidgetVisibilityCloudService', () => { describe('should return the value of the field', () => { let visibilityObjTest: WidgetVisibilityModel; let fakeFormWithField = new FormModel(fakeFormJson); - const jsonFieldFake = { + const jsonFieldFake: { id: string; value: string; visibilityCondition?: WidgetVisibilityModel } = { id: 'FAKE_FORM_FIELD_ID', value: 'FAKE_FORM_FIELD_VALUE', visibilityCondition: undefined @@ -177,10 +177,6 @@ describe('WidgetVisibilityCloudService', () => { jsonFieldFake.visibilityCondition = visibilityObjTest; }); - afterEach(() => { - service.cleanProcessVariable(); - }); - it('should be able to retrieve a field value searching in the form', () => { const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_WITH_CONDITION'); const formValue = service.searchValueInForm(formField, 'FIELD_WITH_CONDITION'); @@ -354,7 +350,7 @@ describe('WidgetVisibilityCloudService', () => { }); it('should return always true when field does not have a visibility condition', () => { - jsonFieldFake.visibilityCondition = null; + jsonFieldFake.visibilityCondition = undefined; const fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake); fakeFormField.isVisible = false; service.refreshEntityVisibility(fakeFormField); @@ -363,14 +359,14 @@ describe('WidgetVisibilityCloudService', () => { }); it('should be able to retrieve the value of a form variable', () => { - const varValue = service.getVariableValue(fakeForm, 'FORM_VARIABLE_TEST', null); + const varValue = service.getVariableValue(fakeForm, 'FORM_VARIABLE_TEST', []); expect(varValue).not.toBeUndefined(); expect(varValue).toBe('form_value_test'); }); it('should return undefined for not existing form variable', () => { - const varValue = service.getVariableValue(fakeForm, 'MYSTERY_FORM_VARIABLE', null); + const varValue = service.getVariableValue(fakeForm, 'MYSTERY_FORM_VARIABLE', []); expect(varValue).toBeUndefined(); }); @@ -596,14 +592,14 @@ describe('WidgetVisibilityCloudService', () => { it('should evaluate radio box LABEL condition', (done) => { visibilityObjTest.leftFormFieldId = 'radioBoxField_LABEL'; - visibilityObjTest.leftRestResponseId = null; + visibilityObjTest.leftRestResponseId = undefined; visibilityObjTest.operator = '=='; visibilityObjTest.rightValue = 'No'; visibilityObjTest.rightType = null; visibilityObjTest.rightFormFieldId = ''; visibilityObjTest.rightRestResponseId = ''; visibilityObjTest.nextConditionOperator = ''; - visibilityObjTest.nextCondition = null; + visibilityObjTest.nextCondition = undefined; const radioBoxForm = new FormModel({ id: '9999', diff --git a/lib/core/src/lib/form/services/widget-visibility.service.spec.ts b/lib/core/src/lib/form/services/widget-visibility.service.spec.ts index 72f94c8e1ad..2740809f555 100644 --- a/lib/core/src/lib/form/services/widget-visibility.service.spec.ts +++ b/lib/core/src/lib/form/services/widget-visibility.service.spec.ts @@ -37,9 +37,9 @@ import { CoreTestingModule } from '../../testing/core.testing.module'; import { TranslateModule } from '@ngx-translate/core'; describe('WidgetVisibilityService', () => { - let service: WidgetVisibilityService; - let booleanResult: boolean; + let booleanResult: boolean | undefined; + const stubFormWithFields = new FormModel(fakeFormJson); beforeEach(() => { @@ -125,7 +125,7 @@ describe('WidgetVisibilityService', () => { }); it('should return undefined for invalid operation', () => { - booleanResult = service.evaluateCondition(null, null, undefined); + booleanResult = service.evaluateCondition(null, null, ''); expect(booleanResult).toBeUndefined(); }); @@ -154,7 +154,7 @@ describe('WidgetVisibilityService', () => { let visibilityObjTest: WidgetVisibilityModel; let fakeFormWithField: FormModel; - const jsonFieldFake = { + const jsonFieldFake: { id: string; value: string; visibilityCondition?: WidgetVisibilityModel } = { id: 'FAKE_FORM_FIELD_ID', value: 'FAKE_FORM_FIELD_VALUE', visibilityCondition: undefined @@ -176,10 +176,6 @@ describe('WidgetVisibilityService', () => { jsonFieldFake.visibilityCondition = visibilityObjTest; }); - afterEach(() => { - service.cleanProcessVariable(); - }); - it('should be able to retrieve a field value searching in the form', () => { const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_WITH_CONDITION'); const formValue = service.searchValueInForm(formField, 'FIELD_WITH_CONDITION'); @@ -336,7 +332,7 @@ describe('WidgetVisibilityService', () => { }); it('should return always true when field does not have a visibility condition', () => { - jsonFieldFake.visibilityCondition = null; + jsonFieldFake.visibilityCondition = undefined; const fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake); fakeFormField.isVisible = false; service.refreshEntityVisibility(fakeFormField); @@ -345,14 +341,14 @@ describe('WidgetVisibilityService', () => { }); it('should be able to retrieve the value of a form variable', () => { - const varValue = service.getVariableValue(fakeForm, 'FORM_VARIABLE_TEST', null); + const varValue = service.getVariableValue(fakeForm, 'FORM_VARIABLE_TEST', []); expect(varValue).not.toBeUndefined(); expect(varValue).toBe('form_value_test'); }); it('should return undefined for not existing form variable', () => { - const varValue = service.getVariableValue(fakeForm, 'MYSTERY_FORM_VARIABLE', null); + const varValue = service.getVariableValue(fakeForm, 'MYSTERY_FORM_VARIABLE', []); expect(varValue).toBeUndefined(); }); @@ -547,14 +543,14 @@ describe('WidgetVisibilityService', () => { it('should evaluate radio box LABEL condition', (done) => { visibilityObjTest.leftFormFieldId = 'radioBoxField_LABEL'; - visibilityObjTest.leftRestResponseId = null; + visibilityObjTest.leftRestResponseId = undefined; visibilityObjTest.operator = '=='; visibilityObjTest.rightValue = 'No'; visibilityObjTest.rightType = null; visibilityObjTest.rightFormFieldId = ''; visibilityObjTest.rightRestResponseId = ''; visibilityObjTest.nextConditionOperator = ''; - visibilityObjTest.nextCondition = null; + visibilityObjTest.nextCondition = undefined; const radioBoxForm = new FormModel({ id: '9999', diff --git a/lib/core/src/lib/form/services/widget-visibility.service.ts b/lib/core/src/lib/form/services/widget-visibility.service.ts index ff6add70a6a..db9bfab869c 100644 --- a/lib/core/src/lib/form/services/widget-visibility.service.ts +++ b/lib/core/src/lib/form/services/widget-visibility.service.ts @@ -15,12 +15,11 @@ * limitations under the License. */ -import { LogService } from '../../common/services/log.service'; import { Injectable } from '@angular/core'; -import moment from 'moment'; import { FormFieldModel, FormModel, TabModel, ContainerModel, FormOutcomeModel } from '../components/widgets/core'; import { TaskProcessVariableModel } from '../models/task-process-variable.model'; import { WidgetVisibilityModel, WidgetTypeEnum } from '../models/widget-visibility.model'; +import { format, isValid, parse } from 'date-fns'; @Injectable({ providedIn: 'root' @@ -29,8 +28,6 @@ export class WidgetVisibilityService { private processVarList: TaskProcessVariableModel[]; private form: FormModel; - constructor(private logService: LogService) {} - public refreshVisibility(form: FormModel, processVarList?: TaskProcessVariableModel[]) { this.form = form; @@ -87,12 +84,12 @@ export class WidgetVisibilityService { return !!result; } - private transformToLiteralExpression(currentExpression: any): string { + private transformToLiteralExpression(currentExpression: { value: any; operator: string }): string { const currentTransformedValue = currentExpression.value ? 'true' : 'false'; return currentTransformedValue.concat(this.transformToLiteralOperator(currentExpression.operator)); } - private transformToLiteralOperator(currentOperator): string { + private transformToLiteralOperator(currentOperator: string): string { switch (currentOperator) { case 'and': return '&&'; @@ -107,11 +104,11 @@ export class WidgetVisibilityService { } } - public getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string { + getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string { let leftValue = ''; - if (visibilityObj.leftType && visibilityObj.leftType === WidgetTypeEnum.variable) { + if (visibilityObj.leftType === WidgetTypeEnum.variable) { leftValue = this.getVariableValue(form, visibilityObj.leftValue, this.processVarList); - } else if (visibilityObj.leftType && visibilityObj.leftType === WidgetTypeEnum.field) { + } else if (visibilityObj.leftType === WidgetTypeEnum.field) { leftValue = this.getFormValue(form, visibilityObj.leftValue); if (leftValue === undefined || leftValue === '') { const variableValue = this.getVariableValue(form, visibilityObj.leftValue, this.processVarList); @@ -121,19 +118,22 @@ export class WidgetVisibilityService { return leftValue; } - public getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string { + getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string { let valueFound = ''; + if (visibilityObj.rightType === WidgetTypeEnum.variable) { valueFound = this.getVariableValue(form, visibilityObj.rightValue, this.processVarList); } else if (visibilityObj.rightType === WidgetTypeEnum.field) { valueFound = this.getFormValue(form, visibilityObj.rightValue); } else { - if (moment(visibilityObj.rightValue, 'YYYY-MM-DD', true).isValid()) { - valueFound = visibilityObj.rightValue + 'T00:00:00.000Z'; + const value = parse(`${visibilityObj.rightValue}`, 'yyyy-mm-dd', new Date()); + if (isValid(value)) { + valueFound = `${format(value, 'yyyy-mm-dd')}T00:00:00.000Z`; } else { valueFound = visibilityObj.rightValue; } } + return valueFound; } @@ -305,7 +305,6 @@ export class WidgetVisibilityService { case '!contains': return !this.contains(leftValue, rightValue); default: - this.logService.error(`Invalid operator: ${operator}`); return undefined; } }