Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import {
2 type RuleAlarmStatus,
3 RuleStatus,
4 type RuleType,
5 type ConditionSet,
6 type Action,
7 type Policy,
8} from '../../services/moderationConfigService/index.js';
9import { type GraphQLUserParent } from '../../graphql/datasources/userKyselyPersistence.js';
10
11export type RuleLatestVersionRow = {
12 ruleId: string;
13 version: string;
14};
15
16/**
17 * Rule row fields shared by the rule engine (no GraphQL resolver methods).
18 */
19export type PlainRuleWithLatestVersion = {
20 id: string;
21 name: string;
22 description: string | null;
23 statusIfUnexpired: Exclude<RuleStatus, typeof RuleStatus.EXPIRED>;
24 status: RuleStatus;
25 tags: string[];
26 maxDailyActions: number | null;
27 dailyActionsRun: number;
28 lastActionDate: string | null;
29 createdAt: Date;
30 updatedAt: Date;
31 orgId: string;
32 creatorId: string;
33 expirationTime: Date | null;
34 conditionSet: ConditionSet;
35 alarmStatus: RuleAlarmStatus;
36 alarmStatusSetAt: Date;
37 ruleType: RuleType;
38 parentId: string | null;
39 latestVersion: RuleLatestVersionRow;
40};
41
42export function computeRuleStatusFromRow(
43 expirationTime: Date | null,
44 statusIfUnexpired: Exclude<RuleStatus, typeof RuleStatus.EXPIRED>,
45): RuleStatus {
46 if (expirationTime && expirationTime.valueOf() < Date.now()) {
47 return RuleStatus.EXPIRED;
48 }
49 return statusIfUnexpired;
50}
51
52export type RuleGraphqlMethods = {
53 getCreator(): Promise<GraphQLUserParent>;
54 getActions(): Promise<Action[]>;
55 getPolicies(): Promise<Policy[]>;
56};
57
58/** GraphQL parent for Rule / ContentRule / UserRule / RuleInsights. */
59export type Rule = PlainRuleWithLatestVersion & RuleGraphqlMethods;
60
61/** @deprecated Use {@link PlainRuleWithLatestVersion} directly. Remove after Kysely migration. */
62export type RuleWithLatestVersion = PlainRuleWithLatestVersion;