-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathgetAllMsAdmins.js
More file actions
77 lines (69 loc) · 2.04 KB
/
getAllMsAdmins.js
File metadata and controls
77 lines (69 loc) · 2.04 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
const MsAdmin = require("../../models/msadmins");
const CustomError = require("../../utils/customError");
const responseHandler = require("../../utils/responseHandler");
const { getAllRecords, getDeletedRecords } = require("../../utils/softDelete");
/**
* @author David Okanlawon
*
* Gets all microservice admins.
*
* @param {*} req - The request object
* @param {*} res - The response object
* @param {*} next - The function executed to call the next middleware
*/
const getAllMsAdmins = async (req, res, next) => {
//get all admins mapping the field names appropriately
let admins, allMsAdmins;
const { filter } = req.query;
let query = {};
const { page, limit } = req.paginateOptions;
try {
if (filter === "disabled") {
admins = await getDeletedRecords(MsAdmin, page, limit);
} else if (filter === "all") {
admins = await getAllRecords(MsAdmin, page, limit);
} else {
admins = await MsAdmin.paginate(query, req.paginateOptions);
}
allMsAdmins = admins.docs.map((admin) => {
return {
msAdminId: admin._id,
fullname: admin.fullname,
email: admin.email,
role: admin.role,
isDisabled: admin.deleted || false,
};
});
} catch (error) {
console.log(error.message);
next(new CustomError(400, "An error occured retrieving admin accounts"));
return;
}
// Set page info.
let pageInfo = {
currentPage: admins.page,
totalPages: admins.totalPages,
hasNext: admins.hasNextPage,
hasPrev: admins.hasPrevPage,
nextPage: admins.nextPage,
prevPage: admins.prevPage,
pageRecordCount: admins.docs.length,
totalRecord: admins.totalDocs,
};
let data = {
records: allMsAdmins,
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, `MsAdmins Retrieved Successfully`);
}
};
module.exports = getAllMsAdmins;