From 845315bcdded6079fa5e624d69495a14d98bf4f9 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 5 Apr 2026 15:08:21 +0330 Subject: [PATCH] buy catalogue --- src/modules/catalogue/catalogue.controller.ts | 7 ++ src/modules/catalogue/catalogue.service.ts | 21 ++++- .../catalogue/dto/create-invoice.dto.ts | 85 +++++++++++++++++++ .../catalogue/dto/purchase-catalogue.dto.ts | 10 +++ 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100755 src/modules/catalogue/dto/create-invoice.dto.ts create mode 100644 src/modules/catalogue/dto/purchase-catalogue.dto.ts diff --git a/src/modules/catalogue/catalogue.controller.ts b/src/modules/catalogue/catalogue.controller.ts index 249b97b..e980807 100644 --- a/src/modules/catalogue/catalogue.controller.ts +++ b/src/modules/catalogue/catalogue.controller.ts @@ -7,6 +7,7 @@ import { AdminAuthGuard } from '../auth/guards/adminAuth.guard'; import { BusinessId } from 'src/common/decorators'; import { AdminId } from 'src/common/decorators'; import { ApiBearerAuth } from '@nestjs/swagger'; +import { PurchaseCatalogueDto } from './dto/purchase-catalogue.dto'; @Controller() @ApiBearerAuth() @@ -33,6 +34,12 @@ export class CatalogueController { return this.catalogueService.create(dto, businessId); } + @Post('admin/catalogue/purchase') + @UseGuards(AdminAuthGuard) + purchaseCatalogues(@Body() dto: PurchaseCatalogueDto, @BusinessId() businessId: string) { + return this.catalogueService.createExternalInvoice(dto, businessId); + } + @Get('admin/catalogue') @UseGuards(AdminAuthGuard) findAll(@Query() queryDto: FindCataloguesDto, @BusinessId() businessId: string) { diff --git a/src/modules/catalogue/catalogue.service.ts b/src/modules/catalogue/catalogue.service.ts index 1905712..76cc94d 100644 --- a/src/modules/catalogue/catalogue.service.ts +++ b/src/modules/catalogue/catalogue.service.ts @@ -1,4 +1,4 @@ -import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; +import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common'; import { CreateCatalogueDto } from './dto/create-catalogue.dto'; import { UpdateCatalogueDto } from './dto/update-catalogue.dto'; import { EntityManager } from '@mikro-orm/postgresql'; @@ -7,13 +7,21 @@ import { CatalogueStatus } from './enums/company-status.enum'; import { CatalogueRepository } from './repositories/catalogue.repository'; import { FindCataloguesDto } from './dto/find-catalogues.dto'; import { BusinessService } from '../business/business.service'; +import { PurchaseCatalogueDto } from './dto/purchase-catalogue.dto'; +// import { HttpService } from '@nestjs/axios'; @Injectable() export class CatalogueService { + private readonly logger = new Logger(CatalogueService.name); + // constructor( + // @Inject(DPAGE_CONFIG) private readonly config: IDPageConfig, + // private readonly subscriptionService: SubscriptionsService, + // ) { } constructor( private readonly em: EntityManager, private readonly catalogueRepository: CatalogueRepository, private readonly businessService: BusinessService, + // private readonly httpService: HttpService, ) { } async create(dto: CreateCatalogueDto, businessId: string) { @@ -24,7 +32,7 @@ export class CatalogueService { if (createdCataloguesCount >= business.maxCataloguesCount) { throw new BadRequestException("شما از حداکثر تعداد کاتالوگ استفاده کرده اید و نمیتوانید کاتالوگ جدیدی بسازید") } - + const catalogue = this.em.create(Catalogue, { ...dto, business, status: CatalogueStatus.PENDING }) @@ -74,4 +82,13 @@ export class CatalogueService { const count = await this.catalogueRepository.count({ business: { id: businessId }, deletedAt: null }) return count ?? 0 } + + async createExternalInvoice(dto: PurchaseCatalogueDto, businessId: string) { + const { count } = dto + + const business = await this.businessService.findByIdOrFail(businessId) + + // create external invoice + + } } diff --git a/src/modules/catalogue/dto/create-invoice.dto.ts b/src/modules/catalogue/dto/create-invoice.dto.ts new file mode 100755 index 0000000..3462d18 --- /dev/null +++ b/src/modules/catalogue/dto/create-invoice.dto.ts @@ -0,0 +1,85 @@ +import { ApiProperty, ApiPropertyOptional, OmitType } from "@nestjs/swagger"; +import { Type } from "class-transformer"; +import { ArrayMinSize, IsBoolean, IsEnum, IsInt, IsNotEmpty, IsOptional, IsString, IsUUID, Length, ValidateNested } from "class-validator"; + +export enum RecurringPeriodEnum { + WEEKLY = 1, // هفتگی + MONTHLY, // ماهانه + QUARTERLY, // سه‌ماهه + SEMIANNUALLY, // شش‌ماهه + ANNUALLY, // سالانه +} + + +export class InvoiceItemDto { + @IsNotEmpty() + @IsString() + @Length(1, 150,) + @ApiProperty() + name: string; + + @IsNotEmpty() + @IsInt() + @ApiProperty({ type: Number, description: "Item count", example: 2 }) + count: number; + + @IsNotEmpty() + @IsInt() + @ApiProperty({ description: "Item unit price", example: 100_000 }) + unitPrice: number; + + @IsOptional() + @IsNotEmpty() + @IsInt() + @ApiProperty({ description: "Item discount", example: 10 }) + discount: number; +} + +export class CreateInvoiceDto { + @IsNotEmpty() + @IsUUID("4",) + @ApiProperty({ description: "User id", example: "123e4567-e89b-12d3-a456-426614174000" }) + userId: string; + + @ApiProperty({ + type: [InvoiceItemDto], + description: "Invoice items", + example: [{ name: "item1", count: 2, unitPrice: 100_000, discount: 10 }], + }) + @ValidateNested({ each: true }) + @ArrayMinSize(1) + @Type(() => InvoiceItemDto) + items: InvoiceItemDto[]; + + @IsOptional() + @IsBoolean() + @ApiProperty({ description: "Is this a recurring invoice", example: false, default: false }) + isRecurring?: boolean; + + @IsOptional() + @IsNotEmpty() + @IsEnum(RecurringPeriodEnum) + @ApiPropertyOptional({ + description: "Recurring period (daily, weekly, monthly, quarterly, yearly)", + example: RecurringPeriodEnum.QUARTERLY, + enum: RecurringPeriodEnum, + }) + recurringPeriod?: RecurringPeriodEnum; + + @IsOptional() + @IsInt() + @ApiPropertyOptional({ description: "Maximum number of recurring cycles (0 for unlimited)", example: 12, default: 0 }) + maxRecurringCycles?: number; +} + +export class CreateExternalInvoiceDto extends OmitType(CreateInvoiceDto, ["userId"]) { + @IsNotEmpty() + @IsUUID("4",) + @ApiProperty() + danakSubscriptionId: string; + + @IsNotEmpty() + @IsString() + @ApiProperty({ description: "Business id", example: "123e4567-e89b-12d3-a456-426614174000" }) + businessId: string; +} diff --git a/src/modules/catalogue/dto/purchase-catalogue.dto.ts b/src/modules/catalogue/dto/purchase-catalogue.dto.ts new file mode 100644 index 0000000..365e7dc --- /dev/null +++ b/src/modules/catalogue/dto/purchase-catalogue.dto.ts @@ -0,0 +1,10 @@ + +import { IsString } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; + +export class PurchaseCatalogueDto { + @IsString() + @ApiProperty() + count: string; + +}