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("envelope").optional().isFloat(),
|
||||||
body("finalCash").optional().isFloat(),
|
body("finalCash").optional().isFloat(),
|
||||||
body("isVerified").optional().isInt({ min: 0, max: 1 }),
|
body("isVerified").optional().isInt({ min: 0, max: 1 }),
|
||||||
|
body("reportDate").optional().isISO8601(), //validate date
|
||||||
|
body("storeId").optional().isInt(), //validate storeId if changed
|
||||||
],
|
],
|
||||||
(req, res) => {
|
(req, res) => {
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
@ -173,6 +175,7 @@ router.put(
|
|||||||
return res.status(400).json({ errors: errors.array() });
|
return res.status(400).json({ errors: errors.array() });
|
||||||
|
|
||||||
const reportId = req.params.id;
|
const reportId = req.params.id;
|
||||||
|
|
||||||
// Only owner or admin can update
|
// Only owner or admin can update
|
||||||
db.get("SELECT * FROM reports WHERE id = ?", [reportId], (err, report) => {
|
db.get("SELECT * FROM reports WHERE id = ?", [reportId], (err, report) => {
|
||||||
if (err || !report)
|
if (err || !report)
|
||||||
@ -195,37 +198,62 @@ router.put(
|
|||||||
.json({ error: "Запрещено редактировать подтвержденный отчет" });
|
.json({ error: "Запрещено редактировать подтвержденный отчет" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields = [];
|
//check for duplicate report, excluding the current, use new values if provided, else keep old ones from original report
|
||||||
const values = [];
|
const newStoreId =
|
||||||
for (const key of [
|
req.body.storeId !== undefined ? req.body.storeId : report.storeId;
|
||||||
"income",
|
const newReportDate =
|
||||||
"initialCash",
|
req.body.reportDate !== undefined
|
||||||
"totalIncome",
|
? req.body.reportDate
|
||||||
"wages",
|
: report.reportDate;
|
||||||
"expenses",
|
const userId = report.userId; // Always the original author
|
||||||
"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" });
|
|
||||||
|
|
||||||
values.push(reportId);
|
db.get(
|
||||||
db.run(
|
`SELECT * FROM reports WHERE storeId = ? AND reportDate = ? AND userId = ? AND id != ?`,
|
||||||
`UPDATE reports SET ${fields.join(
|
[newStoreId, newReportDate, userId, reportId],
|
||||||
", "
|
(err, duplicate) => {
|
||||||
)}, updatedAt = CURRENT_TIMESTAMP WHERE id = ?`,
|
|
||||||
values,
|
|
||||||
function (err) {
|
|
||||||
if (err) return res.status(500).json({ error: "Database error" });
|
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