Vue Storefront is now Alokai! Learn More
UpdateCustomer

UpdateCustomer

Implements UpdateCustomer Unified Method.

Source

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


declare module "@vsf-enterprise/unified-api-sapcc" {
  interface UpdateCustomerExtendedArgs {
    /**
     * Response configuration. List of fields returned in the response body.
     */
    fields?: "BASIC" | "DEFAULT" | "FULL" | string | string[];
    /**
     * The additional fields to be updated in the customer.
     */
    user?: Partial<Omit<User, "firstName" | "lastName" | "uid">>;
  }
}


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

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

  await context.api.updateUser({
    user: {
      firstName,
      lastName,
      uid: email,
    },
  });

  const { data: user } = await context.api.getUser({});

  return {
    customer: normalizeCustomer(user),
  };
});