import { Injectable, NotFoundException } from '@nestjs/common'; import { CreateContactDto } from './dto/create-contact.dto'; import { FindContactsDto } from './dto/find-contacts.dto'; import { UpdateContactStatusDto } from './dto/update-contact-status.dto'; import { ContactRepository } from './repositories/contact.repository'; import { Contact } from './entities/contact.entity'; import { PaginatedResult } from 'src/common/interfaces/pagination.interface'; import { ContactStatusEnum } from './interface/interface'; import { EntityManager } from '@mikro-orm/postgresql'; @Injectable() export class ContactService { constructor( private readonly contactRepository: ContactRepository, private readonly em: EntityManager, ) {} async create(createContactDto: CreateContactDto): Promise { const contact = this.contactRepository.create({ ...createContactDto, status: ContactStatusEnum.New, }); await this.em.persistAndFlush(contact); return contact; } async findAllPaginated(dto: FindContactsDto): Promise> { return this.contactRepository.findAllPaginated({ page: dto.page, limit: dto.limit, search: dto.search, orderBy: dto.orderBy, order: dto.order, status: dto.status, }); } async findOne(id: string): Promise { const contact = await this.contactRepository.findOne({ id }); if (!contact) { throw new NotFoundException(`Contact with ID ${id} not found`); } return contact; } async updateStatus(id: string, dto: UpdateContactStatusDto): Promise { const contact = await this.contactRepository.findOne({ id }); if (!contact) { throw new NotFoundException(`Contact with ID ${id} not found`); } contact.status = dto.status; await this.em.persistAndFlush(contact); return contact; } async remove(id: string): Promise { const contact = await this.contactRepository.findOne({ id }); if (!contact) { throw new NotFoundException(`Contact with ID ${id} not found`); } await this.em.removeAndFlush(contact); } }