Vue Storefront is now Alokai! Learn More
UpdateCustomer

UpdateCustomer

Implements UpdateCustomer Unified Method.

Source

import { assertAuthorized, defineApi, query } from "@vsf-enterprise/unified-api-magento";
import type { Customer } from "@vue-storefront/magento-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const updateCustomer = defineApi.updateCustomer(async (context, args) => {
  await assertAuthorized(context);

  const { email, firstName, lastName } = args;
  const { normalizeCustomer } = getNormalizers(context);

  if (email) {
    // Magento2 API require password to update email
    throw { statusCode: 400, message: "Updating email is not supported" };
  }

  const user = await query(
    context.api.updateCustomer({
      firstname: firstName,
      lastname: lastName,
    }),
  );

  return {
    customer: normalizeCustomer(user.updateCustomerV2?.customer as Customer),
  };
});