From 05e6ec524e2d59f8f7baae5244b4e9ebefe35f32 Mon Sep 17 00:00:00 2001 From: Jaga Date: Sat, 3 Feb 2024 22:21:55 +0530 Subject: [PATCH] feat - autosail constarints update --- .../auto-sail-constraints.service.ts | 23 ++++++++----------- src/modules/auto-sail/auto-sail.service.ts | 1 + .../enum/auto-sail-constraints-type.enum.ts | 2 +- .../mappings/auto-sail-constraints.mapping.ts | 16 ++++--------- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/modules/auto-sail/auto-sail-constraints.service.ts b/src/modules/auto-sail/auto-sail-constraints.service.ts index 886c833..6c3f95e 100644 --- a/src/modules/auto-sail/auto-sail-constraints.service.ts +++ b/src/modules/auto-sail/auto-sail-constraints.service.ts @@ -1,10 +1,7 @@ import { injectable } from 'inversify'; import { AutoSailConfigDto } from './dto/auto-sail-config.dto'; import { AutoSailConstraintsDto } from './dto/auto-sail-constraints.dto'; -import { - AutoSailConstraintsMapping, - AutoSailDynamicFieldsMapping, -} from './mappings/auto-sail-constraints.mapping'; +import { AutoSailConstraintsTypeMapping } from './mappings/auto-sail-constraints.mapping'; @injectable() export class AutoSailConstraintsService { @@ -15,7 +12,7 @@ export class AutoSailConstraintsService { ) { let isValid = false; for (const constraint of constraints) { - const conditionTypeKey = AutoSailConstraintsMapping[constraint.type]; + const conditionTypeKey = AutoSailConstraintsTypeMapping[constraint.type]; isValid = this[`${conditionTypeKey}Validation`]( constraint, @@ -42,10 +39,7 @@ export class AutoSailConstraintsService { case 'equal': case 'notEqual': { const configValue = condition[key]?.value; - const payloadValue = - payload[ - AutoSailDynamicFieldsMapping?.[triggerEvent]?.[constraint.type] - ]; + const payloadValue = payload[constraint.type]; if ( (key === 'equal' && configValue?.includes(payloadValue)) || @@ -72,11 +66,12 @@ export class AutoSailConstraintsService { case 'gt': case 'lt': { const configIsoTime = new Date(condition[key]?.value); - const targetIsoTime = new Date( - payload[ - AutoSailDynamicFieldsMapping?.[triggerEvent]?.[constraint.type] - ], - ); + let targetIsoTime = new Date(payload[constraint.type]); + + /**If value for constraint type is not available, current date is used */ + targetIsoTime = isNaN(targetIsoTime.getTime()) + ? new Date() + : targetIsoTime; if ( isNaN(configIsoTime.getTime()) || diff --git a/src/modules/auto-sail/auto-sail.service.ts b/src/modules/auto-sail/auto-sail.service.ts index c804b49..6a9f345 100644 --- a/src/modules/auto-sail/auto-sail.service.ts +++ b/src/modules/auto-sail/auto-sail.service.ts @@ -19,6 +19,7 @@ export class AutoSailService { triggerEvent: string, autoSailConfig: AutoSailConfigDto[], ) { + /**Filter out configs matching the current event */ const filterConfig = autoSailConfig.filter( (config) => config.triggerEvent === triggerEvent, ); diff --git a/src/modules/auto-sail/enum/auto-sail-constraints-type.enum.ts b/src/modules/auto-sail/enum/auto-sail-constraints-type.enum.ts index 2378df8..fd68041 100644 --- a/src/modules/auto-sail/enum/auto-sail-constraints-type.enum.ts +++ b/src/modules/auto-sail/enum/auto-sail-constraints-type.enum.ts @@ -1,5 +1,5 @@ export enum AutoSailConstraintsType { - MESSAGE = 'message', + PLAIN_MESSAGE = 'plainMessage', CREATED_AT = 'createdAt', USER_ID = 'userId', } diff --git a/src/modules/auto-sail/mappings/auto-sail-constraints.mapping.ts b/src/modules/auto-sail/mappings/auto-sail-constraints.mapping.ts index 54d98e8..67fd606 100644 --- a/src/modules/auto-sail/mappings/auto-sail-constraints.mapping.ts +++ b/src/modules/auto-sail/mappings/auto-sail-constraints.mapping.ts @@ -1,17 +1,9 @@ -export const AutoSailDynamicFieldsMapping = { - messageCreate: { - message: 'plainMessage', - createdAt: 'createdAt', - updatedAt: 'updatedAt', - userId: 'userId', - channelId: 'channelId', - }, -}; - -export const AutoSailConstraintsMapping = { - message: 'text', +/**Mapping constraint type based on key*/ +export const AutoSailConstraintsTypeMapping = { + plainMessage: 'text', userId: 'text', roleId: 'text', channelId: 'text', createdAt: 'date', + currentDate: 'date', };