Vue Storefront is now Alokai! Learn More
UpdateCustomer

UpdateCustomer

Implements UpdateCustomer Unified Method.

Source

import { getNormalizers } from "@alokai/connect/integration-kit";
import { HttpStatusCode } from "@alokai/connect/middleware";

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

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

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

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

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

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