import { getAddedCartItems, getVariant, totalValue } from '../../utils/cart'; import { pushDatalayer } from '../../utils/pushDatalayer'; import type { UpsertTrackingCartRequest } from '../../types'; import type { CartResponse } from '../../../Api/types'; export function upsertToCartAction( upsertPayload: UpsertTrackingCartRequest, cartPayload: CartResponse, ) { if (!getAddedCartItems(cartPayload, upsertPayload).length) return; if ( upsertPayload.items.some(item => (item.oldQuantity || 0) > item.quantity!) ) { pushDatalayer({ event: 'remove_from_cart', ecommerce: { currency: 'BRL', value: totalValue(cartPayload, upsertPayload), items: getAddedCartItems(cartPayload, upsertPayload).map(item => { return { item_category: '', item_list_name: '', item_brand: `${item.product.brandId}`, item_id: item.product.productId, coupon: cartPayload.discountMap.coupon?.discountMainCode || '', item_name: item.product.name, item_variant: getVariant(item), price: item.unitValue, quantity: item.oldQuantity! - item.quantity, }; }), }, }); return; } pushDatalayer({ event: 'add_to_cart', ecommerce: { currency: 'BRL', value: totalValue(cartPayload, upsertPayload), items: getAddedCartItems(cartPayload, upsertPayload).map(item => { return { item_category: '', item_list_name: '', item_brand: `${item.product.brandId}`, item_id: item.product.productId, coupon: cartPayload.discountMap.coupon?.discountMainCode || '', item_name: item.product.name, item_variant: getVariant(item), price: item.unitValue, quantity: item.oldQuantity ? item.quantity - item.oldQuantity : item.quantity, }; }), }, }); }