|
| 1 | +import { addLog } from "../models/logs"; |
| 2 | +const { logType } = require("../constants/logs"); |
| 3 | +import { CustomRequest, CustomResponse } from "../types/global"; |
| 4 | +const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages"); |
| 5 | +const ApplicationModel = require("../models/applications"); |
| 6 | + |
| 7 | +const getAllOrUserApplication = async (req: CustomRequest, res: CustomResponse): Promise<any> => { |
| 8 | + try { |
| 9 | + const { userId } = req.query; |
| 10 | + if (userId) { |
| 11 | + const application = await ApplicationModel.getUserApplications(userId); |
| 12 | + return res.json({ |
| 13 | + message: "application returned successfully!", |
| 14 | + application, |
| 15 | + }); |
| 16 | + } |
| 17 | + |
| 18 | + const applications = await ApplicationModel.getAllApplications(); |
| 19 | + return res.json({ |
| 20 | + message: "applications returned successfully!", |
| 21 | + applications, |
| 22 | + }); |
| 23 | + } catch (err) { |
| 24 | + logger.error(`Error in fetching application: ${err}`); |
| 25 | + return res.boom.badImplementation(INTERNAL_SERVER_ERROR); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +const addApplication = async (req: CustomRequest, res: CustomResponse) => { |
| 30 | + try { |
| 31 | + const rawData = req.body; |
| 32 | + const application = await ApplicationModel.getUserApplications(req.userData.id); |
| 33 | + if (!application.notFound && !application.status) { |
| 34 | + return res.status(409).json({ |
| 35 | + message: "User data is already present!", |
| 36 | + }); |
| 37 | + } |
| 38 | + const data = { |
| 39 | + userId: req.userData.id, |
| 40 | + biodata: { |
| 41 | + firstName: rawData.firstName, |
| 42 | + lastName: rawData.lastName, |
| 43 | + }, |
| 44 | + location: { |
| 45 | + city: rawData.city, |
| 46 | + state: rawData.state, |
| 47 | + country: rawData.country, |
| 48 | + }, |
| 49 | + professional: { |
| 50 | + institution: rawData.college, |
| 51 | + skills: rawData.skills, |
| 52 | + }, |
| 53 | + intro: { |
| 54 | + introduction: rawData.introduction, |
| 55 | + funFact: rawData.funFact, |
| 56 | + forFun: rawData.forFun, |
| 57 | + whyRds: rawData.whyRds, |
| 58 | + numberOfHours: rawData.numberOfHours, |
| 59 | + }, |
| 60 | + foundFrom: rawData.foundFrom, |
| 61 | + }; |
| 62 | + await ApplicationModel.addApplication(data); |
| 63 | + |
| 64 | + return res.status(201).json({ |
| 65 | + message: "User application added.", |
| 66 | + }); |
| 67 | + } catch (err) { |
| 68 | + logger.error(`Error while adding application: ${err}`); |
| 69 | + return res.boom.badImplementation(INTERNAL_SERVER_ERROR); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +const updateApplication = async (req: CustomRequest, res: CustomResponse) => { |
| 74 | + try { |
| 75 | + const { applicationId } = req.params; |
| 76 | + const rawBody = req.body; |
| 77 | + |
| 78 | + const applicationLog = { |
| 79 | + type: logType.APPLICATION_UPDATED, |
| 80 | + meta: { |
| 81 | + applicationId, |
| 82 | + username: req.userData.username, |
| 83 | + userId: req.userData.id, |
| 84 | + }, |
| 85 | + body: rawBody, |
| 86 | + }; |
| 87 | + |
| 88 | + const promises = [ |
| 89 | + ApplicationModel.updateApplication(rawBody, applicationId), |
| 90 | + addLog(applicationLog.type, applicationLog.meta, applicationLog.body), |
| 91 | + ]; |
| 92 | + |
| 93 | + await Promise.all(promises); |
| 94 | + return res.json({ |
| 95 | + message: "Application updated successfully!", |
| 96 | + }); |
| 97 | + } catch (err) { |
| 98 | + logger.error(`Error while updating the application: ${err}`); |
| 99 | + return res.boom.badImplementation(INTERNAL_SERVER_ERROR); |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +module.exports = { |
| 104 | + getAllOrUserApplication, |
| 105 | + addApplication, |
| 106 | + updateApplication, |
| 107 | +}; |
0 commit comments