summaryrefslogtreecommitdiff
path: root/src-migrate/services/product.ts
diff options
context:
space:
mode:
authortrisusilo48 <tri.susilo@altama.co.id>2024-07-10 15:58:51 +0700
committertrisusilo48 <tri.susilo@altama.co.id>2024-07-10 15:58:51 +0700
commit2e3c726bc8217f3960cfecec44b81303b03de72b (patch)
tree1b85ced7f61f3e4c3f1f27b577b37aa161615065 /src-migrate/services/product.ts
parent2b3bd9c0a454dbad69ce29cee877bfb1fca5dfa6 (diff)
parenta99bf6480eea556e53b85e6db45f3b8c2361e693 (diff)
Merge branch 'release' into development
# Conflicts: # src/pages/shop/product/variant/[slug].jsx
Diffstat (limited to 'src-migrate/services/product.ts')
-rw-r--r--src-migrate/services/product.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/src-migrate/services/product.ts b/src-migrate/services/product.ts
new file mode 100644
index 00000000..fe415d11
--- /dev/null
+++ b/src-migrate/services/product.ts
@@ -0,0 +1,66 @@
+import { IProduct, IProductDetail } from '~/types/product';
+import snakeCase from 'snakecase-keys';
+import odooApi from '~/libs/odooApi';
+import { ICategoryBreadcrumb } from '~/types/category';
+
+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));
+};
+
+export const getProductCategoryBreadcrumb = async (
+ id: number
+): Promise<ICategoryBreadcrumb[]> => {
+ return await odooApi('GET', `/api/v1/product/${id}/category-breadcrumb`);
+};