Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 11x 14x 14x 14x 14x 14x 8x 8x 8x 14x 6x 6x 6x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 14x 10x 10x 11x 7x 7x 11x 4x 4x 10x 10x 10x 10x 11x 11x 11x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 15x 15x 5x 5x 10x 10x 10x 10x 10x 10x 10x 4x 10x 10x | /**
* Cart Merge Utilities (Client-Safe)
*
* Pure functions for cart merging logic that don't require database access.
* These can be safely imported on the client side.
*/
import { CartItem } from '@/redux/features/cartSlice';
export interface MergeResult {
merged: CartItem[];
conflicts: ConflictItem[];
action: 'merge' | 'replace' | 'skip';
message: string;
}
export interface ConflictItem {
productId: number;
anonymousQuantity: number;
userQuantity: number;
resolution: 'sum' | 'user' | 'anonymous';
finalQuantity: number;
}
/**
* Database cart item type
*/
interface DbCartItem {
quantity: number;
product: {
id: number;
title: string;
price: number;
discountedPrice: number;
images?: Array<{
url: string;
thumbnailUrl: string;
}>;
};
}
/**
* Merge anonymous cart into user's database cart
*
* Rules:
* 1. If item doesn't exist in user cart → add it
* 2. If item exists → sum quantities (default behavior)
* 3. Track conflicts for user notification
*
* @param anonymousItems - Cart items from cookie (anonymous user)
* @param userItems - Cart items from database (logged-in user)
* @returns MergeResult with merged items and conflict details
*/
export function mergeAnonymousCartToUser(
anonymousItems: CartItem[],
userItems: CartItem[]
): MergeResult {
if (!anonymousItems || anonymousItems.length === 0) {
return {
merged: userItems,
conflicts: [],
action: 'skip',
message: 'No anonymous items to merge'};
}
const merged: CartItem[] = [...userItems];
const conflicts: ConflictItem[] = [];
let addedCount = 0;
let mergedCount = 0;
for (const anonItem of anonymousItems) {
const existingIndex = merged.findIndex(
(item) => item.id === anonItem.id
);
if (existingIndex === -1) {
// Item doesn't exist in user cart → add it
merged.push(anonItem);
addedCount++;
} else {
// Item exists → merge quantities
const existingItem = merged[existingIndex];
if (existingItem.quantity !== anonItem.quantity) {
// Conflict detected - different quantities
const finalQuantity = existingItem.quantity + anonItem.quantity;
conflicts.push({
productId: anonItem.id,
anonymousQuantity: anonItem.quantity,
userQuantity: existingItem.quantity,
resolution: 'sum',
finalQuantity});
// Apply resolution: sum quantities
existingItem.quantity = finalQuantity;
mergedCount++;
}
}
}
let message = `Cart merged successfully. `;
if (addedCount > 0) {
message += `${addedCount} new item(s) added. `;
}
if (mergedCount > 0) {
message += `${mergedCount} item(s) merged with existing cart.`;
}
return {
merged,
conflicts,
action: conflicts.length > 0 || addedCount > 0 ? 'merge' : 'skip',
message: message.trim()};
}
/**
* Convert database cart items to frontend CartItem format
*/
export function transformDbCartToCartItems(dbCartItems: DbCartItem[]): CartItem[] {
return dbCartItems.map((item) => ({
id: item.product.id,
title: item.product.title,
price: item.product.price,
discountedPrice: item.product.discountedPrice,
quantity: item.quantity,
imgs: {
thumbnails: item.product.images?.map((img) => img.thumbnailUrl || img.url) || [],
previews: item.product.images?.map((img) => img.url) || []}}));
}
/**
* Validate cart items before merging
* Ensures all items have valid product IDs and quantities
*/
export function validateCartItems(items: CartItem[]): boolean {
if (!Array.isArray(items)) {
return false;
}
return items.every(
(item) =>
item.id &&
typeof item.id === 'number' &&
item.quantity &&
typeof item.quantity === 'number' &&
item.quantity > 0
);
}
|