add : business list for super admin
This commit is contained in:
@@ -3,10 +3,11 @@ import { ApiBearerAuth, ApiHeader } from "@nestjs/swagger";
|
||||
|
||||
import { AdminRouteGuard } from "../../modules/auth/guards/admin.guard";
|
||||
import { JwtAuthGuard } from "../../modules/auth/guards/auth.guard";
|
||||
import { SuperAdminAuthGuard } from "../../modules/auth/guards/superAdminAuth.guard";
|
||||
|
||||
export function AuthGuards() {
|
||||
return applyDecorators(
|
||||
UseGuards(JwtAuthGuard, AdminRouteGuard),
|
||||
UseGuards(JwtAuthGuard, AdminRouteGuard, SuperAdminAuthGuard),
|
||||
ApiBearerAuth("authorization"),
|
||||
ApiHeader({ name: "x-business-id", description: "Business ID" }),
|
||||
);
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export const SUPER_ADMIN_ROUTE = "shouldSuperAdmin";
|
||||
import { SetMetadata } from "@nestjs/common";
|
||||
|
||||
export const SuperAdminRoute = (isSuperAdminRoute: boolean = true) => SetMetadata(SUPER_ADMIN_ROUTE, isSuperAdminRoute);
|
||||
@@ -0,0 +1,78 @@
|
||||
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException, Inject, Logger } from '@nestjs/common';
|
||||
import { Request } from 'express';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { SuperAdminRoute } from '../../../common/decorators/super-admin.decorator';
|
||||
|
||||
@Injectable()
|
||||
export class SuperAdminAuthGuard implements CanActivate {
|
||||
private readonly logger = new Logger(SuperAdminAuthGuard.name);
|
||||
|
||||
constructor(
|
||||
@Inject(ConfigService)
|
||||
private readonly configService: ConfigService,
|
||||
private reflector:Reflector
|
||||
) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
|
||||
const requiredSuperAdmin = this.reflector.getAllAndOverride<boolean>(SuperAdminRoute, [context.getHandler(), context.getClass()]);
|
||||
if (!requiredSuperAdmin) return true;
|
||||
|
||||
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
|
||||
|
||||
try {
|
||||
|
||||
const credentials = this.extractBasicAuthCredentials(request);
|
||||
if (!credentials) {
|
||||
throw new UnauthorizedException('Basic authentication required');
|
||||
}
|
||||
|
||||
const { username, password } = credentials;
|
||||
|
||||
const expectedUsername = this.configService.get<string>('SUPER_ADMIN_USERNAME') ?? 'danak@dsc.com';
|
||||
const expectedPassword = this.configService.get<string>('SUPER_ADMIN_PASSWORD') ?? 'DsCdAnAk?@ABC';
|
||||
|
||||
if (username !== expectedUsername || password !== expectedPassword) {
|
||||
this.logger.warn(`Invalid super admin credentials attempt for username: ${username}`);
|
||||
throw new UnauthorizedException('Invalid credentials');
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
if (err instanceof UnauthorizedException) {
|
||||
throw err;
|
||||
}
|
||||
this.logger.error('error in SuperAdminAuthGuard', err);
|
||||
throw new UnauthorizedException('Authentication failed');
|
||||
}
|
||||
}
|
||||
|
||||
private extractBasicAuthCredentials(request: Request): { username: string; password: string } | null {
|
||||
const authHeader = request.headers.authorization;
|
||||
if (!authHeader) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [type, encoded] = authHeader.split(' ');
|
||||
if (type?.toLowerCase() !== 'basic' || !encoded) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = Buffer.from(encoded, 'base64').toString('utf-8');
|
||||
const [username, password] = decoded.split(':');
|
||||
|
||||
if (!username || !password) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { username, password };
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to decode Basic Auth credentials', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ export class AuthService {
|
||||
|
||||
const user = await this.checkUserLoginCredentialWithEmail(email, password);
|
||||
|
||||
if (!user.isActive) throw new BadRequestException(AuthMessage.USER_ACCOUNT_INACTIVE);
|
||||
if (!user.isActive) throw new BadRequestException(AuthMessage.USER_ACCOUNT_INACTIVE);
|
||||
|
||||
if (user.is2FAEnabled) {
|
||||
if (!twoFactorToken) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { IsOptional, IsNumber, IsString, IsIn, IsBoolean } from 'class-validator';
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class FindBusinessesDto {
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
@ApiPropertyOptional({ example: 1 })
|
||||
page?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
@ApiPropertyOptional({ example: 10 })
|
||||
limit?: number;
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiPropertyOptional({ example: 'createdAt' })
|
||||
orderBy?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsIn(['asc', 'desc'])
|
||||
@ApiPropertyOptional({ example: 'desc' })
|
||||
order?: 'asc' | 'desc';
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@Type(() => Boolean)
|
||||
@ApiPropertyOptional({ example: true })
|
||||
isActive?: boolean;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post, UseInterceptors } from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Patch, Post, Query, UseInterceptors } from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse } from "@nestjs/swagger";
|
||||
|
||||
import { BusinessQuotaResponseDto } from "./DTO/business-quota-response.dto";
|
||||
@@ -10,10 +10,12 @@ import { AdminRoute } from "../../common/decorators/admin.decorator";
|
||||
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
||||
import { BusinessDec } from "../../common/decorators/business.decorator";
|
||||
import { BusinessInterceptor } from "../../core/interceptors/business.interceptor";
|
||||
import { SuperAdminRoute } from "../../common/decorators/super-admin.decorator";
|
||||
import { FindBusinessesDto } from "./DTO/find-businesses.dto";
|
||||
|
||||
@Controller("business")
|
||||
export class BusinessesController {
|
||||
constructor(private readonly businessesService: BusinessesService) {}
|
||||
constructor(private readonly businessesService: BusinessesService) { }
|
||||
|
||||
@Get("slug/:slug")
|
||||
@ApiOperation({ summary: "Get business by slug" })
|
||||
@@ -58,4 +60,12 @@ export class BusinessesController {
|
||||
purchaseQuota(@BusinessDec("id") businessId: string, @Body() purchaseDto: PurchaseQuotaDto) {
|
||||
return this.businessesService.purchaseQuota(businessId, purchaseDto);
|
||||
}
|
||||
|
||||
@Get("list")
|
||||
@AuthGuards()
|
||||
@SuperAdminRoute()
|
||||
@ApiOperation({ summary: "Get Paginated list of businesses" })
|
||||
getPaginatedList(@Query() query: FindBusinessesDto) {
|
||||
return this.businessesService.findBusinesses(query)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,36 @@
|
||||
import { EntityRepository } from "@mikro-orm/postgresql";
|
||||
|
||||
import { EntityRepository, FilterQuery } from "@mikro-orm/postgresql";
|
||||
import { Business } from "../entities/business.entity";
|
||||
import { FindBusinessesDto } from "../DTO/find-businesses.dto";
|
||||
|
||||
export class BusinessRepository extends EntityRepository<Business> {}
|
||||
export class BusinessRepository extends EntityRepository<Business> {
|
||||
|
||||
async findAllPaginated(opts: FindBusinessesDto = {}) {
|
||||
|
||||
const { page = 1, limit = 10, orderBy = 'createdAt', order = 'desc' } = opts;
|
||||
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const where: FilterQuery<Business> = {};
|
||||
|
||||
const [data, total] = await this.findAndCount(where, {
|
||||
limit,
|
||||
offset,
|
||||
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
||||
populate: [],
|
||||
});
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import { BusinessStaff } from "../entities/business-staff.entity";
|
||||
import { Business } from "../entities/business.entity";
|
||||
import { IDanakCheckAccessResponse } from "../interfaces/IBusiness";
|
||||
import { BusinessRepository } from "../repositories/business.repository";
|
||||
import { FindBusinessesDto } from "../DTO/find-businesses.dto";
|
||||
|
||||
@Injectable()
|
||||
export class BusinessesService {
|
||||
@@ -24,7 +25,7 @@ export class BusinessesService {
|
||||
private readonly httpService: HttpService,
|
||||
private readonly em: EntityManager,
|
||||
private readonly quotaPurchaseService: QuotaPurchaseService,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
async getBusinessByDanakSubscriptionId(danakSubscriptionId: string) {
|
||||
const business = await this.businessRepository.findOne({ danakSubscriptionId, deletedAt: null });
|
||||
@@ -162,4 +163,8 @@ export class BusinessesService {
|
||||
|
||||
return this.quotaPurchaseService.purchaseQuota(business, purchaseDto);
|
||||
}
|
||||
//********************************************* */
|
||||
async findBusinesses(query: FindBusinessesDto) {
|
||||
return this.businessRepository.findAllPaginated(query)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user