Skip to content

Commit

Permalink
Merge pull request #99 from atlp-rwanda/ft-orderitems-retrieval
Browse files Browse the repository at this point in the history
fetch all products from order
  • Loading branch information
niyontwali authored Jul 18, 2024
2 parents 7b48f89 + 1880dc5 commit 68276a0
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
95 changes: 95 additions & 0 deletions src/controllers/orderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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;
9 changes: 8 additions & 1 deletion src/routes/orderRoute.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);
Expand Down

0 comments on commit 68276a0

Please sign in to comment.