-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.ts
323 lines (313 loc) · 7.81 KB
/
models.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { DataTypes } from 'sequelize';
import { ModelDefinition, ServiceRequestInstance } from '../../types';
import { setPublicIdHook } from './helpers';
export const REQUEST_STATUSES = {
'inbox': 'Inbox',
'todo': 'Todo',
'doing': 'Doing',
'blocked': 'Blocked',
'done': 'Done',
'invalid': 'Invalid',
'moved': 'Moved',
'junk': 'Junk'
};
export const SERVICE_REQUEST_CLOSED_STATES = ['done', 'invalid', 'moved', 'junk']
export const REQUEST_STATUS_KEYS = Object.keys(REQUEST_STATUSES);
export const INPUT_CHANNELS = {
'webform': 'Web Form',
'bot': 'Bot',
'sms': 'SMS',
'email': 'Email',
'import': 'Import'
}
export const INPUT_CHANNEL_KEYS = Object.keys(INPUT_CHANNELS);
export const COMMUNICATION_CHANNELS = {
'sms': 'SMS',
'email': 'Email',
}
export const COMMUNICATION_CHANNEL_KEYS = Object.keys(COMMUNICATION_CHANNELS);
export const ServiceRequestModel: ModelDefinition = {
name: 'ServiceRequest',
attributes: {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
publicId: {
type: DataTypes.STRING,
allowNull: true,
},
idCounter: {
type: DataTypes.INTEGER,
allowNull: true,
},
inputChannel: {
allowNull: false,
type: DataTypes.ENUM(...INPUT_CHANNEL_KEYS),
defaultValue: INPUT_CHANNEL_KEYS[0],
},
description: {
allowNull: false,
type: DataTypes.TEXT,
},
address: {
allowNull: true,
type: DataTypes.STRING,
},
lat: {
type: DataTypes.FLOAT,
defaultValue: 0,
},
lon: {
type: DataTypes.FLOAT,
defaultValue: 0,
},
images: {
allowNull: true,
type: DataTypes.ARRAY(DataTypes.STRING),
validate: {
memberURLs(value: string[]) {
if (!value) return value;
value.forEach((member: string) => {
try {
if (member) { member.split('.').length > 1 }
// validator.isURL(member);
} catch (error) {
throw new Error(`Valid filenames are required for images: ${error}`);
}
})
return value;
}
}
},
// Very simple initial state management of requests
status: {
allowNull: false,
type: DataTypes.ENUM(...REQUEST_STATUS_KEYS),
defaultValue: REQUEST_STATUS_KEYS[0],
set(value) {
if (SERVICE_REQUEST_CLOSED_STATES.includes(value as string)) {
this.setDataValue('closeDate', this.getDataValue('closeDate') || new Date());
} else {
this.setDataValue('closeDate', null);
}
this.setDataValue('status', value);
}
},
// PublicUser attributes. Even when we do support a PublicUser entity and login/etc.
// We will still probably copy this info over here from that entity.
firstName: {
allowNull: true,
type: DataTypes.STRING,
},
lastName: {
allowNull: true,
type: DataTypes.STRING,
},
device: {
allowNull: true,
type: DataTypes.STRING,
},
email: {
allowNull: true,
type: DataTypes.STRING,
validate: { isEmail: true },
set(value) {
if (value === '') {
value = null;
} else if (typeof value === 'string') {
value = value.toLowerCase();
}
this.setDataValue('email', value);
}
},
phone: {
allowNull: true,
type: DataTypes.STRING,
validate: {
// isPhone: true TODO: write something like this with libphonenumber
},
set(value) {
if (value === '') { value = null; }
this.setDataValue('phone', value);
}
},
assignedTo: {
allowNull: true,
type: DataTypes.STRING,
},
// Fields for Open311 compatibility.
address_id: {
allowNull: true,
type: DataTypes.STRING,
},
channel: {
allowNull: false,
type: DataTypes.ENUM('email', 'sms', 'anon', 'multiple'),
defaultValue: 'email',
set(value) {
if (!value) {
const email = this.getDataValue('email');
const phone = this.getDataValue('phone');
if (email && phone) {
value = 'multiple'
} else if (email) {
value = 'email';
} else if (phone) {
value = 'sms';
} else {
value = 'anon';
}
}
this.setDataValue('channel', value);
}
},
closeDate: {
allowNull: true,
type: DataTypes.DATE,
// set(value) {
// throw new Error(`The 'closeDate' attribute is not allowed to be directly set: ${value}`);
// }
},
displayName: {
type: DataTypes.VIRTUAL,
get() {
return `${this.getDataValue('firstName')} ${this.getDataValue('lastName')}`;
},
set(value) {
throw new Error(`The 'displayName' attribute is not allowed to be directly set: ${value}`);
}
},
},
options: {
freezeTableName: true,
indexes: [
{
unique: true,
fields: ['id', 'jurisdictionId']
},
{
unique: true,
fields: ['publicId', 'jurisdictionId']
},
{
unique: false,
fields: ['status']
},
{
unique: false,
fields: ['createdAt']
},
{
unique: false,
fields: ['updatedAt']
},
{
unique: false,
fields: ['closeDate']
}
],
validate: {
oneOfAddressOrGeometry() {
if ((this.lat || this.lon === null) && (this.address === null) && (this.address_id === null)) {
throw new Error('A Service Request requires one of a lat/lon pair, address, or address_id.');
}
}
},
hooks: {
beforeSave(instance: ServiceRequestInstance) {
return setPublicIdHook(instance);
}
}
}
}
export const ServiceRequestCommentModel: ModelDefinition = {
name: 'ServiceRequestComment',
attributes: {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
comment: {
type: DataTypes.TEXT,
allowNull: true,
},
addedBy: {
allowNull: true,
type: DataTypes.STRING,
},
images: {
allowNull: true,
type: DataTypes.ARRAY(DataTypes.STRING),
validate: {
memberURLs(value: string[]) {
if (!value) return value;
value.forEach((member: string) => {
try {
if (member) { member.split('.').length > 1 }
// validator.isURL(member);
} catch (error) {
throw new Error(`Valid filenames are required for images: ${error}`);
}
})
return value;
}
}
},
broadcastToSubmitter: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
broadcastToAssignee: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
broadcastToStaff: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
isBroadcast: {
type: DataTypes.VIRTUAL,
get() {
return this.getDataValue('broadcastToSubmitter')
|| this.getDataValue('broadcastToAssignee')
|| this.getDataValue('broadcastToStaff');
},
set(value) {
throw new Error(`The 'isBroadcast' attribute is not allowed to be directly set: ${value}`);
}
},
},
options: {
freezeTableName: true,
indexes: [
{
unique: true,
fields: ['id', 'serviceRequestId']
},
{
unique: false,
fields: ['serviceRequestId']
},
{
unique: false,
fields: ['addedBy']
},
{
unique: false,
fields: ['createdAt']
},
{
unique: false,
fields: ['updatedAt']
}
]
}
}