Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.4 kB
3
Indexable
Never
import applicants from "../models/applicants";
import applicantTools from "../models/applicantTools";
import Question from "../models/applicantQuestions";
import ratings from "../models/applicantRatings"
class applicant {
    static async edit(res, req, next) {
        try {
            const { hireflixId, name, email, location, employmentType, tools, questions, description } = req.body
            const transaction = await sequelize.transaction();
            //create applicant id
            const newApplicant = await applicants.create({
                hireflixId,
                name,
                email,
                location,
                employmentType,
                description
            }, { transaction });
            //create tools that applicant use
            const toolsData = tools.map(question => ({
                title: question.title,
                mediaUrl: question.media ? question.media.url : null,
                applicantId: newApplicant.id
            }));
            await applicantTools.bulkCreate(toolsData, { transaction });
            //create questions that applicants have
            const questionsData = questions.map(question => ({
                title: question.title,
                mediaUrl: question.media ? question.media.url : null,
                applicantId: newApplicant.id
            }));
            await Question.bulkCreate(questionsData, { transaction });
            //create null rating
            const applicantRating = await ratings.create({
                applicantId: newApplicant.id,
                rating: null,
                review: null
            }, { transaction })
            //close mysql db interactions
            await transaction.commit();

            const applicantWithRelatedData = await applicants.findOne({
                where: { id: newApplicant.id },
                include: [
                    { model: tools },
                    { model: questions },
                    { model: applicantRating }
                ]
            });
            res.status(200).json({ message: 'success', data: applicantWithRelatedData })
        } catch (error) {
            res.status(400).json({ message: 'Failed to update user', error: error.message });
        }
    }
}
module.exports = applicant
Leave a Comment