Order api: create order schema
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { configs } from "../../configs";
|
||||
import { configs } from "../../errors/configs";
|
||||
import catchAsync from "../../utils/catch_async";
|
||||
import manageResponse from "../../utils/manage_response";
|
||||
import { account_services } from "./account.service";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import bcrypt from "bcrypt";
|
||||
import { Request } from "express";
|
||||
import { configs } from "../../configs";
|
||||
import { configs } from "../../errors/configs";
|
||||
import { prisma } from "../../lib/prisma";
|
||||
import { emailQueue } from "../../queues/email/email.queue";
|
||||
import { AppError } from "../../utils/app_error";
|
||||
@@ -17,7 +17,7 @@ const create_account_into_db = async (req: Request) => {
|
||||
|
||||
if (existingAccount) {
|
||||
throw new AppError("Email already exists", 403);
|
||||
}
|
||||
}
|
||||
// hash password
|
||||
const hashPassword = bcrypt.hashSync(payload.password, 10);
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
|
||||
import catchAsync from "../../utils/catch_async";
|
||||
import manageResponse from "../../utils/manage_response";
|
||||
import { order_service } from "./order.service";
|
||||
|
||||
const get_all_order = catchAsync(async (req, res) => {
|
||||
const result = await order_service.get_all_order_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "All order fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const get_single_order = catchAsync(async (req, res) => {
|
||||
const result = await order_service.get_single_order_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "Single order fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const create_order = catchAsync(async (req, res) => {
|
||||
const result = await order_service.create_order_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "order created successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const update_order = catchAsync(async (req, res) => {
|
||||
const result = await order_service.update_order_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "order updated successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const delete_order = catchAsync(async (req, res) => {
|
||||
const result = await order_service.delete_order_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "order deleted successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
export const order_controller = {
|
||||
get_all_order,
|
||||
get_single_order,
|
||||
create_order,
|
||||
update_order,
|
||||
delete_order,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
import { Router } from "express";
|
||||
import RequestValidator from "../../middlewares/request_validator";
|
||||
import { order_controller } from "./order.controller";
|
||||
import { order_validations } from "./order.validation";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", order_controller.get_all_order);
|
||||
router.post(
|
||||
"/",
|
||||
RequestValidator(order_validations.create_order),
|
||||
order_controller.create_order,
|
||||
);
|
||||
router.get("/:id", order_controller.get_single_order);
|
||||
router.patch(
|
||||
"/:id",
|
||||
RequestValidator(order_validations.update_order),
|
||||
order_controller.update_order,
|
||||
);
|
||||
router.delete("/:id", order_controller.delete_order);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
|
||||
import { Request } from "express";
|
||||
import { prisma } from "../../lib/prisma";
|
||||
import { AppError } from "../../utils/app_error";
|
||||
|
||||
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;
|
||||
const result = await prisma.order.findUnique({ where: { id } });
|
||||
return result;
|
||||
};
|
||||
|
||||
const create_order_into_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
|
||||
const user = req.user
|
||||
console.log(user)
|
||||
const {
|
||||
shopAccountId,
|
||||
productPrice,
|
||||
productQuantity,
|
||||
productName,
|
||||
customerName,
|
||||
customerPhone,
|
||||
customerAddress,
|
||||
customerNote,
|
||||
paymentType,
|
||||
status,
|
||||
} = req.body
|
||||
const isUserExists = await prisma.account.findFirst({
|
||||
where: {
|
||||
id: user?.accountId
|
||||
}
|
||||
})
|
||||
if (!isUserExists) {
|
||||
throw new AppError("Account not found", 404);
|
||||
}
|
||||
|
||||
const result = await prisma.order.create({
|
||||
data: {
|
||||
productPrice,
|
||||
productQuantity,
|
||||
productName,
|
||||
customerName,
|
||||
customerPhone,
|
||||
customerAddress,
|
||||
customerNote,
|
||||
paymentType,
|
||||
status,
|
||||
account: {
|
||||
connect: {
|
||||
id: user?.accountId,
|
||||
},
|
||||
},
|
||||
}
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
const update_order_into_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
const { id } = req.params;
|
||||
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;
|
||||
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,
|
||||
};
|
||||
@@ -0,0 +1,132 @@
|
||||
|
||||
export const orderSwaggerDocs = {
|
||||
"/api/order": {
|
||||
post: {
|
||||
tags: ["order"],
|
||||
summary: "Create new order",
|
||||
description: "",
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
example: JSON.stringify({
|
||||
|
||||
"productPrice": 1500,
|
||||
"productQuantity": 2,
|
||||
"productName": "Wireless Mouse",
|
||||
"customerName": "Rahim Uddin",
|
||||
"customerPhone": "+8801712345678",
|
||||
"customerAddress": "Rangpur, Bangladesh",
|
||||
"customerNote": "Please deliver between 3-5 PM",
|
||||
"paymentType": "Cash on Delivery",
|
||||
"status": "Pending"
|
||||
|
||||
}), // put your request body
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
201: { description: "order created successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
get: {
|
||||
tags: ["order"],
|
||||
summary: "Get all order",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "page",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
{
|
||||
name: "limit",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "order fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
"/api/order/{id}": {
|
||||
get: {
|
||||
tags: ["order"],
|
||||
summary: "Get single order",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "order fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
patch: {
|
||||
tags: ["order"],
|
||||
summary: "Update order",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
example: JSON.stringify({
|
||||
"shopAccountId": "shop_12345",
|
||||
"productPrice": 1500,
|
||||
"productQuantity": 2,
|
||||
"productName": "Wireless Mouse",
|
||||
"customerName": "Rahim Uddin",
|
||||
"customerPhone": "+8801712345678",
|
||||
"customerAddress": "Rangpur, Bangladesh",
|
||||
"customerNote": "Please deliver between 3-5 PM",
|
||||
"paymentType": "Cash on Delivery",
|
||||
"status": "Pending"
|
||||
}), // put your request body
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: { description: "order updated successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
delete: {
|
||||
tags: ["order"],
|
||||
summary: "Delete order",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "order delete successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
import { uuid, z } from "zod";
|
||||
|
||||
const create_order = z.object({
|
||||
shopAccountId:z.string(),
|
||||
productPrice:z.number(),
|
||||
productQuantity:z.number(),
|
||||
productName:z.string(),
|
||||
customerName:z.string(),
|
||||
customerPhone:z.string(),
|
||||
customerAddress:z.string(),
|
||||
customerNote:z.string(),
|
||||
paymentType:z.string(),
|
||||
status:z.string()
|
||||
});
|
||||
const update_order = z.object({
|
||||
shopAccountId:z.string().optional(),
|
||||
productPrice:z.number().optional(),
|
||||
productQuantity:z.number().optional(),
|
||||
productName:z.string().optional(),
|
||||
customerName:z.string().optional(),
|
||||
customerPhone:z.string().optional(),
|
||||
customerAddress:z.string().optional(),
|
||||
customerNote:z.string().optional(),
|
||||
paymentType:z.string().optional(),
|
||||
status:z.string().optional()
|
||||
});
|
||||
|
||||
export const order_validations = {
|
||||
create_order,
|
||||
update_order,
|
||||
};
|
||||
Reference in New Issue
Block a user