<commit_before>'use strict';

const cleanPayload             = require('./utils/app.js').cleanPayload;
const interviewSchema          = require('./utils/joiSchema.js').interviewSchema;
const client                   = require('../db/client.js');
const getInterviewDetails      = require('../db/redis/getInterviewDetails.js');
const moveToNextInterviewStage = require('../db/redis/moveToNextInterviewStage');
const uuid                     = require('uuid');

const fs                       = require('fs');
const changeInterview          = require('./utils/mailgun.js').changeInterview;
const interviewConfirmation    = require('./utils/mailgun.js').interviewConfirmation;
const proposedInterview        = require('./utils/mailgun.js').proposedInterview;

const mailgun                  = require('./utils/mailgun.js');

exports.register = (server, options, next) => {

    server.route([

         {
            method: 'POST',
            path: '/interview/proposed',
            config: {
                auth: {
                    strategy: 'hrns-cookie'
                        // scope: ['client', 'agent']
                },
                validate: {
                    payload: interviewSchema
                },
                handler: (request, reply) => {
                    ///  must update stage in db;
                    const newTimes = request.payload;
                   
                    const interviewId = uuid.v1() + 'interviewId';
                    newTimes.interviewId = interviewId;
                    newTimes.confirmed = false;                  
                
                    getInterviewDetails(newTimes, (interviewData) => {
                   
                        if (interviewData) {
                      
                            let newProposedInterview  = Object.assign({}, proposedInterview);
                     
                            newProposedInterview.html = newProposedInterview.html
                                .replace('-candidateName-', interviewData.candidateName)
                                .replace('-jobTitle-', interviewData.jobTitle) //switch to client jobTitle. 
                                .replace('-companyName-', interviewData.clientCompanyName)
                                .replace('-interviewId-', interviewData.interviewId);

                            newProposedInterview.to = interviewData.agencyEmail;                           
                            
                            mailgun.messages().send(newProposedInterview, (error, body) => {
                                if (error) {
                                    console.log(error);
                                } else {
                                    console.log(body);
                                    client.hmset(interviewId, interviewData, (err, res) => {
                                        if (err) {
                                            console.log('err', err);
                                            return reply(false);
                                        }
                                        if (res) {
                                            return reply(interviewData.cvid);
                                        }
                                    });

                                }
                            });

                        } else {
                            console.log('error', err)
                            return reply(false);
                        }

                    });

                }
            }
        }, {
            method: 'GET',
            path: '/interview/email/{interviewId*}',
            config: {
                auth: false,
                handler: (request, reply) => {

                    let opts = {};
                    const interviewId = request.params.interviewId;

                    client.hgetall(interviewId, (err, res) => {

                        if ((res) && (res.confirmed === 'false')) {
                            opts = res;
                            opts.interviewId = interviewId;
                            opts.title = 'Interview Page';
                            let cleanedPayload = cleanPayload(opts);
                            return reply.view('interview', cleanedPayload);

                        } else {
                            opts.title = 'Interview Page';
                            opts.message = 'This link has expired';
                            return reply.view('message', opts);
                        }

                    });
                }
            }
        }, {
            method: 'POST',
            path: '/change/interview',
            config: {
                auth: false,
                handler: (request, reply) => {
                    // request.auth.credentials.type;
                    const newTimes = request.payload;
                    const interviewId = newTimes.interviewId;

                    if (interviewId) {
                     
                        client.hgetall(interviewId, (err, res) => {

                            let interviewData = Object.assign(res,newTimes);
                                                  
                            let emailContext =Object.assign({}, changeInterview)                       

                            emailContext.html  = changeInterview.html
                                .replace('-candidateName-', interviewData.candidateName)
                                .replace('-jobTitle-', interviewData.jobTitle)
                                .replace('-interviewId-', interviewData.interviewId);

                            emailContext.to = interviewData.agencyEmail;
                            mailgun.messages().send(emailContext, (error, agencyReply) => {
                                if (error) {
                                    reply(false);
                                } else if (agencyReply) {
                                    emailContext.to = interviewData.clientEmail;
                                    mailgun.messages().send(emailContext, (error, clientReply) => {
                                        if (error) {
                                            reply(false);
                                        } else if (clientReply) {

                                            let cleanedPayload = cleanPayload(interviewData);

                                            client.hmset(interviewData.interviewId, cleanedPayload, (err, res) => {
                                                if (error) {
                                                    console.log('ERROR');
                                                    reply(false);
                                                } else if (res) {
                                                    console.log('true');
                                                    reply(cleanedPayload);
                                                }
                                            });

                                        }
                                    });
                                }

                            });
                        });
                    } else {
                        reply(false);

                    }
                }
            }
        }, {
            method: 'GET',
            path: '/interview/confirmed', ///////this should be connected to confirmation button. 
            config: {
                auth: false,
                handler: (request, reply) => {
                    let confirmedIntTime = request.query.confirmedIntTime;
                    let confirmedIntDate = request.query.confirmedIntDate;
                    let interviewId = request.query.interviewId;

                    client.hgetall(interviewId, (err, res) => {
                        if (err) {
                            return reply(false);
                        }
                        if (res) {
                           <commit_msg>log the emailContext when rearrange interview<commit_after>'use strict';

const cleanPayload             = require('./utils/app.js').cleanPayload;
const interviewSchema          = require('./utils/joiSchema.js').interviewSchema;
const client                   = require('../db/client.js');
const getInterviewDetails      = require('../db/redis/getInterviewDetails.js');
const moveToNextInterviewStage = require('../db/redis/moveToNextInterviewStage');
const uuid                     = require('uuid');

const fs                       = require('fs');
const changeInterview          = require('./utils/mailgun.js').changeInterview;
const interviewConfirmation    = require('./utils/mailgun.js').interviewConfirmation;
const proposedInterview        = require('./utils/mailgun.js').proposedInterview;

const mailgun                  = require('./utils/mailgun.js');

exports.register = (server, options, next) => {

    server.route([

         {
            method: 'POST',
            path: '/interview/proposed',
            config: {
                auth: {
                    strategy: 'hrns-cookie'
                        // scope: ['client', 'agent']
                },
                validate: {
                    payload: interviewSchema
                },
                handler: (request, reply) => {
                    ///  must update stage in db;
                    const newTimes = request.payload;
                   
                    const interviewId = uuid.v1() + 'interviewId';
                    newTimes.interviewId = interviewId;
                    newTimes.confirmed = false;                  
                
                    getInterviewDetails(newTimes, (interviewData) => {
                   
                        if (interviewData) {
                      
                            let newProposedInterview  = Object.assign({}, proposedInterview);
                     
                            newProposedInterview.html = newProposedInterview.html
                                .replace('-candidateName-', interviewData.candidateName)
                                .replace('-jobTitle-', interviewData.jobTitle) //switch to client jobTitle. 
                                .replace('-companyName-', interviewData.clientCompanyName)
                                .replace('-interviewId-', interviewData.interviewId);

                            newProposedInterview.to = interviewData.agencyEmail;                           
                            
                            mailgun.messages().send(newProposedInterview, (error, body) => {
                                if (error) {
                                    console.log(error);
                                } else {
                                    console.log(body);
                                    client.hmset(interviewId, interviewData, (err, res) => {
                                        if (err) {
                                            console.log('err', err);
                                            return reply(false);
                                        }
                                        if (res) {
                                            return reply(interviewData.cvid);
                                        }
                                    });

                                }
                            });

                        } else {
                            console.log('error', err)
                            return reply(false);
                        }

                    });

                }
            }
        }, {
            method: 'GET',
            path: '/interview/email/{interviewId*}',
            config: {
                auth: false,
                handler: (request, reply) => {

                    let opts = {};
                    const interviewId = request.params.interviewId;

                    client.hgetall(interviewId, (err, res) => {

                        if ((res) && (res.confirmed === 'false')) {
                            opts = res;
                            opts.interviewId = interviewId;
                            opts.title = 'Interview Page';
                            let cleanedPayload = cleanPayload(opts);
                            return reply.view('interview', cleanedPayload);

                        } else {
                            opts.title = 'Interview Page';
                            opts.message = 'This link has expired';
                            return reply.view('message', opts);
                        }

                    });
                }
            }
        }, {
            method: 'POST',
            path: '/change/interview',
            config: {
                auth: false,
                handler: (request, reply) => {
                    // request.auth.credentials.type;
                    const newTimes = request.payload;
                    const interviewId = newTimes.interviewId;

                    if (interviewId) {
                     
                        client.hgetall(interviewId, (err, res) => {

                            let interviewData = Object.assign(res,newTimes);
                                                  
                            let emailContext =Object.assign({}, changeInterview)                       

                            emailContext.html  = changeInterview.html
                                .replace('-candidateName-', interviewData.candidateName)
                                .replace('-jobTitle-', interviewData.jobTitle)
                                .replace('-interviewId-', interviewData.interviewId);

                            emailContext.to = interviewData.agencyEmail;
                            console.log('Email Context >>>>>>>>>>>>>>>>>>>>>>>', emailContext);
                            mailgun.messages().send(emailContext, (error, agencyReply) => {
                                if (error) {
                                    reply(false);
                                } else if (agencyReply) {
                                    emailContext.to = interviewData.clientEmail;
                                    mailgun.messages().send(emailContext, (error, clientReply) => {
                                        if (error) {
                                            reply(false);
                                        } else if (clientReply) {

                                            let cleanedPayload = cleanPayload(interviewData);

                                            client.hmset(interviewData.interviewId, cleanedPayload, (err, res) => {
                                                if (error) {
                                                    console.log('ERROR');
                                                    reply(false);
                                                } else if (res) {
                                                    console.log('true');
                                                    reply(cleanedPayload);
                                                }
                                            });

                                        }
                                    });
                                }

                            });
                        });
                    } else {
                        reply(false);
                    }
                }
            }
        }, {
            method: 'GET',
            path: '/interview/confirmed', ///////this should be connected to confirmation button. 
            config: {
                auth: false,
                handler: (request, reply) => {
                    let confirmedIntTime = request.query.confirmedIntTime;
                    let confirmedIntDate = request.query.confirmedIntDate;
                    let interviewId = request.query.interviewId;

                    client.hgetall(interviewId, (err, res) => {
                        if (err) {
                            return reply(false);
                        }
                        if (res) {
                           
{'fraction_non_alphanumeric': 0.08985168318050342, 'fraction_numerical': 0.00012308449750753894, 'mean_word_length': 0.7967713401149934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 106, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2939173', 'n_tokens_mistral': 3290, 'n_tokens_neox': 3060, 'n_words': 574}