import { Body, Controller, Get, Post, Put, Param, UseGuards, Req, Query, SetMetadata, } from "@nestjs/common"; import { Request } from "express"; import { PatientsService } from "./patients.service"; import { JwtAuthGuard } from "../auth/guards/jwt-auth.guard"; import { RolesGuard } from "../auth/guards/roles.guard"; import { GetPatientsDto } from "./dto/get-patient-dto"; import { UpdatePatientDto } from "./dto/update-patient.dto"; const Roles = (...roles: string[]) => SetMetadata("roles", roles); @Controller("patients") @UseGuards(JwtAuthGuard, RolesGuard) export class PatientsController { constructor(private readonly patientsService: PatientsService) {} @Post() @Roles("SUPER_ADMIN", "FACILITY_ADMIN", "FACILITY_STAFF") create( @Body() data: { firstName: string; lastName: string; phone: string; age: number; address?: string; gestationalAge: number; estimatedDeliveryDate?: Date; facilityId: string; email?: string; password: string; }, ) { return this.patientsService.create(data); } @Get() @Roles("SUPER_ADMIN", "FACILITY_ADMIN", "FACILITY_STAFF") findAll( @Req() req: Request & { user: any; }, @Query() query: GetPatientsDto, ) { if (req.user.role === "SUPER_ADMIN") { return this.patientsService.findAll(undefined, query); } return this.patientsService.findAll( req.user.healthcareWorker?.facilityId, query, ); } @Get("defaulters") @Roles("FACILITY_ADMIN", "FACILITY_STAFF") getDefaulters( @Req() req: Request & { user: any; }, ) { return this.patientsService.getDefaulters( req.user.healthcareWorker?.facilityId, ); } @Get(":id") @Roles("SUPER_ADMIN", "FACILITY_ADMIN", "FACILITY_STAFF", "PATIENT") findOne(@Param("id") id: string) { return this.patientsService.findOne(id); } @Put(":id") @Roles("SUPER_ADMIN", "FACILITY_ADMIN", "FACILITY_STAFF") update(@Param("id") id: string, @Body() data: UpdatePatientDto) { return this.patientsService.update(id, data); } }