diff --git a/src/module/order/order.controller.ts b/src/module/order/order.controller.ts index 621c3df..f6eecc0 100644 --- a/src/module/order/order.controller.ts +++ b/src/module/order/order.controller.ts @@ -120,3 +120,17 @@ export const declineOrder: Controller = async ( .json({ status: "error", message: "Failed to decline order" }); } }; + +export const getUnreviewedOrders: Controller = async (req, res) => { + try { + const orders = await orderService.getUnreviewedOrders(req.learner.id); + + res.json({ status: "success", data: orders }); + } catch (error) { + logger.error(`Failed to get unreviewed orders: ${error}`); + + res + .status(500) + .json({ status: "error", message: "Failed to get unreviewed orders" }); + } +}; diff --git a/src/module/order/order.repository.ts b/src/module/order/order.repository.ts index 06cfc88..33ef86f 100644 --- a/src/module/order/order.repository.ts +++ b/src/module/order/order.repository.ts @@ -6,7 +6,7 @@ import { tutories as tutoriesTable, tutors, } from "@/db/schema"; -import { and, eq, desc, inArray, lte } from "drizzle-orm"; +import { and, eq, desc, inArray, lte, isNotNull } from "drizzle-orm"; export class OrderRepository { constructor(private readonly db: typeof dbType) {} @@ -29,12 +29,14 @@ export class OrderRepository { tutoriesId, orderId, status, + unreviewed, }: { learnerId?: string; tutorId?: string; tutoriesId?: string; orderId?: string; status?: "pending" | "scheduled" | "completed"; + unreviewed?: boolean; }) { await this.updateStatusToCompleted(); @@ -56,6 +58,10 @@ export class OrderRepository { conditions.push(eq(orders.id, orderId)); } + if (typeof unreviewed === "boolean" && unreviewed) { + conditions.push(isNotNull(orders.reviewDismissedAt)); + } + if (tutorId) { const tutories = await this.db .select({ id: tutoriesTable.id }) diff --git a/src/module/order/order.route.ts b/src/module/order/order.route.ts index 86b8ec1..66a7001 100644 --- a/src/module/order/order.route.ts +++ b/src/module/order/order.route.ts @@ -51,4 +51,11 @@ orderRouter.post( orderController.declineOrder, ); +orderRouter.get( + "/unreviewed", + // #swagger.tags = ['orders'] + verifyLearner, + orderController.getUnreviewedOrders, +); + export default orderRouter; diff --git a/src/module/order/order.service.ts b/src/module/order/order.service.ts index e678f71..20e3bb6 100644 --- a/src/module/order/order.service.ts +++ b/src/module/order/order.service.ts @@ -116,4 +116,16 @@ export class OrderService { throw new Error(`Failed to decline order: ${error}`); } } + + async getUnreviewedOrders(learnerId: string) { + try { + return this.orderRepository.getOrders({ + learnerId, + status: "completed", + unreviewed: true, + }); + } catch (error) { + throw new Error(`Failed to get unreviewed orders: ${error}`); + } + } }