Order API:All routes was created and fully tested
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
import RequestValidator from "../../middlewares/request_validator";
|
import RequestValidator from "../../middlewares/request_validator";
|
||||||
import { order_controller } from "./order.controller";
|
import { order_controller } from "./order.controller";
|
||||||
import { order_validations } from "./order.validation";
|
import { order_validations } from "./order.validation";
|
||||||
|
import auth from "../../middlewares/auth";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@@ -14,7 +15,7 @@ router.post(
|
|||||||
);
|
);
|
||||||
router.get("/:id", order_controller.get_single_order);
|
router.get("/:id", order_controller.get_single_order);
|
||||||
router.patch(
|
router.patch(
|
||||||
"/:id",
|
"/:id",auth("ADMIN"),
|
||||||
RequestValidator(order_validations.update_order),
|
RequestValidator(order_validations.update_order),
|
||||||
order_controller.update_order,
|
order_controller.update_order,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Request } from "express";
|
|||||||
import { configs } from "../../configs";
|
import { configs } from "../../configs";
|
||||||
import { prisma } from "../../lib/prisma";
|
import { prisma } from "../../lib/prisma";
|
||||||
import { orderEmailQueue } from "../../queues/email/order/order.email.queue";
|
import { orderEmailQueue } from "../../queues/email/order/order.email.queue";
|
||||||
|
import { AppError } from "../../utils/app_error";
|
||||||
import paginationHelper from "../../utils/pagination_helper";
|
import paginationHelper from "../../utils/pagination_helper";
|
||||||
|
|
||||||
const get_all_order_from_db = async (req: Request) => {
|
const get_all_order_from_db = async (req: Request) => {
|
||||||
@@ -9,6 +10,12 @@ const get_all_order_from_db = async (req: Request) => {
|
|||||||
const search = req.query.search as string;
|
const search = req.query.search as string;
|
||||||
const customerName = req.query.customerName as string;
|
const customerName = req.query.customerName as string;
|
||||||
const productName = req.query.productName as string;
|
const productName = req.query.productName as string;
|
||||||
|
|
||||||
|
// for date filter
|
||||||
|
|
||||||
|
const startDate = req.query.startDate as string;
|
||||||
|
const endDate = req.query.endDate as string;
|
||||||
|
|
||||||
const status = (req.query.status as string) || undefined;
|
const status = (req.query.status as string) || undefined;
|
||||||
const { page, limit, skip, sortBy, sortOrder } = paginationHelper(req.query);
|
const { page, limit, skip, sortBy, sortOrder } = paginationHelper(req.query);
|
||||||
|
|
||||||
@@ -62,6 +69,24 @@ const get_all_order_from_db = async (req: Request) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// for date filter
|
||||||
|
const dateFilter: any = {};
|
||||||
|
if (startDate) {
|
||||||
|
const start = new Date(startDate);
|
||||||
|
start.setHours(0, 0, 0, 0);
|
||||||
|
dateFilter.gte = start;
|
||||||
|
}
|
||||||
|
if (endDate) {
|
||||||
|
const end = new Date(endDate);
|
||||||
|
end.setHours(23, 59, 59, 999);
|
||||||
|
dateFilter.lte = end;
|
||||||
|
}
|
||||||
|
if (Object.keys(dateFilter).length > 0) {
|
||||||
|
andCondition.push({
|
||||||
|
createdAt: dateFilter,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const getAllOrders = await prisma.order.findMany({
|
const getAllOrders = await prisma.order.findMany({
|
||||||
take: limit,
|
take: limit,
|
||||||
skip,
|
skip,
|
||||||
@@ -77,7 +102,6 @@ const get_all_order_from_db = async (req: Request) => {
|
|||||||
AND: andCondition,
|
AND: andCondition,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
console.log(status);
|
|
||||||
return {
|
return {
|
||||||
data: getAllOrders,
|
data: getAllOrders,
|
||||||
pagination: {
|
pagination: {
|
||||||
@@ -120,7 +144,16 @@ const create_order_into_db = async (req: Request) => {
|
|||||||
|
|
||||||
const update_order_into_db = async (req: Request) => {
|
const update_order_into_db = async (req: Request) => {
|
||||||
// define your own login here
|
// define your own login here
|
||||||
|
const user = req.user;
|
||||||
|
console.log(user);
|
||||||
|
if (user?.role !== "ADMIN") {
|
||||||
|
throw new AppError("You are not authorized to perform this action", 403);
|
||||||
|
}
|
||||||
const { id } = req.params as { id: string };
|
const { id } = req.params as { id: string };
|
||||||
|
const isProductExist = await prisma.order.findUnique({ where: { id } });
|
||||||
|
if (!isProductExist) {
|
||||||
|
throw new AppError("Order is not found", 404);
|
||||||
|
}
|
||||||
const result = await prisma.order.update({ where: { id }, data: req.body });
|
const result = await prisma.order.update({ where: { id }, data: req.body });
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -68,6 +68,26 @@ export const orderSwaggerDocs = {
|
|||||||
required: false,
|
required: false,
|
||||||
schema: { type: "string" },
|
schema: { type: "string" },
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "date",
|
||||||
|
in: "query",
|
||||||
|
required: false,
|
||||||
|
schema: { type: "string" },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "startDate",
|
||||||
|
in: "query",
|
||||||
|
required: false,
|
||||||
|
schema: { type: "string", format: "date" },
|
||||||
|
example: "2026-04-01",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "endDate",
|
||||||
|
in: "query",
|
||||||
|
required: false,
|
||||||
|
schema: { type: "string", format: "date" },
|
||||||
|
example: "2026-04-31",
|
||||||
|
},
|
||||||
],
|
],
|
||||||
responses: {
|
responses: {
|
||||||
200: { description: "order fetched successfully" },
|
200: { description: "order fetched successfully" },
|
||||||
@@ -111,14 +131,7 @@ export const orderSwaggerDocs = {
|
|||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
example: JSON.stringify({
|
example: JSON.stringify({
|
||||||
shopAccountId: "",
|
status: "INITIATED",
|
||||||
productPrice: 1500,
|
|
||||||
productQuantity: 2,
|
|
||||||
productName: "Wireless Mouse",
|
|
||||||
customerName: "Rahim Uddin",
|
|
||||||
customerPhone: "+8801712345678",
|
|
||||||
customerAddress: "Rangpur, Bangladesh",
|
|
||||||
customerNote: "Please deliver between 3-5 PM",
|
|
||||||
}), // put your request body
|
}), // put your request body
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user