Spaces:
Sleeping
Sleeping
File size: 5,151 Bytes
e67d35c 2523527 e67d35c 2523527 e67d35c 2523527 e67d35c 2523527 e67d35c 2523527 e67d35c | 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 | import {
Controller,
Get,
Post,
Patch,
Delete,
HttpCode,
HttpStatus,
Body,
Param,
Query,
Req,
ParseIntPipe,
UseGuards,
} from '@nestjs/common';
import {
ApiTags,
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiParam,
ApiQuery,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { TasksService } from '../services/tasks.service';
import { RemindersService } from '../services/reminders.service';
import {
CreateTaskDto,
UpdateTaskDto,
TaskQueryDto,
CreateReminderDto,
} from '../dto';
@ApiTags('📋 Tasks & Reminders')
@ApiBearerAuth('JWT-auth')
@Controller('api/tasks')
@UseGuards(JwtAuthGuard, RolesGuard)
export class TasksController {
constructor(
private readonly tasksService: TasksService,
private readonly remindersService: RemindersService,
) {}
@Get()
@ApiOperation({ summary: 'List my tasks' })
@ApiResponse({ status: 200, description: 'Paginated list of tasks' })
async findAll(@Query() query: TaskQueryDto, @Req() req: any) {
const userId = req.user.userId || req.user.id;
return this.tasksService.findAll(userId, query);
}
@Post()
@ApiOperation({ summary: 'Create a new task' })
@ApiResponse({ status: 201, description: 'Task created' })
async create(@Body() dto: CreateTaskDto, @Req() req: any) {
const userId = req.user.userId || req.user.id;
return this.tasksService.create(dto, userId);
}
// Static routes BEFORE :id param routes
@Get('upcoming')
@ApiOperation({ summary: 'Get upcoming tasks' })
@ApiQuery({
name: 'days',
required: false,
type: Number,
description: 'Number of days ahead (default 7)',
})
@ApiResponse({ status: 200, description: 'List of upcoming tasks' })
async findUpcoming(@Query('days') days: number, @Req() req: any) {
const userId = req.user.userId || req.user.id;
return this.tasksService.findUpcoming(userId, days ? +days : 7);
}
@Get('reminders')
@ApiOperation({ summary: 'List my reminders' })
@ApiResponse({ status: 200, description: 'List of reminders' })
async findAllReminders(@Req() req: any) {
const userId = req.user.userId || req.user.id;
return this.remindersService.findAll(userId);
}
@Post('reminders')
@ApiOperation({ summary: 'Create a reminder' })
@ApiResponse({ status: 201, description: 'Reminder created' })
async createReminder(@Body() dto: CreateReminderDto, @Req() req: any) {
const userId = req.user.userId || req.user.id;
return this.remindersService.create(dto, userId);
}
@Delete('reminders/:id')
@ApiOperation({ summary: 'Delete a reminder' })
@ApiParam({ name: 'id', description: 'Reminder ID' })
@ApiResponse({ status: 200, description: 'Reminder deleted' })
@ApiResponse({ status: 404, description: 'Reminder not found' })
async removeReminder(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
const userId = req.user.userId || req.user.id;
await this.remindersService.remove(id, userId);
return { message: 'Reminder deleted successfully' };
}
// Parameterized routes AFTER static routes
@Get(':id')
@ApiOperation({ summary: 'Get task detail' })
@ApiParam({ name: 'id', description: 'Task ID' })
@ApiResponse({ status: 200, description: 'Task details' })
@ApiResponse({ status: 404, description: 'Task not found' })
async findOne(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
const userId = req.user.userId || req.user.id;
return this.tasksService.findOne(id, userId);
}
@Patch(':id')
@ApiOperation({ summary: 'Update a task' })
@ApiParam({ name: 'id', description: 'Task ID' })
@ApiResponse({ status: 200, description: 'Task updated' })
@ApiResponse({ status: 404, description: 'Task not found' })
async update(
@Param('id', ParseIntPipe) id: number,
@Body() dto: UpdateTaskDto,
@Req() req: any,
) {
const userId = req.user.userId || req.user.id;
return this.tasksService.update(id, dto, userId);
}
@Post(':id/complete')
@ApiOperation({ summary: 'Mark task as complete' })
@ApiParam({ name: 'id', description: 'Task ID' })
@HttpCode(HttpStatus.OK)
@ApiResponse({ status: 200, description: 'Task marked as complete' })
@ApiResponse({ status: 404, description: 'Task not found' })
async complete(
@Param('id', ParseIntPipe) id: number,
@Body() body: { notes?: string; timeTakenMinutes?: number },
@Req() req: any,
) {
const userId = req.user.userId || req.user.id;
return this.tasksService.complete(
id,
userId,
body.notes,
body.timeTakenMinutes,
);
}
@Delete(':id')
@ApiOperation({ summary: 'Delete a task' })
@ApiParam({ name: 'id', description: 'Task ID' })
@ApiResponse({ status: 200, description: 'Task deleted' })
@ApiResponse({ status: 404, description: 'Task not found' })
async remove(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
const userId = req.user.userId || req.user.id;
await this.tasksService.remove(id, userId);
return { message: 'Task deleted successfully' };
}
}
|