119 lines
2.8 KiB
TypeScript
119 lines
2.8 KiB
TypeScript
import { FastifyReply, FastifyRequest } from "fastify";
|
|
import {
|
|
CreateTaskInput,
|
|
UpdateTaskInput,
|
|
UploadTaskInput,
|
|
} from "./task.schema";
|
|
import {
|
|
createTask,
|
|
deleteTask,
|
|
getTask,
|
|
listTasks,
|
|
newUpload,
|
|
searchTasks,
|
|
updateTask,
|
|
} from "./task.service";
|
|
import { PageQueryParams } from "../pagination";
|
|
|
|
export async function createTaskHandler(
|
|
req: FastifyRequest,
|
|
res: FastifyReply
|
|
) {
|
|
const input = req.body as CreateTaskInput;
|
|
|
|
try {
|
|
const rts = await createTask(input, req.user);
|
|
return res.code(201).send(rts);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|
|
|
|
export async function getTaskHandler(req: FastifyRequest, res: FastifyReply) {
|
|
const { taskId } = req.params as { taskId: string };
|
|
|
|
try {
|
|
const task = await getTask(taskId, req.user.tenantId);
|
|
if (task == null)
|
|
return res.code(404).send({ error: "resource not found" });
|
|
|
|
return res.code(200).send(task);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|
|
|
|
export async function listTaskHandler(req: FastifyRequest, res: FastifyReply) {
|
|
const queryParams = req.query as PageQueryParams;
|
|
|
|
try {
|
|
const taskList = await listTasks(queryParams, req.user.tenantId);
|
|
return res.code(200).send(taskList);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|
|
|
|
export async function updateTaskHandler(
|
|
req: FastifyRequest,
|
|
res: FastifyReply
|
|
) {
|
|
const input = req.body as UpdateTaskInput;
|
|
const { taskId } = req.params as { taskId: string };
|
|
|
|
try {
|
|
const updatedTask = await updateTask(taskId, input, req.user);
|
|
if (!updatedTask)
|
|
return res.code(404).send({ error: "resource not found" });
|
|
|
|
return res.code(200).send(updatedTask);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|
|
|
|
export async function deleteTaskHandler(
|
|
req: FastifyRequest,
|
|
res: FastifyReply
|
|
) {
|
|
const { taskId } = req.params as { taskId: string };
|
|
|
|
try {
|
|
const deleteResult = await deleteTask(taskId, req.user.tenantId);
|
|
if (deleteResult.deletedCount == 0)
|
|
return res.code(404).send({ error: "resource not found" });
|
|
|
|
return res.code(204).send();
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|
|
|
|
export async function searchTaskHandler(
|
|
req: FastifyRequest,
|
|
res: FastifyReply
|
|
) {
|
|
const queryParams = req.query as PageQueryParams;
|
|
|
|
try {
|
|
const taskList = await searchTasks(queryParams, req.user.tenantId);
|
|
return res.code(200).send(taskList);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|
|
|
|
export async function newFilesHandler(req: FastifyRequest, res: FastifyReply) {
|
|
const input = req.body as UploadTaskInput;
|
|
const { taskId } = req.params as { taskId: string };
|
|
|
|
try {
|
|
const updatedTask = await newUpload(taskId, input, req.user);
|
|
if (!updateTask) return res.code(404).send({ error: "resource not found" });
|
|
|
|
return res.code(200).send(updateTask);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|