added file upload handler to tasks
This commit is contained in:
@@ -33,7 +33,7 @@ export async function updateNote(
|
|||||||
},
|
},
|
||||||
{ ...input },
|
{ ...input },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
).populate({path: 'createdBy', select: 'pid name avatar'});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listNotes(resourceId: string, tenantId: string) {
|
export async function listNotes(resourceId: string, tenantId: string) {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { FastifyReply, FastifyRequest } from "fastify";
|
import { FastifyReply, FastifyRequest } from "fastify";
|
||||||
import { CreateTaskInput, UpdateTaskInput } from "./task.schema";
|
import { CreateTaskInput, UpdateTaskInput, UploadTaskInput } from "./task.schema";
|
||||||
import {
|
import {
|
||||||
createTask,
|
createTask,
|
||||||
deleteTask,
|
deleteTask,
|
||||||
getTask,
|
getTask,
|
||||||
listTasks,
|
listTasks,
|
||||||
|
newUpload,
|
||||||
searchTasks,
|
searchTasks,
|
||||||
updateTask,
|
updateTask,
|
||||||
} from "./task.service";
|
} from "./task.service";
|
||||||
@@ -97,3 +98,17 @@ export async function searchTaskHandler(
|
|||||||
return 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
deleteTaskHandler,
|
deleteTaskHandler,
|
||||||
getTaskHandler,
|
getTaskHandler,
|
||||||
listTaskHandler,
|
listTaskHandler,
|
||||||
|
newFilesHandler,
|
||||||
searchTaskHandler,
|
searchTaskHandler,
|
||||||
updateTaskHandler,
|
updateTaskHandler,
|
||||||
} from "./task.controller";
|
} from "./task.controller";
|
||||||
@@ -94,6 +95,18 @@ export async function taskRoutes(fastify: FastifyInstance) {
|
|||||||
searchTaskHandler
|
searchTaskHandler
|
||||||
);
|
);
|
||||||
|
|
||||||
|
fastify.post(
|
||||||
|
"/:taskId/files",
|
||||||
|
{
|
||||||
|
schema: {
|
||||||
|
body: $task("uploadTaskInput"),
|
||||||
|
},
|
||||||
|
config: { requiredClaims: ["rts:write"] },
|
||||||
|
preHandler: [fastify.authorize],
|
||||||
|
},
|
||||||
|
newFilesHandler
|
||||||
|
);
|
||||||
|
|
||||||
await noteRoutes(fastify, {
|
await noteRoutes(fastify, {
|
||||||
read: "task:read",
|
read: "task:read",
|
||||||
write: "task:write",
|
write: "task:write",
|
||||||
|
|||||||
@@ -86,13 +86,19 @@ const updateTaskInput = z.object({
|
|||||||
.optional(),
|
.optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const uploadTaskInput = z.object({
|
||||||
|
files: z.array(files),
|
||||||
|
});
|
||||||
|
|
||||||
export type CreateTaskInput = z.infer<typeof createTaskInput>;
|
export type CreateTaskInput = z.infer<typeof createTaskInput>;
|
||||||
export type UpdateTaskInput = z.infer<typeof updateTaskInput>;
|
export type UpdateTaskInput = z.infer<typeof updateTaskInput>;
|
||||||
|
export type UploadTaskInput = z.infer<typeof uploadTaskInput>;
|
||||||
|
|
||||||
export const { schemas: taskSchemas, $ref: $task } = buildJsonSchemas(
|
export const { schemas: taskSchemas, $ref: $task } = buildJsonSchemas(
|
||||||
{
|
{
|
||||||
createTaskInput,
|
createTaskInput,
|
||||||
updateTaskInput,
|
updateTaskInput,
|
||||||
|
uploadTaskInput,
|
||||||
pageQueryParams,
|
pageQueryParams,
|
||||||
},
|
},
|
||||||
{ $id: "task" }
|
{ $id: "task" }
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
taskFields,
|
taskFields,
|
||||||
taskModel,
|
taskModel,
|
||||||
UpdateTaskInput,
|
UpdateTaskInput,
|
||||||
|
UploadTaskInput,
|
||||||
} from "./task.schema";
|
} from "./task.schema";
|
||||||
|
|
||||||
export async function createTask(
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user