|
| 1 | +import { jsonToGraphQLQuery } from 'json-to-graphql-query'; |
| 2 | +import { ClientInterface } from './client'; |
| 3 | +import { Customer } from '../types/customer'; |
| 4 | + |
| 5 | +type Deps = { |
| 6 | + apiClient: ClientInterface; |
| 7 | +}; |
| 8 | + |
| 9 | +export const placeCart = async (cartId: string, { apiClient }: Deps, extraQuery?: any): Promise<void> => { |
| 10 | + try { |
| 11 | + const mutation = { |
| 12 | + place: { |
| 13 | + __args: { |
| 14 | + id: cartId, |
| 15 | + }, |
| 16 | + id: true, |
| 17 | + ...extraQuery, |
| 18 | + }, |
| 19 | + }; |
| 20 | + const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation })); |
| 21 | + return response.place; |
| 22 | + } catch (exception: any) { |
| 23 | + console.error(`Failed to place cart ${cartId}`, exception.message); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +export const addSkuItem = async ( |
| 28 | + cartId: string, |
| 29 | + sku: string, |
| 30 | + quantity: number, |
| 31 | + { apiClient }: Deps, |
| 32 | + extraQuery?: any, |
| 33 | +) => { |
| 34 | + try { |
| 35 | + const mutation = { |
| 36 | + addSkuItem: { |
| 37 | + __args: { |
| 38 | + id: cartId, |
| 39 | + input: { |
| 40 | + sku, |
| 41 | + quantity, |
| 42 | + }, |
| 43 | + }, |
| 44 | + id: true, |
| 45 | + ...extraQuery, |
| 46 | + }, |
| 47 | + }; |
| 48 | + const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation })); |
| 49 | + return response.addSkuItem; |
| 50 | + } catch (exception: any) { |
| 51 | + console.error(`Failed to add sku item for cart ${cartId}`, exception.message); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +export const removeCartItem = async ( |
| 56 | + cartId: string, |
| 57 | + sku: string, |
| 58 | + quantity: number, |
| 59 | + { apiClient }: Deps, |
| 60 | + extraQuery?: any, |
| 61 | +) => { |
| 62 | + try { |
| 63 | + const mutation = { |
| 64 | + removeCartItem: { |
| 65 | + __args: { |
| 66 | + id: cartId, |
| 67 | + sku, |
| 68 | + quantity, |
| 69 | + }, |
| 70 | + id: true, |
| 71 | + ...extraQuery, |
| 72 | + }, |
| 73 | + }; |
| 74 | + const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation })); |
| 75 | + return response.removeCartItem; |
| 76 | + } catch (exception: any) { |
| 77 | + console.error(`Failed to remove from cart ${cartId}`, exception.message); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +export const setCartMeta = async ( |
| 82 | + cartId: string, |
| 83 | + meta: Array<{ |
| 84 | + key: string; |
| 85 | + value: string; |
| 86 | + }>, |
| 87 | + merge: Boolean, |
| 88 | + { apiClient }: Deps, |
| 89 | + extraQuery?: any, |
| 90 | +): Promise<void> => { |
| 91 | + try { |
| 92 | + const mutation = { |
| 93 | + setMeta: { |
| 94 | + __args: { |
| 95 | + id: cartId, |
| 96 | + merge, |
| 97 | + meta, |
| 98 | + }, |
| 99 | + id: true, |
| 100 | + ...extraQuery, |
| 101 | + }, |
| 102 | + }; |
| 103 | + const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation })); |
| 104 | + return response.setMeta; |
| 105 | + } catch (exception: any) { |
| 106 | + console.error(`Failed to set meta for ${cartId}`, exception.message); |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +export const setCartCustomer = async ( |
| 111 | + cartId: string, |
| 112 | + customer: Customer, |
| 113 | + isGuest: boolean, |
| 114 | + { apiClient }: Deps, |
| 115 | + extraQuery?: any, |
| 116 | +): Promise<void> => { |
| 117 | + try { |
| 118 | + const mutation = { |
| 119 | + setCustomer: { |
| 120 | + __args: { |
| 121 | + id: cartId, |
| 122 | + input: { |
| 123 | + isGuest, |
| 124 | + ...customer, |
| 125 | + }, |
| 126 | + }, |
| 127 | + id: true, |
| 128 | + ...extraQuery, |
| 129 | + }, |
| 130 | + }; |
| 131 | + const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation })); |
| 132 | + return response.setCustomer; |
| 133 | + } catch (exception: any) { |
| 134 | + console.error('Failed to update customer', exception.message); |
| 135 | + } |
| 136 | +}; |
0 commit comments