chore(checkpoint): save club core backend and pending pos fixes
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user