feat: možnost editace existujících podniků u objednávání
This commit is contained in:
7 files changed
+327
-51
No files matched your search
@@ -95,6 +95,46 @@ export async function addStore(name: string, heslo: string, urls?: string[]): Pr
|
||||
return updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upraví existující obchod — název a/nebo seznam URL na nabídku.
|
||||
*
|
||||
* @param name aktuální název obchodu (identifikuje upravovaný obchod)
|
||||
* @param heslo admin heslo
|
||||
* @param newName nový název; pokud není předán, název zůstane nezměněn
|
||||
* @param urls nový seznam URL (nahradí stávající); pokud není předán, URL zůstanou nezměněné
|
||||
*/
|
||||
export async function updateStore(name: string, heslo: string, newName?: string, urls?: string[]): Promise<Store[]> {
|
||||
const adminPassword = process.env.ADMIN_PASSWORD;
|
||||
if (!adminPassword || heslo !== adminPassword) {
|
||||
throw new Error('UNAUTHORIZED');
|
||||
}
|
||||
const stores = await getStores();
|
||||
const index = stores.findIndex(s => s.name.toLowerCase() === name.trim().toLowerCase());
|
||||
if (index < 0) {
|
||||
throw new Error('Obchod nebyl nalezen');
|
||||
}
|
||||
const current = stores[index];
|
||||
|
||||
let finalName = current.name;
|
||||
if (newName !== undefined) {
|
||||
const trimmedName = newName.trim();
|
||||
if (!trimmedName) {
|
||||
throw new Error('Název obchodu nesmí být prázdný');
|
||||
}
|
||||
// Nový název nesmí kolidovat s jiným obchodem (shoda se sebou samým je v pořádku)
|
||||
if (stores.some((s, i) => i !== index && s.name.toLowerCase() === trimmedName.toLowerCase())) {
|
||||
throw new Error('Obchod s tímto názvem již existuje');
|
||||
}
|
||||
finalName = trimmedName;
|
||||
}
|
||||
|
||||
const finalUrls = urls !== undefined ? normalizeUrls(urls) : (current.urls ?? []);
|
||||
const updatedStore: Store = finalUrls.length > 0 ? { name: finalName, urls: finalUrls } : { name: finalName };
|
||||
const updated = stores.map((s, i) => (i === index ? updatedStore : s));
|
||||
await storage.setData(STORES_KEY, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Odebere obchod ze seznamu povolených (dle názvu).
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user