Možnost příplatků u Pizza day objednávek
This commit is contained in:
@@ -237,8 +237,7 @@ export async function finishPizzaDelivery(login: string, bankAccount?: string, b
|
||||
for (const order of clientData.pizzaDay.orders) {
|
||||
if (order.customer !== login) { // zatím platí creator = objednávající, a pro toho nemá QR kód smysl
|
||||
let message = order.pizzaList.map(pizza => `Pizza ${pizza.name} (${pizza.size})`).join(', ');
|
||||
const price = order.pizzaList.map(pizza => pizza.price).reduce((partial, a) => partial + a, 0);
|
||||
await generateQr(order.customer, bankAccount, bankAccountHolder, price, message);
|
||||
await generateQr(order.customer, bankAccount, bankAccountHolder, order.totalPrice, message);
|
||||
order.hasQr = true;
|
||||
}
|
||||
}
|
||||
@@ -271,3 +270,39 @@ export async function updatePizzaDayNote(login: string, note?: string) {
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualizuje příplatek uživatele k objednávce pizzy.
|
||||
* V případě nevyplnění ceny je příplatek odebrán.
|
||||
*
|
||||
* @param login přihlašovací jméno aktuálního uživatele
|
||||
* @param targetLogin přihlašovací jméno uživatele, kterému je nastavován příplatek
|
||||
* @param text text popisující příplatek
|
||||
* @param price celková cena příplatku
|
||||
*/
|
||||
export async function updatePizzaFee(login: string, targetLogin: string, text?: string, price?: number) {
|
||||
const today = formatDate(getToday());
|
||||
let clientData: ClientData = await storage.getData(today);
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
if (clientData.pizzaDay.state !== PizzaDayState.CREATED) {
|
||||
throw Error(`Pizza day není ve stavu ${PizzaDayState.CREATED}`);
|
||||
}
|
||||
if (clientData.pizzaDay.creator !== login) {
|
||||
throw Error("Příplatky může měnit pouze zakladatel Pizza day");
|
||||
}
|
||||
const targetOrder = clientData.pizzaDay.orders.find(o => o.customer === targetLogin);
|
||||
if (!targetOrder || !targetOrder.pizzaList.length) {
|
||||
throw Error(`Pizza day neobsahuje žádné objednávky uživatele ${targetLogin}`);
|
||||
}
|
||||
if (!price) {
|
||||
delete targetOrder.fee;
|
||||
} else {
|
||||
targetOrder.fee = { text, price };
|
||||
}
|
||||
// Přepočet ceny
|
||||
targetOrder.totalPrice = targetOrder.pizzaList.reduce((price, pizzaOrder) => price + pizzaOrder.price, 0) + (targetOrder.fee?.price || 0);
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
Reference in New Issue
Block a user