full project: admin panel, backend modules, docs
This commit is contained in:
@@ -1,24 +1,31 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException, BadRequestException, ForbiddenException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class ProfessionalsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
findAllActive() {
|
||||
return this.prisma.professionals.findMany({
|
||||
where: { is_active: true },
|
||||
include: {
|
||||
users: { select: { id: true, name: true, picture: true, city: true } },
|
||||
schedules: true,
|
||||
specializations: true,
|
||||
payment_methods: true,
|
||||
},
|
||||
});
|
||||
async findAllActive(page = 1, limit = 20) {
|
||||
const skip = (page - 1) * limit;
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.professionals.findMany({
|
||||
where: { is_active: true },
|
||||
skip,
|
||||
take: limit,
|
||||
include: {
|
||||
users: { select: { id: true, name: true, picture: true, city: true } },
|
||||
schedules: true,
|
||||
specializations: true,
|
||||
payment_methods: true,
|
||||
},
|
||||
}),
|
||||
this.prisma.professionals.count({ where: { is_active: true } }),
|
||||
]);
|
||||
return { data, meta: { total, page, limit, totalPages: Math.ceil(total / limit) || 1 } };
|
||||
}
|
||||
|
||||
findById(id: string) {
|
||||
return this.prisma.professionals.findUnique({
|
||||
async findById(id: string) {
|
||||
const prof = await this.prisma.professionals.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
users: { select: { id: true, name: true, picture: true, city: true } },
|
||||
@@ -27,43 +34,106 @@ export class ProfessionalsService {
|
||||
payment_methods: true,
|
||||
},
|
||||
});
|
||||
if (!prof) throw new NotFoundException('Profesional no encontrado');
|
||||
return prof;
|
||||
}
|
||||
|
||||
findByUserId(userId: string) {
|
||||
return this.prisma.professionals.findUnique({
|
||||
async findByUserId(userId: string) {
|
||||
const prof = await this.prisma.professionals.findUnique({
|
||||
where: { user_id: userId },
|
||||
include: { schedules: true, specializations: true, payment_methods: true },
|
||||
});
|
||||
if (!prof) throw new NotFoundException('No eres un profesional registrado');
|
||||
return prof;
|
||||
}
|
||||
|
||||
async upsert(userId: string, data: any) {
|
||||
const existing = await this.prisma.professionals.findUnique({ where: { user_id: userId } });
|
||||
const prof = await this.prisma.professionals.findUnique({ where: { user_id: userId } });
|
||||
if (!prof) throw new NotFoundException('Debes solicitar ser profesional primero');
|
||||
|
||||
if (existing) {
|
||||
return this.prisma.professionals.update({ where: { user_id: userId }, data });
|
||||
}
|
||||
return this.prisma.professionals.create({ data: { ...data, user_id: userId } });
|
||||
return this.prisma.professionals.update({ where: { user_id: userId }, data });
|
||||
}
|
||||
|
||||
async updateSchedules(professionalId: string, schedules: any[]) {
|
||||
await this.prisma.schedules.deleteMany({ where: { professional_id: professionalId } });
|
||||
return this.prisma.schedules.createMany({
|
||||
data: schedules.map((s: any) => ({
|
||||
professional_id: professionalId,
|
||||
day_of_week: s.day_of_week,
|
||||
enabled: s.enabled,
|
||||
continuous_day: s.continuous_day,
|
||||
range1_hour1: s.range1_hour1,
|
||||
range1_hour2: s.range1_hour2,
|
||||
range2_hour1: s.range2_hour1,
|
||||
range2_hour2: s.range2_hour2,
|
||||
})),
|
||||
const prof = await this.prisma.professionals.findUnique({ where: { id: professionalId } });
|
||||
if (!prof) throw new NotFoundException('Profesional no encontrado');
|
||||
|
||||
for (const s of schedules) {
|
||||
if (s.range1_hour1 && s.range1_hour2 && s.range1_hour1 >= s.range1_hour2) {
|
||||
throw new BadRequestException(`Día ${s.day_of_week}: range1_hour1 debe ser menor que range1_hour2`);
|
||||
}
|
||||
}
|
||||
|
||||
const result = await this.prisma.$transaction(async (tx: any) => {
|
||||
await tx.schedules.deleteMany({ where: { professional_id: professionalId } });
|
||||
if (schedules.length > 0) {
|
||||
await tx.schedules.createMany({
|
||||
data: schedules.map((s: any) => ({
|
||||
professional_id: professionalId,
|
||||
day_of_week: s.day_of_week,
|
||||
enabled: s.enabled ?? false,
|
||||
continuous_day: s.continuous_day ?? false,
|
||||
range1_hour1: s.range1_hour1 ? new Date(`1970-01-01T${s.range1_hour1}:00`) : null,
|
||||
range1_hour2: s.range1_hour2 ? new Date(`1970-01-01T${s.range1_hour2}:00`) : null,
|
||||
range2_hour1: s.range2_hour1 ? new Date(`1970-01-01T${s.range2_hour1}:00`) : null,
|
||||
range2_hour2: s.range2_hour2 ? new Date(`1970-01-01T${s.range2_hour2}:00`) : null,
|
||||
})),
|
||||
});
|
||||
}
|
||||
return tx.schedules.findMany({ where: { professional_id: professionalId } });
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async findPendingApprovals(page = 1, limit = 20) {
|
||||
const skip = (page - 1) * limit;
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.professionals.findMany({
|
||||
where: { is_active: false },
|
||||
skip,
|
||||
take: limit,
|
||||
include: { users: { select: { id: true, name: true, email: true, phone: true, created_at: true } } },
|
||||
}),
|
||||
this.prisma.professionals.count({ where: { is_active: false } }),
|
||||
]);
|
||||
return { data, meta: { total, page, limit, totalPages: Math.ceil(total / limit) || 1 } };
|
||||
}
|
||||
|
||||
async approve(professionalId: string) {
|
||||
const prof = await this.prisma.professionals.findUnique({ where: { id: professionalId } });
|
||||
if (!prof) throw new NotFoundException('Profesional no encontrado');
|
||||
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.professionals.update({
|
||||
where: { id: professionalId },
|
||||
data: { is_active: true },
|
||||
}),
|
||||
this.prisma.users.update({
|
||||
where: { id: prof.user_id },
|
||||
data: { pro_state: 2 },
|
||||
}),
|
||||
]);
|
||||
return { message: 'Profesional aprobado' };
|
||||
}
|
||||
|
||||
async deny(professionalId: string) {
|
||||
const prof = await this.prisma.professionals.findUnique({ where: { id: professionalId } });
|
||||
if (!prof) throw new NotFoundException('Profesional no encontrado');
|
||||
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.professionals.delete({ where: { id: professionalId } }),
|
||||
this.prisma.users.update({
|
||||
where: { id: prof.user_id },
|
||||
data: { pro_state: 3 },
|
||||
}),
|
||||
]);
|
||||
return { message: 'Solicitud rechazada' };
|
||||
}
|
||||
|
||||
async requestProfessional(userId: string, data: any) {
|
||||
const existing = await this.prisma.professionals.findUnique({ where: { user_id: userId } });
|
||||
if (existing) throw new Error('Ya tienes una solicitud de profesional');
|
||||
if (existing) throw new BadRequestException('Ya tienes una solicitud de profesional');
|
||||
|
||||
await this.prisma.users.update({ where: { id: userId }, data: { pro_state: 1 } });
|
||||
return this.prisma.professionals.create({
|
||||
@@ -72,6 +142,7 @@ export class ProfessionalsService {
|
||||
identification: data.identification,
|
||||
profession: data.profession,
|
||||
address: data.address,
|
||||
additional_address: data.additional_address,
|
||||
identification_picture: data.identification_picture,
|
||||
certificate_picture: data.certificate_picture,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user