import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { ChatLog } from '../../entities/chat-log.entity'; import { FlagMessageDto } from './dto/flag-message.dto'; @Injectable() export class ChatService { constructor( @InjectRepository(ChatLog) private readonly chatRepo: Repository, ) {} /** * Get recent chat messages for a license */ async getMessages(licenseId: string, limit: number = 100) { const messages = await this.chatRepo.find({ where: { license_id: licenseId }, order: { created_at: 'DESC' }, take: limit, }); // Return in chronological order (oldest first for display) return { messages: messages.reverse() }; } /** * Flag or unflag a chat message */ async flagMessage( licenseId: string, messageId: string, userId: string, dto: FlagMessageDto, ) { const message = await this.chatRepo.findOne({ where: { id: messageId, license_id: licenseId }, }); if (!message) { throw new NotFoundException('Message not found'); } message.flagged = dto.flagged; message.flagged_by = dto.flagged ? userId : null; message.flag_reason = dto.flagged ? (dto.flag_reason || null) : null; return await this.chatRepo.save(message); } }