diff --git a/CHANGELOG.md b/CHANGELOG.md index c37adfa..078220a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] + ### Fixed - Add rootPath to API requests. +- Prevent multiple pickup point selection for items from white label sellers. ## [3.8.1] - 2023-09-11 diff --git a/react/fetchers/index.js b/react/fetchers/index.js index 8c7e22a..42a93bb 100644 --- a/react/fetchers/index.js +++ b/react/fetchers/index.js @@ -2,8 +2,11 @@ import axios from 'axios' import axiosRetry from 'axios-retry' import { newAddress } from '../utils/newAddress' -import { PICKUP_IN_STORE, SEARCH } from '../constants' -import { isDelivery } from '../utils/DeliveryChannelsUtils' +import { DELIVERY, PICKUP_IN_STORE, SEARCH } from '../constants' +import { + getFirstItemWithSelectedDelivery, + isPickup, +} from '../utils/DeliveryChannelsUtils' axiosRetry(axios, { retries: 2 }) @@ -84,27 +87,60 @@ export function updateShippingData( addressType: SEARCH, }) + const logisticsInfoWithPickupSelected = logisticsInfo.map((li) => { + const hasPickupSla = li.slas.some((sla) => sla.id === pickupPoint.id) + const shouldKeepSelectedSla = !isPickup(li) + + return { + itemIndex: li.itemIndex, + slas: li.slas, + addressId: hasPickupSla + ? pickupAddressWithAddressId.addressId + : li.addressId, + selectedSla: hasPickupSla + ? pickupPoint.id + : shouldKeepSelectedSla + ? li.selectedSla + : null, + selectedDeliveryChannel: hasPickupSla + ? PICKUP_IN_STORE + : shouldKeepSelectedSla + ? li.selectedDeliveryChannel + : null, + } + }) + + const firstItemWithSelectedDelivery = getFirstItemWithSelectedDelivery( + logisticsInfoWithPickupSelected + ) + + const defaultDeliverySla = + firstItemWithSelectedDelivery && firstItemWithSelectedDelivery.selectedSla + + const logisticsInfoWithDefaultDeliverySla = + logisticsInfoWithPickupSelected.map((li) => { + const hasDefaultDeliverySla = li.slas.find( + (sla) => sla.id === defaultDeliverySla + ) + + if (!li.selectedDeliveryChannel && hasDefaultDeliverySla) { + return { + ...li, + selectedSla: defaultDeliverySla, + selectedDeliveryChannel: DELIVERY, + } + } + + return li + }) + const shippingData = { ...(hasGeocoordinates ? { clearAddressIfPostalCodeNotFound: false } : {}), selectedAddresses: [ ...(residentialAddress ? [residentialAddress] : []), pickupAddressWithAddressId, ], - logisticsInfo: logisticsInfo.map((li) => { - const hasSla = li.slas.some((sla) => sla.id === pickupPoint.id) - const hasDeliverySla = li.slas.some((sla) => isDelivery(sla)) - - return { - itemIndex: li.itemIndex, - addressId: hasSla ? pickupAddressWithAddressId.addressId : li.addressId, - selectedSla: hasSla ? pickupPoint.id : li.selectedSla, - selectedDeliveryChannel: hasSla - ? PICKUP_IN_STORE - : hasDeliverySla - ? li.selectedDeliveryChannel - : null, - } - }), + logisticsInfo: logisticsInfoWithDefaultDeliverySla, } return ( diff --git a/react/utils/DeliveryChannelsUtils.js b/react/utils/DeliveryChannelsUtils.js index a86ab1c..b70fd61 100644 --- a/react/utils/DeliveryChannelsUtils.js +++ b/react/utils/DeliveryChannelsUtils.js @@ -32,3 +32,7 @@ export function isDelivery(deliveryChannelSource) { return deliveryChannel === DELIVERY } + +export function getFirstItemWithSelectedDelivery(logisticsInfo) { + return logisticsInfo.find((li) => isDelivery(li)) +}