full project: admin panel, backend modules, docs

This commit is contained in:
Lizandro Guarnizo
2026-06-03 22:11:01 -05:00
parent 1635723035
commit afc096d552
94 changed files with 15994 additions and 240 deletions
@@ -0,0 +1,97 @@
import { IsString, IsOptional, IsArray, IsNumber, IsBoolean, MinLength, Matches } from 'class-validator';
export class CreateProfessionalDto {
@IsString()
@MinLength(5)
identification: string;
@IsString()
profession: string;
@IsString()
address: string;
@IsOptional()
@IsString()
additional_address?: string;
@IsOptional()
@IsString()
identification_picture?: string;
@IsOptional()
@IsString()
certificate_picture?: string;
}
export class UpdateProfessionalDto {
@IsOptional()
@IsString()
identification?: string;
@IsOptional()
@IsString()
address?: string;
@IsOptional()
@IsString()
additional_address?: string;
@IsOptional()
@IsString()
profession?: string;
@IsOptional()
@IsNumber()
rate?: number;
@IsOptional()
@IsString()
banner_picture?: string;
@IsOptional()
@IsNumber()
latitude?: number;
@IsOptional()
@IsNumber()
longitude?: number;
@IsOptional()
@IsString()
location_preferences?: string;
}
export class ScheduleDto {
@IsNumber()
day_of_week: number;
@IsOptional()
@IsBoolean()
enabled?: boolean;
@IsOptional()
@IsBoolean()
continuous_day?: boolean;
@IsOptional()
@Matches(/^\d{2}:\d{2}$/, { message: 'time must be HH:MM' })
range1_hour1?: string;
@IsOptional()
@Matches(/^\d{2}:\d{2}$/)
range1_hour2?: string;
@IsOptional()
@Matches(/^\d{2}:\d{2}$/)
range2_hour1?: string;
@IsOptional()
@Matches(/^\d{2}:\d{2}$/)
range2_hour2?: string;
}
export class UpdateSchedulesDto {
@IsArray()
schedules: ScheduleDto[];
}
@@ -1,22 +1,54 @@
import { Controller, Get, Post, Patch, Param, Body, UseGuards, Req, Query } from '@nestjs/common';
import { Controller, Get, Post, Patch, Param, Body, UseGuards, Req, HttpCode, HttpStatus, NotFoundException } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { ProfessionalsService } from './professionals.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CreateProfessionalDto, UpdateProfessionalDto, UpdateSchedulesDto } from './dto/professional.dto';
import { AuthService } from '../auth/auth.service';
@ApiTags('Professionals')
@Controller('professionals')
export class ProfessionalsController {
constructor(private pros: ProfessionalsService) {}
constructor(
private pros: ProfessionalsService,
private auth: AuthService,
) {}
@Get()
findAllActive() {
return this.pros.findAllActive();
findAllActive(@Req() req) {
const page = +(req.query.page || 1);
const limit = +(req.query.limit || 20);
return this.pros.findAllActive(page, limit);
}
@Get('pending')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
findPending(@Req() req) {
const page = +(req.query.page || 1);
const limit = +(req.query.limit || 20);
return this.pros.findPendingApprovals(page, limit);
}
@Post(':id/approve')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
approve(@Param('id') id: string) {
return this.pros.approve(id);
}
@Post(':id/deny')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
deny(@Param('id') id: string) {
return this.pros.deny(id);
}
@Get('me')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
findByMe(@Req() req) {
async findByMe(@Req() req) {
const profId = await this.auth.getProfessionalId(req.user.sub);
if (!profId) throw new NotFoundException('No eres un profesional');
return this.pros.findByUserId(req.user.sub);
}
@@ -28,21 +60,22 @@ export class ProfessionalsController {
@Post('request')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
request(@Req() req, @Body() data: any) {
return this.pros.requestProfessional(req.user.sub, data);
request(@Req() req, @Body() dto: CreateProfessionalDto) {
return this.pros.requestProfessional(req.user.sub, dto);
}
@Patch('me')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
update(@Req() req, @Body() data: any) {
return this.pros.upsert(req.user.sub, data);
async update(@Req() req, @Body() dto: UpdateProfessionalDto) {
return this.pros.upsert(req.user.sub, dto);
}
@Patch('me/schedules')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
updateSchedules(@Req() req, @Body() data: { schedules: any[] }) {
return this.pros.updateSchedules(req.user.sub, data.schedules);
async updateSchedules(@Req() req, @Body() dto: UpdateSchedulesDto) {
const prof = await this.pros.findByUserId(req.user.sub);
return this.pros.updateSchedules(prof.id, dto.schedules);
}
}
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';
import { ProfessionalsService } from './professionals.service';
import { ProfessionalsController } from './professionals.controller';
import { AuthModule } from '../auth/auth.module';
@Module({
imports: [AuthModule],
providers: [ProfessionalsService],
controllers: [ProfessionalsController],
exports: [ProfessionalsService],
@@ -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,
},