-
Notifications
You must be signed in to change notification settings - Fork 0
/
reportingWindowAssignment.ts
137 lines (123 loc) · 3.6 KB
/
reportingWindowAssignment.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
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
import * as t from 'io-ts';
import type { Brand } from '../../util/types';
import { defineVersionedModel } from '../util/versioned-model';
import { brandedType } from '../../util/io-ts';
import { FILE_REFERENCE } from '../util/datatypes';
import { FORM_ID } from './form';
import { OPERATION_ID, type OperationId } from './operation';
import operationCluster, { OPERATION_CLUSTER_ID } from './operationCluster';
import { REPORTING_WINDOW_ID } from './reportingWindow';
export type ReportingWindowAssignmentId = Brand<
number,
{ readonly s: unique symbol },
'reportingWindowAssignment.id'
>;
export const REPORTING_WINDOW_ASSIGNMENT_ID = brandedType<
number,
ReportingWindowAssignmentId
>(t.number);
export const REPORTING_WINDOW_ASSIGNMENT_FILE = t.type({
name: t.string,
data: FILE_REFERENCE,
});
export const REPORTING_WINDOW_ASSIGNMENT_FILES = t.array(
REPORTING_WINDOW_ASSIGNMENT_FILE
);
export const REPORTING_WINDOW_ASSIGNMENT_STATE = t.union([
t.null,
t.type({
type: t.keyof({
raw: null,
clean: null,
}),
finalized: t.boolean,
data: t.string,
files: t.union([REPORTING_WINDOW_ASSIGNMENT_FILES, t.undefined]),
}),
]);
export type ReportingWindowAssignmentState = t.TypeOf<
typeof REPORTING_WINDOW_ASSIGNMENT_STATE
>;
export const REPORTING_WINDOW_ASSIGNEE_TYPE = t.union([
t.type({
type: t.literal('operation'),
operation: OPERATION_ID,
}),
t.type({
type: t.literal('operationCluster'),
cluster: OPERATION_CLUSTER_ID,
}),
]);
export type ReportingWindowAssigneeType = t.TypeOf<
typeof REPORTING_WINDOW_ASSIGNEE_TYPE
>;
export const REPORTING_WINDOW_ASSIGNMENT_DATA = t.type({
reportingWindowId: REPORTING_WINDOW_ID,
assignee: REPORTING_WINDOW_ASSIGNEE_TYPE,
task: t.type({
type: t.literal('form'),
form: FORM_ID,
formName: t.string,
formVersion: t.number,
state: REPORTING_WINDOW_ASSIGNMENT_STATE,
}),
});
export type ReportingWindowAssignmentData = t.TypeOf<
typeof REPORTING_WINDOW_ASSIGNMENT_DATA
>;
export default defineVersionedModel({
tableBaseName: 'reportingWindowAssignment',
idType: REPORTING_WINDOW_ASSIGNMENT_ID,
data: REPORTING_WINDOW_ASSIGNMENT_DATA,
lookupColumns: {
columns: {
required: {
reportingWindowId: {
kind: 'branded-integer',
brand: REPORTING_WINDOW_ID,
},
assigneeOperation: {
kind: 'branded-integer',
brand: OPERATION_ID,
},
assigneeType: {
kind: 'enum',
values: {
operation: null,
operationCluster: null,
},
},
assigneeId: {
kind: 'checked',
type: t.number,
},
},
},
prepare: async (data, masterConn, replicaConn) => {
let assigneeOperation: OperationId;
let assigneeId: number;
if (data.assignee.type === 'operation') {
assigneeOperation = data.assignee.operation;
assigneeId = data.assignee.operation;
} else if (data.assignee.type === 'operationCluster') {
const oc = operationCluster(masterConn, replicaConn);
const cluster = await oc.get(data.assignee.cluster);
if (!cluster) {
throw new Error(
`Could not find cluster with ID ${data.assignee.cluster}`
);
}
assigneeOperation = cluster.data.operation;
assigneeId = data.assignee.cluster;
} else {
throw new Error('Unknown assignee type');
}
return {
reportingWindowId: data.reportingWindowId,
assigneeOperation,
assigneeType: data.assignee.type,
assigneeId,
};
},
},
});