feat: report model function
This commit is contained in:
parent
3e03485e17
commit
411f392e24
|
|
@ -1,5 +1,6 @@
|
|||
APP_PORT = 3001
|
||||
NODE_ENV = development
|
||||
FE_DOMAIN = http://localhost:5173
|
||||
|
||||
DB_HOST = localhost
|
||||
DB_USER = root
|
||||
|
|
|
|||
|
|
@ -389,7 +389,7 @@ export const forgotPassword = async (req, res) => {
|
|||
}
|
||||
);
|
||||
|
||||
const resetLink = `http://localhost:${process.env.APP_PORT}/resetPassword/${resetToken}`;
|
||||
const resetLink = `${process.env.FE_DOMAIN}/resetPassword/${resetToken}`;
|
||||
|
||||
const mailOptions = {
|
||||
from: process.env.EMAIL_USER,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,81 @@ import response from "../../response.js";
|
|||
import models from "../../models/index.js";
|
||||
|
||||
export const getReports = async (req, res) => {
|
||||
const { page = 1, limit = 10, search = "", sort = "time" } = req.query;
|
||||
|
||||
try {
|
||||
const { count, rows: reports } = await models.Report.findAndCountAll({
|
||||
include: [
|
||||
{
|
||||
model: models.User,
|
||||
as: "reportUser",
|
||||
attributes: ["NAME_USERS", "EMAIL"],
|
||||
},
|
||||
],
|
||||
where: {
|
||||
...(search && {
|
||||
[models.Op.or]: [
|
||||
{
|
||||
"$reportUser.NAME_USERS$": {
|
||||
[models.Op.like]: `%${search}%`,
|
||||
},
|
||||
},
|
||||
{
|
||||
"$reportUser.EMAIL$": {
|
||||
[models.Op.like]: `%${search}%`,
|
||||
},
|
||||
},
|
||||
{
|
||||
REPORTS: {
|
||||
[models.Op.like]: `%${search}%`,
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
distinct: true,
|
||||
order: [
|
||||
sort === "email"
|
||||
? [{ model: models.User, as: "reportUser" }, "EMAIL", "ASC"]
|
||||
: sort === "name"
|
||||
? [{ model: models.User, as: "reportUser" }, "NAME_USERS", "ASC"]
|
||||
: sort === "report"
|
||||
? ["REPORTS", "ASC"]
|
||||
: ["TIME_REPORT", "DESC"],
|
||||
],
|
||||
limit: parseInt(limit),
|
||||
offset: (page - 1) * limit,
|
||||
});
|
||||
|
||||
const modifiedReports = reports.map((report) => {
|
||||
const reportData = report.toJSON();
|
||||
reportData.USER_NAME = reportData.reportUser.NAME_USERS;
|
||||
reportData.USER_EMAIL = reportData.reportUser.EMAIL;
|
||||
delete reportData.reportUser;
|
||||
return reportData;
|
||||
});
|
||||
|
||||
const totalPages = Math.ceil(count / limit);
|
||||
const currentPage = parseInt(page);
|
||||
|
||||
response(
|
||||
200,
|
||||
{
|
||||
reports: modifiedReports,
|
||||
currentPage,
|
||||
totalPages,
|
||||
totalItems: count,
|
||||
},
|
||||
"Reports retrieved successfully",
|
||||
res
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
response(500, null, "Error retrieving reports data!", res);
|
||||
}
|
||||
};
|
||||
|
||||
export const getFiveNewReports = async (req, res) => {
|
||||
try {
|
||||
const reports = await models.Report.findAll({
|
||||
include: [
|
||||
|
|
@ -11,23 +86,27 @@ export const getReports = async (req, res) => {
|
|||
attributes: ["NAME_USERS", "EMAIL"],
|
||||
},
|
||||
],
|
||||
order: [["TIME_REPORT", "DESC"]],
|
||||
limit: 5,
|
||||
});
|
||||
|
||||
const modifiedReports = reports.map((report) => {
|
||||
const reportData = report.toJSON();
|
||||
|
||||
reportData.USER_NAME = reportData.reportUser.NAME_USERS;
|
||||
reportData.USER_EMAIL = reportData.reportUser.EMAIL;
|
||||
|
||||
delete reportData.reportUser;
|
||||
|
||||
return reportData;
|
||||
});
|
||||
|
||||
response(200, modifiedReports, "Success", res);
|
||||
response(
|
||||
200,
|
||||
modifiedReports,
|
||||
"Five latest reports retrieved successfully",
|
||||
res
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
response(500, null, "Error retrieving reports data!", res);
|
||||
console.error(error);
|
||||
response(500, null, "Error retrieving the latest reports", res);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -61,6 +140,8 @@ export const getReportById = async (req, res) => {
|
|||
}
|
||||
};
|
||||
|
||||
export const getReportForAdmin = async (req, res) => {};
|
||||
|
||||
export const getReportByUserId = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
import express from "express";
|
||||
import { getReports, getReportById, getReportByUserId, userReport } from "../../controllers/usersControllers/report.js";
|
||||
import { getReports, getFiveNewReports, getReportById, getReportByUserId, userReport } from "../../controllers/usersControllers/report.js";
|
||||
import { verifyLoginUser, adminOnly } from "../../middlewares/User/authUser.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/report", verifyLoginUser, adminOnly, getReports);
|
||||
|
||||
router.get("/report/new", verifyLoginUser, adminOnly, getFiveNewReports);
|
||||
|
||||
router.get("/report/user/:id", verifyLoginUser, adminOnly, getReportByUserId);
|
||||
|
||||
router.get("/report/:id", verifyLoginUser, adminOnly, getReportById);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user