contactrus module

This commit is contained in:
2025-12-10 10:44:18 +03:30
parent 525362dce1
commit 7ab766a563
11 changed files with 319 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
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<Contact> {
const contact = this.contactRepository.create({
...createContactDto,
status: ContactStatusEnum.New,
});
await this.em.persistAndFlush(contact);
return contact;
}
async findAllPaginated(dto: FindContactsDto): Promise<PaginatedResult<Contact>> {
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<Contact> {
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<Contact> {
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<void> {
const contact = await this.contactRepository.findOne({ id });
if (!contact) {
throw new NotFoundException(`Contact with ID ${id} not found`);
}
await this.em.removeAndFlush(contact);
}
}