diff --git a/src/modules/auth/guards/api-key.guard.ts b/src/modules/auth/guards/api-key.guard.ts index ba20f88..e3a8475 100644 --- a/src/modules/auth/guards/api-key.guard.ts +++ b/src/modules/auth/guards/api-key.guard.ts @@ -12,21 +12,18 @@ export class ApiKeyGuard implements CanActivate { const request = context.switchToHttp().getRequest(); const apiKey = request.headers["x-api-key"] as string; - console.log("apiKey", apiKey); - if (!apiKey) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS); + if (!apiKey) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS); // Get allowed API keys from environment (comma-separated) const allowedApiKeys = this.configService.getOrThrow("EXTERNAL_API_KEYS"); const validApiKeys = allowedApiKeys.split(",").filter((key) => key.trim().length > 0); - console.log("validApiKeys", validApiKeys); - + if (validApiKeys.length === 0) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS); const isValidApiKey = validApiKeys.includes(apiKey); - console.log("isValidApiKey", isValidApiKey); - + if (!isValidApiKey) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS); console.log("true"); diff --git a/src/modules/dmenu/providers/contact.service.ts b/src/modules/dmenu/providers/contact.service.ts index 5278767..d7f2df7 100644 --- a/src/modules/dmenu/providers/contact.service.ts +++ b/src/modules/dmenu/providers/contact.service.ts @@ -14,7 +14,7 @@ export class ContactService { constructor( @Inject(DMENU_CONFIG) private readonly config: IDmenuConfig, private readonly httpService: HttpService, - ) {} + ) { } private getHeaders(): Record { const credentials = Buffer.from(`${this.config.username}:${this.config.password}`).toString("base64"); @@ -42,7 +42,7 @@ export class ContactService { } async findOne(id: number) { - try { + try { const { data } = await firstValueFrom( this.httpService.get(`${this.config.baseUrl}/super-admin/contact/${id}`, { headers: this.getHeaders() }).pipe( catchError((err: AxiosError) => { diff --git a/src/modules/dpage/dpage.service.ts b/src/modules/dpage/dpage.service.ts index fbd5781..bee38e0 100644 --- a/src/modules/dpage/dpage.service.ts +++ b/src/modules/dpage/dpage.service.ts @@ -192,6 +192,38 @@ export class DPageService { throw error; } } + + async finishPurchaseCatalogue(businessId: string, count: number) { + try { + const { data } = await firstValueFrom( + this.httpService + .post(`${this.config.baseUrl}/super-admin/catalogue/purchase/finish`, { + businessId, + count + }, { + headers: this.getHeaders(), + }) + .pipe( + catchError((err: AxiosError) => { + this.logger.error(`Failed to perform finish purchase catalogue: ${err.message}`, err.stack); + return throwError(() => err); + }), + ), + ); + return data; + } catch (error: unknown) { + if (error instanceof AxiosError && error.response) { + this.logger.error(`External API error response:`, error.response.data); + const errorResponse = { + ...error.response.data, + message: error.response.data.error?.message || error.message, + }; + throw new HttpException(errorResponse, error.response.status); + } + this.logger.error(`Error performing to finish purchase catalogue: ${error instanceof Error ? error.message : "Unknown error"}`); + throw error; + } + } } diff --git a/src/modules/invoices/providers/invoices.service.ts b/src/modules/invoices/providers/invoices.service.ts index 509ec0b..bf8b3d9 100755 --- a/src/modules/invoices/providers/invoices.service.ts +++ b/src/modules/invoices/providers/invoices.service.ts @@ -778,7 +778,7 @@ export class InvoicesService { queryRunner = this.dataSource.createQueryRunner(), ) { let transactionStarted = false; - try { + try { if (!queryRunner.isTransactionActive) { await queryRunner.connect(); await queryRunner.startTransaction(); @@ -806,7 +806,7 @@ export class InvoicesService { const purpose = invoice.purpose - + if (purpose == InvoicePurpose.UPGRADE) { // This is an upgrade - update subscription with the new plan const upgradePlan = invoiceItem.plan!; @@ -903,7 +903,7 @@ export class InvoicesService { if (userSubscription.plan.service.slug?.toLowerCase().includes('dpage')) { try { this.logger.log(`Calling external API for dpage setup: subscription ${userSubscription.id}`); - console.log('userSubscription.plan.count',userSubscription.plan.count) + console.log('userSubscription.plan.count', userSubscription.plan.count) await this.dpageService.createDpageAccount({ danakSubscriptionId: userSubscription.id, name: userSubscription.businessName, @@ -946,6 +946,16 @@ export class InvoicesService { await this.walletsService.createSupportPlanTransaction(invoice.totalPrice, userWallet.id, queryRunner); await this.addNotifyForWalletDeduction(invoice, user, userWallet, WalletMessage.SUPPORT_PLAN_WALLET_TRANSFER); // + } else if (invoice.isExternal) { + if (invoice.items[0]?.name == 'purchase_new_catalogue') { + // call dpage api + const businessId = invoice.externalBusinessId + if (!businessId) { + throw new BadRequestException("business id not found") + } + const count = invoice.items[0].count + this.dpageService.finishPurchaseCatalogue(businessId, count) + } } else { await this.walletsService.createInvoiceTransaction(invoice.totalPrice, userWallet.id, queryRunner); await this.addNotifyForWalletDeduction(invoice, user, userWallet, WalletMessage.INVOICE_WALLET_TRANSFER);