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