Skip to content

Commit

Permalink
feat - autosail constarints update
Browse files Browse the repository at this point in the history
  • Loading branch information
jaga-live committed Feb 3, 2024
1 parent 6e0ef9b commit 05e6ec5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
23 changes: 9 additions & 14 deletions src/modules/auto-sail/auto-sail-constraints.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand All @@ -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)) ||
Expand All @@ -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()) ||
Expand Down
1 change: 1 addition & 0 deletions src/modules/auto-sail/auto-sail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum AutoSailConstraintsType {
MESSAGE = 'message',
PLAIN_MESSAGE = 'plainMessage',
CREATED_AT = 'createdAt',
USER_ID = 'userId',
}
16 changes: 4 additions & 12 deletions src/modules/auto-sail/mappings/auto-sail-constraints.mapping.ts
Original file line number Diff line number Diff line change
@@ -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',
};

0 comments on commit 05e6ec5

Please sign in to comment.