mergeCartAction.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use server";
  2. import {cookies} from "next/headers";
  3. import { GUEST_CART_ID, GUEST_CART_TOKEN, IS_GUEST } from "@/utils/constants";
  4. import { bagistoFetch } from "@utils/bagisto";
  5. import { CREATE_MERGE_CART } from "@/graphql";
  6. import { CreateMergeCartData } from "@/types/cart/type";
  7. export async function mergeCartAction(){
  8. const cookieStore = await cookies();
  9. const guestCartId = cookieStore.get(GUEST_CART_ID)?.value;
  10. cookieStore.delete(GUEST_CART_ID);
  11. cookieStore.delete(GUEST_CART_TOKEN);
  12. cookieStore.set(IS_GUEST,"false");
  13. if(!guestCartId){
  14. return {
  15. cartDetail: null,
  16. success:true,
  17. msg: ''
  18. };
  19. }
  20. try{
  21. // 调 Bagisto merge cart mutation
  22. const response = await bagistoFetch<{
  23. data: CreateMergeCartData,
  24. variables: {cartId:number}
  25. }>({
  26. query: CREATE_MERGE_CART,
  27. variables: {
  28. cartId: Number(guestCartId)
  29. },
  30. cache: "no-store",
  31. });
  32. // cookieStore.delete(GUEST_CART_ID);
  33. // cookieStore.delete(GUEST_CART_TOKEN);
  34. // cookieStore.set(IS_GUEST,"false");
  35. return {
  36. cartDetail: response.body.data.createMergeCart.mergeCart,
  37. success:true,
  38. msg: ''
  39. };
  40. } catch(e: any) {
  41. return {
  42. cartDetail: null,
  43. success:false,
  44. msg: e.message || 'Fail to merge cart.'
  45. };
  46. }
  47. }