diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2024-01-13 10:35:22 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2024-01-13 10:35:22 +0700 |
| commit | f62b2345f463695ef0f8f79830cd76b6e0332821 (patch) | |
| tree | c06ff12a8312e3a02b0203f588db0f4da044c911 /src-migrate/services/product.ts | |
| parent | ee0b5893ac039ab05fe8247647364a923d707da3 (diff) | |
Refactor src migrate folder
Diffstat (limited to 'src-migrate/services/product.ts')
| -rw-r--r-- | src-migrate/services/product.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src-migrate/services/product.ts b/src-migrate/services/product.ts new file mode 100644 index 00000000..c9e93396 --- /dev/null +++ b/src-migrate/services/product.ts @@ -0,0 +1,59 @@ +import { IProduct, IProductDetail } from '~/types/product'; +import snakeCase from 'snakecase-keys'; + +const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST; + +export const getProductById = async ( + id: string, + tier: string +): Promise<IProductDetail | null> => { + const url = `${SELF_HOST}/api/shop/product-detail`; + const params = new URLSearchParams({ id, auth: tier }); + return await fetch(`${url}?${params.toString()}`) + .then((res) => res.json()) + .then((res) => { + if (res.length > 0) return snakeCase(res[0]) as IProductDetail; + + return null; + }); +}; + +export interface GetProductSimilarProps { + name: string; + except?: { + productId?: number; + manufactureId?: number; + }; + limit?: number; +} + +export interface GetProductSimilarRes { + products: IProduct[]; + num_found: number; + num_found_exact: boolean; + start: number; +} + +export const getProductSimilar = async ({ + name, + except, + limit = 30, +}: GetProductSimilarProps): Promise<GetProductSimilarRes> => { + const query = [ + `q=${name}`, + 'page=1', + 'orderBy=popular-weekly', + 'operation=OR', + 'priceFrom=1', + ]; + + if (except?.productId) query.push(`fq=-product_id_i:${except.productId}`); + if (except?.manufactureId) + query.push(`fq=-manufacture_id_i:${except.manufactureId}`); + + const url = `${SELF_HOST}/api/shop/search?${query.join('&')}`; + + return await fetch(url) + .then((res) => res.json()) + .then((res) => snakeCase(res.response)); +}; |
