Základní funkčnost Pizza Day
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ClientData, Locations, State } from "./types";
|
||||
import { ClientData, Locations, Order, Pizza, PizzaDayState, PizzaOrder, PizzaSize } from "./types";
|
||||
import { db } from "./database";
|
||||
import { getHumanDate, getIsWeekend } from "./utils";
|
||||
import { formatDate } from "./utils";
|
||||
@@ -34,7 +34,7 @@ export function createPizzaDay(creator: string): ClientData {
|
||||
if (clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den již existuje");
|
||||
}
|
||||
const data: ClientData = { pizzaDay: { state: State.CREATED, creator, orders: [] }, ...clientData };
|
||||
const data: ClientData = { pizzaDay: { state: PizzaDayState.CREATED, creator, orders: [] }, ...clientData };
|
||||
db.set(today, data);
|
||||
return data;
|
||||
}
|
||||
@@ -54,6 +54,70 @@ export function deletePizzaDay(login: string) {
|
||||
db.delete(today);
|
||||
}
|
||||
|
||||
/**
|
||||
* Přidá objednávku pizzy uživateli.
|
||||
*
|
||||
* @param login login uživatele
|
||||
* @param pizza zvolená pizza
|
||||
* @param size zvolená velikost pizzy
|
||||
*/
|
||||
export function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: ClientData = db.get(today);
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
let order: Order | undefined = clientData.pizzaDay.orders.find(o => o.customer === login);
|
||||
if (!order) {
|
||||
order = {
|
||||
customer: login,
|
||||
pizzaList: [],
|
||||
totalPrice: 0,
|
||||
}
|
||||
clientData.pizzaDay.orders.push(order);
|
||||
}
|
||||
const pizzaOrder: PizzaOrder = {
|
||||
name: pizza.name,
|
||||
size: size.size,
|
||||
price: size.price,
|
||||
}
|
||||
order.pizzaList.push(pizzaOrder);
|
||||
order.totalPrice += pizzaOrder.price;
|
||||
db.set(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Odstraní danou objednávku pizzy.
|
||||
*
|
||||
* @param login login uživatele
|
||||
* @param pizzaOrder objednávka pizzy
|
||||
*/
|
||||
export function removePizzaOrder(login: string, pizzaOrder: PizzaOrder) {
|
||||
const today = formatDate(getToday());
|
||||
const clientData: ClientData = db.get(today);
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
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);
|
||||
if (index < 0) {
|
||||
throw Error("Objednávka s danými parametry nebyla nalezena");
|
||||
}
|
||||
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);
|
||||
}
|
||||
db.set(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
|
||||
export function initIfNeeded() {
|
||||
const today = formatDate(getToday());
|
||||
if (!db.has(today)) {
|
||||
|
||||
Reference in New Issue
Block a user