This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
/
GroupRule.ts
94 lines (79 loc) 路 2.01 KB
/
GroupRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {
Table,
Column,
AllowNull,
ForeignKey,
BelongsTo,
BeforeSave,
DataType,
} from "sequelize-typescript";
import { Group } from "./Group";
import { Property } from "./Property";
import { APIData } from "../modules/apiData";
import { CommonModel } from "../classes/commonModel";
import {
GroupRuleOpType,
RelativeMatchDirectionType,
RelativeMatchUnitType,
} from "../modules/ruleOpsDictionary";
@Table({ tableName: "groupRules", paranoid: false })
export class GroupRule extends CommonModel<GroupRule> {
idPrefix() {
return "grr";
}
@AllowNull(false)
@ForeignKey(() => Group)
@Column
groupId: string;
@ForeignKey(() => Property)
@Column
propertyId: string;
@Column
recordColumn: string;
@AllowNull(false)
@Column
position: number;
@Column
match: string;
@AllowNull(false)
@Column(DataType.STRING)
op: GroupRuleOpType;
@Column
relativeMatchNumber: number;
@Column(DataType.STRING)
relativeMatchUnit: RelativeMatchUnitType;
@Column(DataType.STRING)
relativeMatchDirection: RelativeMatchDirectionType;
@BelongsTo(() => Group)
schedule: Group;
@BelongsTo(() => Property)
property: Property;
async apiData() {
return {
id: this.id,
groupId: this.groupId,
propertyId: this.propertyId,
recordColumn: this.recordColumn,
position: this.position,
match: this.match,
op: this.op,
relativeMatchNumber: this.relativeMatchNumber,
relativeMatchUnit: this.relativeMatchUnit,
relativeMatchDirection: this.relativeMatchDirection,
createdAt: APIData.formatDate(this.createdAt),
updatedAt: APIData.formatDate(this.updatedAt),
};
}
// --- Class Methods --- //
@BeforeSave
static async ensureEitherPropertyOrRecordColumn(instance: GroupRule) {
if (
(!instance.propertyId && !instance.recordColumn) ||
(instance.propertyId && instance.recordColumn)
) {
throw new Error(
"either propertyId or recordColumn is required for a GroupRule"
);
}
}
}