import { prisma } from "../../lib/prisma.js"; import { AppError } from "../../utils/app_error.js"; const createSupportIntoDB = async (payload) => { const result = await prisma.support.create({ data: payload }); return result; }; const getAllSupportFromDB = async (user_id, role, search, type, status) => { const andCondition = []; if (search) { andCondition.push({ OR: [ { issueName: { contains: search, mode: "insensitive", }, }, { description: { contains: search, mode: "insensitive", }, }, ], }); } if (type) { andCondition.push({ type: type, }); } if (status) { andCondition.push({ status: status, }); } if (role !== "ADMIN") { andCondition.push({ storeAccountId: user_id, }); } const whereCondition = andCondition.length > 0 ? { AND: andCondition } : {}; const result = await prisma.support.findMany({ where: whereCondition, orderBy: { createdAt: "desc", }, }); return result; }; const getSingleSupportFromDB = async (id, userId, role) => { 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, userId, role, payload) => { 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, userId, role) => { 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, };