| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- "use server";
- import {cookies} from "next/headers";
- import { GUEST_CART_ID, GUEST_CART_TOKEN, IS_GUEST } from "@/utils/constants";
- import { bagistoFetch } from "@utils/bagisto";
- import { CREATE_MERGE_CART } from "@/graphql";
- import { CreateMergeCartData } from "@/types/cart/type";
- export async function mergeCartAction(){
- const cookieStore = await cookies();
- const guestCartId = cookieStore.get(GUEST_CART_ID)?.value;
- cookieStore.delete(GUEST_CART_ID);
- cookieStore.delete(GUEST_CART_TOKEN);
- cookieStore.set(IS_GUEST,"false");
- if(!guestCartId){
-
- return {
- cartDetail: null,
- success:true,
- msg: ''
- };
- }
- try{
- // 调 Bagisto merge cart mutation
- const response = await bagistoFetch<{
- data: CreateMergeCartData,
- variables: {cartId:number}
- }>({
- query: CREATE_MERGE_CART,
- variables: {
- cartId: Number(guestCartId)
- },
- cache: "no-store",
- });
- // cookieStore.delete(GUEST_CART_ID);
- // cookieStore.delete(GUEST_CART_TOKEN);
- // cookieStore.set(IS_GUEST,"false");
- return {
- cartDetail: response.body.data.createMergeCart.mergeCart,
- success:true,
- msg: ''
- };
- } catch(e: any) {
- return {
- cartDetail: null,
- success:false,
- msg: e.message || 'Fail to merge cart.'
- };
- }
- }
|