Skip to content

Commit

Permalink
Refactor product option processing to use temporary array in UserAvai…
Browse files Browse the repository at this point in the history
…labilityServiceGenerate
  • Loading branch information
jamalsoueidan committed Jun 2, 2024
1 parent 073f7b3 commit ebfead3
Showing 1 changed file with 73 additions and 67 deletions.
140 changes: 73 additions & 67 deletions src/functions/user/services/availability/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,73 +45,79 @@ export const UserAvailabilityServiceGenerate = async (

const optionIds = body.optionIds ? body.optionIds : null;
if (optionIds) {
schedule.products = schedule.products.reduce((products, parentProduct) => {
parentProduct.options?.forEach((productOption) => {
const option = optionIds[parentProduct.productId];
if (!option && productOption.required) {
throw new NotFoundError([
{
path: ["optionIds", parentProduct.productId],
message: "MISSING_PARENT_ID",
code: "custom",
},
]);
}

const variantId = option[productOption.productId];
if (!variantId && productOption.required) {
throw new NotFoundError([
{
path: [
"optionIds",
parentProduct.productId,
productOption.productId,
],
message: `MISSING_PRODUCT_ID ${productOption.productId} in ${parentProduct.productId}`,
code: "custom",
},
]);
}

const variant = productOption.variants.find(
(v) => v.variantId === variantId
);

if (!variant && productOption.required) {
throw new NotFoundError([
{
path: [
"optionIds",
parentProduct.productId,
productOption.productId,
variantId,
],
message: "INCORRECT_VARIANT_ID",
code: "custom",
},
]);
}

if (variant) {
products.push({
variantId: variant.variantId,
duration: variant.duration.value,
productId: productOption.productId,
breakTime: 0,
bookingPeriod: {
unit: TimeUnit.MONTHS,
value: 12,
},
noticePeriod: {
unit: TimeUnit.HOURS,
value: 1,
},
parentId: parentProduct.productId,
});
}
});
return products;
}, schedule.products);
schedule.products = schedule.products.reduce(
(products, parentProduct, currentIndex) => {
let tempProducts = [...products];

parentProduct.options?.forEach((productOption) => {
const option = optionIds[parentProduct.productId];
if (!option && productOption.required) {
throw new NotFoundError([
{
path: ["optionIds", parentProduct.productId],
message: "MISSING_PARENT_ID",
code: "custom",
},
]);
}

const variantId = option[productOption.productId];
if (!variantId && productOption.required) {
throw new NotFoundError([
{
path: [
"optionIds",
parentProduct.productId,
productOption.productId,
],
message: `MISSING_PRODUCT_ID ${productOption.productId} in ${parentProduct.productId}`,
code: "custom",
},
]);
}

const variant = productOption.variants.find(
(v) => v.variantId === variantId
);

if (!variant && productOption.required) {
throw new NotFoundError([
{
path: [
"optionIds",
parentProduct.productId,
productOption.productId,
variantId,
],
message: "INCORRECT_VARIANT_ID",
code: "custom",
},
]);
}

if (variant) {
const insertIndex = tempProducts.length; // Append to the end initially
tempProducts.splice(insertIndex, 0, {
variantId: variant.variantId,
duration: variant.duration.value,
productId: productOption.productId,
breakTime: 0,
bookingPeriod: {
unit: TimeUnit.MONTHS,
value: 12,
},
noticePeriod: {
unit: TimeUnit.HOURS,
value: 1,
},
parentId: parentProduct.productId,
});
}
});
return tempProducts;
},
schedule.products
);
}

let shipping: Awaited<ReturnType<typeof ShippingServiceGet>> | undefined;
Expand Down

0 comments on commit ebfead3

Please sign in to comment.