140 lines
2.7 KiB
TypeScript
140 lines
2.7 KiB
TypeScript
|
|
import { prisma } from "../../lib/prisma";
|
||
|
|
import { Prisma } from "../../../../prisma/generated/prisma/client";
|
||
|
|
import { AppError } from "../../utils/app_error";
|
||
|
|
|
||
|
|
const createSupportIntoDB = async (payload: any) => {
|
||
|
|
const result = await prisma.support.create({ data: payload });
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
const getAllSupportFromDB = async (
|
||
|
|
user_id: string,
|
||
|
|
role: string,
|
||
|
|
search?: string,
|
||
|
|
type?: string,
|
||
|
|
status?: string,
|
||
|
|
) => {
|
||
|
|
const andCondition: Prisma.SupportWhereInput[] = [];
|
||
|
|
|
||
|
|
if (search) {
|
||
|
|
andCondition.push({
|
||
|
|
OR: [
|
||
|
|
{
|
||
|
|
issueName: {
|
||
|
|
contains: search,
|
||
|
|
mode: "insensitive",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
description: {
|
||
|
|
contains: search,
|
||
|
|
mode: "insensitive",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
],
|
||
|
|
});
|
||
|
|
}
|
||
|
|
if (type) {
|
||
|
|
andCondition.push({
|
||
|
|
type: type as any,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (status) {
|
||
|
|
andCondition.push({
|
||
|
|
status: status as any,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (role !== "ADMIN") {
|
||
|
|
andCondition.push({
|
||
|
|
storeAccountId: user_id,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const whereCondition: Prisma.SupportWhereInput =
|
||
|
|
andCondition.length > 0 ? { AND: andCondition } : {};
|
||
|
|
|
||
|
|
const result = await prisma.support.findMany({
|
||
|
|
where: whereCondition,
|
||
|
|
orderBy: {
|
||
|
|
createdAt: "desc",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
const getSingleSupportFromDB = async (
|
||
|
|
id: string,
|
||
|
|
userId: string,
|
||
|
|
role: string,
|
||
|
|
) => {
|
||
|
|
const support = await prisma.support.findUnique({
|
||
|
|
where: { id },
|
||
|
|
});
|
||
|
|
if (!support) {
|
||
|
|
throw new AppError("Support not found", 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (role !== "ADMIN" && support.storeAccountId !== userId) {
|
||
|
|
throw new AppError("You are not authorized", 403);
|
||
|
|
}
|
||
|
|
return support;
|
||
|
|
};
|
||
|
|
|
||
|
|
const updateSupportIntoDB = async (
|
||
|
|
id: string,
|
||
|
|
userId: string,
|
||
|
|
role: string,
|
||
|
|
payload: any,
|
||
|
|
) => {
|
||
|
|
const support = await prisma.support.findUnique({
|
||
|
|
where: { id },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!support) {
|
||
|
|
throw new AppError("Support not found", 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (role !== "ADMIN" && support.storeAccountId !== userId) {
|
||
|
|
throw new AppError("You are not authorized", 403);
|
||
|
|
}
|
||
|
|
|
||
|
|
const result = await prisma.support.update({
|
||
|
|
where: { id },
|
||
|
|
data: payload,
|
||
|
|
});
|
||
|
|
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
const deleteSupportFromDB = async (
|
||
|
|
id: string,
|
||
|
|
userId: string,
|
||
|
|
role: string,
|
||
|
|
) => {
|
||
|
|
const support = await prisma.support.findUnique({
|
||
|
|
where: { id },
|
||
|
|
});
|
||
|
|
if (!support) {
|
||
|
|
throw new AppError("Support not found", 404);
|
||
|
|
}
|
||
|
|
if (role !== "ADMIN" && support.storeAccountId !== userId) {
|
||
|
|
throw new AppError("You are not authorized", 403);
|
||
|
|
}
|
||
|
|
|
||
|
|
const result = await prisma.support.delete({
|
||
|
|
where: {id}
|
||
|
|
})
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const support_service = {
|
||
|
|
createSupportIntoDB,
|
||
|
|
getAllSupportFromDB,
|
||
|
|
getSingleSupportFromDB,
|
||
|
|
updateSupportIntoDB,
|
||
|
|
deleteSupportFromDB,
|
||
|
|
};
|