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);
}
@Get('admin/catalogue/plans')
@UseGuards(AdminAuthGuard)
getPlans() {
return this.catalogueService.getPlans();
}
@Post('super-admin/catalogue/purchase/finish')
@UseGuards(SuperAdminAuthGuard)
finishPurchaseCatalogues(@Body() dto: FinishPurchaseCatalogueDto) {
+67 -5
View File
@@ -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<number>('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
}
@@ -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;
}