Skip to content

Commit

Permalink
[deliver #187584937] buyer track order status
Browse files Browse the repository at this point in the history
  • Loading branch information
solangeihirwe03 committed Jul 16, 2024
1 parent cb1e7b0 commit f7e2d69
Show file tree
Hide file tree
Showing 16 changed files with 953 additions and 139 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Our e-commerce web application server, developed by Team Ninjas, facilitates smo
- Submit a seller request
- Buyer track order status Endpoint
- Admin update order status EndPoint
- Buyer get order history Endpoint

## TABLE OF API ENDPOINTS SPECIFICATION AND DESCRIPTION

Expand Down Expand Up @@ -122,8 +123,9 @@ Our e-commerce web application server, developed by Team Ninjas, facilitates smo
| 44 | PUT | /api/user/user-mark-notification/:id | 200 OK | private | user mark notification |
| 45 | POST | /api/shop/buyer-review-product/:id | 200 OK | private | Buyer Create review |
| 46 | POST | /api/user/user-submit-seller-request | 200 OK | private | Submit a seller request |
| 46 | POST | /api/cart/user-get-order-status/ | 200 OK | private | user get order status |
| 47 | PUT | /api/cart/admin-update-order-status/:id | 200 OK | private | admin update order status |
| 47 | GET | /api/cart/user-get-order-status/:id | 200 OK | private | user get order status |
| 48 | GET | /api/cart/buyer-get-order-history | 200 OK | private | buyer get order history |
| 49 | PUT | /api/cart/admin-update-order-status/:id | 200 OK | private | admin update order status |


## INSTALLATION
Expand Down
24 changes: 1 addition & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/databases/migrations/20240604150804-create-orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export = {
type: DataTypes.STRING,
allowNull: false
},
shippingProcess: {
type: DataTypes.STRING,
allowNull: false
},
expectedDeliveryDate:{
type:DataTypes.DATE,
allowNull: false
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
Expand Down
2 changes: 1 addition & 1 deletion src/databases/models/carts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Carts extends Model<CartAttributes> implements CartAttributes {
static associate() {
Carts.belongsTo(Users, { foreignKey: "userId", as: "buyer" });
Carts.hasMany(CartProducts, { foreignKey: "cartId", as: "cartProducts" });
Carts.hasMany(Orders,{foreignKey: "cartId", as: "order"})
Carts.hasMany(Orders,{foreignKey: "cartId", as: "orders"})
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/databases/models/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface OrderAttributes {
paymentMethodId: string;
orderDate: Date;
status: string;
shippingProcess: string;
expectedDeliveryDate: Date
createdAt: Date;
updatedAt: Date;
}
Expand All @@ -26,6 +28,8 @@ class Orders extends Model<OrderAttributes> implements OrderAttributes {
declare paymentMethodId: string;
declare orderDate: Date;
declare status: string;
declare shippingProcess: string;
declare expectedDeliveryDate: Date;
declare createdAt: Date;
declare updatedAt: Date;

Expand Down Expand Up @@ -68,6 +72,14 @@ Orders.init(
type: new DataTypes.STRING,
allowNull: false
},
shippingProcess: {
type: DataTypes.STRING,
allowNull: false
},
expectedDeliveryDate:{
type:DataTypes.DATE,
allowNull: false
},
createdAt: {
field: "createdAt",
type: DataTypes.DATE,
Expand Down
4 changes: 4 additions & 0 deletions src/databases/seeders/20240604133044-orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = {
paymentMethodId: 1,
orderDate: new Date("2024-01-01"),
status: "completed",
shippingProcess : "your order have been completed",
expectedDeliveryDate: new Date("2024-08-01"),
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -42,6 +44,8 @@ module.exports = {
paymentMethodId: 2,
orderDate: new Date("2024-01-15"),
status: "completed",
shippingProcess : "your order have reached Kigali in 30 minutes it will be reached to you",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
}
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ eventEmitter.on("passwordExpiry", async ({ userId, message }) => {
await saveAndEmitNotification(userId, message, "passwordExpiry");
});

eventEmitter.on('orderStatusUpdated', async (order) => {
eventEmitter.on("orderStatusUpdated", async (order) => {
const orderStatus = await fetchOrderWithCarts(order.id)
const userId = orderStatus.carts.userId
const message = `The order that was created on ${order.orderDate} status has been updated to ${order.status}.`;
const message = order.shippingProcess;
await userRepositories.addNotification(userId, message);
await sendEmailOrderStatus(userId, message);
io.to(userId).emit('orderStatusUpdated', message)
io.to(userId).emit("orderStatusUpdated", message)
});

cron.schedule("0 0 * * *", async () => {
Expand Down
49 changes: 47 additions & 2 deletions src/middlewares/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,50 @@ const isSellerRequestExist = async (req: Request, res: Response, next: NextFunct
}
};

const isOrderExist = async (req: Request, res:Response, next:NextFunction)=>{
try{
let order: any;
if (req.user.role === "buyer") {
if(req.params.id){
order = await cartRepositories.getOrderByOrderIdAndUserId(req.params.id, req.user.id)
if(!order){
return res.status(httpStatus.NOT_FOUND).json({
status: httpStatus.NOT_FOUND,
error: "order not found"
})
}
}else{
order = await cartRepositories.getOrdersByUserId(req.user.id)
if(!order.orders || order.orders.length === 0){
return res.status(httpStatus.NOT_FOUND).json({
status: httpStatus.NOT_FOUND,
error: "orders not found"
})
}
}

}
if(req.user.role === "admin"){
order = await cartRepositories.getOrderById(req.params.id)
if (!order) {
return res.status(httpStatus.NOT_FOUND).json({
status: httpStatus.NOT_FOUND,
error: "order Not Found",
});
}
}
(req as any).order = order
return next();
}
catch(error){
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
error: error.message
})
}
}


export {
validation,
isUserExist,
Expand Down Expand Up @@ -922,5 +966,6 @@ export {
isProductExistIntoWishList,
isProductOrdered,
isUserProfileComplete,
isSellerRequestExist
};
isSellerRequestExist,
isOrderExist
};
53 changes: 53 additions & 0 deletions src/modules/cart/controller/cartControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,56 @@ const paymentCanceled = (req: Request, res: Response) => {
}
};

const buyerGetOrderStatus = async (req: ExtendRequest, res: Response) => {
try {
const order = req.order.shippingProcess
return res.status(httpStatus.OK).json({
message: "Order Status found successfully",
data: {order}
})

} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
error: error.message
})
}
}

const buyerGetOrders = async(req:ExtendRequest, res:Response)=>{
try{
const orders = req.order
return res.status(httpStatus.OK).json({
message: "Orders found successfully",
data: {orders}
})
}
catch(error){
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
error: error.message
})
}
}

const adminUpdateOrderStatus = async (req: ExtendRequest, res: Response) => {
try {
const order = req.order
await cartRepositories.updateOrderStatus(req.params.id, req.body.status,req.body.shippingProcess);
eventEmitter.emit("orderStatusUpdated", order);
return res.status(httpStatus.OK).json({
message: "Status updated successfully!",
data: { order }
})
}catch(error){
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
error: error.message
})
}

}

