chore: add otp based login
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
const { validationResult, query } = require("express-validator");
|
||||
const Logger = require("../models/Logger");
|
||||
const { paginationOptions } = require("../plugins/pagination");
|
||||
|
||||
// get admin login logs with optional filtering and pagination
|
||||
module.exports.getAdminLoginLogs = [
|
||||
[
|
||||
query("page")
|
||||
.optional()
|
||||
.isInt({ min: 1 })
|
||||
.withMessage("Page must be a positive integer."),
|
||||
query("limit")
|
||||
.optional()
|
||||
.isInt({ min: 1 })
|
||||
.withMessage("Limit must be a positive integer."),
|
||||
query("sort")
|
||||
.optional()
|
||||
.isIn(["asc", "desc"])
|
||||
.withMessage('Sort must be either "asc" or "desc".'),
|
||||
query("fromDate")
|
||||
.optional()
|
||||
.isISO8601()
|
||||
.withMessage("From date must be a valid date."),
|
||||
query("toDate")
|
||||
.optional()
|
||||
.isISO8601()
|
||||
.withMessage("To date must be a valid date."),
|
||||
],
|
||||
async (req, res) => {
|
||||
const errors = validationResult(req);
|
||||
if (!errors.isEmpty())
|
||||
return res.status(422).json({ validation: errors.mapped() });
|
||||
|
||||
try {
|
||||
const {
|
||||
page = 1,
|
||||
limit = 1000,
|
||||
sort = "desc",
|
||||
fromDate,
|
||||
toDate,
|
||||
} = req?.query;
|
||||
|
||||
const filters = {};
|
||||
if (fromDate) filters.LastLogin = { $gte: new Date(fromDate) };
|
||||
if (toDate)
|
||||
filters.LastLogin = { ...filters.LastLogin, $lte: new Date(toDate) };
|
||||
|
||||
const options = paginationOptions({ page, limit, sort });
|
||||
const logs = await Logger.find(filters)
|
||||
.populate({ path: "user", select: { password: 0 } })
|
||||
.sort({ updated_at: sort === "desc" ? -1 : 1 })
|
||||
.skip(options.skip)
|
||||
.limit(options.limit);
|
||||
|
||||
const totalLogs = await Logger.countDocuments(filters);
|
||||
const totalPages = Math.ceil(totalLogs / options.limit);
|
||||
|
||||
return res.status(200).json({
|
||||
logs: logs.map((log) => {
|
||||
log.LastLogin = new Date(log.LastLogin).toLocaleString("fa-IR", {
|
||||
timeZone: "Asia/Tehran",
|
||||
// hour: "2-digit",
|
||||
// minute: "2-digit",
|
||||
// day: "2-digit",
|
||||
// month: "2-digit",
|
||||
// weekday: "short",
|
||||
// year: "2-digit",
|
||||
// timeZoneName: "long",
|
||||
});
|
||||
return log;
|
||||
}),
|
||||
page: options.page,
|
||||
totalPages,
|
||||
totalLogs,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching admin login logs:", error);
|
||||
return res.status(500).json({ message: "Server error occurred." });
|
||||
}
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user