feat: podpora ručního generování QR kódů pro platby
ci/woodpecker/push/workflow Pipeline was successful
ci/woodpecker/push/workflow Pipeline was successful
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import express, { Request } from "express";
|
||||
import { getLogin } from "../auth";
|
||||
import { parseToken, formatDate } from "../utils";
|
||||
import { generateQr } from "../qr";
|
||||
import { addPendingQr } from "../pizza";
|
||||
import { GenerateQrData } from "../../../types";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* Vygeneruje QR kódy pro platbu vybraným uživatelům.
|
||||
*/
|
||||
router.post("/generate", async (req: Request<{}, any, GenerateQrData["body"]>, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
try {
|
||||
const { recipients, bankAccount, bankAccountHolder } = req.body;
|
||||
|
||||
if (!recipients || !Array.isArray(recipients) || recipients.length === 0) {
|
||||
return res.status(400).json({ error: "Nebyl předán seznam příjemců" });
|
||||
}
|
||||
if (!bankAccount) {
|
||||
return res.status(400).json({ error: "Nebylo předáno číslo účtu" });
|
||||
}
|
||||
if (!bankAccountHolder) {
|
||||
return res.status(400).json({ error: "Nebylo předáno jméno držitele účtu" });
|
||||
}
|
||||
|
||||
const today = formatDate(new Date());
|
||||
|
||||
for (const recipient of recipients) {
|
||||
if (!recipient.login) {
|
||||
return res.status(400).json({ error: "Příjemce nemá vyplněný login" });
|
||||
}
|
||||
if (!recipient.purpose || recipient.purpose.trim().length === 0) {
|
||||
return res.status(400).json({ error: `Příjemce ${recipient.login} nemá vyplněný účel platby` });
|
||||
}
|
||||
if (typeof recipient.amount !== 'number' || recipient.amount <= 0) {
|
||||
return res.status(400).json({ error: `Příjemce ${recipient.login} má neplatnou částku` });
|
||||
}
|
||||
// Validace max 2 desetinná místa
|
||||
const amountStr = recipient.amount.toString();
|
||||
if (amountStr.includes('.') && amountStr.split('.')[1].length > 2) {
|
||||
return res.status(400).json({ error: `Částka pro ${recipient.login} má více než 2 desetinná místa` });
|
||||
}
|
||||
|
||||
// Vygenerovat QR kód
|
||||
await generateQr(recipient.login, bankAccount, bankAccountHolder, recipient.amount, recipient.purpose);
|
||||
|
||||
// Uložit jako nevyřízený QR kód
|
||||
await addPendingQr(recipient.login, {
|
||||
date: today,
|
||||
creator: login,
|
||||
totalPrice: recipient.amount,
|
||||
purpose: recipient.purpose,
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({ success: true, count: recipients.length });
|
||||
} catch (e: any) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user