forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounty-state-machine.ts
More file actions
53 lines (49 loc) · 1.52 KB
/
Copy pathbounty-state-machine.ts
File metadata and controls
53 lines (49 loc) · 1.52 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
import { BountyStatus } from '../common/enums';
/**
* Valid forward transitions for a bounty's lifecycle:
*
* open -> funded -> claimed -> in_review -> merged -> paid
* \-> refunded
* (open|funded|claimed) -> expired
* (open|funded) -> refunded
*/
export const BOUNTY_TRANSITIONS: Record<BountyStatus, BountyStatus[]> = {
[BountyStatus.OPEN]: [
BountyStatus.FUNDED,
BountyStatus.EXPIRED,
BountyStatus.REFUNDED,
],
[BountyStatus.FUNDED]: [
BountyStatus.CLAIMED,
BountyStatus.EXPIRED,
BountyStatus.REFUNDED,
],
[BountyStatus.CLAIMED]: [
BountyStatus.IN_REVIEW,
BountyStatus.EXPIRED,
BountyStatus.REFUNDED,
],
[BountyStatus.IN_REVIEW]: [
BountyStatus.MERGED,
BountyStatus.CLAIMED,
BountyStatus.REFUNDED,
],
[BountyStatus.MERGED]: [BountyStatus.PAID, BountyStatus.REFUNDED],
[BountyStatus.PAID]: [],
[BountyStatus.REFUNDED]: [],
[BountyStatus.EXPIRED]: [BountyStatus.REFUNDED],
};
export class InvalidBountyTransitionError extends Error {
constructor(from: BountyStatus, to: BountyStatus) {
super(`Cannot transition bounty from "${from}" to "${to}"`);
this.name = 'InvalidBountyTransitionError';
}
}
export function canTransition(from: BountyStatus, to: BountyStatus): boolean {
return BOUNTY_TRANSITIONS[from]?.includes(to) ?? false;
}
export function assertTransition(from: BountyStatus, to: BountyStatus): void {
if (!canTransition(from, to)) {
throw new InvalidBountyTransitionError(from, to);
}
}