diff --git a/src/controllers/orderController.ts b/src/controllers/orderController.ts index b7cd7989..86c11fb1 100644 --- a/src/controllers/orderController.ts +++ b/src/controllers/orderController.ts @@ -174,3 +174,98 @@ export const deleteOrder = async (req: Request, res: Response) => { sendInternalErrorResponse(res, error); } }; + +// order Items +export const getOrderItems = async (req: Request, res: Response): Promise => { + const { id: userId } = req.user as { id: string }; + const { orderId } = req.params; + + const transaction = await sequelize.transaction(); + try { + const order = await Order.findOne({ + where: { id: orderId, userId }, + include: [ + { + model: OrderItems, + as: 'orderItems', + include: [ + { + model: Product, + as: 'products', + include: [ + { + model: Size, + as: 'sizes', + attributes: ['size', 'price'], + }, + ], + }, + ], + }, + ], + transaction, + }); + + if (!order) { + res.status(404).json({ ok: false, message: 'Order not found' }); + return; + } + + const orderItems = await OrderItems.findAll({ where: { orderId: order.id }, transaction }); + const allProducts = await Promise.all( + orderItems.map(async item => { + const product = await Product.findOne({ + where: { id: item.productId }, + attributes: ['id', 'name', 'sellerId', 'images'], + include: [ + { + model: Size, + as: 'sizes', + where: { id: item.sizeId }, + attributes: ['price', 'size', 'id'], + }, + ], + transaction, + }); + + return { + product, + quantity: item.quantity, + price: item.price, + createdAt: item.createdAt, + }; + }) + ); + + if (allProducts.length < 1) { + res.status(404).json({ ok: false, message: 'No products found in the order' }); + return; + } + + const sendProducts = allProducts + .map(item => { + if (!item.product) return null; + const { id, name, sellerId, images, sizes } = item.product; + return { + id, + name, + sizes, + sellerId, + images, + quantity: item.quantity, + price: item.price, + createdAt: item.createdAt, + }; + }) + .filter(Boolean); + + await transaction.commit(); + res.status(200).json({ ok: true, orderId: order.id, orderItems: sendProducts }); + } catch (error) { + await transaction.rollback(); + logger.error(error); + sendInternalErrorResponse(res, error instanceof Error ? error.message : error); + } +}; + +export default getOrderItems; diff --git a/src/routes/orderRoute.ts b/src/routes/orderRoute.ts index 9dce194d..d5a84d4e 100644 --- a/src/routes/orderRoute.ts +++ b/src/routes/orderRoute.ts @@ -1,6 +1,12 @@ import { Router } from 'express'; import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares'; -import { getUserOrders, createOrder, deleteOrder, sellerProductOrders } from '../controllers/orderController'; +import { + getUserOrders, + createOrder, + deleteOrder, + sellerProductOrders, + getOrderItems, +} from '../controllers/orderController'; import { processOrder, cancelOrder, sellerChangeOrderStatus } from '../controllers/orderStatusController'; const orderRouter = Router(); @@ -11,6 +17,7 @@ orderRouter orderRouter.route('/get-orders').get(isAuthenticated, checkUserRoles('seller'), sellerProductOrders); orderRouter.route('/:id').delete(isAuthenticated, checkUserRoles('admin'), deleteOrder); orderRouter.get('/:orderId/check-status', isAuthenticated, checkUserRoles('buyer'), processOrder); +orderRouter.get('/:orderId', isAuthenticated, checkUserRoles('buyer'), getOrderItems); orderRouter.put('/:orderId/cancel', isAuthenticated, checkUserRoles('buyer'), cancelOrder); orderRouter.put('/:orderId/seller-change-status', isAuthenticated, checkUserRoles('seller'), sellerChangeOrderStatus); orderRouter.get('/seller-products-status', isAuthenticated, checkUserRoles('seller'), sellerProductOrders);