-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathgetSingleApplicationLog.js
More file actions
65 lines (56 loc) · 1.86 KB
/
getSingleApplicationLog.js
File metadata and controls
65 lines (56 loc) · 1.86 KB
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
const RequestLog = require("../../models/requestLogs");
const Application = require("../../models/applications");
const CustomError = require("../../utils/customError");
const responseHandler = require("../../utils/responseHandler");
const getSingleApplicationLog = async (req, res, next) => {
const { applicationId } = req.params;
const { organizationId } = req.token;
const { page = 1 } = req.query; //default to page 1 if not specified
//reuse this controller for msAdmins, they can find any application
if (req.token.msAdminId) {
const application = await Application.findById(applicationId);
if (!application) {
return next(new CustomError(404, "Application not found"));
}
} else {
//check if requested application belongs to organization of admin user
const application = await Application.find({
_id: applicationId,
organizationId,
});
if (!application) {
return next(new CustomError(404, "Application not found"));
}
}
//get all logs for application - logPageSize will come from system settings
const pageSize = process.env.logPageSize || 100;
const requestLog = await RequestLog.paginate(
{ applicationId },
{
page,
limit: pageSize,
select: "-__v -maxLogRetention",
sort: { createdAt: "desc" },
}
);
const pageInfo = {
currentPage: requestLog.page,
totalPages: requestLog.totalPages,
hasNext: requestLog.hasNextPage,
hasPrev: requestLog.hasPrevPage,
nextPage: requestLog.nextPage,
prevPage: requestLog.prevPage,
pageRecordCount: requestLog.docs.length,
totalRecord: requestLog.totalDocs,
};
const records = requestLog.docs;
const data = { records, pageInfo };
//return log details
return responseHandler(
res,
200,
data,
"Application log retrived successfully"
);
};
module.exports = getSingleApplicationLog;