schon/storefront/app/composables/orders/useOrderSync.ts
Alexandr SaVBaD Waltz e65e7b7d73 **chore(storefront): apply consistent code formatting and improve readability**
Refactored multiple files for code styling consistency, using proper indentation and spacing to align with team standards. Improved readability and maintainability across composables, Apollo plugin, and localization files.

Enhancements:
- Standardized import and function indentation across all composables.
- Updated `biome.json` schema to the latest version (v2.4.4) for tool compatibility.
- Organized code blocks in Apollo plugin for better understandability.

No functional changes introduced—this is a non-breaking, code refinement commit.
2026-02-28 17:41:25 +03:00

73 lines
1.6 KiB
TypeScript

import { useOrderOverwrite } from '@composables/orders/useOrderOverwrite';
export function useOrderSync() {
const cartStore = useCartStore();
const userStore = useUserStore();
const { $appHelpers } = useNuxtApp();
const { overwriteOrder } = useOrderOverwrite();
const isAuthenticated = computed(() => userStore.isAuthenticated);
const orderUuid = computed(() => cartStore.currentOrder?.uuid);
const cookieCart = useCookie($appHelpers.COOKIES_CART_KEY, {
default: () => [],
path: '/',
});
async function syncOrder() {
if (!isAuthenticated.value || !orderUuid.value) {
return;
}
const cookieCartItems = cookieCart.value || [];
if (cookieCartItems.length === 0) {
return;
}
const apiCartProducts = cartStore.currentOrder?.orderProducts?.edges || [];
const apiProductMap = new Map(
apiCartProducts.map((e) => [
e.node.product.uuid,
e.node.quantity,
]),
);
const productsToSync = [];
for (const cartItem of cookieCartItems) {
const apiQuantity = apiProductMap.get(cartItem.product.uuid) || 0;
const quantityDifference = cartItem.quantity - apiQuantity;
if (quantityDifference > 0) {
for (let i = 0; i < quantityDifference; i++) {
productsToSync.push({
uuid: cartItem.product.uuid,
});
}
}
}
if (productsToSync.length === 0) {
cookieCart.value = [];
return;
}
try {
await overwriteOrder({
type: 'bulk',
bulkAction: 'add',
isBulkSync: true,
products: productsToSync,
});
} catch (err) {
console.error('Failed to sync cart:', err);
}
}
return {
syncOrder,
};
}