UpdateCustomerAddress
Implements UpdateCustomerAddress Unified Method.
Source
import { getNormalizers } from "@alokai/connect/integration-kit";
import { HttpStatusCode } from "@alokai/connect/middleware";
import { defineApi, getCurrentCustomer } from "@vsf-enterprise/unified-api-commercetools";
export const updateCustomerAddress = defineApi.updateCustomerAddress(async (context, args) => {
const { address, id } = args;
const { defaultShippingAddressId, version } = await getCurrentCustomer(context);
const { normalizeCustomerAddress, unnormalizeAddress } = getNormalizers(context);
const { api } = await context.getApiClient();
await api.updateShippingAddress({
address: { id, ...unnormalizeAddress(address) },
// seems that there is an issue with types for defaultShippingAddressId, it shouldn't be required
user: { defaultShippingAddressId: defaultShippingAddressId!, version },
});
const customerUpdated = await getCurrentCustomer(context);
const updatedAddress = customerUpdated.shippingAddresses.find((address) => address.id === id);
if (!updatedAddress) {
throw context.createHttpError({
message: "Address not found",
statusCode: HttpStatusCode.NOT_FOUND,
});
}
return {
address: normalizeCustomerAddress(updatedAddress),
};
});