69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
|
|
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,
|
|||
|
|
};
|