From 0bde898342b1be1dc1aea91ab938515a70aa2ebd Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Fri, 31 Oct 2025 16:10:34 +0530 Subject: [PATCH] GWM : cost sum added and filter added in view submission --- app/controllers/dashboard.controller.js | 9 ++--- app/controllers/submission.controller.js | 48 +++++++++++++++++------- app/routes/routes.js | 32 +++++++++++++--- 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/app/controllers/dashboard.controller.js b/app/controllers/dashboard.controller.js index d84262a..d451fce 100644 --- a/app/controllers/dashboard.controller.js +++ b/app/controllers/dashboard.controller.js @@ -152,14 +152,11 @@ exports.getEstablishmentDashboard = async (req, res) => { } }; - - - exports.adminDashboard = async (req, res) => { try { const { quarter, year } = req.query; // e.g., Q1, 2025 - // --- 1️⃣ Summary Counts --- + // --- Summary Counts --- const [ totalEstablishments, submittedCount, @@ -184,7 +181,7 @@ exports.adminDashboard = async (req, res) => { where: { id: { [Op.notIn]: startedIds } }, }); - // --- 2️⃣ Recent 10 submissions for given quarter & year --- + // --- Recent 10 submissions for given quarter & year --- const whereCond = {}; if (quarter) whereCond.quarter = quarter; if (year) whereCond.year = year; @@ -222,7 +219,7 @@ exports.adminDashboard = async (req, res) => { limit: 10, }); - // --- 3️⃣ Send Response --- + // --- Send Response --- return res.status(200).json({ status: "success", summary: { diff --git a/app/controllers/submission.controller.js b/app/controllers/submission.controller.js index 200917b..dc5603a 100644 --- a/app/controllers/submission.controller.js +++ b/app/controllers/submission.controller.js @@ -92,8 +92,6 @@ exports.createSubmission = async (req, res) => { res.status(500).json({ status: "failed", message: err.message }); } }; - - exports.updateSubmission = async (req, res) => { try { @@ -188,16 +186,28 @@ exports.updateSubmission = async (req, res) => { } }; - exports.submissionHistory = async (req, res) => { try { const { establishment_id } = req.params; const data = await Submission.findAll({ + attributes: { + include: [ + [ + Sequelize.literal(`(SELECT COUNT(*) FROM submission_products AS sp1 WHERE sp1.submission_id = submission.id)`), + "product_count" + ], + [ + Sequelize.literal(`(SELECT SUM(sp2.current_cost) FROM submission_products AS sp2 WHERE sp2.submission_id = submission.id)`), + "total_cost" + ] + ] + }, where: { establishment_id }, order: [["id", "DESC"]], }); + res.status(200).json({ status: "success", data }); @@ -206,7 +216,6 @@ exports.submissionHistory = async (req, res) => { } }; - // exports.submissionList = async (req, res) => { // try { @@ -244,7 +253,6 @@ exports.submissionHistory = async (req, res) => { // } // }; - exports.submissionList = async (req, res) => { try { const { @@ -352,8 +360,26 @@ exports.viewSubmissionDetails = async (req, res) => { try { const { id } = req.params; + const { establishment_id, year, quarter } = req.query; // optional search params - const data = await Submission.findByPk(id, { + let whereCondition = {}; + + // Primary search by ID (default) + if (id) { + whereCondition.id = id; + } + + // Secondary combination search if establishment_id, year, quarter given + if (establishment_id && year && quarter) { + whereCondition = { + establishment_id, + year, + quarter, + }; + } + + const data = await Submission.findOne( { + where: whereCondition, include: [ { model: Establishment, @@ -388,11 +414,10 @@ exports.viewSubmissionDetails = async (req, res) => { ], }); - const quarter_periods = getQuarterPeriods(Number(data.year), data.quarter); - - if (!data) return res.status(404).json({ status: "failed", message: "Not found" }); + const quarter_periods = getQuarterPeriods(Number(data.year), data.quarter); + res.status(200).json({ status: "success", data , quarter_periods }); } catch (err) { @@ -400,8 +425,6 @@ exports.viewSubmissionDetails = async (req, res) => { } }; - - exports.submissionEditRequest = async (req, res) => { try { @@ -454,7 +477,6 @@ exports.enableOrDisableSubmissionEditAccess = async (req, res) => { } }; - exports.approveOrRejectSubmission = async (req, res) => { try { @@ -487,8 +509,6 @@ exports.approveOrRejectSubmission = async (req, res) => { } }; - - exports.getQuarterPeriods = async (req, res) => { try { const { current_year, current_quarter } = req.body; diff --git a/app/routes/routes.js b/app/routes/routes.js index c441da3..f53cf7c 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -1811,9 +1811,9 @@ router.get("/submissions",[verifySignature, verifyToken], submissionController.s /** * @swagger - * /api/submissions/{id}: + * /api/submissions/view/{id}: * get: - * summary: Get submission with product details + * summary: Get submission details by ID or by (establishment_id, year, quarter) * tags: [Submissions] * security: * - appSignature: [] @@ -1821,12 +1821,34 @@ router.get("/submissions",[verifySignature, verifyToken], submissionController.s * parameters: * - name: id * in: path - * required: true + * required: false + * description: Submission ID (optional if using query combination) + * schema: + * type: integer + * - name: establishment_id + * in: query + * required: false + * schema: + * type: integer + * - name: year + * in: query + * required: false + * schema: + * type: integer + * - name: quarter + * in: query + * required: false + * schema: + * type: string * responses: * 200: - * description: Submission details + * description: Submission details retrieved successfully + * 404: + * description: Submission not found + * 500: + * description: Server error */ -router.get("/submissions/:id",[verifySignature, verifyToken], submissionController.viewSubmissionDetails); +router.get("/submissions/view/:id",[verifySignature, verifyToken], submissionController.viewSubmissionDetails);