chore: add new feature for checking the user has access to the service or not
This commit is contained in:
@@ -1,18 +1,27 @@
|
||||
import { EntityManager } from "@mikro-orm/postgresql";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { HttpService } from "@nestjs/axios";
|
||||
import { BadRequestException, Inject, Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
|
||||
import { AxiosError } from "axios";
|
||||
import { catchError, firstValueFrom } from "rxjs";
|
||||
|
||||
import { QuotaPurchaseService } from "./quota-purchase.service";
|
||||
import { BusinessMessage } from "../../../common/enums/message.enum";
|
||||
import { QUOTA_CONSTANTS } from "../constant";
|
||||
import { DANAKCORP_CONFIG, IDanakCorpConfig } from "../../../configs/danakcorp.config";
|
||||
import { QUOTA_CONSTANTS, SUBSCRIPTIONS } from "../constant";
|
||||
import { PurchaseQuotaDto } from "../DTO/purchase-quota.dto";
|
||||
import { UpdateBusinessSettingsDto } from "../DTO/update-business-settings.dto";
|
||||
import { BusinessStaff } from "../entities/business-staff.entity";
|
||||
import { Business } from "../entities/business.entity";
|
||||
import { IDanakCheckAccessResponse } from "../interfaces/IBusiness";
|
||||
import { BusinessRepository } from "../repositories/business.repository";
|
||||
|
||||
@Injectable()
|
||||
export class BusinessesService {
|
||||
private readonly logger = new Logger(BusinessesService.name);
|
||||
constructor(
|
||||
private readonly businessRepository: BusinessRepository,
|
||||
@Inject(DANAKCORP_CONFIG) private readonly danakCorpConfig: IDanakCorpConfig,
|
||||
private readonly httpService: HttpService,
|
||||
private readonly em: EntityManager,
|
||||
private readonly quotaPurchaseService: QuotaPurchaseService,
|
||||
) {}
|
||||
@@ -23,6 +32,41 @@ export class BusinessesService {
|
||||
|
||||
return business;
|
||||
}
|
||||
|
||||
//************************************************** */
|
||||
async hasAccessToService(business: Business, userId: string) {
|
||||
try {
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService
|
||||
.get<IDanakCheckAccessResponse>(`${this.danakCorpConfig.apiUrl}/subscriptions/check-access/${SUBSCRIPTIONS.DMAIL_SERVICE_ID}`, {
|
||||
params: {
|
||||
userId,
|
||||
danakSubscriptionId: business.danakSubscriptionId,
|
||||
},
|
||||
headers: {
|
||||
"X-API-Key": this.danakCorpConfig.apiKey,
|
||||
},
|
||||
timeout: this.danakCorpConfig.timeout,
|
||||
})
|
||||
.pipe(
|
||||
catchError((error: AxiosError) => {
|
||||
this.logger.error(`Failed to check access to service for business ${business.id}:`, error.message);
|
||||
throw new InternalServerErrorException(BusinessMessage.USER_DOES_NOT_HAVE_ACCESS_TO_SERVICE);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (!data.success || !data.data?.hasAccess) {
|
||||
this.logger.error(`DanakCorp API returned error for business ${business.id}:`, data.error);
|
||||
throw new InternalServerErrorException(BusinessMessage.QUOTA_PURCHASE_FAILED);
|
||||
}
|
||||
|
||||
return data.data?.hasAccess;
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to check access to service for business ${business.id}:`, error);
|
||||
throw new InternalServerErrorException(BusinessMessage.USER_DOES_NOT_HAVE_ACCESS_TO_SERVICE);
|
||||
}
|
||||
}
|
||||
//************************************************** */
|
||||
|
||||
async getBusinessBySlug(slug: string) {
|
||||
@@ -48,7 +92,6 @@ export class BusinessesService {
|
||||
|
||||
async getBusinessSettings(businessId: string) {
|
||||
const business = await this.businessRepository.findOne({ id: businessId, deletedAt: null });
|
||||
console.log("business", business);
|
||||
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
|
||||
|
||||
return { business };
|
||||
|
||||
Reference in New Issue
Block a user