Skip to content

Commit

Permalink
Refactor UserAvailabilityServiceGenerate to streamline product option…
Browse files Browse the repository at this point in the history
… handling
  • Loading branch information
jamalsoueidan committed Jun 2, 2024
1 parent ebfead3 commit 5201025
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/functions/user/services/availability/generate.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -114,9 +115,9 @@ export const UserAvailabilityServiceGenerate = async (
});
}
});
return tempProducts;
return products;
},
schedule.products
[] as UserScheduleServiceGetWithCustomerResponse["products"]
);
}

Expand Down

0 comments on commit 5201025

Please sign in to comment.