2026-04-21 03:12:39 +06:00
|
|
|
import { prisma } from "../../lib/prisma.js";
|
|
|
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
|
import { AppError } from "../../utils/app_error.js";
|
2026-04-13 00:35:51 +06:00
|
|
|
|
|
|
|
|
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,
|
2026-04-17 00:19:22 +06:00
|
|
|
page: number = 1,
|
|
|
|
|
limit: number = 10,
|
2026-04-13 00:35:51 +06:00
|
|
|
) => {
|
|
|
|
|
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 } : {};
|
|
|
|
|
|
2026-04-17 00:19:22 +06:00
|
|
|
const skip = (page - 1) * limit;
|
|
|
|
|
|
|
|
|
|
const [data, total] = await Promise.all([
|
|
|
|
|
prisma.support.findMany({
|
|
|
|
|
where: whereCondition,
|
|
|
|
|
skip,
|
|
|
|
|
take: limit,
|
|
|
|
|
orderBy: {
|
|
|
|
|
createdAt: "desc",
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
prisma.support.count({
|
|
|
|
|
where: whereCondition,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
meta: {
|
|
|
|
|
page,
|
|
|
|
|
limit,
|
|
|
|
|
total,
|
|
|
|
|
totalPage: Math.ceil(total / limit),
|
2026-04-13 00:35:51 +06:00
|
|
|
},
|
2026-04-17 00:19:22 +06:00
|
|
|
data,
|
|
|
|
|
};
|
2026-04-13 00:35:51 +06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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({
|
2026-04-17 00:19:22 +06:00
|
|
|
where: { id },
|
|
|
|
|
});
|
2026-04-13 00:35:51 +06:00
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const support_service = {
|
|
|
|
|
createSupportIntoDB,
|
|
|
|
|
getAllSupportFromDB,
|
|
|
|
|
getSingleSupportFromDB,
|
|
|
|
|
updateSupportIntoDB,
|
|
|
|
|
deleteSupportFromDB,
|
|
|
|
|
};
|