feat: backend API NestJS + Prisma + JWT auth + todos los módulos
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { Controller, Get, Post, Patch, Param, Body, UseGuards, Req, Query } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { ProfessionalsService } from './professionals.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
|
||||
@ApiTags('Professionals')
|
||||
@Controller('professionals')
|
||||
export class ProfessionalsController {
|
||||
constructor(private pros: ProfessionalsService) {}
|
||||
|
||||
@Get()
|
||||
findAllActive() {
|
||||
return this.pros.findAllActive();
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
findByMe(@Req() req) {
|
||||
return this.pros.findByUserId(req.user.sub);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findById(@Param('id') id: string) {
|
||||
return this.pros.findById(id);
|
||||
}
|
||||
|
||||
@Post('request')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
request(@Req() req, @Body() data: any) {
|
||||
return this.pros.requestProfessional(req.user.sub, data);
|
||||
}
|
||||
|
||||
@Patch('me')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
update(@Req() req, @Body() data: any) {
|
||||
return this.pros.upsert(req.user.sub, data);
|
||||
}
|
||||
|
||||
@Patch('me/schedules')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
updateSchedules(@Req() req, @Body() data: { schedules: any[] }) {
|
||||
return this.pros.updateSchedules(req.user.sub, data.schedules);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ProfessionalsService } from './professionals.service';
|
||||
import { ProfessionalsController } from './professionals.controller';
|
||||
|
||||
@Module({
|
||||
providers: [ProfessionalsService],
|
||||
controllers: [ProfessionalsController],
|
||||
exports: [ProfessionalsService],
|
||||
})
|
||||
export class ProfessionalsModule {}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { Injectable, NotFoundException } 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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
findById(id: string) {
|
||||
return this.prisma.professionals.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
users: { select: { id: true, name: true, picture: true, city: true } },
|
||||
schedules: { orderBy: { day_of_week: 'asc' } },
|
||||
specializations: true,
|
||||
payment_methods: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
findByUserId(userId: string) {
|
||||
return this.prisma.professionals.findUnique({
|
||||
where: { user_id: userId },
|
||||
include: { schedules: true, specializations: true, payment_methods: true },
|
||||
});
|
||||
}
|
||||
|
||||
async upsert(userId: string, data: any) {
|
||||
const existing = await this.prisma.professionals.findUnique({ where: { user_id: userId } });
|
||||
|
||||
if (existing) {
|
||||
return this.prisma.professionals.update({ where: { user_id: userId }, data });
|
||||
}
|
||||
return this.prisma.professionals.create({ data: { ...data, user_id: userId } });
|
||||
}
|
||||
|
||||
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,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
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');
|
||||
|
||||
await this.prisma.users.update({ where: { id: userId }, data: { pro_state: 1 } });
|
||||
return this.prisma.professionals.create({
|
||||
data: {
|
||||
user_id: userId,
|
||||
identification: data.identification,
|
||||
profession: data.profession,
|
||||
address: data.address,
|
||||
identification_picture: data.identification_picture,
|
||||
certificate_picture: data.certificate_picture,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user