All files / src/api/placeOrder index.ts

33.33% Statements 5/15
0% Branches 0/2
0% Functions 0/2
41.67% Lines 5/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 5346x     46x           46x                                 46x                                                 46x  
import updateCart from '../updateCart';
import { CartResponse, OrderResponse } from '../../types/Api';
import { Cart, Address } from '../../types/GraphQL';
import {
  setShippingAddressAction,
  setShippingMethodAction,
  setBillingAddressAction,
  addPaymentAction
} from '../../helpers/cart/actions';
import createMyOrderFromCart from './../createMyOrderFromCart';
import { CartUpdateAction } from '../../types/GraphQL';
 
interface Order {
  shippingDetails: Address;
  billingDetails: Address;
  shippingMethod: string;
  paymentMethod?: string;
}
 
/* istanbul ignore file */
 
interface PlaceOrderResponse {
  cartResponse: CartResponse;
  orderResponse: OrderResponse;
}
 
const placeOrder = async (cart: Cart, order: Order): Promise<PlaceOrderResponse> => {
  const actions: CartUpdateAction[] = [
    setShippingAddressAction(order.shippingDetails as any),
    setShippingMethodAction(order.shippingMethod),
    setBillingAddressAction(order.billingDetails)
  ];
 
  if (order.paymentMethod) {
    actions.push(addPaymentAction(order.paymentMethod));
  }
 
  const cartResponse = await updateCart({
    id: cart.id,
    version: cart.version,
    actions
  });
 
  const { id, version } = cartResponse.data.cart;
  const orderResponse = await createMyOrderFromCart({ id,
    version });
 
  return { cartResponse,
    orderResponse };
};
 
export default placeOrder;