Files
canina/backend/src/doctors/doctors.service.ts
T

99 lines
2.3 KiB
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { DoctorQueryDto } from './dto/doctor-query.dto';
export { DoctorQueryDto as DoctorQuery };
@Injectable()
export class DoctorsService {
constructor(private readonly prisma: PrismaService) {}
async findAll(query: DoctorQueryDto = {}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 50;
const skip = (page - 1) * limit;
const where: Prisma.DoctorWhereInput = {};
if (query.activeOnly === true) {
where.isActive = true;
}
if (query.search) {
where.OR = [
{ name: { contains: query.search, mode: 'insensitive' } },
{ title: { contains: query.search, mode: 'insensitive' } },
{ clinicName: { contains: query.search, mode: 'insensitive' } },
{ bio: { contains: query.search, mode: 'insensitive' } },
{ cv: { contains: query.search, mode: 'insensitive' } },
];
}
const [doctors, total] = await Promise.all([
this.prisma.doctor.findMany({
where,
skip,
take: limit,
orderBy: [{ order: 'asc' }, { createdAt: 'desc' }],
include: {
_count: {
select: { videos: true },
},
},
}),
this.prisma.doctor.count({ where }),
]);
return {
doctors,
meta: {
total,
page,
limit,
totalPages: Math.ceil(total / limit),
},
};
}
async findOne(id: string) {
const doctor = await this.prisma.doctor.findUnique({
where: { id },
include: {
videos: {
orderBy: { createdAt: 'desc' },
},
},
});
if (!doctor) {
throw new NotFoundException(`پزشک با شناسه ${id} یافت نشد`);
}
return doctor;
}
async create(data: Prisma.DoctorCreateInput) {
return this.prisma.doctor.create({
data,
});
}
async update(id: string, data: Prisma.DoctorUpdateInput) {
await this.findOne(id);
return this.prisma.doctor.update({
where: { id },
data,
});
}
async remove(id: string) {
await this.findOne(id);
return this.prisma.doctor.delete({
where: { id },
});
}
}