Skip to content

Commit

Permalink
fix: update order error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Feb 9, 2024
1 parent 0837944 commit bf3e5b5
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions packages/common/src/controllers/CheckoutController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
Payment,
PaymentMethod,
SwitchOffer,
UpdateOrderPayload,
} from '../../types/checkout';
import CheckoutService from '../services/integrations/CheckoutService';
import SubscriptionService from '../services/integrations/SubscriptionService';
Expand Down Expand Up @@ -73,36 +72,33 @@ export default class CheckoutController {
};

updateOrder = async (order: Order, paymentMethodId?: number, couponCode?: string | null): Promise<void> => {
const updateOrderPayload: UpdateOrderPayload = {
order,
paymentMethodId,
couponCode,
};
let response;

try {
const response = await this.checkoutService.updateOrder(updateOrderPayload);

if (response.errors.length > 0) {
// clear the order when the order doesn't exist on the server
if (response.errors[0].includes(`Order with ${order.id} not found`)) {
useCheckoutStore.getState().setOrder(null);
}
response = await this.checkoutService.updateOrder({ order, paymentMethodId, couponCode });
} catch (error: unknown) {
// TODO: we currently (falsely) assume that the only error caught is because the coupon is not valid, but there
// could be a network failure as well (JWPCheckoutService)
throw new FormValidationError({ couponCode: [i18next.t('account:checkout.coupon_not_valid')] });
}

throw new FormValidationError({ order: [response.errors[0]] });
if (response.errors.length > 0) {
// clear the order when the order doesn't exist on the server
if (response.errors[0].includes(`Order with ${order.id} not found`)) {
useCheckoutStore.getState().setOrder(null);
}

if (response.responseData?.order) {
useCheckoutStore.getState().setOrder(response.responseData?.order);
}
} catch (error: unknown) {
if (error instanceof FormValidationError) {
throw error;
// TODO: this handles the `Coupon ${couponCode} not found` message (CleengCheckoutService)
if (response.errors[0].includes(`not found`)) {
throw new FormValidationError({ couponCode: [i18next.t('account:checkout.coupon_not_valid')] });
}

throw new FormValidationError({ couponCode: [i18next.t('account:checkout.coupon_not_valid')] });
throw new FormValidationError({ form: response.errors });
}

return;
if (response.responseData.order) {
useCheckoutStore.getState().setOrder(response.responseData?.order);
}
};

getPaymentMethods = async (): Promise<PaymentMethod[]> => {
Expand Down

0 comments on commit bf3e5b5

Please sign in to comment.