Implement 'getMe' endpoint in AdminController to retrieve current admin details; update AdminService to return transformed admin details and adjust findById method for improved data handling. Remove unused user profile retrieval method from AdminUserController.
This commit is contained in:
@@ -19,6 +19,14 @@ export class AdminController {
|
||||
return admin;
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
@ApiOperation({ summary: 'admin' })
|
||||
@ApiResponse({ status: 201, description: 'Admins' })
|
||||
async getMe() {
|
||||
const admin = await this.adminService.findAll();
|
||||
return admin;
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiOperation({ summary: 'Create a new admin' })
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Role } from '../entities/role.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { CacheService } from '../../utils/cache.service';
|
||||
import { AdminDetailTransformer, AdminDetailResponse } from '../transformers/admin-detail.transformer';
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
@@ -22,8 +23,14 @@ export class AdminService {
|
||||
return this.adminRepository.findOne({ phone });
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Admin | null> {
|
||||
return this.adminRepository.findOne({ id });
|
||||
async findById(id: string): Promise<AdminDetailResponse | null> {
|
||||
const admin = await this.adminRepository.findOne({ id }, { populate: ['role', 'role.permissions', 'restaurant'] });
|
||||
|
||||
if (!admin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return AdminDetailTransformer.transform(admin);
|
||||
}
|
||||
async findAll(): Promise<Admin[]> {
|
||||
return this.adminRepository.findAll();
|
||||
@@ -63,7 +70,7 @@ export class AdminService {
|
||||
const cachedPermissions = await this.cacheService.get<string>(cacheKey);
|
||||
if (cachedPermissions) {
|
||||
try {
|
||||
const parsed = JSON.parse(cachedPermissions);
|
||||
const parsed: unknown = JSON.parse(cachedPermissions);
|
||||
// Ensure it's an array of strings
|
||||
if (Array.isArray(parsed) && parsed.every((p): p is string => typeof p === 'string')) {
|
||||
return parsed;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import type { Admin } from '../entities/admin.entity';
|
||||
|
||||
export interface AdminDetailResponse {
|
||||
id: string;
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
phone: string;
|
||||
role: {
|
||||
id: string;
|
||||
name: string;
|
||||
title: string;
|
||||
};
|
||||
permissions: string[];
|
||||
restaurant?: {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
};
|
||||
}
|
||||
|
||||
export class AdminDetailTransformer {
|
||||
static async transform(admin: Admin): Promise<AdminDetailResponse | null> {
|
||||
if (!admin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Extract role information
|
||||
const role = admin.role
|
||||
? {
|
||||
id: admin.role.id,
|
||||
name: admin.role.name,
|
||||
title: admin.role.title,
|
||||
}
|
||||
: null;
|
||||
|
||||
if (!role) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Extract permissions - ensure collection is loaded if needed
|
||||
let permissions: string[] = [];
|
||||
if (admin.role?.permissions) {
|
||||
if (admin.role.permissions.isInitialized()) {
|
||||
permissions = admin.role.permissions.getItems().map(p => p.name);
|
||||
} else {
|
||||
await admin.role.permissions.loadItems();
|
||||
permissions = admin.role.permissions.getItems().map(p => p.name);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: admin.id,
|
||||
firstName: admin.firstName,
|
||||
lastName: admin.lastName,
|
||||
phone: admin.phone,
|
||||
role,
|
||||
permissions,
|
||||
restaurant: admin.restaurant
|
||||
? {
|
||||
id: admin.restaurant.id,
|
||||
name: admin.restaurant.name,
|
||||
slug: admin.restaurant.slug,
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ import { ApiTags, ApiBearerAuth, ApiOperation, ApiOkResponse } from '@nestjs/swa
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
import { UserService } from '../user.service';
|
||||
import { FindUsersDto } from '../dto/find-user.dto';
|
||||
import { UserId } from 'src/common/decorators/user-id.decorator';
|
||||
import { RestId } from 'src/common/decorators';
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@@ -13,21 +12,6 @@ import { RestId } from 'src/common/decorators';
|
||||
export class AdminUserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get the current authenticated user profile' })
|
||||
@Get('/me')
|
||||
async getUser(@UserId() userId: string) {
|
||||
const user = await this.userService.findById(userId);
|
||||
return {
|
||||
message: `GET request: Retrieving profile for authenticated user`,
|
||||
user,
|
||||
userId,
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get paginated list of users with filters' })
|
||||
@ApiOkResponse({ description: 'Paginated list of users' })
|
||||
@Get('/')
|
||||
|
||||
Reference in New Issue
Block a user