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 "@vue-storefront/unified-data-model";
import { resolveRegionId } from "../helpers/resolveRegionId";
import type { CustomerAddressInput } from "@vue-storefront/magento-types";

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),
  };
});