feat: backend API NestJS + Prisma + JWT auth + todos los módulos
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { Controller, Get, Post, Patch, Param, Body, UseGuards, Req } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { ServicesService } from './services.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
|
||||
@ApiTags('Services')
|
||||
@Controller('services')
|
||||
export class ServicesController {
|
||||
constructor(private services: ServicesService) {}
|
||||
|
||||
@Post()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
create(@Req() req, @Body() data: any) {
|
||||
return this.services.create({ ...data, user_id: req.user.sub });
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
findByMe(@Req() req) {
|
||||
return this.services.findByUser(req.user.sub);
|
||||
}
|
||||
|
||||
@Get('professional')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
findByProfessional(@Req() req) {
|
||||
return this.services.findByProfessional(req.user.sub);
|
||||
}
|
||||
|
||||
@Get('professional/requests')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
requestsByProfessional(@Req() req) {
|
||||
return this.services.findRequestsByProfessional(req.user.sub);
|
||||
}
|
||||
|
||||
@Get('professional/history')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
historyByProfessional(@Req() req) {
|
||||
return this.services.getHistoryByProfessional(req.user.sub);
|
||||
}
|
||||
|
||||
@Get('me/history')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
historyByUser(@Req() req) {
|
||||
return this.services.getHistoryByUser(req.user.sub);
|
||||
}
|
||||
|
||||
@Get('professional/calendar')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
calendarByProfessional(@Req() req) {
|
||||
return this.services.getCalendarByProfessional(req.user.sub);
|
||||
}
|
||||
|
||||
@Get('public-calendar/:professionalId')
|
||||
getPublicCalendar(@Param('professionalId') id: string) {
|
||||
return this.services.getPublicCalendar(id);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
findById(@Param('id') id: string) {
|
||||
return this.services.findById(id);
|
||||
}
|
||||
|
||||
@Patch(':id/status')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
updateStatus(@Param('id') id: string, @Body() dto: { status: string }) {
|
||||
return this.services.updateStatus(id, dto.status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ServicesService } from './services.service';
|
||||
import { ServicesController } from './services.controller';
|
||||
|
||||
@Module({
|
||||
providers: [ServicesService],
|
||||
controllers: [ServicesController],
|
||||
exports: [ServicesService],
|
||||
})
|
||||
export class ServicesModule {}
|
||||
@@ -0,0 +1,94 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class ServicesService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
create(data: {
|
||||
professional_id: string;
|
||||
user_id: string;
|
||||
day: string;
|
||||
description?: string;
|
||||
rate?: number;
|
||||
range1_hour1?: string;
|
||||
range1_hour2?: string;
|
||||
address?: string;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
location_preference?: 'office' | 'delivery';
|
||||
}) {
|
||||
return this.prisma.services.create({ data: { ...data, day: new Date(data.day) } as any });
|
||||
}
|
||||
|
||||
findByUser(userId: string) {
|
||||
return this.prisma.services.findMany({
|
||||
where: { user_id: userId },
|
||||
include: { professionals: { include: { users: true } } },
|
||||
orderBy: { created_at: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
findByProfessional(professionalId: string) {
|
||||
return this.prisma.services.findMany({
|
||||
where: { professional_id: professionalId },
|
||||
include: { users: { select: { id: true, name: true, picture: true, phone: true } } },
|
||||
orderBy: { created_at: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
findRequestsByProfessional(professionalId: string) {
|
||||
return this.prisma.services.findMany({
|
||||
where: { professional_id: professionalId, status: 'pending' },
|
||||
include: { users: { select: { id: true, name: true, picture: true, phone: true } } },
|
||||
orderBy: { created_at: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
updateStatus(id: string, status: string) {
|
||||
return this.prisma.services.update({ where: { id }, data: { status: status as any } });
|
||||
}
|
||||
|
||||
findById(id: string) {
|
||||
return this.prisma.services.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
users: { select: { id: true, name: true, picture: true, phone: true } },
|
||||
professionals: { include: { users: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getHistoryByUser(userId: string) {
|
||||
return this.prisma.services.findMany({
|
||||
where: { user_id: userId, status: { in: ['completed', 'cancelled'] } },
|
||||
include: { professionals: { include: { users: true } } },
|
||||
orderBy: { day: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
getHistoryByProfessional(professionalId: string) {
|
||||
return this.prisma.services.findMany({
|
||||
where: { professional_id: professionalId, status: { in: ['completed', 'cancelled'] } },
|
||||
include: { users: { select: { id: true, name: true, picture: true } } },
|
||||
orderBy: { day: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
getCalendarByProfessional(professionalId: string) {
|
||||
return this.prisma.services.findMany({
|
||||
where: { professional_id: professionalId, status: { notIn: ['denied', 'cancelled'] } },
|
||||
orderBy: { day: 'asc' },
|
||||
});
|
||||
}
|
||||
|
||||
async getPublicCalendar(professionalId: string) {
|
||||
const schedules = await this.prisma.schedules.findMany({
|
||||
where: { professional_id: professionalId, enabled: true },
|
||||
});
|
||||
const services = await this.prisma.services.findMany({
|
||||
where: { professional_id: professionalId, status: { notIn: ['denied', 'cancelled'] } },
|
||||
});
|
||||
return { schedules, services };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user