-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathgetAllApplications.js
More file actions
97 lines (85 loc) · 2.64 KB
/
getAllApplications.js
File metadata and controls
97 lines (85 loc) · 2.64 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
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
const Applications = require("../../../models/applications");
const MsAdmin = require("../../../models/msadmins");
const CustomError = require("../../../utils/customError");
const responseHandler = require("../../../utils/responseHandler");
const {
getAllRecords,
getDeletedRecords,
} = require("../../../utils/softDelete");
/**
* @author Ekeyekwu Oscar
*
* Gets all applications using the microservice.
*
* @param {*} req - The request object
* @param {*} res - The response object
* @param {*} next - The function executed to call the next middleware
*/
const getAllApplications = async (req, res, next) => {
//get msAdminId from token
const { msAdminId } = req.token;
//get all applications and map field names appropriately
let allApplications;
let query = {};
const { page, limit } = req.paginateOptions;
try {
//check if msAdmin exists
const msAdmin = await MsAdmin.findById(msAdminId);
if (!msAdmin) {
next(new CustomError(404, "MsAdmin account not found"));
return;
}
const { filter } = req.query;
//get all applications
let applications;
if (filter === "disabled") {
applications = await getDeletedRecords(Applications, page, limit);
} else if (filter === "all") {
applications = await getAllRecords(Applications, page, limit);
} else {
applications = await Applications.paginate(query, {
...req.paginateOptions,
populate: "organizationId",
});
}
allApplications = applications.docs.map((application) => {
return {
applicationId: application._id,
applicationName: application.name,
organizationId: application.organizationId,
organizationName: application.organizationId.name,
isBlocked: application.deleted || false,
};
});
// Set page info.
let pageInfo = {
currentPage: applications.page,
totalPages: applications.totalPages,
hasNext: applications.hasNextPage,
hasPrev: applications.hasPrevPage,
nextPage: applications.nextPage,
prevPage: applications.prevPage,
pageRecordCount: applications.docs.length,
totalRecord: applications.totalDocs,
};
let data = {
records: allApplications,
pageInfo: pageInfo,
};
if (data.pageInfo.currentPage > data.pageInfo.totalPages) {
return next(
new CustomError(
"404",
"Page limit exceeded, No records found!",
data.pageInfo
)
);
} else {
responseHandler(res, 200, data, `Applications Retrieved Successfully`);
}
} catch (error) {
next(error);
return;
}
};
module.exports = getAllApplications;