SetCartAddress
Implements SetCartAddress
Unified Method.
Source
import { getCartId, defineApi } from "@vsf-enterprise/unified-api-sapcc";
import {
type SfCreateAddressBody,
type SfCustomerAddress,
getNormalizers,
} from "@vsf-enterprise/unified-api-sapcc/udl";
import { Address } from "@vsf-enterprise/sapcc-types";
declare module "@vsf-enterprise/unified-api-sapcc" {
interface SetCartAddressExtendedArgs {
/**
* Response configuration. List of fields returned in the response body.
*/
fields?: "BASIC" | "DEFAULT" | "FULL" | string | string[];
/**
* The additional address fields to be included in created address.
* */
address?: Omit<
Address,
| "title"
| "firstName"
| "lastName"
| "postalCode"
| "country"
| "town"
| "line1"
| "line2"
| "phone"
| "district"
>;
}
}
import type { Address, RequiredAddressProps } from "@vsf-enterprise/sapcc-types";
function isSfCustomerAddress(
address: SfCustomerAddress | SfCreateAddressBody,
): address is SfCustomerAddress {
return "id" in address;
}
export const setCartAddress = defineApi.setCartAddress(async (context, args) => {
const cartId = getCartId(context);
const { shippingAddress } = args;
const { normalizeCart, unnormalizeAddress } = getNormalizers(context);
if (isSfCustomerAddress(shippingAddress)) {
await context.api.replaceCartDeliveryAddress({
addressId: shippingAddress.id,
cartId,
});
} else {
const address = unnormalizeAddress(shippingAddress) as Address & RequiredAddressProps;
await context.api.createCartDeliveryAddress({
address,
cartId,
});
}
const { data: cart } = await context.api.getCart({ cartId });
return normalizeCart(cart);
});