File size: 7,738 Bytes
8aa5b53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Param,
  Query,
  Req,
  UseGuards,
  ParseIntPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiParam,
  ApiBody,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles } from '../../auth/roles.decorator';
import { RoleName } from '../../auth/entities/role.entity';
import { CommunitiesService } from '../services/communities.service';
import { CommunityPostsService } from '../services/community-posts.service';
import { CommunityTagsService } from '../services/community-tags.service';
import {
  CreateCommunityDto,
  UpdateCommunityDto,
  CommunityQueryDto,
  CreatePostDto,
  PostQueryDto,
  CreateTagDto,
} from '../dto';

@ApiTags('🏘️ Communities')
@ApiBearerAuth('JWT-auth')
@Controller('api/community')
@UseGuards(JwtAuthGuard, RolesGuard)
export class CommunitiesController {
  constructor(
    private readonly communitiesService: CommunitiesService,
    private readonly postsService: CommunityPostsService,
    private readonly tagsService: CommunityTagsService,
  ) {}

  // ============== COMMUNITY CRUD ==============

  @Get('communities')
  @ApiOperation({
    summary: 'List communities',
    description: 'List all communities the user has access to. Global communities are visible to all. Department communities are filtered by enrollment.',
  })
  @ApiResponse({ status: 200, description: 'Paginated list of communities' })
  async findAllCommunities(@Query() query: CommunityQueryDto, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    return this.communitiesService.findAll(query, userId, roles);
  }

  @Post('communities')
  @HttpCode(HttpStatus.CREATED)
  @Roles(RoleName.ADMIN, RoleName.IT_ADMIN, RoleName.DEPARTMENT_HEAD, RoleName.INSTRUCTOR)
  @ApiOperation({
    summary: 'Create a community',
    description: 'Create a new global or department community. Admin/Instructor/Department Head only.',
  })
  @ApiBody({ type: CreateCommunityDto })
  @ApiResponse({ status: 201, description: 'Community created' })
  async createCommunity(@Body() dto: CreateCommunityDto, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    return this.communitiesService.create(dto, userId);
  }

  @Get('communities/:communityId')
  @ApiOperation({ summary: 'Get community details' })
  @ApiParam({ name: 'communityId', description: 'Community ID' })
  @ApiResponse({ status: 200, description: 'Community details' })
  async getCommunity(@Param('communityId', ParseIntPipe) communityId: number, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    return this.communitiesService.checkAccess(communityId, userId, roles);
  }

  @Put('communities/:communityId')
  @Roles(RoleName.ADMIN, RoleName.IT_ADMIN, RoleName.DEPARTMENT_HEAD, RoleName.INSTRUCTOR)
  @ApiOperation({ summary: 'Update a community' })
  @ApiParam({ name: 'communityId', description: 'Community ID' })
  @ApiBody({ type: UpdateCommunityDto })
  async updateCommunity(
    @Param('communityId', ParseIntPipe) communityId: number,
    @Body() dto: UpdateCommunityDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    return this.communitiesService.update(communityId, dto, userId, roles);
  }

  // ============== COMMUNITY POSTS ==============

  @Get('communities/:communityId/posts')
  @ApiOperation({
    summary: 'List posts in a community',
    description: 'Get paginated posts from a specific community. Access is validated.',
  })
  @ApiParam({ name: 'communityId', description: 'Community ID' })
  async getCommunityPosts(
    @Param('communityId', ParseIntPipe) communityId: number,
    @Query() query: PostQueryDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    // Validate access
    await this.communitiesService.checkAccess(communityId, userId, roles);
    // Override communityId in query
    query.communityId = communityId;
    return this.postsService.findAll(query);
  }

  @Post('communities/:communityId/posts')
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({
    summary: 'Create a post in a community',
    description: 'Create a new post in a specific community. Access is validated.',
  })
  @ApiParam({ name: 'communityId', description: 'Community ID' })
  @ApiBody({ type: CreatePostDto })
  async createCommunityPost(
    @Param('communityId', ParseIntPipe) communityId: number,
    @Body() dto: CreatePostDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    // Validate access
    await this.communitiesService.checkAccess(communityId, userId, roles);
    // Override communityId
    dto.communityId = communityId;
    return this.postsService.create(dto, userId);
  }

  // ============== CONVENIENCE ROUTES ==============

  @Get('global')
  @ApiOperation({
    summary: 'Get global community posts',
    description: 'Shortcut to list posts from the global community (community_id = 1).',
  })
  async getGlobalPosts(@Query() query: PostQueryDto) {
    query.communityId = 1; // Global community is always ID 1
    return this.postsService.findAll(query);
  }

  @Post('global/posts')
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({
    summary: 'Create a post in global community',
    description: 'Shortcut to create a post in the global community.',
  })
  @ApiBody({ type: CreatePostDto })
  async createGlobalPost(@Body() dto: CreatePostDto, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    dto.communityId = 1;
    return this.postsService.create(dto, userId);
  }

  @Get('department/:departmentId')
  @ApiOperation({
    summary: 'List department communities',
    description: 'List all communities for a specific department. Access is validated by enrollment.',
  })
  @ApiParam({ name: 'departmentId', description: 'Department ID' })
  async getDepartmentCommunities(
    @Param('departmentId', ParseIntPipe) departmentId: number,
    @Query() query: CommunityQueryDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    query.departmentId = departmentId;
    return this.communitiesService.findAll(query, userId, roles);
  }

  @Get('my-departments')
  @ApiOperation({
    summary: 'Get my accessible departments',
    description: 'Returns department IDs the user has access to based on enrollment.',
  })
  async getMyDepartments(@Req() req: any) {
    const userId = req.user.userId || req.user.id;
    const departmentIds = await this.communitiesService.getUserDepartmentIds(userId);
    return { departmentIds };
  }

  // ============== TAGS ==============

  @Get('tags')
  @ApiOperation({ summary: 'List all tags' })
  async getAllTags() {
    return this.tagsService.findAll();
  }

  @Post('tags')
  @HttpCode(HttpStatus.CREATED)
  @Roles(RoleName.INSTRUCTOR, RoleName.ADMIN, RoleName.IT_ADMIN, RoleName.TA)
  @ApiOperation({
    summary: 'Create a tag',
    description: 'Create a new tag. Instructor/Admin/TA only.',
  })
  @ApiBody({ type: CreateTagDto })
  async createTag(@Body() dto: CreateTagDto) {
    return this.tagsService.create(dto);
  }

  // ============== HELPER ==============

  private extractRoles(user: any): string[] {
    if (!user.roles) return [];
    return user.roles.map((r: any) => r.roleName || r.name || r);
  }
}