Migrace na OpenAPI - TypeScript typy
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { formatDate } from "./utils";
|
||||
import { callNotifikace } from "./notifikace";
|
||||
import { generateQr } from "./qr";
|
||||
import { ClientData, PizzaDayState, UdalostEnum, Pizza, PizzaSize, Order, PizzaOrder, DayData } from "../../types";
|
||||
import getStorage from "./storage";
|
||||
import { downloadPizzy } from "./chefie";
|
||||
import { getToday, initIfNeeded } from "./service";
|
||||
import { getClientData, getToday, initIfNeeded } from "./service";
|
||||
import { Pizza, ClientData, PizzaDayState, PizzaSize, PizzaOrder, PizzaVariant, UdalostEnum } from "../../types";
|
||||
|
||||
const storage = getStorage();
|
||||
|
||||
@@ -14,8 +14,7 @@ const storage = getStorage();
|
||||
*/
|
||||
export async function getPizzaList(): Promise<Pizza[] | undefined> {
|
||||
await initIfNeeded();
|
||||
const today = formatDate(getToday());
|
||||
let clientData: DayData = await storage.getData(today);
|
||||
let clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaList) {
|
||||
const mock = process.env.MOCK_DATA === 'true';
|
||||
clientData = await savePizzaList(await downloadPizzy(mock));
|
||||
@@ -31,9 +30,9 @@ export async function getPizzaList(): Promise<Pizza[] | undefined> {
|
||||
export async function savePizzaList(pizzaList: Pizza[]): Promise<ClientData> {
|
||||
await initIfNeeded();
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
clientData.pizzaList = pizzaList;
|
||||
clientData.pizzaListLastUpdate = new Date();
|
||||
clientData.pizzaListLastUpdate = formatDate(new Date());
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
@@ -43,14 +42,14 @@ export async function savePizzaList(pizzaList: Pizza[]): Promise<ClientData> {
|
||||
*/
|
||||
export async function createPizzaDay(creator: string): Promise<ClientData> {
|
||||
await initIfNeeded();
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
if (clientData.pizzaDay) {
|
||||
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 today = formatDate(getToday());
|
||||
await storage.setData(today, data);
|
||||
callNotifikace({ input: { udalost: UdalostEnum.ZAHAJENA_PIZZA, user: creator } })
|
||||
return data;
|
||||
@@ -60,8 +59,7 @@ export async function createPizzaDay(creator: string): Promise<ClientData> {
|
||||
* Smaže pizza day pro aktuální den.
|
||||
*/
|
||||
export async function deletePizzaDay(login: string): Promise<ClientData> {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
@@ -69,6 +67,7 @@ export async function deletePizzaDay(login: string): Promise<ClientData> {
|
||||
throw Error("Login uživatele se neshoduje se zakladatelem Pizza Day");
|
||||
}
|
||||
delete clientData.pizzaDay;
|
||||
const today = formatDate(getToday());
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
@@ -82,28 +81,35 @@ export async function deletePizzaDay(login: string): Promise<ClientData> {
|
||||
*/
|
||||
export async function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
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: Order | undefined = clientData.pizzaDay.orders.find(o => o.customer === login);
|
||||
let order: PizzaOrder | undefined = clientData.pizzaDay?.orders?.find(o => o.customer === login);
|
||||
if (!order) {
|
||||
order = {
|
||||
customer: login,
|
||||
pizzaList: [],
|
||||
totalPrice: 0,
|
||||
hasQr: false,
|
||||
}
|
||||
if (!clientData.pizzaDay.orders) {
|
||||
clientData.pizzaDay.orders = [];
|
||||
}
|
||||
clientData.pizzaDay.orders.push(order);
|
||||
}
|
||||
const pizzaOrder: PizzaOrder = {
|
||||
const pizzaOrder: PizzaVariant = {
|
||||
varId: size.varId,
|
||||
name: pizza.name,
|
||||
size: size.size,
|
||||
price: size.price,
|
||||
}
|
||||
if (!order.pizzaList) {
|
||||
order.pizzaList = [];
|
||||
}
|
||||
order.pizzaList.push(pizzaOrder);
|
||||
order.totalPrice += pizzaOrder.price;
|
||||
await storage.setData(today, clientData);
|
||||
@@ -116,26 +122,26 @@ export async function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize
|
||||
* @param login login uživatele
|
||||
* @param pizzaOrder objednávka pizzy
|
||||
*/
|
||||
export async function removePizzaOrder(login: string, pizzaOrder: PizzaOrder) {
|
||||
export async function removePizzaOrder(login: string, pizzaOrder: PizzaVariant) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
const orderIndex = clientData.pizzaDay.orders.findIndex(o => o.customer === login);
|
||||
const orderIndex = clientData.pizzaDay!.orders!.findIndex(o => o.customer === login);
|
||||
if (orderIndex < 0) {
|
||||
throw Error("Nebyly nalezeny žádné objednávky pro uživatele " + login);
|
||||
}
|
||||
const order = clientData.pizzaDay.orders[orderIndex];
|
||||
const index = order.pizzaList.findIndex(o => o.name === pizzaOrder.name && o.size === pizzaOrder.size);
|
||||
const order = clientData.pizzaDay!.orders![orderIndex];
|
||||
const index = order.pizzaList!.findIndex(o => o.name === pizzaOrder.name && o.size === pizzaOrder.size);
|
||||
if (index < 0) {
|
||||
throw Error("Objednávka s danými parametry nebyla nalezena");
|
||||
}
|
||||
const price = order.pizzaList[index].price;
|
||||
order.pizzaList.splice(index, 1);
|
||||
const price = order.pizzaList![index].price;
|
||||
order.pizzaList!.splice(index, 1);
|
||||
order.totalPrice -= price;
|
||||
if (order.pizzaList.length == 0) {
|
||||
clientData.pizzaDay.orders.splice(orderIndex, 1);
|
||||
if (order.pizzaList!.length == 0) {
|
||||
clientData.pizzaDay.orders!.splice(orderIndex, 1);
|
||||
}
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
@@ -149,7 +155,7 @@ export async function removePizzaOrder(login: string, pizzaOrder: PizzaOrder) {
|
||||
*/
|
||||
export async function lockPizzaDay(login: string) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
@@ -172,7 +178,7 @@ export async function lockPizzaDay(login: string) {
|
||||
*/
|
||||
export async function unlockPizzaDay(login: string) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
@@ -195,7 +201,7 @@ export async function unlockPizzaDay(login: string) {
|
||||
*/
|
||||
export async function finishPizzaOrder(login: string) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
@@ -220,7 +226,7 @@ export async function finishPizzaOrder(login: string) {
|
||||
*/
|
||||
export async function finishPizzaDelivery(login: string, bankAccount?: string, bankAccountHolder?: string) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: DayData = await storage.getData(today);
|
||||
const clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
@@ -234,9 +240,9 @@ export async function finishPizzaDelivery(login: string, bankAccount?: string, b
|
||||
|
||||
// Vygenerujeme QR kód, pokud k tomu máme data
|
||||
if (bankAccount?.length && bankAccountHolder?.length) {
|
||||
for (const order of clientData.pizzaDay.orders) {
|
||||
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(pizza => `Pizza ${pizza.name} (${pizza.size})`).join(', ');
|
||||
await generateQr(order.customer, bankAccount, bankAccountHolder, order.totalPrice, message);
|
||||
order.hasQr = true;
|
||||
}
|
||||
@@ -255,15 +261,15 @@ export async function finishPizzaDelivery(login: string, bankAccount?: string, b
|
||||
*/
|
||||
export async function updatePizzaDayNote(login: string, note?: string) {
|
||||
const today = formatDate(getToday());
|
||||
let clientData: DayData = await storage.getData(today);
|
||||
let 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);
|
||||
}
|
||||
const myOrder = clientData.pizzaDay.orders.find(o => o.customer === login);
|
||||
if (!myOrder || !myOrder.pizzaList.length) {
|
||||
const myOrder = clientData.pizzaDay.orders!.find(o => o.customer === login);
|
||||
if (!myOrder?.pizzaList?.length) {
|
||||
throw Error("Pizza day neobsahuje žádné objednávky uživatele " + login);
|
||||
}
|
||||
myOrder.note = note;
|
||||
@@ -282,7 +288,7 @@ export async function updatePizzaDayNote(login: string, note?: string) {
|
||||
*/
|
||||
export async function updatePizzaFee(login: string, targetLogin: string, text?: string, price?: number) {
|
||||
const today = formatDate(getToday());
|
||||
let clientData: DayData = await storage.getData(today);
|
||||
let clientData = await getClientData(getToday());
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
@@ -292,8 +298,8 @@ export async function updatePizzaFee(login: string, targetLogin: string, text?:
|
||||
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) {
|
||||
const targetOrder = clientData.pizzaDay.orders!.find(o => o.customer === targetLogin);
|
||||
if (!targetOrder?.pizzaList?.length) {
|
||||
throw Error(`Pizza day neobsahuje žádné objednávky uživatele ${targetLogin}`);
|
||||
}
|
||||
if (!price) {
|
||||
|
||||
Reference in New Issue
Block a user