Skip to content

Commit

Permalink
[finishes #187584937] buyer track order status
Browse files Browse the repository at this point in the history
  • Loading branch information
solangeihirwe03 committed Jun 25, 2024
1 parent c962363 commit 7d2d3a4
Show file tree
Hide file tree
Showing 10 changed files with 613 additions and 69 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Our e-commerce web application server, developed by Team Ninjas, facilitates smo
- buyer delete product from wishlist
- buyer view All products fromwishList Endpoint
- buyer view single product fromwishList Endpoint
- Buyer track order status Endpoint
- Admin update order status Endpoint

## TABLE OF API ENDPOINTS SPECIFICATION AND DESCRIPTION

Expand Down Expand Up @@ -108,6 +110,8 @@ Our e-commerce web application server, developed by Team Ninjas, facilitates smo
| 38 | delete | /api/shop/delete-whishlist-product:id | 200 OK | private | buyer delete product from wishlist |
| 39 | get | /buyer-view-whishlist-product | 200 ok | private | buyer view All product from wishList|
| 40 | get | /api/shop/buyer-view-whishlist-product/{id}| 200 ok | private | buyer view product from wishList |
| 41 | POST | /api/cart/user-get-order-status/ | 200 OK | private | user get order status |
| 42 | PUT | /api/cart/admin-update-order-status/:id | 200 OK | private | admin update order status |

## INSTALLATION

Expand Down Expand Up @@ -176,12 +180,4 @@ Our e-commerce web application server, developed by Team Ninjas, facilitates smo
7. Run the Migration:
```sh
npm run createAllTables
```
8. Delete the Seeder:
```sh
npm run deleteAllSeeders
```
9. Delete the Migration:
```sh
npm run deleteAllTables
```
84 changes: 59 additions & 25 deletions src/middlewares/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ import db from "../databases/models";

const validation =
(schema: Joi.ObjectSchema | Joi.ArraySchema) =>
async (req: Request, res: Response, next: NextFunction) => {
try {
const { error } = schema.validate(req.body, { abortEarly: false });

if (error) {
throw new Error(
error.details
.map((detail) => detail.message.replace(/"/g, ""))
.join(", ")
);
async (req: Request, res: Response, next: NextFunction) => {
try {
const { error } = schema.validate(req.body, { abortEarly: false });

if (error) {
throw new Error(
error.details
.map((detail) => detail.message.replace(/"/g, ""))
.join(", ")
);
}
return next();
} catch (error) {
res
.status(httpStatus.BAD_REQUEST)
.json({ status: httpStatus.BAD_REQUEST, message: error.message });
}
return next();
} catch (error) {
res
.status(httpStatus.BAD_REQUEST)
.json({ status: httpStatus.BAD_REQUEST, message: error.message });
}
};
};

const isUserExist = async (req: Request, res: Response, next: NextFunction) => {
try {
Expand Down Expand Up @@ -193,8 +193,7 @@ const verifyUserCredentials = async (
await sendEmail(
user.email,
"E-Commerce Ninja Login",
`Dear ${
user.lastName || user.email
`Dear ${user.lastName || user.email
}\n\nUse This Code To Confirm Your Account: ${otp}`
);

Expand Down Expand Up @@ -529,13 +528,13 @@ const isGoogleEnabled = async (req: any, res: Response, next: NextFunction) => {

const isCartExist = async (req: ExtendRequest, res: Response, next: NextFunction) => {
try {
const cart = await cartRepositories.getCartsByUserId (req.user.id);
const cart = await cartRepositories.getCartsByUserId(req.user.id);
if (!cart) {
return res.status(httpStatus.NOT_FOUND).json({ status: httpStatus.NOT_FOUND, message: "No cart found. Please create a cart first." });
}
req.cart = cart;
return next();
return res.status(httpStatus.NOT_FOUND).json({ status: httpStatus.NOT_FOUND, message: "No cart found. Please create a cart first." });
}
req.cart = cart;
return next();

} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
Expand Down Expand Up @@ -763,6 +762,40 @@ const isUserWishlistExistById = async (
}
};

const isOrderExist = async (req: ExtendRequest, res: Response, next: NextFunction) => {
try {
let order;
if (req.user.role === "buyer") {

order = await cartRepositories.getOrderByOrderIdAndUserId(req.body.orderId, req.user.id)
if (!order) {
return res.status(httpStatus.NOT_FOUND).json({
status: httpStatus.NOT_FOUND,
message: "order Not Found",
});
}
}
if(req.user.role === "admin"){
order = await cartRepositories.getOrderById(req.body.orderId)
if (!order) {
return res.status(httpStatus.NOT_FOUND).json({
status: httpStatus.NOT_FOUND,
message: "order Not Found",
});
}
}
req.order = order

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


export {
validation,
isUserExist,
Expand Down Expand Up @@ -790,4 +823,5 @@ export {
isProductExistToWishlist,
isUserWishlistExist,
isUserWishlistExistById,
isOrderExist
};
59 changes: 30 additions & 29 deletions src/modules/cart/controller/cartControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,35 +200,6 @@ const buyerCreateUpdateCart = async (req: ExtendRequest, res: Response) => {
});
}
};
const buyerGetOrderStatus = async(req:ExtendRequest, res:Response)=>{
try{
const status= await cartRepositories.getOrderStatus(req.params.id)
return res.status(200).json({
message: "Order Status found successfully",
data: status
})

}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)=>{
const orderId = req.params.id;
const updatedStatus: any = {
status:req.body.status
};

const updateStatus = await cartRepositories.updateOrderStatus(orderId, updatedStatus);
return res.status(httpStatus.OK).json({
status: "Status updated successfully!",
data:updateStatus
})
}

const buyerClearCartProduct = async (req: ExtendRequest, res: Response) => {
try {
Expand Down Expand Up @@ -301,6 +272,36 @@ const buyerCheckout = async (req: ExtendRequest, res: Response) => {
});
}
};

const buyerGetOrderStatus = async (req: ExtendRequest, res: Response) => {
try {
const order = req.order
return res.status(httpStatus.OK).json({
status: httpStatus.OK,
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 adminUpdateOrderStatus = async (req: ExtendRequest, res: Response) => {

const order = req.order
await cartRepositories.updateOrderStatus(req.body.orderId, req.body.status);
return res.status(httpStatus.OK).json({
status: httpStatus.OK,
message: "Status updated successfully!",
data: { order }
})
}
export {
buyerGetCart,
buyerGetCarts,
Expand Down
29 changes: 28 additions & 1 deletion src/modules/cart/repositories/cartRepositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ const deleteCartById = async (id: string) => {
const findCartByAttributes = async(key1: string, value1:any, key2: string, value2:any): Promise<any> => {
return await db.Carts.findOne({ where: { [key1]: value1, [key2]: value2 } })
}
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)=>{
return await db.Orders.update(
{status: status},
{where: {id: orderId}}
)
}

export default {
getCartsByUserId,
getCartProductsByCartId,
Expand All @@ -100,5 +124,8 @@ export default {
deleteCartById,
deleteCartProduct,
deleteAllCartProducts,
findCartByAttributes
findCartByAttributes,
getOrderByOrderIdAndUserId,
getOrderById,
updateOrderStatus
};
Loading

0 comments on commit 7d2d3a4

Please sign in to comment.