-
Notifications
You must be signed in to change notification settings - Fork 1
/
wss.js
284 lines (276 loc) · 9.47 KB
/
wss.js
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
const i18n = require('i18n');
const url = require('url');
const WebSocket = require('ws');
const models = require('./models');
const incidentsServer = new WebSocket.Server({ noServer: true });
incidentsServer.on('connection', async (ws, req) => {
// eslint-disable-next-line no-param-reassign
ws.info = { userId: req.user.id, agencyId: req.agency.id, assignmentId: req.assignment.id, vehicleId: req.assignment.vehicleId };
// query for any active MCIs- for now, incidents from the same region
let agencyIds = [];
if (req.agency.regionId) {
const region = await req.agency.getRegion();
const agencies = await region.getAgencies({ include: [{ model: models.Agency, as: 'claimedAgency', required: false }] });
agencyIds = agencies.map((a) => a.claimedAgency?.id ?? a.id);
}
if (!agencyIds.includes(req.agency.id)) {
agencyIds.push(req.agency.id);
}
const incidents = await models.Incident.findAll({
include: [
{
model: models.Scene,
as: 'scene',
where: {
isMCI: true,
closedAt: null,
[models.Sequelize.Op.or]: [
{
createdByAgencyId: agencyIds,
},
{
updatedByAgencyId: agencyIds,
},
],
},
},
{
model: models.Dispatch,
as: 'dispatches',
required: false,
},
],
});
const payload = await models.Incident.createPayload(incidents);
const data = JSON.stringify(payload);
ws.send(data);
});
async function dispatchIncidentUpdate(incidentId) {
const incident = await models.Incident.findByPk(incidentId, {
include: [
{ model: models.Scene, as: 'scene' },
{
model: models.Dispatch,
as: 'dispatches',
required: false,
},
],
});
if (!incident) {
return;
}
let agencyIds = [];
if (incident.scene.isMCI) {
const agency = (await incident.scene.getCreatedByAgency()) ?? (await incident.scene.getUpdatedByAgency());
if (agency.regionId) {
const region = await agency.getRegion();
const agencies = await region.getAgencies({ include: [{ model: models.Agency, as: 'claimedAgency', required: false }] });
agencyIds = agencies.map((a) => a.claimedAgency?.id ?? a.id);
}
if (!agencyIds.includes(agency.id)) {
agencyIds.push(agency.id);
}
}
const payload = await models.Incident.createPayload([incident]);
const data = JSON.stringify(payload);
for (const ws of incidentsServer.clients) {
if (incident.scene.isMCI && agencyIds.includes(ws.info.agencyId)) {
ws.send(data);
} else if (incident.dispatches.find((d) => d.vehicleId === ws.info.vehicleId)) {
// otherwise send to the dispatched vehicles
ws.send(data);
}
}
}
const sceneServer = new WebSocket.Server({ noServer: true });
sceneServer.on('connection', async (ws, req) => {
// eslint-disable-next-line no-param-reassign
ws.info = { userId: req.user.id, sceneId: req.scene.id };
let payload;
await models.sequelize.transaction(async (transaction) => {
const scene = await models.Scene.findByPk(req.scene.id, {
include: ['current', 'city', 'incident', 'state'],
transaction,
});
const responders = await scene.getResponders({
include: ['user', 'agency', 'vehicle'],
transaction,
});
const reports = await scene.incident.getReports({
include: ['patient', 'disposition'],
transaction,
order: [['id', 'ASC']],
});
payload = await models.Report.createPayload(reports, { transaction });
// during MCI, rewrite all Reports to refer to latest Scene
for (const report of payload.Report) {
report.sceneId = scene.currentId;
}
payload.Agency = responders.map((r) => r.agency?.toJSON()).filter(Boolean);
payload.City = scene.city?.toJSON();
payload.Incident = scene.incident.toJSON();
payload.Responder = responders.map((r) => r.toJSON());
payload.Scene = scene.toJSON();
payload.State = scene.state?.toJSON();
payload.User = responders.map((r) => r.user?.toJSON()).filter(Boolean);
payload.Vehicle = responders
.map((r) => r.vehicle)
.filter((v) => v)
.map((v) => v.toJSON());
});
const data = JSON.stringify(payload);
ws.send(data);
});
async function dispatchSceneUpdate(sceneId) {
let payload = {};
await models.sequelize.transaction(async (transaction) => {
const scene = await models.Scene.findByPk(sceneId, { transaction });
const responders = await scene.getResponders({
include: ['user', 'agency', 'vehicle'],
transaction,
});
payload.Agency = responders.map((r) => r.agency?.toJSON()).filter(Boolean);
payload.Responder = responders.map((r) => r.toJSON());
payload.Scene = scene.toJSON();
payload.User = responders.map((r) => r.user?.toJSON()).filter(Boolean);
payload.Vehicle = responders
.map((r) => r.vehicle)
.filter((v) => v)
.map((v) => v.toJSON());
});
payload = JSON.stringify(payload);
// dispatch to all clients watching this specific scene
for (const ws of sceneServer.clients) {
if (ws.info.sceneId === sceneId) {
ws.send(payload);
}
}
// dispatch to all clients watching dispatched incidents
const incidents = await models.Incident.scope({ method: ['scene', sceneId] }).findAll();
await Promise.all(incidents.map((i) => dispatchIncidentUpdate(i.id)));
}
async function dispatchReportUpdate(reportId) {
let report;
let incident;
let scene;
let payload;
await models.sequelize.transaction(async (transaction) => {
report = await models.Report.findByPk(reportId, {
include: [
'disposition',
{ model: models.Incident, as: 'incident', include: [{ model: models.Dispatch, as: 'dispatches', required: false }] },
'patient',
'scene',
],
transaction,
});
incident = report.incident;
scene = await report.scene.getCanonical({ transaction });
payload = await models.Report.createPayload([report], { transaction });
// during MCI, rewrite all Reports to refer to latest Scene
for (const rep of payload.Report) {
rep.sceneId = scene.currentId;
}
payload = JSON.stringify(payload);
});
// send updates to all clients watching the same scene
for (const ws of sceneServer.clients) {
if (ws.info.sceneId === scene.id) {
ws.send(payload);
}
}
// send updates to all clients that were dispatched to the same incident
for (const ws of incidentsServer.clients) {
if (incident.dispatches.find((d) => d.vehicleId === ws.info.vehicleId)) {
ws.send(payload);
}
}
}
function configure(server, app) {
server.on('upgrade', (req, socket, head) => {
i18n.init(req);
app.sessionParser(req, {}, async () => {
const params = new URLSearchParams(url.parse(req.url).query);
/// ensure agency specified
const subdomains = req.headers.host.split('.');
let agency;
if (req.headers['X-Agency-Subdomain'] || req.headers['x-agency-subdomain']) {
agency = (req.headers['X-Agency-Subdomain'] || req.headers['x-agency-subdomain']).trim();
} else if (subdomains.length > parseInt(process.env.EXPRESS_SUBDOMAIN_OFFSET, 10)) {
agency = subdomains[0].trim();
}
if (agency) {
req.agency = await models.Agency.findOne({
where: { subdomain: agency, isDraft: false },
});
}
if (!req.agency) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
/// ensure user logged in
if (req.session?.passport?.user) {
req.user = await models.User.findByPk(req.session.passport.user);
}
if (!req.user) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
/// ensure user is actively employed by agency (or is superuser admin)
if (!req.user.isAdmin) {
const employment = await models.Employment.scope('finalOrNew').findOne({
where: { userId: req.user.id, createdByAgencyId: req.agency.id },
});
if (!employment?.isActive) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
}
/// connect based on pathname
const { pathname } = url.parse(req.url);
switch (pathname) {
case '/incidents':
if (params.get('assignmentId')) {
req.assignment = await models.Assignment.findByPk(params.get('assignmentId'));
}
if (!req.assignment) {
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
socket.destroy();
return;
}
incidentsServer.handleUpgrade(req, socket, head, (ws) => {
incidentsServer.emit('connection', ws, req);
});
break;
case '/scene':
/// ensure active scene
if (params.get('id')) {
req.scene = await models.Scene.findByPk(params.get('id'), {
include: ['city', 'incident', 'state'],
});
}
if (!req.scene) {
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
socket.destroy();
return;
}
sceneServer.handleUpgrade(req, socket, head, (ws) => {
sceneServer.emit('connection', ws, req);
});
break;
default:
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
socket.destroy();
}
});
});
}
module.exports = {
configure,
dispatchIncidentUpdate,
dispatchReportUpdate,
dispatchSceneUpdate,
};