78a9b99aae
- Added `customerEmail` field to Order model. - Made `customerNote` optional in the Order model. - Updated order creation logic to send a tracking email. - Updated order validation to handle optional fields. - Refactored configurations import path from `errors/configs` to `configs`. - Minor modifications across account and order services for consistency.
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
|
|
import { Request } from "express";
|
|
import { configs } from "../../configs";
|
|
import { prisma } from "../../lib/prisma";
|
|
import { orderEmailQueue } from "../../queues/email/order/order.email.queue";
|
|
|
|
const get_all_order_from_db = async (req: Request) => {
|
|
// define your own login here
|
|
const result = await prisma.order.findMany();
|
|
return result;
|
|
};
|
|
|
|
const get_single_order_from_db = async (req: Request) => {
|
|
// define your own login here
|
|
const { id } = req.params as { id: string };
|
|
const result = await prisma.order.findUnique({ where: { id } });
|
|
return result;
|
|
};
|
|
|
|
const create_order_into_db = async (req: Request) => {
|
|
const payload = req?.body;
|
|
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: `<p>Your order has been created. Track your order here: <a href="${trackingLink}">Track Order</a></p>`
|
|
})
|
|
}
|
|
return result;
|
|
};
|
|
|
|
const update_order_into_db = async (req: Request) => {
|
|
// define your own login here
|
|
const { id } = req.params as { id: string };
|
|
const result = await prisma.order.update({ where: { id }, data: req.body });
|
|
return result;
|
|
};
|
|
|
|
const delete_order_from_db = async (req: Request) => {
|
|
// define your own login here
|
|
const { id } = req.params as { id: string };
|
|
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,
|
|
};
|