Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Inject,
|
|
NotFoundException,
|
|
Param,
|
|
Patch,
|
|
Post,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import type { Brain } from '@mosaic/brain';
|
|
import { BRAIN } from '../brain/brain.tokens.js';
|
|
import { AuthGuard } from '../auth/auth.guard.js';
|
|
import type { CreateTaskDto, UpdateTaskDto } from './tasks.dto.js';
|
|
|
|
@Controller('api/tasks')
|
|
@UseGuards(AuthGuard)
|
|
export class TasksController {
|
|
constructor(@Inject(BRAIN) private readonly brain: Brain) {}
|
|
|
|
@Get()
|
|
async list(
|
|
@Query('projectId') projectId?: string,
|
|
@Query('missionId') missionId?: string,
|
|
@Query('status') status?: string,
|
|
) {
|
|
if (projectId) return this.brain.tasks.findByProject(projectId);
|
|
if (missionId) return this.brain.tasks.findByMission(missionId);
|
|
if (status)
|
|
return this.brain.tasks.findByStatus(
|
|
status as Parameters<typeof this.brain.tasks.findByStatus>[0],
|
|
);
|
|
return this.brain.tasks.findAll();
|
|
}
|
|
|
|
@Get(':id')
|
|
async findOne(@Param('id') id: string) {
|
|
const task = await this.brain.tasks.findById(id);
|
|
if (!task) throw new NotFoundException('Task not found');
|
|
return task;
|
|
}
|
|
|
|
@Post()
|
|
async create(@Body() dto: CreateTaskDto) {
|
|
return this.brain.tasks.create({
|
|
title: dto.title,
|
|
description: dto.description,
|
|
status: dto.status,
|
|
priority: dto.priority,
|
|
projectId: dto.projectId,
|
|
missionId: dto.missionId,
|
|
assignee: dto.assignee,
|
|
tags: dto.tags,
|
|
dueDate: dto.dueDate ? new Date(dto.dueDate) : undefined,
|
|
});
|
|
}
|
|
|
|
@Patch(':id')
|
|
async update(@Param('id') id: string, @Body() dto: UpdateTaskDto) {
|
|
const task = await this.brain.tasks.update(id, {
|
|
...dto,
|
|
dueDate: dto.dueDate ? new Date(dto.dueDate) : dto.dueDate === null ? null : undefined,
|
|
});
|
|
if (!task) throw new NotFoundException('Task not found');
|
|
return task;
|
|
}
|
|
|
|
@Delete(':id')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
async remove(@Param('id') id: string) {
|
|
const deleted = await this.brain.tasks.remove(id);
|
|
if (!deleted) throw new NotFoundException('Task not found');
|
|
}
|
|
}
|