This commit is contained in:
+76
-6
@@ -2,9 +2,9 @@ import { formatDate } from "./utils";
|
||||
import { callNotifikace } from "./notifikace";
|
||||
import { generateQr } from "./qr";
|
||||
import getStorage from "./storage";
|
||||
import { downloadPizzy } from "./chefie";
|
||||
import { downloadPizzy, downloadSalaty } from "./chefie";
|
||||
import { getClientData, getToday, initIfNeeded } from "./service";
|
||||
import { Pizza, ClientData, PizzaDayState, PizzaSize, PizzaOrder, PizzaVariant, UdalostEnum, PendingQr } from "../../types/gen/types.gen";
|
||||
import { Pizza, Salat, ClientData, PizzaDayState, PizzaSize, PizzaOrder, PizzaVariant, UdalostEnum, PendingQr } from "../../types/gen/types.gen";
|
||||
|
||||
const storage = getStorage();
|
||||
const PENDING_QR_PREFIX = 'pending_qr';
|
||||
@@ -25,7 +25,7 @@ export async function getPizzaList(): Promise<Pizza[] | undefined> {
|
||||
|
||||
/**
|
||||
* Uloží seznam dostupných pizz pro dnešní den.
|
||||
*
|
||||
*
|
||||
* @param pizzaList seznam dostupných pizz
|
||||
*/
|
||||
export async function savePizzaList(pizzaList: Pizza[]): Promise<ClientData> {
|
||||
@@ -38,6 +38,34 @@ export async function savePizzaList(pizzaList: Pizza[]): Promise<ClientData> {
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vrátí seznam dostupných salátů pro dnešní den.
|
||||
* Stáhne je, pokud je pro dnešní den nemá.
|
||||
*/
|
||||
export async function getSalatList(): Promise<Salat[] | undefined> {
|
||||
await initIfNeeded();
|
||||
let clientData = await getClientData(getToday());
|
||||
if (!clientData.salatList) {
|
||||
const mock = process.env.MOCK_DATA === 'true';
|
||||
clientData = await saveSalatList(await downloadSalaty(mock));
|
||||
}
|
||||
return Promise.resolve(clientData.salatList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uloží seznam dostupných salátů pro dnešní den.
|
||||
*
|
||||
* @param salatList seznam dostupných salátů
|
||||
*/
|
||||
export async function saveSalatList(salatList: Salat[]): Promise<ClientData> {
|
||||
await initIfNeeded();
|
||||
const today = formatDate(getToday());
|
||||
const clientData = await getClientData(getToday());
|
||||
clientData.salatList = salatList;
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vytvoří pizza day pro aktuální den a vrátí data pro klienta.
|
||||
*/
|
||||
@@ -48,8 +76,8 @@ export async function createPizzaDay(creator: string): Promise<ClientData> {
|
||||
throw Error("Pizza day pro dnešní den již existuje");
|
||||
}
|
||||
// TODO berka rychlooprava, vyřešit lépe - stahovat jednou, na jediném místě!
|
||||
const pizzaList = await getPizzaList();
|
||||
const data: ClientData = { pizzaDay: { state: PizzaDayState.CREATED, creator, orders: [] }, pizzaList, ...clientData };
|
||||
const [pizzaList, salatList] = await Promise.all([getPizzaList(), getSalatList()]);
|
||||
const data: ClientData = { pizzaDay: { state: PizzaDayState.CREATED, creator, orders: [] }, pizzaList, salatList, ...clientData };
|
||||
const today = formatDate(getToday());
|
||||
await storage.setData(today, data);
|
||||
callNotifikace({ input: { udalost: UdalostEnum.ZAHAJENA_PIZZA, user: creator } })
|
||||
@@ -113,6 +141,46 @@ export async function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Přidá objednávku salátu uživateli.
|
||||
*
|
||||
* @param login login uživatele
|
||||
* @param salat zvolený salát
|
||||
*/
|
||||
export async function addSalatOrder(login: string, salat: Salat) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData = await getClientData(getToday());
|
||||
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);
|
||||
}
|
||||
let order: PizzaOrder | undefined = clientData.pizzaDay?.orders?.find(o => o.customer === login);
|
||||
if (!order) {
|
||||
order = {
|
||||
customer: login,
|
||||
pizzaList: [],
|
||||
totalPrice: 0,
|
||||
hasQr: false,
|
||||
}
|
||||
clientData.pizzaDay.orders ??= [];
|
||||
clientData.pizzaDay.orders.push(order);
|
||||
}
|
||||
const salatOrder: PizzaVariant = {
|
||||
varId: 0,
|
||||
name: salat.name,
|
||||
size: "1 porce",
|
||||
price: salat.price,
|
||||
category: 'salat',
|
||||
}
|
||||
order.pizzaList ??= [];
|
||||
order.pizzaList.push(salatOrder);
|
||||
order.totalPrice += salatOrder.price;
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Odstraní všechny pizzy uživatele (celou jeho objednávku).
|
||||
* Pokud Pizza day neexistuje nebo není ve stavu CREATED, neudělá nic.
|
||||
@@ -269,7 +337,9 @@ export async function finishPizzaDelivery(login: string, bankAccount?: string, b
|
||||
if (bankAccount?.length && bankAccountHolder?.length) {
|
||||
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(', ');
|
||||
let message = order.pizzaList!.map(item =>
|
||||
item.category === 'salat' ? `Salát ${item.name}` : `Pizza ${item.name} (${item.size})`
|
||||
).join(', ');
|
||||
await generateQr(order.customer, bankAccount, bankAccountHolder, order.totalPrice, message);
|
||||
order.hasQr = true;
|
||||
// Uložíme nevyřízený QR kód pro persistentní zobrazení
|
||||
|
||||
Reference in New Issue
Block a user