export {
buyerGetCart,
buyerGetCarts,
Expand All @@ -362,4 +412,7 @@ export {
getProductDetails,
paymentSuccess,
paymentCanceled,
buyerGetOrderStatus,
buyerGetOrders,
adminUpdateOrderStatus
};
47 changes: 46 additions & 1 deletion src/modules/cart/repositories/cartRepositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,47 @@ const saveOrder = async(lineItems: any, shopIds: any, productIds: any, session:
return await db.Orders.create(order)
}

const getOrderByOrderIdAndUserId = async (orderId: string, userId: string) => {
return await db.Orders.findOne({
where: { id: orderId },
include: [
{
model: db.Carts,
as: "carts",
where: { userId: userId }
}
]
})
}

const getOrderById = async (orderId: string) => {
return await db.Orders.findOne({ where: { id: orderId } })
}

const updateOrderStatus = async (orderId: string, status: string, shippingProcess: string) => {
return await db.Orders.update(
{
status: status,
shippingProcess: shippingProcess
},
{ where: { id: orderId } }
);
};
const getOrdersByUserId = async (userId: string) => {
return await db.Carts.findOne(
{
where:
{ userId: userId },
include: [
{
model: db.Orders,
as: "orders",
}
]
});
};


export default {
getCartsByUserId,
getCartProductsByCartId,
Expand All @@ -154,5 +195,9 @@ export default {
findCartProductByCartId,
findCartIdbyUserId,
findProductById,
saveOrder
saveOrder,
getOrderByOrderIdAndUserId,
getOrderById,
getOrdersByUserId,
updateOrderStatus
};
Loading

0 comments on commit f7e2d69

Please sign in to comment.