Vue Storefront is now Alokai! Learn More
CreateCustomerAddress

CreateCustomerAddress

Implements CreateCustomerAddress Unified Method.

Source

import { getNormalizers } from "@alokai/connect/integration-kit";
import { HttpStatusCode } from "@alokai/connect/middleware";

import { defineApi, getCurrentCustomer } from "@vsf-enterprise/unified-api-commercetools";

export const createCustomerAddress = defineApi.createCustomerAddress(async (context, args) => {
  const { address } = args;
  const { version } = await getCurrentCustomer(context);
  const { normalizeCustomerAddress, unnormalizeAddress } = getNormalizers(context);

  const { api } = await context.getApiClient();
  await api.addShippingAddress({
    address: unnormalizeAddress(address),
    user: { version },
  });
  const meUpdated = await api.getMe({ customer: true });
  const createdAddress = meUpdated.me.customer?.shippingAddresses.at(0);

  if (!createdAddress) {
    throw context.createHttpError({
      message: "Address not created",
      statusCode: HttpStatusCode.BAD_REQUEST,
    });
  }

  return {
    address: normalizeCustomerAddress(createdAddress),
  };
});