Skip to content

Commit

Permalink
[ACS-5877] Migrate widget-visibility service do date-fns (#8870)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
  • Loading branch information
arohilaGL and DenysVuika authored Oct 10, 2023
1 parent a86b9e6 commit 2f28ec9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 41 deletions.
4 changes: 2 additions & 2 deletions lib/core/src/lib/form/models/widget-visibility.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class WidgetVisibilityModel {
leftRestResponseId?: string;
leftFormFieldId?: string;
operator: string;
nextCondition: WidgetVisibilityModel;
nextCondition?: WidgetVisibilityModel;
nextConditionOperator: string;

constructor(private json?: any) {
Expand Down Expand Up @@ -81,7 +81,7 @@ export class WidgetVisibilityModel {
return null;
}

set rightType(rightType: string) {
set rightType(rightType: string | null) {
this.json.rightType = rightType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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();
});

Expand All @@ -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
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});
Expand Down Expand Up @@ -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',
Expand Down
22 changes: 9 additions & 13 deletions lib/core/src/lib/form/services/widget-visibility.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});
Expand Down Expand Up @@ -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',
Expand Down
25 changes: 12 additions & 13 deletions lib/core/src/lib/form/services/widget-visibility.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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;

Expand Down Expand Up @@ -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 '&&';
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -305,7 +305,6 @@ export class WidgetVisibilityService {
case '!contains':
return !this.contains(leftValue, rightValue);
default:
this.logService.error(`Invalid operator: ${operator}`);
return undefined;
}
}
Expand Down

0 comments on commit 2f28ec9

Please sign in to comment.