diff --git a/src/modules/catalogue/catalogue.service.ts b/src/modules/catalogue/catalogue.service.ts index 17b676c..1905712 100644 --- a/src/modules/catalogue/catalogue.service.ts +++ b/src/modules/catalogue/catalogue.service.ts @@ -1,4 +1,4 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +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'; @@ -19,6 +19,12 @@ export class CatalogueService { async create(dto: CreateCatalogueDto, businessId: string) { const business = await this.businessService.findByIdOrFail(businessId) + const createdCataloguesCount = await this.findCataloguesCountByBusinessId(businessId) + + if (createdCataloguesCount >= business.maxCataloguesCount) { + throw new BadRequestException("شما از حداکثر تعداد کاتالوگ استفاده کرده اید و نمیتوانید کاتالوگ جدیدی بسازید") + } + const catalogue = this.em.create(Catalogue, { ...dto, business, status: CatalogueStatus.PENDING }) @@ -63,4 +69,9 @@ export class CatalogueService { await this.em.flush() return catalogue } + + async findCataloguesCountByBusinessId(businessId: string) { + const count = await this.catalogueRepository.count({ business: { id: businessId }, deletedAt: null }) + return count ?? 0 + } }