import { Injectable } from '@angular/core'; import { PpDataParserService } from '@penpencil/common'; import { PpConditionConfig, PpGroupCondition } from '../forms.type'; @Injectable() export class PpConditionValidationService { constructor(private dataParserService: PpDataParserService) {} /** * * @param condition :PpConditionConfig * @param data :object * Check whether to display field action or not */ public validate(condition: PpConditionConfig, data: object) { // Check Display Condition Type if (condition.type === 'single') { // Check condition and return result return this.singleValidate(condition, data); } else if (condition.type === 'group') { return this.groupValidate(condition.group!, data); } return; } /** * * @param condition :PpConditionConfig * @param data : object * * Check Display Condition for field action and return result */ private singleValidate(condition: PpConditionConfig, data: object) { // Check operator and condition accordingly switch (condition?.match?.operator) { case 'equals': return ( this.dataParserService.getValue(data, condition.match.key!) === condition.match.value ); case 'notEquals': return ( this.dataParserService.getValue(data, condition.match.key!) !== condition.match.value ); case 'empty': return this.empty( this.dataParserService.getValue(data, condition.match.key!) ); case 'notEmpty': return !this.empty( this.dataParserService.getValue(data, condition.match.key!) ); } return; } private empty(val: any) { const result = val === null || val === undefined || val === '' || JSON.stringify(val) === JSON.stringify({}) || JSON.stringify(val) === JSON.stringify([]); return result; } /** * * @param group :CanGroupCondition * @param data : object * * Check Display Condition for field action and return result */ private groupValidate(group: PpGroupCondition, data: object) { // Check for operator if (group.type === 'and') { // If any value in array if false return false for (let element of group.values) { if (element.type === 'single') { if (!this.singleValidate(element, data)) { return false; } } else if (element.type === 'group') { if (!this.groupValidate(element.group!, data)) { return false; } } } return true; } else if (group.type === 'or') { // If any value in array if true return true for (let element of group.values) { if (element.type === 'single') { if (this.singleValidate(element, data)) { return true; } } else if (element.type === 'group') { if (!this.groupValidate(element.group!, data)) { return true; } } } return false; } return false; } }