-
Notifications
You must be signed in to change notification settings - Fork 5
/
calc_update.js
103 lines (83 loc) · 3.29 KB
/
calc_update.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
const { StatusCodes } = require('http-status-codes');
const {
db,
USER_CALCULATIONS_TABLE,
USERS_TABLE,
selectUserCalculations,
selectUserDataSources,
selectUserCollections,
selectFirstUser,
} = require('../../../services/db');
const { is_valid_uuid } = require('./_helpers');
const { getAndPrepareCalcResults, deleteAndClearCalculation } = require('../calculations/_helpers');
const { getAndPrepareDataSources } = require('../datasources/_helpers');
const { getAndPrepareCollections } = require('../collections/_helpers');
module.exports = { post };
let mapQueryFromDSforLimits = new Map(); // This is the hack TODO FIXME
/**
* @api {post} /v0/webhooks/calc_update Update a status of a calculation
* @apiName UpdateCalculation
* @apiGroup Calculations
* @apiPermission unprotected
* @apiSuccess (202) reqId response sent to a separate server-side event stream.
* @apiUse SSEStreamResponse
*/
async function post(req, res, next) {
const { uuid, progress, result } = req.body;
if (
req.user &&
req.session.passport.user &&
req.user.id === req.session.passport.user &&
req.query.limit
) {
mapQueryFromDSforLimits.set(req.user.id, req.query);
return next({ status: StatusCodes.ACCEPTED });
}
if (!uuid || !is_valid_uuid(uuid) || !progress) {
return next({ status: StatusCodes.BAD_REQUEST });
}
res.status(StatusCodes.ACCEPTED).json({ reqId: req.id });
const record = await db(USER_CALCULATIONS_TABLE).where({ uuid }).first('id', 'userId');
const { userId, id } = record || {};
if (!userId) {
return next({
status: StatusCodes.UNPROCESSABLE_ENTITY,
error: 'Calculation is not available for provided UUID',
});
}
const calculations = await selectUserCalculations({ id: userId });
const output = await getAndPrepareCalcResults(userId, calculations, progress, result);
if (output.error) {
return next({ status: StatusCodes.UNPROCESSABLE_ENTITY, error: output.error });
}
res.sse.send(
({ session }) => {
return userId && session.passport && userId === session.passport.user;
},
{ reqId: req.id, ...output },
'calculations'
);
if (output.data.some(({ progress }) => progress === 100)) {
const user = await selectFirstUser({ [`${USERS_TABLE}.id`]: userId });
const query = mapQueryFromDSforLimits.get(userId);
const filters = await selectUserCollections(user);
const preparedFilters = await getAndPrepareCollections(filters);
res.sse.send(
({ session }) => {
return userId && session.passport && userId === session.passport.user;
},
{ reqId: req.id, ...preparedFilters },
'filters'
);
const datasources = await selectUserDataSources(user, query);
const preparedDataSouces = await getAndPrepareDataSources(datasources);
res.sse.send(
({ session }) => {
return userId && session.passport && userId === session.passport.user;
},
{ reqId: req.id, ...preparedDataSouces },
'datasources'
);
}
if (progress === 100) setTimeout(async () => await deleteAndClearCalculation(userId, id), 3000);
}