Vue Storefront is now Alokai! Learn More
UpdateCustomerAddress

UpdateCustomerAddress

Implements UpdateCustomerAddress Unified Method.

Source

import { assertAuthorized, defineApi } from "@vsf-enterprise/unified-api-sapcc";
import { getNormalizers } from "@vsf-enterprise/unified-api-sapcc/udl";
import { Address } from "@vsf-enterprise/sapcc-types";


declare module "@vsf-enterprise/unified-api-sapcc" {
  interface UpdateCustomerAddressExtendedArgs {
    /**
     * Response configuration. List of fields returned in the response body.
     */
    fields?: "BASIC" | "DEFAULT" | "FULL" | string | string[];
    /**
     * The additional address fields to be included in updated address.
     * */
    address?: Partial<
      Omit<
        Address,
        | "title"
        | "firstName"
        | "lastName"
        | "postalCode"
        | "country"
        | "town"
        | "line1"
        | "line2"
        | "phone"
        | "district"
        | "addressId"
      >
    >;
  }
}


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

  await context.api.updateAddress({
    addressId: id,
    address: unnormalizeAddress(address),
  });
  const { data: updatedAddress } = await context.api.getAddress({ addressId: id });

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