{"size": 1303, "ext": "js", "max_stars_repo_path": "src/api/task/TaskApply.js", "max_stars_repo_name": "Jurnace/InRo-backend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/api/task/TaskApply.js", "max_issues_repo_name": "Jurnace/InRo-backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/api/task/TaskApply.js", "max_forks_repo_name": "Jurnace/InRo-backend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 38.3235294118, "max_line_length": 170, "alphanum_fraction": 0.6139677667}
import { UserInfo, TaskInfo } from "../../Inro";
import { newNotification } from "../notification/NotificationNew";
import Terminal from "../../Terminal";

/**
 * @param {import("fastify").FastifyInstance} fastify
 */
export default async function routes(fastify, options) {
    fastify.post("/task/apply", { preValidation: [fastify.auth] }, async (request, reply) => {
        const taskInfo = await TaskInfo.findOne({ taskId: request.body.taskId });

        if(!taskInfo) {
            reply.code(403).send({ error: "Task not found!" });
            return;
        }

        taskInfo.applied.push(request.body.userId);

        const userInfo = await UserInfo.findOne({ userId: request.body.userId });
        userInfo.tasksApplied.push(request.body.taskId);

        const userTarget = await UserInfo.findOne({ userId: taskInfo.ownerId });

        try {
            await taskInfo.save();
            await userInfo.save();
            await newNotification(userTarget.userId, "New offer", `${userInfo.username} has applied for the task ${taskInfo.title}.`, taskInfo.taskId, "channel_applied");
            reply.send({ message: "success" });
        } catch(err) {
            Terminal.severe(err.stack);
            reply.code(403).send({ error: "Something went wrong..."});
        }
    });
}