import { configs } from "../../configs/index.js"; import { prisma } from "../../lib/prisma.js"; import { orderEmailQueue } from "../../queues/email/order/order.email.queue.js"; import { AppError } from "../../utils/app_error.js"; import paginationHelper from "../../utils/pagination_helper.js"; const get_all_order_from_db = async (req) => { // define your own login here const search = req.query.search; const customerName = req.query.customerName; const productName = req.query.productName; // for date filter const startDate = req.query.startDate; const endDate = req.query.endDate; const status = req.query.status || undefined; const { page, limit, skip, sortBy, sortOrder } = paginationHelper(req.query); const andCondition = []; 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", }, }, ], }); } if (status) { andCondition.push({ OR: [ { status: { contains: status, mode: "insensitive", }, }, ], }); } // for date filter const dateFilter = {}; 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({ take: limit, skip, where: { AND: andCondition, }, orderBy: { [sortBy]: sortOrder, }, }); const result = await prisma.order.count({ where: { AND: andCondition, }, }); return { data: getAllOrders, pagination: { total: result, page, limit, totalPages: Math.ceil(result / limit), }, }; }; const get_single_order_from_db = async (req) => { // define your own login here const { id } = req.params; const result = await prisma.order.findUnique({ where: { id } }); return result; }; const create_order_into_db = async (req) => { const payload = req?.body; console.log(payload); payload.status = "INITIATED"; payload.paymentType = "COD"; // nwo init order const result = await prisma.order.create({ data: payload }); // if email exist sent tracking link if (payload.customerEmail) { const trackingLink = `${configs.jwt.front_end_url}/track-order/${result.id}`; await orderEmailQueue.add("order-email-queue", { email: payload.customerEmail, subject: "Order Tracking", textBody: `Your order has been created. Track your order here: ${trackingLink}`, htmlBody: `

Your order has been created. Track your order here: Track Order

`, }); } return result; }; const update_order_into_db = async (req) => { // 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; 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 }); return result; }; const delete_order_from_db = async (req) => { // define your own login here const { id } = req.params; const result = await prisma.order.delete({ where: { id } }); return result; }; export const order_service = { get_all_order_from_db, get_single_order_from_db, create_order_into_db, update_order_into_db, delete_order_from_db, };