chore: better processing of dates

This commit is contained in:
Chris Anderson 2025-07-14 20:21:16 -05:00
parent 188edf4f81
commit 76f717bf66
3 changed files with 10 additions and 4 deletions

View file

@ -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

View file

@ -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'

View file

@ -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'