Vue Storefront is now Alokai! Learn More
UpdateCustomerAddress

UpdateCustomerAddress

Implements UpdateCustomerAddress Unified Method.

Source

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

import { getNormalizers } from "@vsf-enterprise/unified-api-commercetools/udl";

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

  await context.api.updateShippingAddress({
    address: { id, ...unnormalizeAddress(address) },
    // seems that there is an issue with types for defaultShippingAddressId, it shouldn't be required
    user: { version, defaultShippingAddressId: defaultShippingAddressId! },
  });
  const customerUpdated = await getCurrentCustomer(context);
  const updatedAddress = customerUpdated.shippingAddresses.find((address) => address.id === id);

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

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