Vue Storefront is now Alokai! Learn More
UpdateCustomerAddress

UpdateCustomerAddress

Implements UpdateCustomerAddress Unified Method.

Source

import { assertAuthorized, defineApi, query } from "@vsf-enterprise/unified-api-magento";
import { getNormalizers } from "@vsf-enterprise/unified-api-magento/udl";
import { resolveRegionId } from "@vsf-enterprise/unified-api-magento";
import type { CustomerAddressInput } from "@vsf-enterprise/unified-api-magento/ecommerceTypes";

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

  const { unnormalizeAddress, normalizeCustomerAddress } = getNormalizers(context);

  const selectedRegion = await resolveRegionId(context, address);

  const updatedAddress = await query(
    context.api.updateCustomerAddress({
      id: Number.parseInt(id),
      input: {
        ...unnormalizeAddress(address),
        region: { region_id: selectedRegion.id },
      } as CustomerAddressInput,
    }),
  );

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

  return {
    address: normalizeCustomerAddress(updatedAddress.updateCustomerAddress as CustomerAddressInput),
  };
});