File size: 3,407 Bytes
c01955c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Interview from "../models/interviews.models.js";
import { expressRepre } from "@vashuthegreat/vexpress";
import ApiError from "../utils/ApiError.js";
import asyncHandler from "../utils/asyncHandler.js";
import ApiResponse from "../utils/ApiResponse.js";
import Coding from "../models/CodingQuestions.models.js";
import { Request, Response } from "express";

import logger from "../logger/create.logger.js";

export const create_coding_schema = expressRepre(
  {
    summary: "creates coding performace schema",
    response: "coding performance"
  },

  asyncHandler(async (req:Request, res:Response) => {
    logger.info("Entered in the create coding schema")
    const user = req.user;
    if (!user) throw new ApiError(400, "User not found");

    let coding:any = await Coding.find({ user: user._id });

    if (coding && coding.length > 0) {
      return res.status(200).json(
        new ApiResponse(200, coding, "user already have coding schema")
      );
    }

    coding = await Coding.create({ user: user._id });

    if (!coding)
      throw new ApiError(500, "Error while initialising coding");

    logger.info("Exited from the create coding schema")
    return res.status(200).json(
      new ApiResponse(200, coding, "coding schema created")
    );
  })
);


export const get_coding_schema = expressRepre(
  {
    summary: "returns existing coding performace schema",
    response: "coding performance"
  },

  asyncHandler(async (req:Request, res:Response) => {
    const user = req.user;
    if (!user) throw new ApiError(400, "User not found");

    const coding = await Coding.find({ user: user._id });

    if (!coding || coding.length === 0) {
      // Auto-create to silently fix the issue for users without a schema
      const newCoding = await Coding.create({ user: user._id });
      return res.status(200).json(
        new ApiResponse(200, [newCoding], "coding schema created automatically")
      );
    }

    return res.status(200).json(
      new ApiResponse(200, coding, "coding schema fetched")
    );
  })
);

export const update_coding_schema = expressRepre(
  {
    summary: "updates existing coding performace schema",
    body: {
      recently_solved: ['1'],
      recently_visited: ['1'],
      all_questions_solved: ["1"],
      easy: 1,
      medium: 2,
      hard: 1
    },
    response: "coding performance"
  },

  asyncHandler(async (req:Request, res:Response) => {
    const user_id = req.user?._id;
    if (!user_id) throw new ApiError(400, "User not found");

    const {
      recently_solved,
      recently_visited,
      all_questions_solved,
      easy,
      medium,
      hard
    } = req.body;

    const to_map = {
      recently_solved,
      recently_visited,
      all_questions_solved,
      easy,
      medium,
      hard
    };

    const parameters_to_update = Object.fromEntries(
      Object.entries(to_map).filter(
        ([key, value]) => value !== undefined && value !== null
      )
    );

    logger.info(`Updating coding schema for user: ${user_id}`, { parameters_to_update });

    const coding = await Coding.findOneAndUpdate(
      { user: user_id },           
      { $set: parameters_to_update },
      { new: true }
    );

    if (!coding)
      throw new ApiError(500, "Error while updating coding");

    return res.status(200).json(
      new ApiResponse(200, coding, "data updated successfully")
    );
  })
);