Vue Storefront is now Alokai! Learn More
UpdateCustomerAddress

UpdateCustomerAddress

Implements UpdateCustomerAddress Unified Method.

Source

import { assertAuthorized, defineApi } from "@vsf-enterprise/unified-api-bigcommerce";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const updateCustomerAddress = defineApi.updateCustomerAddress(async (context, args) => {
  await assertAuthorized(context);
  const { address, id } = args;
  const { unnormalizeAddress, normalizeCustomerAddress } = getNormalizers(context);

  const body = {
    id: Number(id),
    ...unnormalizeAddress(address),
  };

  const { data } = await context.api.updateCustomerAddress(body);
  const updatedAddress = data.at(0);

  if (!updatedAddress) {
    throw new Error("Address not updated");
  }

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