forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.controller.ts
More file actions
106 lines (97 loc) · 3.07 KB
/
Copy pathadmin.controller.ts
File metadata and controls
106 lines (97 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Controller, Post, Param, Body, UseGuards, Patch, BadRequestException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { UserService } from '../users/user.service';
import { AdminGuard } from '../auth/guards/admin.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { IsString, IsOptional } from 'class-validator';
class FreezeUserDto {
@IsOptional()
@IsString()
reason?: string;
}
@ApiTags('admin')
@Controller('admin/users')
@UseGuards(JwtAuthGuard, AdminGuard)
@ApiBearerAuth('JWT-auth')
export class AdminController {
constructor(private readonly userService: UserService) {}
@Post(':id/freeze')
@ApiOperation({ summary: 'Freeze a user account' })
@ApiResponse({
status: 200,
description: 'User frozen successfully',
})
@ApiResponse({ status: 400, description: 'User already frozen' })
@ApiResponse({ status: 403, description: 'Forbidden - Admin only' })
@ApiResponse({ status: 404, description: 'User not found' })
async freezeUser(
@Param('id') userId: string,
@Body() body: FreezeUserDto,
@Request() req: any,
) {
const adminId = req.user.userId;
const user = await this.userService.freezeUser(userId, adminId, body.reason);
return {
success: true,
message: `User ${user.email} has been frozen`,
user: {
id: user.id,
email: user.email,
isFrozen: user.isFrozen,
frozenReason: user.frozenReason,
frozenAt: user.frozenAt,
},
};
}
@Post(':id/unfreeze')
@ApiOperation({ summary: 'Unfreeze a user account' })
@ApiResponse({
status: 200,
description: 'User unfrozen successfully',
})
@ApiResponse({ status: 400, description: 'User is not frozen' })
@ApiResponse({ status: 403, description: 'Forbidden - Admin only' })
@ApiResponse({ status: 404, description: 'User not found' })
async unfreezeUser(
@Param('id') userId: string,
@Request() req: any,
) {
const adminId = req.user.userId;
const user = await this.userService.unfreezeUser(userId, adminId);
return {
success: true,
message: `User ${user.email} has been unfrozen`,
user: {
id: user.id,
email: user.email,
isFrozen: user.isFrozen,
},
};
}
@Patch(':id/role')
@ApiOperation({ summary: 'Update user role (admin/user)' })
@ApiResponse({
status: 200,
description: 'User role updated successfully',
})
@ApiResponse({ status: 403, description: 'Forbidden - Admin only' })
@ApiResponse({ status: 404, description: 'User not found' })
async updateRole(
@Param('id') userId: string,
@Body('role') role: string,
) {
if (!['user', 'admin'].includes(role)) {
throw new BadRequestException('Role must be either "user" or "admin"');
}
const user = await this.userService.updateRole(userId, role);
return {
success: true,
message: `User ${user.email} role updated to ${role}`,
user: {
id: user.id,
email: user.email,
role: user.role,
},
};
}
}