refactor(CheckoutView): dedup code in getAgencies/getWarehouses

This commit is contained in:
taro 2025-04-09 02:51:37 -03:00
parent bddfb74bd8
commit d67f1f2427
1 changed files with 24 additions and 34 deletions
src/pages/Ecomerce

View File

@ -223,25 +223,30 @@ const getOrder = async (orderId) => {
return data;
};
const getAgenciesInZone = async () => {
const agenciesInZone = await api.get('Agencies/landsThatDay', {
params: {
addressFk: orderForm.value.address,
landed: new Date(orderForm.value.date),
}
});
const deliveryMethods = await api.get('DeliveryMethods');
return agenciesInZone.data
.filter(agency => agency.isVisible)
.map(agency => ({
id: agency.agencyModeFk,
description: agency.description,
deliveryMethod: deliveryMethods.data.find(dm => dm.id === agency.deliveryMethodFk).code,
}))
.toSorted((a, b) => a.description.localeCompare(b.description));
};
const getAgencies = async () => {
try {
const agenciesInZone = await api.get('Agencies/landsThatDay', {
params: {
addressFk: orderForm.value.address,
landed: new Date(orderForm.value.date),
}
});
const deliveryMethods = await api.get('DeliveryMethods');
const results = agenciesInZone.data
.filter(agency => agency.isVisible)
.map(agency => ({
id: agency.agencyModeFk,
description: agency.description,
deliveryMethod: deliveryMethods.data.find(dm => dm.id === agency.deliveryMethodFk).code,
}))
.filter(agency => agency.deliveryMethod === 'AGENCY' || agency.deliveryMethod === 'DELIVERY')
.toSorted((a, b) => a.description.localeCompare(b.description));
const agenciesInZone = await getAgenciesInZone();
const results = agenciesInZone
.filter(agency => agency.deliveryMethod === 'AGENCY' || agency.deliveryMethod === 'DELIVERY');
agencies.value = results;
@ -260,23 +265,8 @@ const getAgencies = async () => {
const getWarehouses = async () => {
try {
const agenciesInZone = await api.get('Agencies/landsThatDay', {
params: {
addressFk: orderForm.value.address,
landed: new Date(orderForm.value.date),
}
});
const deliveryMethods = await api.get('DeliveryMethods');
const results = agenciesInZone.data
.filter(agency => agency.isVisible)
.map(agency => ({
id: agency.agencyModeFk,
description: agency.description,
deliveryMethod: deliveryMethods.data.find(dm => dm.id === agency.deliveryMethodFk).code,
}))
.filter(agency => agency.deliveryMethod === 'PICKUP')
.toSorted((a, b) => a.description.localeCompare(b.description));
const agenciesInZone = await getAgenciesInZone();
const results = agenciesInZone.filter(agency => agency.deliveryMethod === 'PICKUP');
warehouses.value = results;