CreateCustomerAddress
Implements CreateCustomerAddress 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 { CustomerAddress, CustomerAddressInput } from "@vsf-enterprise/unified-api-magento/ecommerceTypes";
import { resolveRegionId } from "@vsf-enterprise/unified-api-magento";
export const createCustomerAddress = defineApi.createCustomerAddress(async (context, args) => {
const { api } = await context.getApiClient();
await assertAuthorized(context);
const { address } = args;
const { normalizeCustomerAddress, unnormalizeAddress } = getNormalizers(context);
const selectedRegion = await resolveRegionId(context, address);
const createdAddress = await query(
api.createCustomerAddress({
...unnormalizeAddress(address),
region: {
region_id: selectedRegion.id,
},
} as CustomerAddressInput),
);
if (!createdAddress?.createCustomerAddress) {
throw context.createHttpError({
message: "Address not created",
statusCode: HttpStatusCode.INTERNAL_SERVER_ERROR,
});
}
return {
address: normalizeCustomerAddress(createdAddress.createCustomerAddress as CustomerAddress),
};
});