From 411f392e24ce4e2c45576a2b424bc2de914e3d7b Mon Sep 17 00:00:00 2001 From: elangptra Date: Mon, 21 Oct 2024 12:45:41 +0700 Subject: [PATCH] feat: report model function --- .envexample | 1 + controllers/auth/auth.js | 2 +- controllers/usersControllers/report.js | 93 ++++++++++++++++++++++++-- routes/user/report.js | 4 +- 4 files changed, 92 insertions(+), 8 deletions(-) diff --git a/.envexample b/.envexample index 9cd0154..564ee2c 100644 --- a/.envexample +++ b/.envexample @@ -1,5 +1,6 @@ APP_PORT = 3001 NODE_ENV = development +FE_DOMAIN = http://localhost:5173 DB_HOST = localhost DB_USER = root diff --git a/controllers/auth/auth.js b/controllers/auth/auth.js index 0fe480d..016c1fd 100644 --- a/controllers/auth/auth.js +++ b/controllers/auth/auth.js @@ -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, diff --git a/controllers/usersControllers/report.js b/controllers/usersControllers/report.js index 2e2e760..54024a4 100644 --- a/controllers/usersControllers/report.js +++ b/controllers/usersControllers/report.js @@ -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; diff --git a/routes/user/report.js b/routes/user/report.js index 006b67e..5442bf9 100644 --- a/routes/user/report.js +++ b/routes/user/report.js @@ -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);