♻️ refactor(account, order, plan, profile, support, email): restructure application modules and enhance error handling
Updated Docker configuration, refactored middleware for improved error handling, and restructured account, order, plan, profile, and support modules, including their routes, services, and validations. Enhanced email processing queues and utilities for token generation, pagination, and response management to streamline the application architecture and enhance maintainability.
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
import catchAsync from "../../utils/catch_async.js";
|
||||
import manageResponse from "../../utils/manage_response.js";
|
||||
import { plan_service } from "./plan.service.js";
|
||||
const get_all_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.get_all_plan_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "All plan fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
const get_single_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.get_single_plan_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "Single plan fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
const create_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.create_plan_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "plan created successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
const update_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.update_plan_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "plan updated successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
const delete_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.delete_plan_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "plan deleted successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
export const plan_controller = {
|
||||
get_all_plan,
|
||||
get_single_plan,
|
||||
create_plan,
|
||||
update_plan,
|
||||
delete_plan,
|
||||
};
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
import { Router } from "express";
|
||||
import RequestValidator from "../../middlewares/request_validator.js";
|
||||
import { plan_controller } from "./plan.controller.js";
|
||||
import { plan_validations } from "./plan.validation.js";
|
||||
const router = Router();
|
||||
router.get("/", plan_controller.get_all_plan);
|
||||
router.post("/", RequestValidator(plan_validations.create_plan), plan_controller.create_plan);
|
||||
router.get("/:id", plan_controller.get_single_plan);
|
||||
router.patch("/:id", RequestValidator(plan_validations.update_plan), plan_controller.update_plan);
|
||||
router.delete("/:id", plan_controller.delete_plan);
|
||||
export default router;
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
import { prisma } from "../../lib/prisma.js";
|
||||
import { AppError } from "../../utils/app_error.js";
|
||||
const get_all_plan_from_db = async (req) => {
|
||||
// define your own login here
|
||||
const result = await prisma.plan.findMany();
|
||||
return result;
|
||||
};
|
||||
const get_single_plan_from_db = async (req) => {
|
||||
// define your own login here
|
||||
const { id } = req.params;
|
||||
const result = await prisma.plan.findUnique({
|
||||
where: {
|
||||
id: id
|
||||
}
|
||||
});
|
||||
return result;
|
||||
};
|
||||
const create_plan_into_db = async (req) => {
|
||||
// define your own login here
|
||||
const user = req.user;
|
||||
if (user?.role !== "ADMIN") {
|
||||
throw new AppError("You don’t have permission to create plan information.!!!", 401);
|
||||
}
|
||||
const result = await prisma.plan.create({ data: req.body });
|
||||
return result;
|
||||
};
|
||||
const update_plan_into_db = async (req) => {
|
||||
// define your own login here
|
||||
const { id } = req.params;
|
||||
const user = req.user;
|
||||
if (user?.role !== "ADMIN") {
|
||||
throw new AppError("You don’t have permission to update plan information.!!!", 401);
|
||||
}
|
||||
const isPlanExist = await prisma.plan.findFirst({
|
||||
where: {
|
||||
id: id
|
||||
}
|
||||
});
|
||||
if (!isPlanExist) {
|
||||
throw new AppError("The plan is not available!!!", 404);
|
||||
}
|
||||
const result = await prisma.plan.update({
|
||||
where: {
|
||||
id: isPlanExist.id
|
||||
},
|
||||
data: {
|
||||
...req.body
|
||||
}
|
||||
});
|
||||
return result;
|
||||
};
|
||||
const delete_plan_from_db = async (req) => {
|
||||
// define your own login here
|
||||
const { id } = req.params;
|
||||
const user = req.user;
|
||||
if (user?.role !== "ADMIN") {
|
||||
throw new AppError("You don’t have permission to delete plan information.!!!", 401);
|
||||
}
|
||||
const result = await prisma.plan.delete({ where: { id: id } });
|
||||
return result;
|
||||
};
|
||||
export const plan_service = {
|
||||
get_all_plan_from_db,
|
||||
get_single_plan_from_db,
|
||||
create_plan_into_db,
|
||||
update_plan_into_db,
|
||||
delete_plan_from_db,
|
||||
};
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
export const planSwaggerDocs = {
|
||||
"/api/plan": {
|
||||
post: {
|
||||
tags: ["plan"],
|
||||
summary: "Create new plan",
|
||||
description: "",
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
example: JSON.stringify({
|
||||
"planName": "PRO Plan",
|
||||
"price": 12,
|
||||
"planType": "PRO",
|
||||
"planDesc": "The plan is only for pro users",
|
||||
"planFeatures": {
|
||||
"storage": "10GB",
|
||||
"projects": 5,
|
||||
"support": "Email Support"
|
||||
}
|
||||
}), // put your request body
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
201: { description: "plan created successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
get: {
|
||||
tags: ["plan"],
|
||||
summary: "Get all plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "page",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
{
|
||||
name: "limit",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "plan fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
"/api/plan/{id}": {
|
||||
get: {
|
||||
tags: ["plan"],
|
||||
summary: "Get single plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "plan fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
patch: {
|
||||
tags: ["plan"],
|
||||
summary: "Update plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
example: JSON.stringify({
|
||||
"planName": "PRO Plan",
|
||||
"price": 12,
|
||||
"planType": "PRO",
|
||||
"planDesc": "The plan is only for pro users",
|
||||
"planFeatures": {
|
||||
"storage": "10GB",
|
||||
"projects": 5,
|
||||
"support": "Email Support"
|
||||
}
|
||||
}), // put your request body
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: { description: "plan updated successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
delete: {
|
||||
tags: ["plan"],
|
||||
summary: "Delete plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "plan delete successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
import { z } from "zod";
|
||||
const create_plan = z.object({
|
||||
planName: z.string("Enter the plan name..."),
|
||||
price: z.number("Enter the plan price..."),
|
||||
planType: z.enum(["FREE", "STANDARD", "PRO"]),
|
||||
planDesc: z.string("Enter the plan description..."),
|
||||
planFeatures: z.union([
|
||||
z.string(),
|
||||
z.number(),
|
||||
z.boolean(),
|
||||
z.null(),
|
||||
z.array(z.any()),
|
||||
z.record(z.string(), z.any())
|
||||
])
|
||||
});
|
||||
const update_plan = z.object({
|
||||
planName: z.string(),
|
||||
price: z.number(),
|
||||
planType: z.enum(["FREE", "STANDARD", "PRO"]),
|
||||
planDesc: z.string().optional(),
|
||||
planFeatures: z.union([
|
||||
z.string(),
|
||||
z.number(),
|
||||
z.boolean(),
|
||||
z.null(),
|
||||
z.array(z.any()),
|
||||
z.record(z.string(), z.any())
|
||||
])
|
||||
});
|
||||
export const plan_validations = {
|
||||
create_plan,
|
||||
update_plan,
|
||||
};
|
||||
Reference in New Issue
Block a user