Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Put, | |
| Delete, | |
| 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 { CalendarEventsService } from '../services'; | |
| import { | |
| CreateCalendarEventDto, | |
| UpdateCalendarEventDto, | |
| QueryCalendarEventDto, | |
| } from '../dto'; | |
| ('📆 Calendar Events') | |
| ('JWT-auth') | |
| ('api/calendar/events') | |
| (JwtAuthGuard, RolesGuard) | |
| export class CalendarEventsController { | |
| constructor(private readonly calendarService: CalendarEventsService) {} | |
| () | |
| ({ | |
| summary: 'List calendar events', | |
| description: ` | |
| ## List Calendar Events | |
| Returns paginated list of calendar events for the current user. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles**: ALL | |
| ### Role-Based Filtering | |
| Users see: | |
| - Their own personal events | |
| - Course events for enrolled/taught courses | |
| ### Query Parameters | |
| - \`courseId\`: Filter by course | |
| - \`eventType\`: Filter by event type (lecture, lab, exam, etc.) | |
| - \`status\`: Filter by status | |
| - \`fromDate\`: Filter from date | |
| - \`toDate\`: Filter until date | |
| `, | |
| }) | |
| ({ status: 200, description: 'Paginated list of calendar events' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| async findAll(() query: QueryCalendarEventDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| const roles = this.extractRoles(req.user); | |
| return this.calendarService.findAll(query, userId, roles); | |
| } | |
| (':id') | |
| ({ | |
| summary: 'Get calendar event by ID', | |
| description: ` | |
| ## Get Event Details | |
| Returns detailed information about a specific calendar event. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles**: ALL (must own event or have course access) | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Calendar event ID', type: Number, example: 1 }) | |
| ({ status: 200, description: 'Calendar event details' }) | |
| ({ status: 404, description: 'Event not found' }) | |
| async findById(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| const roles = this.extractRoles(req.user); | |
| return this.calendarService.findById(id, userId, roles); | |
| } | |
| () | |
| ({ | |
| summary: 'Create calendar event', | |
| description: ` | |
| ## Create Calendar Event | |
| Creates a new calendar event. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles**: | |
| - Personal events: ALL users | |
| - Course events: INSTRUCTOR, ADMIN only | |
| ### Event Types | |
| - \`lecture\`: Class lecture | |
| - \`lab\`: Lab session | |
| - \`exam\`: Examination | |
| - \`assignment\`: Assignment deadline | |
| - \`quiz\`: Quiz | |
| - \`meeting\`: Meeting | |
| - \`custom\`: Custom event | |
| ### Recurring Events | |
| Set \`isRecurring: true\` and specify \`recurrencePattern\` (daily, weekly, monthly). | |
| `, | |
| }) | |
| ({ type: CreateCalendarEventDto }) | |
| ({ status: 201, description: 'Calendar event created successfully' }) | |
| ({ status: 400, description: 'Invalid input data' }) | |
| ({ status: 403, description: 'Forbidden - Cannot create course events' }) | |
| (HttpStatus.CREATED) | |
| async create(() dto: CreateCalendarEventDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| const roles = this.extractRoles(req.user); | |
| return this.calendarService.create(dto, userId, roles); | |
| } | |
| (':id') | |
| ({ | |
| summary: 'Update calendar event', | |
| description: ` | |
| ## Update Calendar Event | |
| Updates an existing calendar event. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles**: | |
| - Own events: Event owner | |
| - Course events: INSTRUCTOR, ADMIN | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Calendar event ID', type: Number, example: 1 }) | |
| ({ type: UpdateCalendarEventDto }) | |
| ({ status: 200, description: 'Calendar event updated successfully' }) | |
| ({ status: 404, description: 'Event not found' }) | |
| ({ status: 403, description: 'Forbidden - Cannot edit this event' }) | |
| async update( | |
| ('id', ParseIntPipe) id: number, | |
| () dto: UpdateCalendarEventDto, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| const roles = this.extractRoles(req.user); | |
| return this.calendarService.update(id, dto, userId, roles); | |
| } | |
| (':id') | |
| (HttpStatus.OK) | |
| ({ | |
| summary: 'Delete calendar event', | |
| description: ` | |
| ## Delete Calendar Event | |
| Removes a calendar event. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles**: | |
| - Own events: Event owner | |
| - Course events: INSTRUCTOR, ADMIN | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Calendar event ID', type: Number, example: 1 }) | |
| ({ status: 200, description: 'Calendar event deleted successfully' }) | |
| ({ status: 404, description: 'Event not found' }) | |
| ({ status: 403, description: 'Forbidden - Cannot delete this event' }) | |
| async delete(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| const roles = this.extractRoles(req.user); | |
| return this.calendarService.delete(id, userId, roles); | |
| } | |
| private extractRoles(user: any): string[] { | |
| if (Array.isArray(user.roles)) { | |
| return user.roles.map((r: any) => (typeof r === 'string' ? r : r.name || r.roleName)); | |
| } | |
| return []; | |
| } | |
| } | |