diff --git a/apps/platform/src/rules/DateRule.ts b/apps/platform/src/rules/DateRule.ts index 7dd29642..6b1a0170 100644 --- a/apps/platform/src/rules/DateRule.ts +++ b/apps/platform/src/rules/DateRule.ts @@ -1,4 +1,4 @@ -import { isAfter, isBefore, isEqual } from 'date-fns' +import { format, isAfter, isBefore, isEqual } from 'date-fns' import { RuleCheck, RuleEvalException } from './RuleEngine' import { compile, queryPath, queryValue, whereQuery, whereQueryNullable } from './RuleHelpers' import { Rule, RuleTree } from './Rule' @@ -44,7 +44,9 @@ export default { }) }, query({ rule }) { - const path = queryPath(rule) + + // Make sure we can handle numbers, strings and dates + const path = `parseDateTimeBestEffortOrNull(NULLIF(toString(${queryPath(rule)}), ''))` if (rule.operator === 'is set') { return whereQueryNullable(path, false) @@ -60,6 +62,10 @@ export default { return whereQuery(path, rule.operator, ruleValue.getTime()) } + if (rule.operator === 'is same day') { + return `toDate(${path}) = toDate('${format(ruleValue, 'yyyy-MM-dd')}')` + } + throw new RuleEvalException(rule, 'unknown operator: ' + rule.operator) }, } satisfies RuleCheck diff --git a/apps/platform/src/rules/Rule.ts b/apps/platform/src/rules/Rule.ts index 9482f182..a2696633 100644 --- a/apps/platform/src/rules/Rule.ts +++ b/apps/platform/src/rules/Rule.ts @@ -1,4 +1,4 @@ -export type Operator = '=' | '!=' | '<' |'<=' | '>' | '>=' | '=' | 'is set' | 'is not set' | 'or' | 'and' | 'empty' | 'contains' | 'not contain' | 'starts with' | 'not start with' | 'ends with' | 'any' | 'none' +export type Operator = '=' | '!=' | '<' |'<=' | '>' | '>=' | '=' | 'is set' | 'is not set' | 'or' | 'and' | 'empty' | 'contains' | 'not contain' | 'starts with' | 'not start with' | 'ends with' | 'any' | 'none' | 'is same day' export type RuleType = 'wrapper' | 'string' | 'number' | 'boolean' | 'date' | 'array' export type RuleGroup = 'user' | 'event' | 'parent' diff --git a/apps/ui/src/types.ts b/apps/ui/src/types.ts index 98193d90..fcf0acd9 100644 --- a/apps/ui/src/types.ts +++ b/apps/ui/src/types.ts @@ -43,7 +43,7 @@ export interface OAuthResponse { refresh_expires_at: Date } -export type Operator = '=' | '!=' | '<' | '<=' | '>' | '>=' | '=' | 'is set' | 'is not set' | 'or' | 'and' | 'xor' | 'empty' | 'contains' | 'not contain' | 'starts with' | 'not start with' | 'any' | 'none' +export type Operator = '=' | '!=' | '<' | '<=' | '>' | '>=' | '=' | 'is set' | 'is not set' | 'or' | 'and' | 'xor' | 'empty' | 'contains' | 'not contain' | 'starts with' | 'not start with' | 'any' | 'none' | 'is same day' export type RuleType = 'wrapper' | 'string' | 'number' | 'boolean' | 'date' | 'array' export type RuleGroup = 'user' | 'event' | 'parent'