-
Notifications
You must be signed in to change notification settings - Fork 368
/
activity.controller.ts
84 lines (74 loc) · 3.52 KB
/
activity.controller.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
import type { Request, Response, NextFunction } from 'express';
import { getUserAccountAndEnvironmentFromSession } from '../utils/utils.js';
import { activityFilter, getAllSyncAndActionNames, getTopLevelLogByEnvironment, getLogMessagesForLogs, errorManager } from '@nangohq/shared';
class ActivityController {
public async retrieve(req: Request, res: Response, next: NextFunction) {
try {
const limit = req.query['limit'] ? parseInt(req.query['limit'] as string) : 20;
const offset = req.query['offset'] ? parseInt(req.query['offset'] as string) : 0;
const status = req.query['status']?.toString();
const script = req.query['script']?.toString();
const connection = req.query['connection']?.toString();
const integration = req.query['integration']?.toString();
const date = req.query['date']?.toString();
const { success: sessionSuccess, error: sessionError, response } = await getUserAccountAndEnvironmentFromSession(req);
if (!sessionSuccess || response === null) {
errorManager.errResFromNangoErr(res, sessionError);
return;
}
const { environment } = response;
const logs = await getTopLevelLogByEnvironment(environment.id, limit, offset, { status, script, connection, integration, date });
res.send(logs);
} catch (error) {
next(error);
}
}
public async getMessages(req: Request, res: Response, next: NextFunction) {
try {
const rawLogIds = req.query['logIds'];
if (typeof rawLogIds !== 'string') {
res.status(400).send({ message: 'Missing logsIds parameter' });
return;
}
const logIds = new Set<number>();
// Deduplicate and exclude NaN
for (const logId of rawLogIds.split(',')) {
const parsed = parseInt(logId, 10);
if (parsed) {
logIds.add(parsed);
}
}
if (logIds.size <= 0) {
res.send([]);
return;
}
const { success: sessionSuccess, error: sessionError, response } = await getUserAccountAndEnvironmentFromSession(req);
if (!sessionSuccess || response === null) {
errorManager.errResFromNangoErr(res, sessionError);
return;
}
const { environment } = response;
const logs = await getLogMessagesForLogs(Array.from(logIds.values()), environment.id);
res.send(logs);
} catch (error) {
next(error);
}
}
public async getPossibleFilters(req: Request, res: Response, next: NextFunction) {
try {
const { success: sessionSuccess, error: sessionError, response } = await getUserAccountAndEnvironmentFromSession(req);
if (!sessionSuccess || response === null) {
errorManager.errResFromNangoErr(res, sessionError);
return;
}
const { environment } = response;
const scripts = await getAllSyncAndActionNames(environment.id);
const integrations = await activityFilter(environment.id, 'provider_config_key');
const connections = await activityFilter(environment.id, 'connection_id');
res.send({ scripts, integrations, connections });
} catch (error) {
next(error);
}
}
}
export default new ActivityController();