From e29d28ac189f7b57b4b894bcf65fab97ba74b2b1 Mon Sep 17 00:00:00 2001 From: Leon Xu Date: Sat, 30 Dec 2023 15:59:33 -0800 Subject: [PATCH] feat: added getSimilarProducts --- lib/actions/index.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/actions/index.ts b/lib/actions/index.ts index 3b1a06e..c680827 100644 --- a/lib/actions/index.ts +++ b/lib/actions/index.ts @@ -71,4 +71,20 @@ export async function getAllProducts() { } catch (error) { console.log(error); } +} +export async function getSimilarProducts(productId: string) { + try { + connectToDB(); + + const currentProduct = await Product.findById(productId); + + if (!currentProduct) return null; + + const similarProducts = await Product.find({ _id: { $ne: productId } }).limit(3); + + return similarProducts + + } catch (error) { + console.log(error); + } } \ No newline at end of file