Zavedení podpory pro Redis, agnostické úložiště dat

This commit is contained in:
2023-08-06 17:46:51 +02:00
parent 8a75c98c9a
commit 37542499a9
11 changed files with 282 additions and 97 deletions

View File

@@ -1,8 +1,10 @@
import { db } from "./database";
import { formatDate, getHumanDate, getIsWeekend } from "./utils";
import { callNotifikace } from "./notifikace";
import { generateQr } from "./qr";
import { ClientData, PizzaDayState, UdalostEnum, Pizza, PizzaSize, Order, PizzaOrder, Locations } from "../../types";
import getStorage from "./storage";
const storage = getStorage();
/** Vrátí dnešní datum, případně fiktivní datum pro účely vývoje a testování. */
function getToday(): Date {
@@ -20,23 +22,22 @@ function getEmptyData(): ClientData {
/**
* Vrátí veškerá klientská data pro aktuální den.
*/
export function getData(): ClientData {
const data = db.get(formatDate(getToday())) || getEmptyData();
return data;
export async function getData(): Promise<ClientData> {
return await storage.getData(formatDate(getToday())) || getEmptyData();
}
/**
* Vytvoří pizza day pro aktuální den a vrátí data pro klienta.
*/
export function createPizzaDay(creator: string): ClientData {
initIfNeeded();
export async function createPizzaDay(creator: string): Promise<ClientData> {
await initIfNeeded();
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den již existuje");
}
const data: ClientData = { pizzaDay: { state: PizzaDayState.CREATED, creator, orders: [] }, ...clientData };
db.set(today, data);
await storage.setData(today, data);
callNotifikace({ input: { udalost: UdalostEnum.ZAHAJENA_PIZZA, user: creator } })
return data;
}
@@ -44,9 +45,9 @@ export function createPizzaDay(creator: string): ClientData {
/**
* Smaže pizza day pro aktuální den.
*/
export function deletePizzaDay(login: string) {
export async function deletePizzaDay(login: string): Promise<ClientData> {
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -54,7 +55,7 @@ export function deletePizzaDay(login: string) {
throw Error("Login uživatele se neshoduje se zakladatelem Pizza Day");
}
delete clientData.pizzaDay;
db.set(today, clientData);
await storage.setData(today, clientData);
return clientData;
}
@@ -65,9 +66,9 @@ export function deletePizzaDay(login: string) {
* @param pizza zvolená pizza
* @param size zvolená velikost pizzy
*/
export function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize) {
export async function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize) {
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -91,7 +92,7 @@ export function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize) {
}
order.pizzaList.push(pizzaOrder);
order.totalPrice += pizzaOrder.price;
db.set(today, clientData);
await storage.setData(today, clientData);
return clientData;
}
@@ -101,9 +102,9 @@ export function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize) {
* @param login login uživatele
* @param pizzaOrder objednávka pizzy
*/
export function removePizzaOrder(login: string, pizzaOrder: PizzaOrder) {
export async function removePizzaOrder(login: string, pizzaOrder: PizzaOrder) {
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -122,7 +123,7 @@ export function removePizzaOrder(login: string, pizzaOrder: PizzaOrder) {
if (order.pizzaList.length == 0) {
clientData.pizzaDay.orders.splice(orderIndex, 1);
}
db.set(today, clientData);
await storage.setData(today, clientData);
return clientData;
}
@@ -132,9 +133,9 @@ export function removePizzaOrder(login: string, pizzaOrder: PizzaOrder) {
* @param login login uživatele
* @returns aktuální data pro uživatele
*/
export function lockPizzaDay(login: string) {
export async function lockPizzaDay(login: string) {
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -145,7 +146,7 @@ export function lockPizzaDay(login: string) {
throw Error("Pizza day není ve stavu " + PizzaDayState.CREATED + " nebo " + PizzaDayState.ORDERED);
}
clientData.pizzaDay.state = PizzaDayState.LOCKED;
db.set(today, clientData);
await storage.setData(today, clientData);
return clientData;
}
@@ -155,9 +156,9 @@ export function lockPizzaDay(login: string) {
* @param login login uživatele
* @returns aktuální data pro uživatele
*/
export function unlockPizzaDay(login: string) {
export async function unlockPizzaDay(login: string) {
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -168,7 +169,7 @@ export function unlockPizzaDay(login: string) {
throw Error("Pizza day není ve stavu " + PizzaDayState.LOCKED);
}
clientData.pizzaDay.state = PizzaDayState.CREATED;
db.set(today, clientData);
await storage.setData(today, clientData);
return clientData;
}
@@ -178,9 +179,9 @@ export function unlockPizzaDay(login: string) {
* @param login login uživatele
* @returns aktuální data pro uživatele
*/
export function finishPizzaOrder(login: string) {
export async function finishPizzaOrder(login: string) {
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -191,7 +192,7 @@ export function finishPizzaOrder(login: string) {
throw Error("Pizza day není ve stavu " + PizzaDayState.LOCKED);
}
clientData.pizzaDay.state = PizzaDayState.ORDERED;
db.set(today, clientData);
await storage.setData(today, clientData);
callNotifikace({ input: { udalost: UdalostEnum.OBJEDNANA_PIZZA, user: clientData?.pizzaDay?.creator } })
return clientData;
}
@@ -203,9 +204,9 @@ export function finishPizzaOrder(login: string) {
* @param login login uživatele
* @returns aktuální data pro uživatele
*/
export function finishPizzaDelivery(login: string, bankAccount?: string, bankAccountHolder?: string) {
export async function finishPizzaDelivery(login: string, bankAccount?: string, bankAccountHolder?: string) {
const today = formatDate(getToday());
const clientData: ClientData = db.get(today);
const clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -228,14 +229,15 @@ export function finishPizzaDelivery(login: string, bankAccount?: string, bankAcc
}
}
}
db.set(today, clientData);
await storage.setData(today, clientData);
return clientData;
}
export function initIfNeeded() {
export async function initIfNeeded() {
const today = formatDate(getToday());
if (!db.has(today)) {
db.set(today, getEmptyData());
const hasData = await storage.hasData(today);
if (!hasData) {
await storage.setData(today, getEmptyData());
}
}
@@ -246,9 +248,9 @@ export function initIfNeeded() {
* @param location vybrané "umístění"
* @returns
*/
export function removeChoices(login: string, location: Locations) {
export async function removeChoices(login: string, location: Locations) {
const today = formatDate(getToday());
let data: ClientData = db.get(today);
let data: ClientData = await storage.getData(today);
// TODO zajistit, že neověřený uživatel se stejným loginem nemůže mazat volby ověřeného
if (location in data.choices) {
if (login in data.choices[location]) {
@@ -256,7 +258,7 @@ export function removeChoices(login: string, location: Locations) {
if (Object.keys(data.choices[location]).length === 0) {
delete data.choices[location]
}
db.set(today, data);
await storage.setData(today, data);
}
}
return data;
@@ -271,16 +273,16 @@ export function removeChoices(login: string, location: Locations) {
* @param foodIndex index jídla v jídelním lístku daného umístění, pokud existuje
* @returns
*/
export function removeChoice(login: string, location: Locations, foodIndex: number) {
export async function removeChoice(login: string, location: Locations, foodIndex: number) {
const today = formatDate(getToday());
let data: ClientData = db.get(today);
let data: ClientData = await storage.getData(today);
// TODO řešit ověření uživatele
if (location in data.choices) {
if (login in data.choices[location]) {
const index = data.choices[location][login].options.indexOf(foodIndex);
if (index > -1) {
data.choices[location][login].options.splice(index, 1)
db.set(today, data);
await storage.setData(today, data);
}
}
}
@@ -292,17 +294,19 @@ export function removeChoice(login: string, location: Locations, foodIndex: numb
*
* @param login login uživatele
*/
function removeChoiceIfPresent(login: string) {
async function removeChoiceIfPresent(login: string) {
const today = formatDate(getToday());
let data: ClientData = db.get(today);
let data: ClientData = await storage.getData(today);
for (const key of Object.keys(data.choices)) {
if (login in data.choices[key]) {
delete data.choices[key][login];
if (Object.keys(data.choices[key]).length === 0) {
delete data.choices[key];
}
await storage.setData(today, data);
}
}
return data;
}
/**
@@ -314,10 +318,10 @@ function removeChoiceIfPresent(login: string) {
* @param trusted příznak, zda se jedná o ověřeného uživatele
* @returns aktuální data
*/
export function addChoice(login: string, trusted: boolean, location: Locations, foodIndex?: number) {
initIfNeeded();
export async function addChoice(login: string, trusted: boolean, location: Locations, foodIndex?: number) {
await initIfNeeded();
const today = formatDate(getToday());
let data: ClientData = db.get(today);
let data: ClientData = await storage.getData(today);
// Ověření, že se neověřený užívatel nepokouší přepsat údaje ověřeného
const locations = Object.values(data?.choices);
let found = false;
@@ -333,7 +337,7 @@ export function addChoice(login: string, trusted: boolean, location: Locations,
}
// Pokud měníme pouze lokaci, mažeme případné předchozí
if (foodIndex == null) {
removeChoiceIfPresent(login);
data = await removeChoiceIfPresent(login);
}
if (!(location in data.choices)) {
data.choices[location] = {};
@@ -347,14 +351,14 @@ export function addChoice(login: string, trusted: boolean, location: Locations,
if (foodIndex != null && !data.choices[location][login].options.includes(foodIndex)) {
data.choices[location][login].options.push(foodIndex);
}
db.set(today, data);
await storage.setData(today, data);
return data;
}
// TODO přejmenovat, ať je jasné že to patří k pizza day
export function updateNote(login: string, note?: string) {
export async function updateNote(login: string, note?: string) {
const today = formatDate(getToday());
let clientData: ClientData = db.get(today);
let clientData: ClientData = await storage.getData(today);
if (!clientData.pizzaDay) {
throw Error("Pizza day pro dnešní den neexistuje");
}
@@ -366,6 +370,6 @@ export function updateNote(login: string, note?: string) {
throw Error("Pizza day neobsahuje žádné objednávky uživatele " + login);
}
myOrder.note = note;
db.set(today, clientData);
await storage.setData(today, clientData);
return clientData;
}