CreateCustomerAddress
Implements CreateCustomerAddress
Unified Method.
Source
import { defineApi, getCurrentCustomer } from "@vsf-enterprise/unified-api-commercetools";
import { getNormalizers } from "@vsf-enterprise/unified-api-commercetools/udl";
export const createCustomerAddress = defineApi.createCustomerAddress(async (context, args) => {
const { address } = args;
const { version } = await getCurrentCustomer(context);
const { unnormalizeAddress, normalizeCustomerAddress } = getNormalizers(context);
await context.api.addShippingAddress({
address: unnormalizeAddress(address),
user: { version },
});
const meUpdated = await context.api.getMe({ customer: true });
const createdAddress = meUpdated.me.customer?.shippingAddresses.at(0);
if (!createdAddress) {
throw new Error("Address not created");
}
return {
address: normalizeCustomerAddress(createdAddress),
};
});