From c644419ed8c34804be179b82dea8d27eafda054e Mon Sep 17 00:00:00 2001 From: Angie Date: Tue, 5 Aug 2025 14:07:57 +0200 Subject: [PATCH] feat: update report, check for dublicated, if the user want to change date and shop of the today's report --- backend/routes/reports.js | 86 ++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/backend/routes/reports.js b/backend/routes/reports.js index d0345a7..38db5fe 100644 --- a/backend/routes/reports.js +++ b/backend/routes/reports.js @@ -166,6 +166,8 @@ router.put( body("envelope").optional().isFloat(), body("finalCash").optional().isFloat(), body("isVerified").optional().isInt({ min: 0, max: 1 }), + body("reportDate").optional().isISO8601(), //validate date + body("storeId").optional().isInt(), //validate storeId if changed ], (req, res) => { const errors = validationResult(req); @@ -173,6 +175,7 @@ router.put( return res.status(400).json({ errors: errors.array() }); const reportId = req.params.id; + // Only owner or admin can update db.get("SELECT * FROM reports WHERE id = ?", [reportId], (err, report) => { if (err || !report) @@ -195,37 +198,62 @@ router.put( .json({ error: "Запрещено редактировать подтвержденный отчет" }); } - const fields = []; - const values = []; - for (const key of [ - "income", - "initialCash", - "totalIncome", - "wages", - "expenses", - "totalWages", - "totalExpenses", - "envelope", - "finalCash", - "isVerified", - ]) { - if (req.body[key] !== undefined) { - fields.push(`${key} = ?`); - values.push(req.body[key]); - } - } - if (fields.length === 0) - return res.status(400).json({ error: "No data to update" }); + //check for duplicate report, excluding the current, use new values if provided, else keep old ones from original report + const newStoreId = + req.body.storeId !== undefined ? req.body.storeId : report.storeId; + const newReportDate = + req.body.reportDate !== undefined + ? req.body.reportDate + : report.reportDate; + const userId = report.userId; // Always the original author - values.push(reportId); - db.run( - `UPDATE reports SET ${fields.join( - ", " - )}, updatedAt = CURRENT_TIMESTAMP WHERE id = ?`, - values, - function (err) { + db.get( + `SELECT * FROM reports WHERE storeId = ? AND reportDate = ? AND userId = ? AND id != ?`, + [newStoreId, newReportDate, userId, reportId], + (err, duplicate) => { if (err) return res.status(500).json({ error: "Database error" }); - res.json({ updated: this.changes }); + if (duplicate) { + return res.status(409).json({ + error: + "Отчет за этот магазин и дату уже был отправлен этим пользователем.", + }); + } + + const fields = []; + const values = []; + for (const key of [ + "income", + "initialCash", + "totalIncome", + "wages", + "expenses", + "totalWages", + "totalExpenses", + "envelope", + "finalCash", + "isVerified", + "reportDate", + "storeId", + ]) { + if (req.body[key] !== undefined) { + fields.push(`${key} = ?`); + values.push(req.body[key]); + } + } + if (fields.length === 0) + return res.status(400).json({ error: "No data to update" }); + + values.push(reportId); + db.run( + `UPDATE reports SET ${fields.join( + ", " + )}, updatedAt = CURRENT_TIMESTAMP WHERE id = ?`, + values, + function (err) { + if (err) return res.status(500).json({ error: "Database error" }); + res.json({ updated: this.changes }); + } + ); } ); });