74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
|
||
import { Request } from "express";
|
||
import { prisma } from "../../lib/prisma";
|
||
import { AppError } from "../../utils/app_error";
|
||
|
||
const get_all_plan_from_db = async (req: Request) => {
|
||
// define your own login here
|
||
const result = await prisma.plan.findMany();
|
||
return result;
|
||
};
|
||
|
||
const get_single_plan_from_db = async (req: Request) => {
|
||
// define your own login here
|
||
const { id } = req.params;
|
||
const result = await prisma.plan.findUnique({
|
||
where: {
|
||
id: id as string
|
||
}
|
||
});
|
||
return result;
|
||
};
|
||
|
||
const create_plan_into_db = async (req: Request) => {
|
||
// 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: Request) => {
|
||
// define your own login here
|
||
const { id } = req.params;
|
||
console.log(req.body)
|
||
const isPlanExist = await prisma.plan.findFirst({
|
||
where: {
|
||
id: id as string
|
||
}
|
||
})
|
||
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: Request) => {
|
||
// 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 as string } });
|
||
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,
|
||
};
|