[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 <denys.vuika@gmail.com>
This commit is contained in:
Aayush Rohila
2023-10-10 21:42:44 +05:30
committed by GitHub
parent a86b9e625c
commit 2f28ec9b6f
4 changed files with 32 additions and 41 deletions

View File

@@ -21,7 +21,7 @@ export class WidgetVisibilityModel {
leftRestResponseId?: string; leftRestResponseId?: string;
leftFormFieldId?: string; leftFormFieldId?: string;
operator: string; operator: string;
nextCondition: WidgetVisibilityModel; nextCondition?: WidgetVisibilityModel;
nextConditionOperator: string; nextConditionOperator: string;
constructor(private json?: any) { constructor(private json?: any) {
@@ -81,7 +81,7 @@ export class WidgetVisibilityModel {
return null; return null;
} }
set rightType(rightType: string) { set rightType(rightType: string | null) {
this.json.rightType = rightType; this.json.rightType = rightType;
} }

View File

@@ -38,9 +38,9 @@ import { TranslateModule } from '@ngx-translate/core';
declare let jasmine: any; declare let jasmine: any;
describe('WidgetVisibilityCloudService', () => { describe('WidgetVisibilityCloudService', () => {
let service: WidgetVisibilityService; let service: WidgetVisibilityService;
let booleanResult: boolean; let booleanResult: boolean | undefined;
const stubFormWithFields = new FormModel(fakeFormJson); const stubFormWithFields = new FormModel(fakeFormJson);
beforeEach(() => { beforeEach(() => {
@@ -131,7 +131,7 @@ describe('WidgetVisibilityCloudService', () => {
}); });
it('should return undefined for invalid operation', () => { it('should return undefined for invalid operation', () => {
booleanResult = service.evaluateCondition(null, null, undefined); booleanResult = service.evaluateCondition(null, null, '');
expect(booleanResult).toBeUndefined(); expect(booleanResult).toBeUndefined();
}); });
@@ -156,7 +156,7 @@ describe('WidgetVisibilityCloudService', () => {
describe('should return the value of the field', () => { describe('should return the value of the field', () => {
let visibilityObjTest: WidgetVisibilityModel; let visibilityObjTest: WidgetVisibilityModel;
let fakeFormWithField = new FormModel(fakeFormJson); let fakeFormWithField = new FormModel(fakeFormJson);
const jsonFieldFake = { const jsonFieldFake: { id: string; value: string; visibilityCondition?: WidgetVisibilityModel } = {
id: 'FAKE_FORM_FIELD_ID', id: 'FAKE_FORM_FIELD_ID',
value: 'FAKE_FORM_FIELD_VALUE', value: 'FAKE_FORM_FIELD_VALUE',
visibilityCondition: undefined visibilityCondition: undefined
@@ -177,10 +177,6 @@ describe('WidgetVisibilityCloudService', () => {
jsonFieldFake.visibilityCondition = visibilityObjTest; jsonFieldFake.visibilityCondition = visibilityObjTest;
}); });
afterEach(() => {
service.cleanProcessVariable();
});
it('should be able to retrieve a field value searching in the form', () => { it('should be able to retrieve a field value searching in the form', () => {
const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_WITH_CONDITION'); const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_WITH_CONDITION');
const formValue = service.searchValueInForm(formField, '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', () => { 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); const fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake);
fakeFormField.isVisible = false; fakeFormField.isVisible = false;
service.refreshEntityVisibility(fakeFormField); service.refreshEntityVisibility(fakeFormField);
@@ -363,14 +359,14 @@ describe('WidgetVisibilityCloudService', () => {
}); });
it('should be able to retrieve the value of a form variable', () => { 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).not.toBeUndefined();
expect(varValue).toBe('form_value_test'); expect(varValue).toBe('form_value_test');
}); });
it('should return undefined for not existing form variable', () => { 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(); expect(varValue).toBeUndefined();
}); });
@@ -596,14 +592,14 @@ describe('WidgetVisibilityCloudService', () => {
it('should evaluate radio box LABEL condition', (done) => { it('should evaluate radio box LABEL condition', (done) => {
visibilityObjTest.leftFormFieldId = 'radioBoxField_LABEL'; visibilityObjTest.leftFormFieldId = 'radioBoxField_LABEL';
visibilityObjTest.leftRestResponseId = null; visibilityObjTest.leftRestResponseId = undefined;
visibilityObjTest.operator = '=='; visibilityObjTest.operator = '==';
visibilityObjTest.rightValue = 'No'; visibilityObjTest.rightValue = 'No';
visibilityObjTest.rightType = null; visibilityObjTest.rightType = null;
visibilityObjTest.rightFormFieldId = ''; visibilityObjTest.rightFormFieldId = '';
visibilityObjTest.rightRestResponseId = ''; visibilityObjTest.rightRestResponseId = '';
visibilityObjTest.nextConditionOperator = ''; visibilityObjTest.nextConditionOperator = '';
visibilityObjTest.nextCondition = null; visibilityObjTest.nextCondition = undefined;
const radioBoxForm = new FormModel({ const radioBoxForm = new FormModel({
id: '9999', id: '9999',

View File

@@ -37,9 +37,9 @@ import { CoreTestingModule } from '../../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
describe('WidgetVisibilityService', () => { describe('WidgetVisibilityService', () => {
let service: WidgetVisibilityService; let service: WidgetVisibilityService;
let booleanResult: boolean; let booleanResult: boolean | undefined;
const stubFormWithFields = new FormModel(fakeFormJson); const stubFormWithFields = new FormModel(fakeFormJson);
beforeEach(() => { beforeEach(() => {
@@ -125,7 +125,7 @@ describe('WidgetVisibilityService', () => {
}); });
it('should return undefined for invalid operation', () => { it('should return undefined for invalid operation', () => {
booleanResult = service.evaluateCondition(null, null, undefined); booleanResult = service.evaluateCondition(null, null, '');
expect(booleanResult).toBeUndefined(); expect(booleanResult).toBeUndefined();
}); });
@@ -154,7 +154,7 @@ describe('WidgetVisibilityService', () => {
let visibilityObjTest: WidgetVisibilityModel; let visibilityObjTest: WidgetVisibilityModel;
let fakeFormWithField: FormModel; let fakeFormWithField: FormModel;
const jsonFieldFake = { const jsonFieldFake: { id: string; value: string; visibilityCondition?: WidgetVisibilityModel } = {
id: 'FAKE_FORM_FIELD_ID', id: 'FAKE_FORM_FIELD_ID',
value: 'FAKE_FORM_FIELD_VALUE', value: 'FAKE_FORM_FIELD_VALUE',
visibilityCondition: undefined visibilityCondition: undefined
@@ -176,10 +176,6 @@ describe('WidgetVisibilityService', () => {
jsonFieldFake.visibilityCondition = visibilityObjTest; jsonFieldFake.visibilityCondition = visibilityObjTest;
}); });
afterEach(() => {
service.cleanProcessVariable();
});
it('should be able to retrieve a field value searching in the form', () => { it('should be able to retrieve a field value searching in the form', () => {
const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_WITH_CONDITION'); const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_WITH_CONDITION');
const formValue = service.searchValueInForm(formField, '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', () => { 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); const fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake);
fakeFormField.isVisible = false; fakeFormField.isVisible = false;
service.refreshEntityVisibility(fakeFormField); service.refreshEntityVisibility(fakeFormField);
@@ -345,14 +341,14 @@ describe('WidgetVisibilityService', () => {
}); });
it('should be able to retrieve the value of a form variable', () => { 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).not.toBeUndefined();
expect(varValue).toBe('form_value_test'); expect(varValue).toBe('form_value_test');
}); });
it('should return undefined for not existing form variable', () => { 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(); expect(varValue).toBeUndefined();
}); });
@@ -547,14 +543,14 @@ describe('WidgetVisibilityService', () => {
it('should evaluate radio box LABEL condition', (done) => { it('should evaluate radio box LABEL condition', (done) => {
visibilityObjTest.leftFormFieldId = 'radioBoxField_LABEL'; visibilityObjTest.leftFormFieldId = 'radioBoxField_LABEL';
visibilityObjTest.leftRestResponseId = null; visibilityObjTest.leftRestResponseId = undefined;
visibilityObjTest.operator = '=='; visibilityObjTest.operator = '==';
visibilityObjTest.rightValue = 'No'; visibilityObjTest.rightValue = 'No';
visibilityObjTest.rightType = null; visibilityObjTest.rightType = null;
visibilityObjTest.rightFormFieldId = ''; visibilityObjTest.rightFormFieldId = '';
visibilityObjTest.rightRestResponseId = ''; visibilityObjTest.rightRestResponseId = '';
visibilityObjTest.nextConditionOperator = ''; visibilityObjTest.nextConditionOperator = '';
visibilityObjTest.nextCondition = null; visibilityObjTest.nextCondition = undefined;
const radioBoxForm = new FormModel({ const radioBoxForm = new FormModel({
id: '9999', id: '9999',

View File

@@ -15,12 +15,11 @@
* limitations under the License. * limitations under the License.
*/ */
import { LogService } from '../../common/services/log.service';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import moment from 'moment';
import { FormFieldModel, FormModel, TabModel, ContainerModel, FormOutcomeModel } from '../components/widgets/core'; import { FormFieldModel, FormModel, TabModel, ContainerModel, FormOutcomeModel } from '../components/widgets/core';
import { TaskProcessVariableModel } from '../models/task-process-variable.model'; import { TaskProcessVariableModel } from '../models/task-process-variable.model';
import { WidgetVisibilityModel, WidgetTypeEnum } from '../models/widget-visibility.model'; import { WidgetVisibilityModel, WidgetTypeEnum } from '../models/widget-visibility.model';
import { format, isValid, parse } from 'date-fns';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -29,8 +28,6 @@ export class WidgetVisibilityService {
private processVarList: TaskProcessVariableModel[]; private processVarList: TaskProcessVariableModel[];
private form: FormModel; private form: FormModel;
constructor(private logService: LogService) {}
public refreshVisibility(form: FormModel, processVarList?: TaskProcessVariableModel[]) { public refreshVisibility(form: FormModel, processVarList?: TaskProcessVariableModel[]) {
this.form = form; this.form = form;
@@ -87,12 +84,12 @@ export class WidgetVisibilityService {
return !!result; return !!result;
} }
private transformToLiteralExpression(currentExpression: any): string { private transformToLiteralExpression(currentExpression: { value: any; operator: string }): string {
const currentTransformedValue = currentExpression.value ? 'true' : 'false'; const currentTransformedValue = currentExpression.value ? 'true' : 'false';
return currentTransformedValue.concat(this.transformToLiteralOperator(currentExpression.operator)); return currentTransformedValue.concat(this.transformToLiteralOperator(currentExpression.operator));
} }
private transformToLiteralOperator(currentOperator): string { private transformToLiteralOperator(currentOperator: string): string {
switch (currentOperator) { switch (currentOperator) {
case 'and': case 'and':
return '&&'; return '&&';
@@ -107,11 +104,11 @@ export class WidgetVisibilityService {
} }
} }
public getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string { getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string {
let leftValue = ''; let leftValue = '';
if (visibilityObj.leftType && visibilityObj.leftType === WidgetTypeEnum.variable) { if (visibilityObj.leftType === WidgetTypeEnum.variable) {
leftValue = this.getVariableValue(form, visibilityObj.leftValue, this.processVarList); 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); leftValue = this.getFormValue(form, visibilityObj.leftValue);
if (leftValue === undefined || leftValue === '') { if (leftValue === undefined || leftValue === '') {
const variableValue = this.getVariableValue(form, visibilityObj.leftValue, this.processVarList); const variableValue = this.getVariableValue(form, visibilityObj.leftValue, this.processVarList);
@@ -121,19 +118,22 @@ export class WidgetVisibilityService {
return leftValue; return leftValue;
} }
public getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string { getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string {
let valueFound = ''; let valueFound = '';
if (visibilityObj.rightType === WidgetTypeEnum.variable) { if (visibilityObj.rightType === WidgetTypeEnum.variable) {
valueFound = this.getVariableValue(form, visibilityObj.rightValue, this.processVarList); valueFound = this.getVariableValue(form, visibilityObj.rightValue, this.processVarList);
} else if (visibilityObj.rightType === WidgetTypeEnum.field) { } else if (visibilityObj.rightType === WidgetTypeEnum.field) {
valueFound = this.getFormValue(form, visibilityObj.rightValue); valueFound = this.getFormValue(form, visibilityObj.rightValue);
} else { } else {
if (moment(visibilityObj.rightValue, 'YYYY-MM-DD', true).isValid()) { const value = parse(`${visibilityObj.rightValue}`, 'yyyy-mm-dd', new Date());
valueFound = visibilityObj.rightValue + 'T00:00:00.000Z'; if (isValid(value)) {
valueFound = `${format(value, 'yyyy-mm-dd')}T00:00:00.000Z`;
} else { } else {
valueFound = visibilityObj.rightValue; valueFound = visibilityObj.rightValue;
} }
} }
return valueFound; return valueFound;
} }
@@ -305,7 +305,6 @@ export class WidgetVisibilityService {
case '!contains': case '!contains':
return !this.contains(leftValue, rightValue); return !this.contains(leftValue, rightValue);
default: default:
this.logService.error(`Invalid operator: ${operator}`);
return undefined; return undefined;
} }
} }