From d45b48504c0222a7c47f2293c6a7c26e530fa5c1 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Wed, 17 Jun 2026 20:40:59 +0330 Subject: [PATCH] refactor purchase plan --- src/modules/catalogue/catalogue.controller.ts | 6 ++ src/modules/catalogue/catalogue.service.ts | 72 +++++++++++++++++-- .../catalogue/dto/purchase-catalogue.dto.ts | 11 +-- 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/modules/catalogue/catalogue.controller.ts b/src/modules/catalogue/catalogue.controller.ts index b894d9e..1c723a7 100644 --- a/src/modules/catalogue/catalogue.controller.ts +++ b/src/modules/catalogue/catalogue.controller.ts @@ -74,6 +74,12 @@ export class CatalogueController { return this.catalogueService.createExternalInvoice(dto, businessId); } + @Get('admin/catalogue/plans') + @UseGuards(AdminAuthGuard) + getPlans() { + return this.catalogueService.getPlans(); + } + @Post('super-admin/catalogue/purchase/finish') @UseGuards(SuperAdminAuthGuard) finishPurchaseCatalogues(@Body() dto: FinishPurchaseCatalogueDto) { diff --git a/src/modules/catalogue/catalogue.service.ts b/src/modules/catalogue/catalogue.service.ts index b519632..192f7a5 100644 --- a/src/modules/catalogue/catalogue.service.ts +++ b/src/modules/catalogue/catalogue.service.ts @@ -98,14 +98,14 @@ export class CatalogueService { } async createExternalInvoice(dto: PurchaseCatalogueDto, businessId: string) { - const { count } = dto + const { planId } = dto const business = await this.businessService.findByIdOrFail(businessId) - // create external invoice const danakApiUrl = this.configService.getOrThrow('DANAK_API_URL') const xApiKey = this.configService.getOrThrow('EXTERNAL_API_KEYS') - const cataloguePrice = +this.configService.getOrThrow('CATALOGUE_PRICE') + + const { price: cataloguePrice, count: cataloguesCount } = await this.getPlanDetail(planId) const params: CreateExternalInvoiceDto = { danakSubscriptionId: business.danakSubscriptionId, @@ -113,7 +113,7 @@ export class CatalogueService { items: [ { name: 'purchase_new_catalogue', - count, + count: cataloguesCount, unitPrice: cataloguePrice, discount: 0 } @@ -142,6 +142,68 @@ export class CatalogueService { } + private async getPlanDetail(planId: string): Promise<{ price: number; count: number }> { + const danakApiUrl = this.configService.getOrThrow('DANAK_API_URL') + const xApiKey = this.configService.getOrThrow('EXTERNAL_API_KEYS') + + try { + const { data: planResponse } = await firstValueFrom( + this.httpService + .get(`${danakApiUrl}/dpage-admin/plans/${planId}`, { + headers: { + 'Content-Type': 'application/json', + 'X-API-Key': xApiKey, + }, + }) + .pipe( + catchError((err: AxiosError) => { + this.logger.error(`Failed to get plan detail: ${err.message}`, err.stack) + return throwError(() => err) + }), + ), + ) + return { + price: planResponse.data.price, + count: planResponse.data.count, + } + } catch (error: unknown) { + this.logger.error( + `Error getting plan detail: ${error instanceof Error ? error.message : 'Unknown error'}`, + ) + throw error + } + } + + async getPlans() { + const danakApiUrl = this.configService.getOrThrow('DANAK_API_URL'); + const xApiKey = this.configService.getOrThrow('EXTERNAL_API_KEYS'); + + try { + const { data } = await firstValueFrom( + this.httpService + .get(`${danakApiUrl}/dpage-admin/plans`, { + headers: { + 'Content-Type': 'application/json', + 'X-API-Key': xApiKey, + }, + }) + .pipe( + catchError((err: AxiosError) => { + this.logger.error(`Failed to get plans: ${err.message}`, err.stack); + return throwError(() => err); + }), + ), + ); + + return data; + } catch (error: unknown) { + this.logger.error( + `Error getting plans: ${error instanceof Error ? error.message : 'Unknown error'}`, + ); + throw error; + } + } + async finishPurchaseCatalogue(dto: FinishPurchaseCatalogueDto) { const { businessId, count } = dto @@ -150,7 +212,7 @@ export class CatalogueService { business.maxCataloguesCount += count await this.em.flush() - + return business } diff --git a/src/modules/catalogue/dto/purchase-catalogue.dto.ts b/src/modules/catalogue/dto/purchase-catalogue.dto.ts index bd74a03..d82086e 100644 --- a/src/modules/catalogue/dto/purchase-catalogue.dto.ts +++ b/src/modules/catalogue/dto/purchase-catalogue.dto.ts @@ -1,10 +1,11 @@ -import { IsInt } from 'class-validator'; +import { IsUUID } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; - + export class PurchaseCatalogueDto { - @IsInt() - @ApiProperty() - count: number; + + @IsUUID('4') + @ApiProperty({ description: 'DSC plan id' }) + planId: string; }