import { Injectable, NotFoundException, ForbiddenException } from "@nestjs/common"; import { PrismaService } from "../prisma/prisma.service"; import { CreatePreAlertDto, UpdatePreAlertStatusDto } from "./dto/pre-alert.dto"; @Injectable() export class PreAlertsService { constructor(private prisma: PrismaService) {} async findAll(user: any): Promise { const where: any = { tenantId: user.tenantId }; if (user.role === "CLIENTE") where.userId = user.id; return this.prisma.client.preAlert.findMany({ where, include: { user: { select: { firstName: true, lastName: true, email: true } } }, orderBy: { createdAt: "desc" }, }); } async create(dto: CreatePreAlertDto, user: any): Promise { return this.prisma.client.preAlert.create({ data: { tenantId: user.tenantId, userId: user.id, store: dto.store, description: dto.description, declaredValue: dto.declaredValue ?? 0, vendorTracking: dto.vendorTracking, status: "PENDIENTE", }, }); } async updateStatus(id: string, dto: UpdatePreAlertStatusDto): Promise { const alert = await this.prisma.client.preAlert.findUnique({ where: { id } }); if (!alert) throw new NotFoundException("Pre-alerta no encontrada."); return this.prisma.client.preAlert.update({ where: { id }, data: { status: dto.status as any } }); } async remove(id: string, user: any): Promise { const alert = await this.prisma.client.preAlert.findUnique({ where: { id } }); if (!alert) throw new NotFoundException("Pre-alerta no encontrada."); if (user.role === "CLIENTE" && alert.userId !== user.id) throw new ForbiddenException(); return this.prisma.client.preAlert.delete({ where: { id } }); } }