183 lines
4.2 KiB
TypeScript
183 lines
4.2 KiB
TypeScript
import {prisma} from "@/common/lib/prisma";
|
|
import {ServerResponse} from "@/common/types";
|
|
import {handlePrismaError} from "@/common/utils/functions";
|
|
import {Controller} from "@/core/controller/main.controller";
|
|
|
|
class MedicalPackageServiceClass extends Controller {
|
|
async create(data: any) {
|
|
const {translations, id, icon, parent_id, priority, thumbnail_id,price} = data;
|
|
try {
|
|
await prisma.medicalPackage.create({
|
|
data: {
|
|
icon,
|
|
translations: {
|
|
create: translations,
|
|
},
|
|
priority,
|
|
price,
|
|
parent_id: parent_id ? +parent_id : null,
|
|
...(thumbnail_id && {thumbnail_id: +thumbnail_id}),
|
|
},
|
|
|
|
include: {
|
|
translations: true,
|
|
},
|
|
});
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.log(error);
|
|
handlePrismaError(error);
|
|
}
|
|
}
|
|
async getAllWithPagination(req: any, res: ServerResponse) {
|
|
const langId = req?.langId;
|
|
const page = req?.query?.page;
|
|
const limit = req?.query?.limit;
|
|
const skip = (page - 1) * limit;
|
|
const search = req?.query?.search;
|
|
try {
|
|
const [data, total] = await Promise.all([
|
|
prisma.medicalPackage.findMany({
|
|
skip,
|
|
take: +limit,
|
|
where: {
|
|
translations: {
|
|
some: {
|
|
lang_id: langId,
|
|
...(search && {
|
|
displayName: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
|
|
select: {
|
|
id: true,
|
|
icon: true,
|
|
priority: true,
|
|
thumbnail_id: true,
|
|
parent_id: true,
|
|
price:true,
|
|
thumbnail: {
|
|
select: {
|
|
filename: true,
|
|
fileUrl:true,
|
|
fileKey:true,
|
|
},
|
|
},
|
|
children: true,
|
|
translations: {
|
|
where: {
|
|
lang_id: langId,
|
|
},
|
|
select: {
|
|
lang_id: true,
|
|
content: true,
|
|
title: true,
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
await prisma.medicalPackage.count({
|
|
where: {
|
|
translations: {
|
|
some: {
|
|
lang_id: langId,
|
|
...(search && {
|
|
displayName: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
]);
|
|
|
|
return {
|
|
page,
|
|
limit,
|
|
total,
|
|
totalPages: Math.ceil(total / limit),
|
|
hasNext: page * limit < total,
|
|
hasPrev: page > 1,
|
|
data,
|
|
};
|
|
} catch (error) {
|
|
handlePrismaError(error);
|
|
}
|
|
}
|
|
async getAllParentsWithoutPagination() {
|
|
try {
|
|
const data = await prisma.medicalPackage.findMany({
|
|
where: {
|
|
parent_id: null,
|
|
},
|
|
include: {
|
|
translations: true,
|
|
},
|
|
});
|
|
|
|
return data;
|
|
} catch (error) {
|
|
handlePrismaError(error);
|
|
}
|
|
}
|
|
async getSingleMedicalPackage(req: any, id: string) {
|
|
const langId = req?.langId;
|
|
|
|
try {
|
|
const data = await prisma.medicalPackage.findUnique({
|
|
where: {
|
|
id: +id,
|
|
translations: {
|
|
some: {
|
|
lang_id: +langId,
|
|
},
|
|
},
|
|
},
|
|
include: {
|
|
translations: {
|
|
select: {
|
|
lang_id: true,
|
|
title: true,
|
|
content: true,
|
|
},
|
|
},
|
|
thumbnail:{
|
|
select:{
|
|
fileKey:true,
|
|
filename:true,
|
|
fileUrl:true
|
|
}
|
|
}
|
|
},
|
|
});
|
|
return data;
|
|
} catch (error) {
|
|
handlePrismaError(error);
|
|
}
|
|
}
|
|
async delete(id: string) {
|
|
try {
|
|
await prisma.medicalPackage.delete({
|
|
where: {
|
|
id: +id,
|
|
},
|
|
});
|
|
|
|
return true;
|
|
} catch (error) {
|
|
handlePrismaError(error);
|
|
}
|
|
}
|
|
}
|
|
|
|
const MedicalPackageService = new MedicalPackageServiceClass();
|
|
export default MedicalPackageService;
|