|
1 | 1 | import { cookies } from 'next/headers';
|
2 | 2 | import { redirect } from 'next/navigation';
|
| 3 | +import 'server-only'; |
3 | 4 |
|
4 | 5 | type RequestBody = {
|
5 | 6 | query: string;
|
6 | 7 | };
|
7 | 8 |
|
| 9 | +type WishlistBody = { |
| 10 | + id: string; |
| 11 | + content: { wishlist_ids: string[] }; |
| 12 | +}; |
| 13 | + |
8 | 14 | const SWELL_STORE_ID = process.env.SWELL_STORE_ID as string;
|
| 15 | +const SWELL_SECRET_KEY = process.env.SWELL_SECRET_KEY as string; |
9 | 16 |
|
10 | 17 | /*****************************************************************************
|
11 | 18 | * Extract Swell session cookie
|
@@ -41,6 +48,26 @@ const makeRequest = async (path: string, method = 'GET', body?: RequestBody) =>
|
41 | 48 | return response.json();
|
42 | 49 | };
|
43 | 50 |
|
| 51 | +/***************************************************************************** |
| 52 | + * Make Admin API request to Swell using secret key |
| 53 | + ****************************************************************************/ |
| 54 | +const makeAdminRequest = async (path: string, method = 'GET', body?: unknown) => { |
| 55 | + const requestHeaders: HeadersInit = new Headers(); |
| 56 | + requestHeaders.set('Content-Type', 'application/json'); |
| 57 | + requestHeaders.set( |
| 58 | + 'Authorization', |
| 59 | + `Basic ${Buffer.from(`${SWELL_STORE_ID}:${SWELL_SECRET_KEY}`).toString('base64')}` |
| 60 | + ); |
| 61 | + |
| 62 | + const response = await fetch(`https://api.swell.store${path}`, { |
| 63 | + method: method, |
| 64 | + headers: requestHeaders, |
| 65 | + body: JSON.stringify(body) |
| 66 | + }); |
| 67 | + |
| 68 | + return response.json(); |
| 69 | +}; |
| 70 | + |
44 | 71 | /*****************************************************************************
|
45 | 72 | * Get required data in a single GraphQL request
|
46 | 73 | ****************************************************************************/
|
@@ -173,3 +200,55 @@ const getCards = async () => {
|
173 | 200 |
|
174 | 201 | return response?.results;
|
175 | 202 | };
|
| 203 | + |
| 204 | +/***************************************************************************** |
| 205 | + * Get logged user wishlist products ids |
| 206 | + ****************************************************************************/ |
| 207 | +const getWishlistIds = async (): Promise<string[]> => { |
| 208 | + const { content } = (await makeRequest('/api/account')) as WishlistBody; |
| 209 | + return content.wishlist_ids; |
| 210 | +}; |
| 211 | + |
| 212 | +/***************************************************************************** |
| 213 | + * Get logged user wishlist |
| 214 | + ****************************************************************************/ |
| 215 | +export const getWishlist = async (): Promise<Product[]> => { |
| 216 | + const productsIds = await getWishlistIds(); |
| 217 | + |
| 218 | + if (productsIds.length === 0) { |
| 219 | + return []; |
| 220 | + } |
| 221 | + |
| 222 | + const { results } = (await makeRequest( |
| 223 | + `/api/products?where[id][$in]=${productsIds.join(',')}` |
| 224 | + )) as { results: Product[] }; |
| 225 | + |
| 226 | + return results; |
| 227 | +}; |
| 228 | + |
| 229 | +/***************************************************************************** |
| 230 | + * Check if product is in logged user wishlist |
| 231 | + ****************************************************************************/ |
| 232 | +export const isProductInWishlist = async (productId: string): Promise<boolean> => { |
| 233 | + const productsIds = await getWishlistIds(); |
| 234 | + return productsIds.includes(productId); |
| 235 | +}; |
| 236 | + |
| 237 | +/***************************************************************************** |
| 238 | + * Add product to logged user wishlist |
| 239 | + ****************************************************************************/ |
| 240 | +export const toggleWishlist = async (productId: string): Promise<string[]> => { |
| 241 | + const { id, content } = (await makeRequest('/api/account')) as WishlistBody; |
| 242 | + |
| 243 | + // Add or remove product depending on if it's already in the wishlist |
| 244 | + const wishlistIds = content.wishlist_ids.includes(productId) |
| 245 | + ? content.wishlist_ids.filter((id) => id !== productId) |
| 246 | + : [...content.wishlist_ids, productId]; |
| 247 | + |
| 248 | + // Overwrite wishlist with new list of products |
| 249 | + const wishlist = (await makeAdminRequest(`/accounts/${id}`, 'PUT', { |
| 250 | + $set: { content: { wishlist_ids: wishlistIds } } |
| 251 | + })) as WishlistBody; |
| 252 | + |
| 253 | + return wishlist.content.wishlist_ids; |
| 254 | +}; |
0 commit comments