-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapproval-rules.ts
More file actions
201 lines (173 loc) · 7.39 KB
/
Copy pathapproval-rules.ts
File metadata and controls
201 lines (173 loc) · 7.39 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {isUUIDv4, PrefixUnion} from "@utils"
import * as E from "fp-ts/Either"
import {Either, left, right} from "fp-ts/lib/Either"
import {pipe} from "fp-ts/lib/function"
import * as A from "fp-ts/Array"
import {ApproveVote, getNormalizedEntityId} from "@domain"
export enum ApprovalRuleType {
AND = "AND",
OR = "OR",
GROUP_REQUIREMENT = "GROUP_REQUIREMENT"
}
export type ApprovalRule = ApprovalRuleData & ApprovalRuleLogic
export type ApprovalRuleData = AndRule | OrRule | GroupRequirementRule
export type GroupRequirementRule = Readonly<PrivateGroupRequirementRule>
interface PrivateGroupRequirementRule {
type: ApprovalRuleType.GROUP_REQUIREMENT
groupId: string
minCount: number
requireHighPrivilege?: boolean
}
export type AndRule = Readonly<PrivateAndRule>
interface PrivateAndRule {
type: ApprovalRuleType.AND
rules: ReadonlyArray<ApprovalRule>
}
export type OrRule = Readonly<PrivateOrRule>
interface PrivateOrRule {
type: ApprovalRuleType.OR
rules: ReadonlyArray<ApprovalRule>
}
interface ApprovalRuleLogic {
/**
* Return the voting group ids that are part of the approval rule. Also nested rules are considered.
* @returns The voting group ids.
*/
getVotingGroupIds(): ReadonlyArray<string>
/**
* Determines if a high privilege token is explicitly required to cast a vote for the given groups.
* @param groupIds The groups the entity wants to cast a vote for.
* @returns true if high privilege is required, false otherwise.
*/
isHighPrivilegeRequired(groupIds: ReadonlyArray<string>): boolean
}
export type ApprovalRuleValidationError = PrefixUnion<"approval_rule", UnprefixedApprovalRuleValidationError>
type UnprefixedApprovalRuleValidationError =
| "malformed_content"
| "invalid_rule_type"
| "and_rule_must_have_rules"
| "or_rule_must_have_rules"
| "group_rule_invalid_min_count"
| "group_rule_invalid_group_id"
| "max_rule_nesting_exceeded"
const MAX_NESTING_DEPTH = 2
export class ApprovalRuleFactory {
/**
* Validates a raw object structure against the ApprovalRules domain model.
* This function handles the discriminated union and recursively validates nested rules.
* @param data The raw object to validate.
* @returns Either a validation error or the valid ApprovalRules object.
*/
static validate(data: unknown): Either<ApprovalRuleValidationError, ApprovalRule> {
const rule = this.validateApprovalRuleData(data, 0)
return E.isRight(rule) ? right(this.decorateApprovalRuleData(rule.right)) : rule
}
private static validateApprovalRuleData(data: unknown, depth = 0): Either<ApprovalRuleValidationError, ApprovalRule> {
if (depth > MAX_NESTING_DEPTH) return left("approval_rule_max_rule_nesting_exceeded")
if (!isObject(data) || typeof data.type !== "string") return left("approval_rule_invalid_rule_type")
switch (data.type) {
case ApprovalRuleType.GROUP_REQUIREMENT:
return pipe(this.validateGroupRequirementRule(data), E.map(this.decorateApprovalRuleData))
case ApprovalRuleType.AND:
return pipe(this.validateAndRule(data, depth), E.map(this.decorateApprovalRuleData))
case ApprovalRuleType.OR:
return pipe(this.validateOrRule(data, depth), E.map(this.decorateApprovalRuleData))
default:
return left("approval_rule_invalid_rule_type")
}
}
private static validateGroupRequirementRule(
data: Record<string, unknown>
): Either<ApprovalRuleValidationError, GroupRequirementRule> {
if (typeof data.groupId !== "string") return left("approval_rule_group_rule_invalid_group_id")
if (!isUUIDv4(data.groupId)) return left("approval_rule_group_rule_invalid_group_id")
if (typeof data.minCount !== "number" || !Number.isInteger(data.minCount))
return left("approval_rule_group_rule_invalid_min_count")
if (data.minCount < 1) return left("approval_rule_group_rule_invalid_min_count")
let requireHighPrivilege: boolean | undefined = undefined
if (data.requireHighPrivilege !== undefined) {
if (typeof data.requireHighPrivilege !== "boolean") return left("approval_rule_malformed_content")
requireHighPrivilege = data.requireHighPrivilege
}
return right({
type: ApprovalRuleType.GROUP_REQUIREMENT,
groupId: data.groupId,
minCount: data.minCount,
...(requireHighPrivilege !== undefined && {requireHighPrivilege})
})
}
private static validateAndRule(
data: Record<string, unknown>,
depth: number
): Either<ApprovalRuleValidationError, AndRule> {
if (!Array.isArray(data.rules) || data.rules.length === 0) return left("approval_rule_and_rule_must_have_rules")
return pipe(
data.rules,
A.traverse(E.Applicative)(rule => ApprovalRuleFactory.validateApprovalRuleData(rule, depth + 1)),
E.map(rules => ({type: ApprovalRuleType.AND, rules}))
)
}
private static validateOrRule(
data: Record<string, unknown>,
depth: number
): Either<ApprovalRuleValidationError, OrRule> {
if (!Array.isArray(data.rules) || data.rules.length === 0) {
return left("approval_rule_or_rule_must_have_rules")
}
return pipe(
data.rules,
A.traverse(E.Applicative)(rule => ApprovalRuleFactory.validateApprovalRuleData(rule, depth + 1)),
E.map(rules => ({type: ApprovalRuleType.OR, rules}))
)
}
private static decorateApprovalRuleData(data: ApprovalRuleData): ApprovalRule {
return {
...data,
getVotingGroupIds: () => getVotingGroupIds(data),
isHighPrivilegeRequired: (groupIds: ReadonlyArray<string>) => isHighPrivilegeRequired(data, groupIds)
}
}
}
function isObject(val: unknown): val is Record<string, unknown> {
return typeof val === "object" && val !== null && !Array.isArray(val)
}
/**
* Checks if the given votes cover the given approval rule.
* @param rule The approval rule to check.
* @param votes The votes to check.
* @returns True if the votes cover the rule, false otherwise.
*/
export function doesVotesCoverApprovalRules(rule: ApprovalRuleData, votes: ReadonlyArray<ApproveVote>): boolean {
switch (rule.type) {
case ApprovalRuleType.GROUP_REQUIREMENT:
return doesVotesCoverGroupRequirementRule(rule, votes)
case ApprovalRuleType.AND:
return rule.rules.every(rule => doesVotesCoverApprovalRules(rule, votes))
case ApprovalRuleType.OR:
return rule.rules.some(rule => doesVotesCoverApprovalRules(rule, votes))
}
}
function doesVotesCoverGroupRequirementRule(rule: GroupRequirementRule, votes: ReadonlyArray<ApproveVote>): boolean {
const votesForGroup = votes.filter(vote => vote.votedForGroups.includes(rule.groupId))
const uniqueVotersWhoVotedForGroup = new Set(votesForGroup.map(vote => getNormalizedEntityId(vote.voter)))
return uniqueVotersWhoVotedForGroup.size >= rule.minCount
}
function getVotingGroupIds(rule: ApprovalRuleData): ReadonlyArray<string> {
switch (rule.type) {
case ApprovalRuleType.GROUP_REQUIREMENT:
return [rule.groupId]
case ApprovalRuleType.AND:
return rule.rules.flatMap(getVotingGroupIds)
case ApprovalRuleType.OR:
return rule.rules.flatMap(getVotingGroupIds)
}
}
function isHighPrivilegeRequired(rule: ApprovalRuleData, groupIds: ReadonlyArray<string>): boolean {
switch (rule.type) {
case ApprovalRuleType.GROUP_REQUIREMENT:
return groupIds.includes(rule.groupId) && rule.requireHighPrivilege === true
case ApprovalRuleType.AND:
case ApprovalRuleType.OR:
return rule.rules.some(r => isHighPrivilegeRequired(r, groupIds))
}
}