refactor purchase plan

This commit is contained in:
2026-06-17 20:40:59 +03:30
parent 526ced0101
commit d45b48504c
3 changed files with 79 additions and 10 deletions
@@ -74,6 +74,12 @@ export class CatalogueController {
return this.catalogueService.createExternalInvoice(dto, businessId); return this.catalogueService.createExternalInvoice(dto, businessId);
} }
@Get('admin/catalogue/plans')
@UseGuards(AdminAuthGuard)
getPlans() {
return this.catalogueService.getPlans();
}
@Post('super-admin/catalogue/purchase/finish') @Post('super-admin/catalogue/purchase/finish')
@UseGuards(SuperAdminAuthGuard) @UseGuards(SuperAdminAuthGuard)
finishPurchaseCatalogues(@Body() dto: FinishPurchaseCatalogueDto) { finishPurchaseCatalogues(@Body() dto: FinishPurchaseCatalogueDto) {
+66 -4
View File
@@ -98,14 +98,14 @@ export class CatalogueService {
} }
async createExternalInvoice(dto: PurchaseCatalogueDto, businessId: string) { async createExternalInvoice(dto: PurchaseCatalogueDto, businessId: string) {
const { count } = dto const { planId } = dto
const business = await this.businessService.findByIdOrFail(businessId) const business = await this.businessService.findByIdOrFail(businessId)
// create external invoice
const danakApiUrl = this.configService.getOrThrow('DANAK_API_URL') const danakApiUrl = this.configService.getOrThrow('DANAK_API_URL')
const xApiKey = this.configService.getOrThrow('EXTERNAL_API_KEYS') const xApiKey = this.configService.getOrThrow('EXTERNAL_API_KEYS')
const cataloguePrice = +this.configService.getOrThrow<number>('CATALOGUE_PRICE')
const { price: cataloguePrice, count: cataloguesCount } = await this.getPlanDetail(planId)
const params: CreateExternalInvoiceDto = { const params: CreateExternalInvoiceDto = {
danakSubscriptionId: business.danakSubscriptionId, danakSubscriptionId: business.danakSubscriptionId,
@@ -113,7 +113,7 @@ export class CatalogueService {
items: [ items: [
{ {
name: 'purchase_new_catalogue', name: 'purchase_new_catalogue',
count, count: cataloguesCount,
unitPrice: cataloguePrice, unitPrice: cataloguePrice,
discount: 0 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) { async finishPurchaseCatalogue(dto: FinishPurchaseCatalogueDto) {
const { businessId, count } = dto const { businessId, count } = dto
@@ -1,10 +1,11 @@
import { IsInt } from 'class-validator'; import { IsUUID } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
export class PurchaseCatalogueDto { export class PurchaseCatalogueDto {
@IsInt()
@ApiProperty() @IsUUID('4')
count: number; @ApiProperty({ description: 'DSC plan id' })
planId: string;
} }