feat: podpora více odkazů na podnik při objednávání
This commit is contained in:
+26
-4
@@ -47,19 +47,41 @@ export async function getOrderDates(): Promise<string[]> {
|
||||
return dates.sort();
|
||||
}
|
||||
|
||||
export async function createGroup(creatorLogin: string, name: string, date?: Date): Promise<ClientData> {
|
||||
/**
|
||||
* Vytvoří novou skupinu objednávky.
|
||||
*
|
||||
* @param creatorLogin login zakladatele
|
||||
* @param name název obchodu (musí být v seznamu povolených obchodů)
|
||||
* @param date den, ke kterému skupina patří (výchozí dnešní)
|
||||
* @param url vybraná URL na nabídku podniku — musí být jednou z URL obchodu.
|
||||
* Pokud není předána a obchod má právě jednu URL, dosadí se automaticky.
|
||||
*/
|
||||
export async function createGroup(creatorLogin: string, name: string, date?: Date, url?: string): Promise<ClientData> {
|
||||
const stores = await getStores();
|
||||
if (!stores.some(s => s.name.toLowerCase() === name.trim().toLowerCase())) {
|
||||
const store = stores.find(s => s.name.toLowerCase() === name.trim().toLowerCase());
|
||||
if (!store) {
|
||||
throw new Error('Obchod není v seznamu povolených obchodů');
|
||||
}
|
||||
const storeUrls = store.urls ?? [];
|
||||
const requestedUrl = url?.trim();
|
||||
let selectedUrl: string | undefined;
|
||||
if (requestedUrl) {
|
||||
selectedUrl = storeUrls.find(u => u === requestedUrl);
|
||||
if (!selectedUrl) {
|
||||
throw new Error('Vybraná URL nepatří k tomuto obchodu');
|
||||
}
|
||||
} else if (storeUrls.length === 1) {
|
||||
// Obchod má jedinou nabídku — vybírat není z čeho
|
||||
selectedUrl = storeUrls[0];
|
||||
}
|
||||
const data = await getExtraData(date);
|
||||
const canonical = stores.find(s => s.name.toLowerCase() === name.trim().toLowerCase())!.name;
|
||||
const group: OrderGroup = {
|
||||
id: crypto.randomUUID(),
|
||||
name: canonical,
|
||||
name: store.name,
|
||||
creatorLogin,
|
||||
state: GroupState.OPEN,
|
||||
members: { [creatorLogin]: {} },
|
||||
...(selectedUrl ? { url: selectedUrl } : {}),
|
||||
};
|
||||
data.groups = [...(data.groups ?? []), group];
|
||||
return saveExtraData(data, date);
|
||||
|
||||
Reference in New Issue
Block a user