From f9094fcbb3a18586b1586e4c25d6170d05e35010 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 15 Mar 2026 16:02:16 +0330 Subject: [PATCH] max catalogue count --- src/modules/catalogue/catalogue.service.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 + } }