Add task routes, bug fixes
This commit is contained in:
84
src/task/task.controller.ts
Normal file
84
src/task/task.controller.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { CreateTaskInput, UpdateTaskInput } from "./task.schema";
|
||||
import {
|
||||
createTask,
|
||||
deleteTask,
|
||||
getTask,
|
||||
listTasks,
|
||||
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.tenantId);
|
||||
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;
|
||||
}
|
||||
}
|
||||
82
src/task/task.route.ts
Normal file
82
src/task/task.route.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
import { $task } from "./task.schema";
|
||||
import {
|
||||
createTaskHandler,
|
||||
deleteTaskHandler,
|
||||
getTaskHandler,
|
||||
listTaskHandler,
|
||||
updateTaskHandler,
|
||||
} from "./task.controller";
|
||||
|
||||
export async function taskRoutes(fastify: FastifyInstance) {
|
||||
fastify.post(
|
||||
"/",
|
||||
{
|
||||
schema: {
|
||||
body: $task("createTaskInput"),
|
||||
},
|
||||
config: { requiredClaims: ["task:write"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
createTaskHandler
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
"/:taskId",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: "object",
|
||||
properties: {
|
||||
taskId: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
config: { requiredClaims: ["task:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
getTaskHandler
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
"/",
|
||||
{
|
||||
schema: {
|
||||
querystring: $task("pageQueryParams"),
|
||||
},
|
||||
|
||||
config: { requiredClaims: ["task:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
listTaskHandler
|
||||
);
|
||||
|
||||
fastify.patch(
|
||||
"/:taskId",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: "object",
|
||||
properties: { taskId: { type: "string" } },
|
||||
},
|
||||
body: $task("updateTaskInput"),
|
||||
},
|
||||
},
|
||||
updateTaskHandler
|
||||
);
|
||||
|
||||
fastify.delete(
|
||||
"/:taskId",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: "object",
|
||||
properties: { taskId: { type: "string" } },
|
||||
},
|
||||
},
|
||||
config: { requiredClaims: ["task:delete"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
deleteTaskHandler
|
||||
);
|
||||
}
|
||||
54
src/task/task.schema.ts
Normal file
54
src/task/task.schema.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import mongoose from "mongoose";
|
||||
import { z } from "zod";
|
||||
import { files } from "../file/file.schema";
|
||||
import { buildJsonSchemas } from "fastify-zod";
|
||||
import { pageQueryParams } from "../pagination";
|
||||
|
||||
const taskSchema = new mongoose.Schema({
|
||||
tenantId: { type: String, required: true },
|
||||
pid: { type: String, required: true, unique: true },
|
||||
title: String,
|
||||
dueDate: Date,
|
||||
files: Object,
|
||||
createdAt: Date,
|
||||
createdBy: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "user",
|
||||
},
|
||||
assignedTo: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "user",
|
||||
},
|
||||
});
|
||||
|
||||
export const taskFields = Object.keys(taskSchema.paths).filter(
|
||||
(path) => path !== "__v"
|
||||
);
|
||||
|
||||
export const taskModel = mongoose.model("task", taskSchema);
|
||||
|
||||
const createTaskInput = z.object({
|
||||
title: z.string(),
|
||||
dueDate: z.date().optional(),
|
||||
files: z.array(files).optional(),
|
||||
assignedTo: z.string().optional(),
|
||||
});
|
||||
|
||||
const updateTaskInput = z.object({
|
||||
title: z.string().optional(),
|
||||
dueDate: z.date().optional(),
|
||||
files: z.array(files).optional(),
|
||||
assignedTo: z.string().optional(),
|
||||
});
|
||||
|
||||
export type CreateTaskInput = z.infer<typeof createTaskInput>;
|
||||
export type UpdateTaskInput = z.infer<typeof updateTaskInput>;
|
||||
|
||||
export const { schemas: taskSchemas, $ref: $task } = buildJsonSchemas(
|
||||
{
|
||||
createTaskInput,
|
||||
updateTaskInput,
|
||||
pageQueryParams,
|
||||
},
|
||||
{ $id: "task" }
|
||||
);
|
||||
140
src/task/task.service.ts
Normal file
140
src/task/task.service.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { generateId } from "../utils/id";
|
||||
import {
|
||||
CreateTaskInput,
|
||||
taskFields,
|
||||
taskModel,
|
||||
UpdateTaskInput,
|
||||
} from "./task.schema";
|
||||
|
||||
export async function createTask(
|
||||
input: CreateTaskInput,
|
||||
user: AuthenticatedUser
|
||||
) {
|
||||
if (!input.files) {
|
||||
return await taskModel.create({
|
||||
...input,
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
});
|
||||
} else {
|
||||
return await taskModel.create({
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
documents: [
|
||||
{
|
||||
files: input.files,
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
},
|
||||
],
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateTask(
|
||||
taskId: string,
|
||||
input: UpdateTaskInput,
|
||||
tenantId: string
|
||||
) {
|
||||
const updatedTask = await taskModel
|
||||
.findOneAndUpdate({ tenantId: tenantId, pid: taskId }, input, { new: true })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
|
||||
return updatedTask;
|
||||
}
|
||||
|
||||
export async function getTask(taskId: string, tenantId: string) {
|
||||
return await taskModel
|
||||
.findOne({ tenantId: tenantId, pid: taskId })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
}
|
||||
|
||||
export async function listTasks(params: PageQueryParams, tenantId: string) {
|
||||
const page = params.page || 1;
|
||||
const pageSize = params.pageSize || 10;
|
||||
const sortObj = getSortObject(params, taskFields);
|
||||
const filterObj = getFilterObject(params, taskFields);
|
||||
|
||||
const taskList = await taskModel.aggregate([
|
||||
{
|
||||
$match: { $and: [{ tenantId: tenantId }, ...filterObj] },
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "createdBy",
|
||||
foreignField: "_id",
|
||||
as: "createdBy",
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "assignedTo",
|
||||
foreignField: "_id",
|
||||
as: "assignedTo",
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
pid: 1,
|
||||
createdAt: 1,
|
||||
createdBy: {
|
||||
$let: {
|
||||
vars: { createdBy: { $arrayElemAt: ["$createdBy", 0] } },
|
||||
in: {
|
||||
_id: "$$createdBy._id",
|
||||
pid: "$$createdBy.pid",
|
||||
name: "$$createdBy.name",
|
||||
},
|
||||
},
|
||||
},
|
||||
client: {
|
||||
$let: {
|
||||
vars: { assignedTo: { $arrayElemAt: ["$assignedTo", 0] } },
|
||||
in: {
|
||||
_id: "$$assignedTo._id",
|
||||
pid: "$$assignedTo.pid",
|
||||
name: "$$assignedTo.name",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
metadata: [{ $count: "count" }],
|
||||
data: [
|
||||
{ $skip: (page - 1) * pageSize },
|
||||
{ $limit: pageSize },
|
||||
{ $sort: sortObj },
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
if (taskList[0].data.length === 0)
|
||||
return { tasks: [], metadata: { count: 0, page, pageSize } };
|
||||
|
||||
return {
|
||||
tasks: taskList[0]?.data,
|
||||
metadata: {
|
||||
count: taskList[0].metadata[0].count,
|
||||
page,
|
||||
pageSize,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function deleteTask(taskId: string, tenantId: string) {
|
||||
return await taskModel.deleteOne({ pid: taskId, tenantId: tenantId });
|
||||
}
|
||||
Reference in New Issue
Block a user