109 lines
3.3 KiB
TypeScript
109 lines
3.3 KiB
TypeScript
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';
|
|
import { ContactScope } from '../interface/interface';
|
|
import { Shop } from '../../shops/entities/shop.entity';
|
|
|
|
@Injectable()
|
|
export class ContactService {
|
|
constructor(
|
|
private readonly contactRepository: ContactRepository,
|
|
private readonly em: EntityManager,
|
|
) { }
|
|
|
|
async create(dto: CreateContactDto, shopId?: string, slug?: string): Promise<Contact> {
|
|
|
|
|
|
let shop: Shop | null = null;
|
|
|
|
if ((shopId || slug) && dto.scope === ContactScope.SHOP) {
|
|
shop = await this.em.findOne(Shop, {
|
|
...(shopId ? { id: shopId } : {})
|
|
, ...(slug ? { slug: slug } : {})
|
|
});
|
|
if (!shop) {
|
|
throw new NotFoundException(`Shop with ID ${shopId} not found`);
|
|
}
|
|
}
|
|
|
|
const contact = this.contactRepository.create({
|
|
...dto,
|
|
status: ContactStatusEnum.New,
|
|
shop,
|
|
});
|
|
await this.em.persistAndFlush(contact);
|
|
return contact;
|
|
}
|
|
|
|
async findAllPaginatedAdmin(dto: FindContactsDto, shopId?: string): 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,
|
|
scope: ContactScope.SHOP,
|
|
shopId
|
|
});
|
|
}
|
|
async findAllPaginatedSuper(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,
|
|
scope: ContactScope.APPLICATION
|
|
});
|
|
}
|
|
|
|
async findOne(id: string, shopId?: string): Promise<Contact> {
|
|
const where: any = { id };
|
|
if (shopId) {
|
|
where.shop = shopId;
|
|
}
|
|
|
|
const contact = await this.contactRepository.findOne(where);
|
|
if (!contact) {
|
|
throw new NotFoundException(`Contact with ID ${id} not found`);
|
|
}
|
|
return contact;
|
|
}
|
|
|
|
async updateStatus(id: string, dto: UpdateContactStatusDto, shopId?: string): Promise<Contact> {
|
|
const where: any = { id };
|
|
if (shopId) {
|
|
where.shop = shopId;
|
|
}
|
|
|
|
const contact = await this.contactRepository.findOne(where);
|
|
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, shopId?: string): Promise<void> {
|
|
const where: any = { id };
|
|
if (shopId) {
|
|
where.shop = shopId;
|
|
}
|
|
|
|
const contact = await this.contactRepository.findOne(where);
|
|
if (!contact) {
|
|
throw new NotFoundException(`Contact with ID ${id} not found`);
|
|
}
|
|
await this.em.removeAndFlush(contact);
|
|
}
|
|
}
|