Skip to content

Commit

Permalink
fix(core): Correctly filter out ineligible shipping methods
Browse files Browse the repository at this point in the history
Found a logic error (trying to use Array.filter on async function) which returns true for all shipping methods.
  • Loading branch information
michaelbromley committed Aug 9, 2019
1 parent bc860e0 commit 911463a
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,15 +19,16 @@ export class ShippingCalculator {
order: Order,
): Promise<Array<{ method: ShippingMethod; price: ShippingPrice }>> {
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);
}
}

0 comments on commit 911463a

Please sign in to comment.