summaryrefslogtreecommitdiff
path: root/src/pages
diff options
context:
space:
mode:
Diffstat (limited to 'src/pages')
-rw-r--r--src/pages/api/shop/generate-recomendation.js36
-rw-r--r--src/pages/my/recomendation/components/products-recomendatison.jsx107
2 files changed, 109 insertions, 34 deletions
diff --git a/src/pages/api/shop/generate-recomendation.js b/src/pages/api/shop/generate-recomendation.js
index 11dd153e..aea39adf 100644
--- a/src/pages/api/shop/generate-recomendation.js
+++ b/src/pages/api/shop/generate-recomendation.js
@@ -9,7 +9,7 @@ export default async function handler(req, res) {
return res.status(422).json({ error: 'parameter missing' })
}
- let parameter = [
+ /*let parameter = [
`q=${escapeSolrQuery(q)}`,
`q.op=${op}`,
`indent=true`,
@@ -22,23 +22,27 @@ export default async function handler(req, res) {
];
let result = await axios(
process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&')
+ );*/
+ let parameter = [
+ `q=${escapeSolrQuery(q)}`,
+ `q.op=${op}`,
+ `debugQuery=on`,
+ `defType=edismax`,
+ `df=display_name_s`,
+ `fq=-publish_b:false`,
+ `rows=5`,
+ ];
+ if(op == 'AND'){
+ parameter.push(`sort=product_rating_f DESC, price_discount_f DESC`);
+ parameter.push(`rows=1`);
+ }
+
+ let result = await axios(
+ process.env.SOLR_HOST + '/solr/recommendation/select?' + parameter.join('&')
);
try {
- let { auth } = req.cookies;
- if (auth) auth = JSON.parse(auth);
- result.data.response.products = productMappingSolr(
- result.data.response.docs,
- auth?.pricelist || false
- );
- result.data.responseHeader.params.start = parseInt(
- result.data.responseHeader.params.start
- );
- result.data.responseHeader.params.rows = parseInt(
- result.data.responseHeader.params.rows
- );
- delete result.data.response.docs;
- result.data = camelcaseObjectDeep(result.data);
- res.status(200).json(result.data);
+ result.data = camelcaseObjectDeep(result.data)
+ res.status(200).json(result.data)
} catch (error) {
res.status(400).json({ error: error.message });
}
diff --git a/src/pages/my/recomendation/components/products-recomendatison.jsx b/src/pages/my/recomendation/components/products-recomendatison.jsx
index bb5950c8..d39d2a99 100644
--- a/src/pages/my/recomendation/components/products-recomendatison.jsx
+++ b/src/pages/my/recomendation/components/products-recomendatison.jsx
@@ -8,6 +8,15 @@ import Image from 'next/image';
import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
import formatCurrency from '~/libs/formatCurrency';
+const exportToExcel = (data) => {
+ const worksheet = XLSX.utils.json_to_sheet(data);
+ const workbook = XLSX.utils.book_new();
+ XLSX.utils.book_append_sheet(workbook, worksheet, 'Results');
+
+ // Generate Excel file and trigger download in the browser
+ XLSX.writeFile(workbook, 'ProductRecommendations.xlsx');
+};
+
const ProductsRecomendation = ({ id }) => {
const [excelData, setExcelData] = useState(null);
const [products, setProducts] = useState([]);
@@ -22,13 +31,8 @@ const ProductsRecomendation = ({ id }) => {
product: product,
result: {
id: result?.id || '-',
- name: result?.displayName || '-',
- code: result?.code || '-',
- image: result?.image || '-',
- price: result?.lowestPrice.priceDiscount || '-',
- manufacture: result?.manufacture.name || '-',
- variantTotal: result?.variantTotal || '-',
- variants: variants || '-',
+ name: result?.nameS || '-',
+ code: result?.defaultCodeS || '-',
},
};
@@ -76,9 +80,7 @@ const ProductsRecomendation = ({ id }) => {
}
});
- console.log('ini result', searchProduct.data.response);
-
-
+ console.log('ini result', searchProduct.data.response);
}
return resultMapping;
@@ -91,26 +93,45 @@ const ProductsRecomendation = ({ id }) => {
const results = await Promise.all(
excelData.map(async (row, i) => {
const index = i + 1;
- const product = row[0];
- return await searchRecomendation({ product, index });
+ const product = row['product'];
+ return await generateProductRecomendation({ product, index });
})
- ).then((results) => setProducts(results));
+ );
+
+ const formattedResults = results.map((result) => {
+ const formattedResult = { product: result.product };
+ for (let i = 0; i <= 5; i++) {
+ formattedResult[`recomendation product ${i + 1} - code`] = result.result[i] == null ? '-' : result.result[i]?.code;
+ formattedResult[`recomendation product ${i + 1} - name`] = result.result[i] == null ? '-' : result.result[i]?.name ;
+ }
+ return formattedResult;
+ });
+
+ exportToExcel(formattedResults);
+ setProducts(results);
setIsLoading(false);
} else {
+ setIsLoading(false);
console.log('No excel data available');
}
};
const handleFileChange = (e) => {
+ setIsLoading(true);
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
const data = new Uint8Array(event.target.result);
const workbook = XLSX.read(data, { type: 'array' });
- const sheetName = workbook.SheetNames[0];
- const sheet = workbook.Sheets[sheetName];
- const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1, range: 1 });
+
+ const firstSheetName = workbook.SheetNames[0];
+ const worksheet = workbook.Sheets[firstSheetName];
+ const jsonData = XLSX.utils.sheet_to_json(worksheet);
+
setExcelData(jsonData);
+ console.log('ini json data', jsonData);
+
+ setIsLoading(false);
};
reader.readAsArrayBuffer(file);
};
@@ -137,6 +158,56 @@ const ProductsRecomendation = ({ id }) => {
result();
};
+
+ const generateProductRecomendation = async ({ product, index }) => {
+ let variants = [];
+ let resultMapping = {};
+ const searchProduct = await axios.post(
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}&op=AND`
+ );
+ const searchProductOR = await axios.post(
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}&op=OR`
+ );
+ const resultAND =
+ searchProduct.data.response.numFound > 0
+ ? searchProduct.data.response.docs[0]
+ : null; // hasil satu
+ const resultOR =
+ searchProductOR.data.response.numFound > 0
+ ? searchProductOR.data.response.docs
+ : []; // hasil 5
+
+ resultMapping = {
+ index: index,
+ product: product,
+ result: {},
+ };
+
+ // Add resultAND to resultMapping if it exists
+ resultMapping.result[0] = resultAND
+ ? {
+ id: resultAND?.id || '-',
+ name: resultAND?.nameS || '-',
+ code: resultAND?.defaultCodeS || '-',
+ }
+ : null;
+
+ // Add resultOR to resultMapping
+ if (resultOR.length > 0) {
+ resultOR.forEach((item, idx) => {
+ resultMapping.result[idx + 1] = {
+ id: item?.id || '-',
+ name: item?.nameS || '-',
+ code: item?.defaultCodeS || '-',
+ };
+ });
+ } else {
+ for (let i = 0; i <= 5; i++) {
+ resultMapping.result[i + 1] = null;
+ }
+ }
+ return resultMapping;
+ };
return (
<>
<BottomPopup
@@ -302,7 +373,7 @@ const ProductsRecomendation = ({ id }) => {
Generate
</Button>
</div>
- <div className='grup mt-8'>
+ {/* <div className='grup mt-8'>
{products && products.length > 0 && (
<div className='group'>
<table className='table-data'>
@@ -396,7 +467,7 @@ const ProductsRecomendation = ({ id }) => {
</table>
</div>
)}
- </div>
+ </div> */}
</div>
</div>
</>