chore(checkpoint): save club core backend and pending pos fixes

This commit is contained in:
Deploy
2026-08-26 17:58:45 +02:00
parent cf3c906ed2
commit 49dfd00406
44 changed files with 2241 additions and 219 deletions

View File

@@ -100,6 +100,17 @@ function parseItems(raw: RawItem[] | undefined): {
});
}
async function readErrorMessage(response: Response, fallback: string): Promise<string> {
const text = await response.text();
if (!text) return fallback;
try {
const data = JSON.parse(text) as { error?: { message?: string }; message?: string };
return data.error?.message ?? data.message ?? text;
} catch {
return text;
}
}
async function syncCart(
cookies: string,
items: { productId: string; variantId: string; quantity: number }[],
@@ -125,7 +136,9 @@ async function syncCart(
}
const nextByVariant = new Map(items.map((item) => [item.variantId, item]));
const currentByVariant = new Map(serverItems.map((item) => [item.variantId, item]));
const operations: Promise<Response>[] = [];
for (const item of serverItems) {
if (!nextByVariant.has(item.variantId)) {
operations.push(
@@ -136,20 +149,35 @@ async function syncCart(
);
}
}
for (const item of items) {
operations.push(
fetch(`${API}/cart/items`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Cookie: cookies },
body: JSON.stringify(item),
}),
);
const existing = currentByVariant.get(item.variantId);
if (!existing) {
operations.push(
fetch(`${API}/cart/items`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Cookie: cookies },
body: JSON.stringify(item),
}),
);
continue;
}
if (existing.quantity !== item.quantity) {
operations.push(
fetch(`${API}/cart/items/${item.variantId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json', Cookie: cookies },
body: JSON.stringify({ quantity: item.quantity }),
}),
);
}
}
const results = await Promise.all(operations);
for (const response of results) {
if (!response.ok && response.status !== 404) {
const message = await response.text();
return { ok: false, status: response.status, message: message || 'Error al sincronizar el carrito' };
const message = await readErrorMessage(response, 'Error al sincronizar el carrito');
return { ok: false, status: response.status, message };
}
}
return { ok: true };