62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { CreateCatalogueDto } from './dto/create-catalogue.dto';
|
|
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
|
|
import { EntityManager } from '@mikro-orm/postgresql';
|
|
import { Catalogue } from './entities/catalogue.entity';
|
|
import { Business } from '../business/entities/business.entity';
|
|
import { CatalogueStatus } from './enums/company-status.enum';
|
|
import { CatalogueRepository } from './repositories/catalogue.repository';
|
|
import { FindCataloguesDto } from './dto/find-catalogues.dto';
|
|
|
|
@Injectable()
|
|
export class CatalogueService {
|
|
constructor(
|
|
private readonly em: EntityManager,
|
|
private readonly catalogueRepository: CatalogueRepository
|
|
) { }
|
|
|
|
async create(dto: CreateCatalogueDto) {
|
|
const business = await this.em.findOne(Business, {})
|
|
if (!business) {
|
|
throw new BadRequestException()
|
|
}
|
|
const catalogue = this.em.create(Catalogue,
|
|
{ ...dto, business, status: CatalogueStatus.PENDING })
|
|
|
|
await this.em.persistAndFlush(catalogue)
|
|
|
|
return catalogue
|
|
}
|
|
|
|
findAll(dto: FindCataloguesDto) {
|
|
return this.catalogueRepository.findAllPaginated(dto)
|
|
}
|
|
|
|
async findOne(id: string) {
|
|
const catalogue = await this.findOneOrFail(id)
|
|
return catalogue
|
|
}
|
|
|
|
async findOneOrFail(id: string) {
|
|
const catalogue = await this.catalogueRepository.findOne({ id })
|
|
if (!catalogue) {
|
|
throw new NotFoundException("catalogue not found")
|
|
}
|
|
return catalogue
|
|
}
|
|
|
|
async update(id: string, dto: UpdateCatalogueDto) {
|
|
const catalogue = await this.findOneOrFail(id)
|
|
this.em.assign(catalogue, dto)
|
|
await this.em.flush()
|
|
return catalogue
|
|
}
|
|
|
|
async remove(id: string) {
|
|
const catalogue = await this.findOneOrFail(id)
|
|
catalogue.deletedAt = new Date()
|
|
await this.em.flush()
|
|
return catalogue
|
|
}
|
|
}
|