added file upload handler to tasks

This commit is contained in:
2025-02-14 12:48:24 +05:30
parent a933c25f34
commit 5276f2a6cb
5 changed files with 56 additions and 2 deletions

View File

@@ -33,7 +33,7 @@ export async function updateNote(
},
{ ...input },
{ new: true }
);
).populate({path: 'createdBy', select: 'pid name avatar'});
}
export async function listNotes(resourceId: string, tenantId: string) {

View File

@@ -1,10 +1,11 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { CreateTaskInput, UpdateTaskInput } from "./task.schema";
import { CreateTaskInput, UpdateTaskInput, UploadTaskInput } from "./task.schema";
import {
createTask,
deleteTask,
getTask,
listTasks,
newUpload,
searchTasks,
updateTask,
} from "./task.service";
@@ -97,3 +98,17 @@ export async function searchTaskHandler(
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;
}
}

View File

@@ -5,6 +5,7 @@ import {
deleteTaskHandler,
getTaskHandler,
listTaskHandler,
newFilesHandler,
searchTaskHandler,
updateTaskHandler,
} from "./task.controller";
@@ -94,6 +95,18 @@ export async function taskRoutes(fastify: FastifyInstance) {
searchTaskHandler
);
fastify.post(
"/:taskId/files",
{
schema: {
body: $task("uploadTaskInput"),
},
config: { requiredClaims: ["rts:write"] },
preHandler: [fastify.authorize],
},
newFilesHandler
);
await noteRoutes(fastify, {
read: "task:read",
write: "task:write",

View File

@@ -86,13 +86,19 @@ const updateTaskInput = z.object({
.optional(),
});
const uploadTaskInput = z.object({
files: z.array(files),
});
export type CreateTaskInput = z.infer<typeof createTaskInput>;
export type UpdateTaskInput = z.infer<typeof updateTaskInput>;
export type UploadTaskInput = z.infer<typeof uploadTaskInput>;
export const { schemas: taskSchemas, $ref: $task } = buildJsonSchemas(
{
createTaskInput,
updateTaskInput,
uploadTaskInput,
pageQueryParams,
},
{ $id: "task" }

View File

@@ -6,6 +6,7 @@ import {
taskFields,
taskModel,
UpdateTaskInput,
UploadTaskInput,
} from "./task.schema";
export async function createTask(
@@ -227,3 +228,22 @@ export async function searchTasks(params: PageQueryParams, tenantId: string) {
},
};
}
export async function newUpload(
id: string,
newUpload: UploadTaskInput,
user: AuthenticatedUser
) {
return await taskModel.findOneAndUpdate(
{ pid: id, tenantId: user.tenantId },
{
$push: {
documents: {
files: newUpload.files,
createdAt: new Date(),
createdBy: user.userId,
},
},
}
);
}