feat: update report, check for dublicated, if the user want to change date and shop of the today's report
This commit is contained in:
parent
05c04bf68d
commit
c644419ed8
@ -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 });
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user