Skip to content

Commit

Permalink
order module update: update order status api
Browse files Browse the repository at this point in the history
  • Loading branch information
ataur39n-sharif committed Dec 26, 2023
1 parent f16252c commit d0ad36e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/App/modules/Auth/auth.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ AuthRoutes
AccessLimit([ERole.admin]),
AuthController.deleteAccount
)
// delete account

export default AuthRoutes
28 changes: 24 additions & 4 deletions src/App/modules/Orders/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {OrderService} from "@/App/modules/Orders/order.services";
import {TOrderPayload} from "@/App/modules/Orders/order.types";
import {sendResponse} from "@/Utils/helper/sendResponse";
import {z} from "zod";
import {Types} from "mongoose";


const getOrders = catchAsync(async (req: Request, res: Response, next: NextFunction) => {
Expand All @@ -18,15 +19,20 @@ const getOrders = catchAsync(async (req: Request, res: Response, next: NextFunct
})

const getSingleUserOrders = catchAsync(async (req: Request, res: Response, next: NextFunction) => {

// set uid in body from validation middleware
const uid = z.instanceof(Types.ObjectId).parse(new Types.ObjectId(req.body.uid))
const data = await OrderService.allOrdersOfUser(uid)
sendResponse.success(res, {
statusCode: 200,
message: 'All orders fetched successfully',
data
})
})

const getSingleOrder = catchAsync(async (req: Request, res: Response, next: NextFunction) => {
const id = z.string({
required_error: 'Id is required.'
required_error: 'Order Id is required.'
}).parse(req.params.id)

const data = await OrderService.singleOrder(id)
Expand All @@ -50,17 +56,30 @@ const placeOrder = catchAsync(async (req: Request, res: Response, next: NextFunc
})
})

const updateOrderStatus = catchAsync(async (req: Request, res: Response, next: NextFunction) => {
const id = z.instanceof(Types.ObjectId).parse(new Types.ObjectId(req.params.id))
const status = z.enum(['pending', 'hold', 'paid', 'shipped', 'delivered']).parse(req.body.status)
const data = await OrderService.updateStatus(id, status)

sendResponse.success(res, {
statusCode: 200,
message: 'Order created successfully',
data
})
})


const deleteOrder = catchAsync(async (req: Request, res: Response, next: NextFunction) => {

const id = z.string({
required_error: 'Id is required.'
required_error: 'Order id is required.'
}).parse(req.params.id)

await OrderService.deleteOrder(id)
const response = await OrderService.deleteOrder(id)

sendResponse.success(res, {
statusCode: 200,
message: 'Deleted successfully.'
message: response ? 'Deleted successfully.' : 'No order found.'
})

})
Expand All @@ -74,5 +93,6 @@ export const OrderController = {
getSingleOrder,
getSingleUserOrders,
placeOrder,
updateOrderStatus,
deleteOrder
}
29 changes: 20 additions & 9 deletions src/App/modules/Orders/order.services.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import {IOrder, TOrderPayload} from "@/App/modules/Orders/order.types";
import {OrderModel} from "@/App/modules/Orders/order.model";
import {OrderUtils} from "@/App/modules/Orders/order.utils";

import {Types} from "mongoose";

const allOrders = async (): Promise<IOrder[]> => {
return OrderModel.find()
return OrderModel.find().lean()
}

const allOrdersOfUser = async (uid: string) => {
const allOrdersOfUser = async (uid: Types.ObjectId | string) => {
return OrderModel.find({
uid
})
}).lean()
}

const singleOrder = async (_id: string): Promise<IOrder | null> => {
return OrderModel.findOne({
_id
})
}).lean()
}

const createOrder = async (payload: TOrderPayload): Promise<IOrder> => {
Expand All @@ -32,16 +32,27 @@ const createOrder = async (payload: TOrderPayload): Promise<IOrder> => {
}

//status update
const updateStatus = async (id: string, status: 'pending' | 'hold' | 'paid' | 'shipped' | 'delivered') => {
const updateStatus = async (id: Types.ObjectId | string, status: 'pending' | 'hold' | 'paid' | 'shipped' | 'delivered') => {
return OrderModel.findOneAndUpdate({_id: id}, {
status
})
$set: {
status
}
}, {
new: true
}).lean()
}

const deleteOrder = async (_id: string) => {
return OrderModel.findOneAndDelete({
const result = await OrderModel.deleteOne({
_id
})
console.log(result.deletedCount)
if (result.deletedCount > 0) {
return true
} else {
return false
}

}


Expand Down
18 changes: 9 additions & 9 deletions src/App/modules/Orders/order.utils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {TOrderLineItems} from "@/App/modules/Orders/order.types";

const processOrderAmountDetails = (payload:TOrderLineItems[])=>{
let total=0;
let subTotal=0;
let shippingCost =0;
const processOrderAmountDetails = (payload: TOrderLineItems[]) => {

for (const lineItem of payload){
subTotal+=Number(lineItem.price)*Number(lineItem.quantity)
let subTotal = 0;
let shippingCost = 0;

for (const lineItem of payload) {
subTotal += Number(lineItem.price) * Number(lineItem.quantity)
}

total= subTotal+shippingCost
const total = subTotal + shippingCost

return{
return {
subTotal,
shippingCost,
total
}
}

export const OrderUtils={
export const OrderUtils = {
processOrderAmountDetails
}
1 change: 1 addition & 0 deletions src/App/modules/User/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* * DELETE - /users/:id => headers-authorization
* */


import {Router} from "express";
import {UserController} from "@/App/modules/User/user.controller";

Expand Down

0 comments on commit d0ad36e

Please sign in to comment.