order api:implement pagination system
This commit is contained in:
@@ -1,61 +1,92 @@
|
|||||||
|
|
||||||
import { Request } from "express";
|
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 paginationHelper from "../../utils/pagination_helper";
|
||||||
|
|
||||||
const get_all_order_from_db = async (req: Request) => {
|
const get_all_order_from_db = async (req: Request) => {
|
||||||
// define your own login here
|
// define your own login here
|
||||||
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;
|
||||||
console.log(productName)
|
const status = (req.query.status as string) || undefined;
|
||||||
const andCondition=[] as any[]
|
const { page, limit, skip, sortBy, sortOrder } = paginationHelper(req.query);
|
||||||
if(search){
|
|
||||||
andCondition.push({
|
|
||||||
OR:[
|
|
||||||
{
|
|
||||||
productName:{
|
|
||||||
contains:search,
|
|
||||||
mode:"insensitive"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if(customerName){
|
|
||||||
andCondition.push({
|
|
||||||
OR:[
|
|
||||||
{
|
|
||||||
customerName:{
|
|
||||||
contains:customerName,
|
|
||||||
mode:"insensitive"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if(productName){
|
|
||||||
andCondition.push({
|
|
||||||
OR:[
|
|
||||||
{
|
|
||||||
productName:{
|
|
||||||
contains:productName,
|
|
||||||
mode:"insensitive"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(search)
|
|
||||||
|
|
||||||
const result = await prisma.order.findMany({
|
const andCondition = [] as any[];
|
||||||
where:{
|
if (search) {
|
||||||
AND:andCondition
|
andCondition.push({
|
||||||
}
|
OR: [
|
||||||
|
{
|
||||||
|
productName: {
|
||||||
|
contains: search,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (customerName) {
|
||||||
|
andCondition.push({
|
||||||
|
OR: [
|
||||||
|
{
|
||||||
|
customerName: {
|
||||||
|
contains: customerName,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (productName) {
|
||||||
|
andCondition.push({
|
||||||
|
OR: [
|
||||||
|
{
|
||||||
|
productName: {
|
||||||
|
contains: productName,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (status) {
|
||||||
|
andCondition.push({
|
||||||
|
OR: [
|
||||||
|
{
|
||||||
|
status: {
|
||||||
|
contains: status,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const getAllOrders = await prisma.order.findMany({
|
||||||
|
take: limit,
|
||||||
|
skip,
|
||||||
|
where: {
|
||||||
|
AND: andCondition,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
[sortBy as string]: sortOrder,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return result;
|
const result = await prisma.order.count({
|
||||||
|
where: {
|
||||||
|
AND: andCondition,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(status);
|
||||||
|
return {
|
||||||
|
data: getAllOrders,
|
||||||
|
pagination: {
|
||||||
|
total: result,
|
||||||
|
page,
|
||||||
|
limit,
|
||||||
|
totalPages: Math.ceil(result / limit),
|
||||||
|
},
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const get_single_order_from_db = async (req: Request) => {
|
const get_single_order_from_db = async (req: Request) => {
|
||||||
@@ -67,9 +98,9 @@ const get_single_order_from_db = async (req: Request) => {
|
|||||||
|
|
||||||
const create_order_into_db = async (req: Request) => {
|
const create_order_into_db = async (req: Request) => {
|
||||||
const payload = req?.body;
|
const payload = req?.body;
|
||||||
console.log(payload)
|
console.log(payload);
|
||||||
payload.status = "INITIATED";
|
payload.status = "INITIATED";
|
||||||
payload.paymentType = "COD"
|
payload.paymentType = "COD";
|
||||||
|
|
||||||
// nwo init order
|
// nwo init order
|
||||||
const result = await prisma.order.create({ data: payload });
|
const result = await prisma.order.create({ data: payload });
|
||||||
@@ -81,8 +112,8 @@ const create_order_into_db = async (req: Request) => {
|
|||||||
email: payload.customerEmail,
|
email: payload.customerEmail,
|
||||||
subject: "Order Tracking",
|
subject: "Order Tracking",
|
||||||
textBody: `Your order has been created. Track your order here: ${trackingLink}`,
|
textBody: `Your order has been created. Track your order here: ${trackingLink}`,
|
||||||
htmlBody: `<p>Your order has been created. Track your order here: <a href="${trackingLink}">Track Order</a></p>`
|
htmlBody: `<p>Your order has been created. Track your order here: <a href="${trackingLink}">Track Order</a></p>`,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
export const orderSwaggerDocs = {
|
export const orderSwaggerDocs = {
|
||||||
"/api/order": {
|
"/api/order": {
|
||||||
post: {
|
post: {
|
||||||
@@ -10,16 +9,16 @@ export const orderSwaggerDocs = {
|
|||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
example: JSON.stringify({
|
example: JSON.stringify({
|
||||||
"shopAccountId": "",
|
shopAccountId: "",
|
||||||
"productPrice": 1500,
|
productPrice: 1500,
|
||||||
"productQuantity": 2,
|
productQuantity: 2,
|
||||||
"productName": "Wireless Mouse",
|
productName: "Wireless Mouse",
|
||||||
"customerName": "Rahim Uddin",
|
customerName: "Rahim Uddin",
|
||||||
"customerPhone": "+8801712345678",
|
customerPhone: "+8801712345678",
|
||||||
"customerEmail": "softvence.abumahid@gmail.com",
|
customerEmail: "softvence.abumahid@gmail.com",
|
||||||
"customerAddress": "Rangpur, Bangladesh",
|
customerAddress: "Rangpur, Bangladesh",
|
||||||
"customerNote": "Please deliver between 3-5 PM"
|
customerNote: "Please deliver between 3-5 PM",
|
||||||
})
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -63,6 +62,12 @@ export const orderSwaggerDocs = {
|
|||||||
required: false,
|
required: false,
|
||||||
schema: { type: "string" },
|
schema: { type: "string" },
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "status",
|
||||||
|
in: "query",
|
||||||
|
required: false,
|
||||||
|
schema: { type: "string" },
|
||||||
|
},
|
||||||
],
|
],
|
||||||
responses: {
|
responses: {
|
||||||
200: { description: "order fetched successfully" },
|
200: { description: "order fetched successfully" },
|
||||||
@@ -106,14 +111,14 @@ export const orderSwaggerDocs = {
|
|||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
example: JSON.stringify({
|
example: JSON.stringify({
|
||||||
"shopAccountId": "",
|
shopAccountId: "",
|
||||||
"productPrice": 1500,
|
productPrice: 1500,
|
||||||
"productQuantity": 2,
|
productQuantity: 2,
|
||||||
"productName": "Wireless Mouse",
|
productName: "Wireless Mouse",
|
||||||
"customerName": "Rahim Uddin",
|
customerName: "Rahim Uddin",
|
||||||
"customerPhone": "+8801712345678",
|
customerPhone: "+8801712345678",
|
||||||
"customerAddress": "Rangpur, Bangladesh",
|
customerAddress: "Rangpur, Bangladesh",
|
||||||
"customerNote": "Please deliver between 3-5 PM"
|
customerNote: "Please deliver between 3-5 PM",
|
||||||
}), // put your request body
|
}), // put your request body
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -142,5 +147,3 @@ export const orderSwaggerDocs = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
// options type
|
||||||
|
type IOptions = {
|
||||||
|
page?: number | string;
|
||||||
|
limit?: number | string;
|
||||||
|
sortOrder?: string;
|
||||||
|
sortBy?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// return type
|
||||||
|
export type IPaginationResult = {
|
||||||
|
page: number;
|
||||||
|
limit: number;
|
||||||
|
skip: number;
|
||||||
|
sortOrder?: string;
|
||||||
|
sortBy?: string;
|
||||||
|
};
|
||||||
|
const paginationHelper = (options:IOptions): IPaginationResult => {
|
||||||
|
const page = Number(options?.page) || 1;
|
||||||
|
const limit = Number(options?.limit) || 10;
|
||||||
|
const skip = (page-1)*limit
|
||||||
|
const sortBy =options?.sortBy ||"createdAt";
|
||||||
|
const sortOrder = options?.sortOrder || "desc";
|
||||||
|
|
||||||
|
return {
|
||||||
|
page,
|
||||||
|
limit,
|
||||||
|
skip,
|
||||||
|
sortBy,
|
||||||
|
sortOrder
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export default paginationHelper;
|
||||||
Reference in New Issue
Block a user