Untitled

 avatar
unknown
javascript
a year ago
4.1 kB
7
Indexable
const Applicants = require("../models").Applicant;
const ApplicantTools = require("../models").applicantTools;
const Question = require("../models").applicantQuestions;
const ratings = require("../models").applicantRatings;
const ToolsData = require("../models").toolsData;
const { sequelize } = require("../models");
const models = require("../models");
const applicantQuestions = require("../models/applicantQuestions");
class applicant {
    static async edit(req, res) {
        const { hireflixId, name, email, location, employmentType, applicantTools, questions, description, verrified, position } = req.body;
        if (!hireflixId || !name || !email || !location || !employmentType || !applicantTools || !questions || !description || !verrified || !position) {
            return res.status(400).json({ message: 'Please fill all data' });
        }
        try {
            //open safe way to connect with db
            const result = await sequelize.transaction(async (t) => {

                //create applicant id
                const newApplicant = await Applicants.create({
                    hireflix_id: hireflixId,
                    name,
                    email,
                    location,
                    employment_type: employmentType,
                    description,
                    verrified,
                    position
                }, { transaction: t });
                //create tools that applicant use
                const toolsData = applicantTools.map(applicantTools => ({
                    tools: applicantTools.tools,
                    applicant_id: newApplicant.id,
                }));
                await ApplicantTools.bulkCreate(toolsData, { transaction: t });
                //create questions that applicants have
                const questionsData = questions.map(question => ({
                    tittle: question.tittle,
                    url: question.media ? question.media.url : null,
                    applicant_id: newApplicant.id
                }));
                await Question.bulkCreate(questionsData, { transaction: t });
                //create null rating
                await ratings.create({
                    applicant_id: newApplicant.id,
                    rating: null,
                    review: null
                }, { transaction: t });
            });
            const applicantWithRelatedData = await Applicants.findOne({
                where: { hireflix_id: hireflixId },
                include: [
                    'questions',
                    'tools',
                    'review'
                ]
            });
            res.status(200).json({ message: 'success', data: applicantWithRelatedData });
        } catch (error) {
            res.status(400).json({ message: 'Failed to update user', error: error.message });
        }
    }

    static async get(req, res, next) {
        try {
            const getApplicant = await Applicants.findAll({
                where: { verrified: true },
                include: [
                    {
                        model: models.applicantQuestions,
                        as: 'questions'
                    },
                    {
                        model: models.ApplicantTools,
                        as: 'tools',
                        include: {
                            model: ToolsData,
                            as: 'toolData',
                        },
                    },
                    {
                        model: models.ratings,
                        as: 'review'
                    }
                ]
            });
            if (!getApplicant) {
                res.status(404).json({ message: 'There is no data or data not found' })
            } else {
                res.status(200).json({ message: 'Success get data', data: getApplicant })
            }
        } catch (error) {
            res.status(400).json({ message: 'Failed to get data', error: error.message })
        }
    }
}
module.exports = applicant;
Editor is loading...
Leave a Comment