Skip to content

Commit

Permalink
Merge pull request #210 from vtex/fix/chk-2929
Browse files Browse the repository at this point in the history
[CHK-3296]: Previne múltiplos Pickups Points para items de sellers whitelabel
  • Loading branch information
vmarcosp authored Jan 4, 2024
2 parents d32927d + 89a322a commit 8e802db
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
70 changes: 53 additions & 17 deletions react/fetchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down Expand Up @@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions react/utils/DeliveryChannelsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ export function isDelivery(deliveryChannelSource) {

return deliveryChannel === DELIVERY
}

export function getFirstItemWithSelectedDelivery(logisticsInfo) {
return logisticsInfo.find((li) => isDelivery(li))
}

0 comments on commit 8e802db

Please sign in to comment.