ML-Learner / Backend_node /src /controllers /interview.controller.ts
VashuTheGreat2's picture
Upload folder using huggingface_hub
c01955c verified
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 { Query } from "mongoose";
import { Request, Response } from "express";
import logger from "../logger/create.logger.js";
export const createInterview = expressRepre(
{
summary: "schedule an interview",
body: {
companyName: "VTech",
topic: "AIML",
job_Role: "AI Engineer",
time: "2026-01-14 10:00:00",
status: "pending"
},
response: "interview scheduled"
},
asyncHandler(async (req:Request, res:Response) => {
const { companyName, topic, job_Role, time, status } = req.body;
const user_id = req.user?._id;
logger.info(`Scheduling interview for user ${user_id} at ${companyName}`);
if (!companyName || !topic || !job_Role || !time || !status) {
throw new ApiError(400, "All fields are required");
}
const interview = await Interview.findOne({
user_id,
companyName,
topic,
job_Role,
time
});
if (interview) {
logger.info("Interview already exists");
return res
.status(200)
.json(new ApiResponse(200, interview, "Interview already exists"));
}
const newInterview = await Interview.create({
user_id,
companyName,
topic,
job_Role,
time,
status
});
logger.info(`Interview created successfully: ${newInterview._id}`);
return res
.status(201)
.json(new ApiResponse(201, newInterview, "Interview created successfully"));
})
);
export const updateInterviewStatus = expressRepre(
{
summary: "update interview status",
body: {
id: "69671fa2d697bab6f0176ccc",
status: "live"
},
response: "interview updated"
},
asyncHandler(async (req, res) => {
const { id, status } = req.body;
logger.info(`Updating interview ${id} status to ${status}`);
if (!id || !status) {
throw new ApiError(400, "All fields are required");
}
const interview = await Interview.findByIdAndUpdate(
id,
{ status },
{ new: true }
);
if (!interview) {
throw new ApiError(404, "Interview not found");
}
res
.status(200)
.json(new ApiResponse(200, interview, "Interview updated successfully"));
})
);
export const getUserInterviews = expressRepre(
{
summary: "get user Interviews",
response: "interview fetched"
},
asyncHandler(async (req, res) => {
const user = req.user;
if (!user) {
throw new ApiError(400, "user required");
}
logger.info(`Fetching interviews for user: ${user._id}`);
const interview = await Interview.find(
{user_id:user?._id}
);
if (!interview) {
throw new ApiError(404, "Interviews not found");
}
res
.status(200)
.json(new ApiResponse(200, interview, "Interview fetched successfully"));
})
);
export const getInterviewById = expressRepre(
{
summary: "get interview by id",
query: {
id: "69671fa2d697bab6f0176ccc",
},
response: "interview "
},
asyncHandler(async (req, res) => {
const { id } = req.query;
logger.info(`Fetching interview by id: ${id}`);
if (!id ) {
throw new ApiError(400, "All fields are required");
}
const interview = await Interview.findById(id
);
if (!interview) {
throw new ApiError(404, "Interview not found");
}
res
.status(200)
.json(new ApiResponse(200, interview, "Interview fetched successfully"));
})
);
export const deleteInterview = expressRepre(
{
summary: "delete an interview",
params: {
id: "69671fa2d697bab6f0176ccc"
},
response: "interview deleted"
},
asyncHandler(async (req, res) => {
const { id } = req.params;
const user_id = req.user?._id;
logger.info(`Deleting interview ${id} for user ${user_id}`);
const interview = await Interview.findOneAndDelete({ _id: id, user_id });
if (!interview) {
throw new ApiError(404, "Interview not found or unauthorized");
}
res
.status(200)
.json(new ApiResponse(200, null, "Interview deleted successfully"));
})
);