From 520102554ceff9e9d3fb69f22ab7328175664039 Mon Sep 17 00:00:00 2001 From: Jamal Soueidan Date: Mon, 3 Jun 2024 01:51:27 +0200 Subject: [PATCH] Refactor UserAvailabilityServiceGenerate to streamline product option handling --- .../user/services/availability/generate.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/functions/user/services/availability/generate.ts b/src/functions/user/services/availability/generate.ts index 3215eb89..6080d610 100644 --- a/src/functions/user/services/availability/generate.ts +++ b/src/functions/user/services/availability/generate.ts @@ -1,5 +1,8 @@ import { Types } from "mongoose"; -import { UserScheduleServiceGetWithCustomer } from "~/functions/user/services/schedule/get-with-customer"; +import { + UserScheduleServiceGetWithCustomer, + UserScheduleServiceGetWithCustomerResponse, +} from "~/functions/user/services/schedule/get-with-customer"; import { CustomerBlockedServiceRange } from "~/functions/customer/services/blocked/range"; import { ScheduleProduct, TimeUnit } from "~/functions/schedule"; @@ -47,8 +50,7 @@ export const UserAvailabilityServiceGenerate = async ( if (optionIds) { schedule.products = schedule.products.reduce( (products, parentProduct, currentIndex) => { - let tempProducts = [...products]; - + products.push(parentProduct); parentProduct.options?.forEach((productOption) => { const option = optionIds[parentProduct.productId]; if (!option && productOption.required) { @@ -96,8 +98,7 @@ export const UserAvailabilityServiceGenerate = async ( } if (variant) { - const insertIndex = tempProducts.length; // Append to the end initially - tempProducts.splice(insertIndex, 0, { + products.push({ variantId: variant.variantId, duration: variant.duration.value, productId: productOption.productId, @@ -114,9 +115,9 @@ export const UserAvailabilityServiceGenerate = async ( }); } }); - return tempProducts; + return products; }, - schedule.products + [] as UserScheduleServiceGetWithCustomerResponse["products"] ); }