@@ -13,6 +13,7 @@ import { insert_function, read_function } from "../utils/db_methods";
13
13
import { category_utils } from "../utils/controller" ;
14
14
import { Info , Message } from "../types/upload" ;
15
15
import { isAvailable } from "../utils/nodeEvents" ;
16
+ import { Op , OrderItem } from "sequelize" ;
16
17
17
18
let product_id ;
18
19
const include = [
@@ -206,6 +207,7 @@ const read_single_product = async (req: Request, res: Response) => {
206
207
"Product not found or not owned!" ,
207
208
) ;
208
209
}
210
+
209
211
return sendResponse (
210
212
res ,
211
213
200 ,
@@ -225,6 +227,57 @@ const read_single_product = async (req: Request, res: Response) => {
225
227
}
226
228
} ;
227
229
230
+ const getRecommendedProducts = async ( req : Request , res : Response ) => {
231
+ try {
232
+ const productId = req . params . id ;
233
+ const product = await read_function < ProductAttributes > (
234
+ "Product" ,
235
+ "findOne" ,
236
+ { where : { id : productId } , include } ,
237
+ ) ;
238
+
239
+ if ( ! product ) {
240
+ return sendResponse (
241
+ res ,
242
+ 404 ,
243
+ "NOT FOUND" ,
244
+ "Product not found or not owned!" ,
245
+ ) ;
246
+ }
247
+
248
+ const condition = {
249
+ where : {
250
+ categoryId : product . categoryId ,
251
+ isAvailable : true ,
252
+ id : { [ Op . ne ] : productId } ,
253
+ } ,
254
+ include,
255
+ limit : 5 ,
256
+ order : [ [ "createdAt" , "DESC" ] ] as OrderItem [ ] ,
257
+ } ;
258
+ const recommended = await read_function < ProductAttributes > (
259
+ "Product" ,
260
+ "findAll" ,
261
+ condition ,
262
+ ) ;
263
+ return sendResponse (
264
+ res ,
265
+ 200 ,
266
+ "SUCCESS" ,
267
+ "Recommended Products fetched successfully!" ,
268
+ recommended ,
269
+ ) ;
270
+ } catch ( error : unknown ) {
271
+ return sendResponse (
272
+ res ,
273
+ 500 ,
274
+ "SERVER ERROR" ,
275
+ "Something went wrong!" ,
276
+ error as Error ,
277
+ ) ;
278
+ }
279
+ } ;
280
+
228
281
const update_product = async ( req : Request , res : Response ) => {
229
282
try {
230
283
product_id = category_utils ( req , res ) . getId ;
@@ -530,4 +583,5 @@ export default {
530
583
delete_product,
531
584
guest_read_all_products,
532
585
guest_read_single_product,
586
+ getRecommendedProducts,
533
587
} ;
0 commit comments