omnivore/packages/api/src/services/rules.ts
2023-10-05 14:27:17 +08:00

30 lines
586 B
TypeScript

import { ILike } from 'typeorm'
import { Rule, RuleAction } from '../entity/rule'
import { authTrx } from '../repository'
export const createRule = async (
userId: string,
rule: {
name: string
description?: string
actions: RuleAction[]
filter: string
}
): Promise<Rule> => {
const existingRule = await authTrx((t) =>
t.getRepository(Rule).findOneBy({
name: ILike(rule.name),
})
)
if (existingRule) {
return existingRule
}
return authTrx((t) =>
t.getRepository(Rule).save({
...rule,
user: { id: userId },
})
)
}