From 911463a3e3bc8ad958c927984b98c6db510ff90a Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Fri, 9 Aug 2019 13:36:33 +0200 Subject: [PATCH] fix(core): Correctly filter out ineligible shipping methods Found a logic error (trying to use Array.filter on async function) which returns true for all shipping methods. --- .../shipping-calculator/shipping-calculator.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts b/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts index 086c9d297e..7eaaee8e77 100644 --- a/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts +++ b/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts @@ -1,5 +1,4 @@ import { Injectable } from '@nestjs/common'; -import { notNullOrUndefined } from '@vendure/common/lib/shared-utils'; import { RequestContext } from '../../../api/common/request-context'; import { ShippingPrice } from '../../../config/shipping-method/shipping-calculator'; @@ -20,15 +19,16 @@ export class ShippingCalculator { order: Order, ): Promise> { const shippingMethods = this.shippingMethodService.getActiveShippingMethods(ctx.channel); - const methodsPromiseArray = shippingMethods - .filter(async sm => await sm.test(order)) - .map(async method => { + const eligibleMethods: Array<{ method: ShippingMethod; price: ShippingPrice }> = []; + for (const method of shippingMethods) { + const eligible = await method.test(order); + if (eligible) { const price = await method.apply(order); if (price) { - return { method, price }; + eligibleMethods.push({ method, price }); } - }); - const methods = await Promise.all(methodsPromiseArray); - return methods.filter(notNullOrUndefined).sort((a, b) => a.price.price - b.price.price); + } + } + return eligibleMethods.sort((a, b) => a.price.price - b.price.price); } }