{"size": 4406, "ext": "js", "max_stars_repo_path": "Node.js Server Workspace/routes/emailOrder.js", "max_stars_repo_name": "haasj22/cs341_Sprint3", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Node.js Server Workspace/routes/emailOrder.js", "max_issues_repo_name": "haasj22/cs341_Sprint3", "max_issues_repo_issues_event_min_datetime": "2021-04-08T16:11:48.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-01T23:51:12.000Z", "max_forks_repo_path": "Node.js Server Workspace/routes/emailOrder.js", "max_forks_repo_name": "haasj22/cs341_Sprint3", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 21, "max_forks_count": null, "avg_line_length": 33.1278195489, "max_line_length": 142, "alphanum_fraction": 0.6398093509}
//Author: Alex Junkins, Adrian Muth, Reggie Nillo and Justin Cao
//Version: April 27 2021
//A router for the staff request page to send requests as an email
var express = require('express');
var router = express.Router();
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());


//email function retrieved from this tutorial:
//https://www.w3schools.com/nodejs/nodejs_email.asp

//import email module
var nodemailer = require('nodemailer');
const { json } = require('body-parser');
const e = require('express');

//success variable
var success;

//create email variables
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'UP.AVS.Item.Request.Bot@gmail.com',
        pass: 'Goodbye-Software-Engineering'
    }
});

var mailOptions = {
    from: 'UP.AVS.Item.Request.Bot@gmail.com',
    to: 'av@up.edu, UP.AVS.Item.Request.Bot@gmail.com', //to be sent to the official account, av@up.edu, as well as recorded by the email bot
    subject: '',
    text: '',
};


//a function to send an email to the designated address 
function sendEmailViaServer(body, requestee){
    
    console.log("Using the Node.js server to send an email with body: " + body + " from user " + requestee);
    mailOptions.subject = "UP Equipment Rental Website request from " + requestee;
    mailOptions.text = body;

    transporter.sendMail(mailOptions, function(error, info){
        if (error) {
          console.log(error);
          success = false;
        } else {
          console.log('Email sent: ' + info.response);
          success = true;
        }
    });
}

//POST reciever, turning the received ids into strings
router.post('/', function(req, res, next) {
    console.log("Received email order request.");

    //assuming these are in a json object in this format:
    /* var jsonObj = {
        requestee: string, (null w/o sign-on feature)
        itemCount: int, (number of items requested)
        itemArray: [
            item1 = "key1*itemName1*quant1*comment1",
            item2 = "key2*itemName2*quant1*comment2",
            ...
        ]
    }; */

    //retrieve data variables
    var jsonObj = req.body;
    var requestee = jsonObj.requestee;
    console.log("Requestee: " + requestee)
    var itemCount = jsonObj.itemCount;
    console.log("Item Count: " + itemCount)
    var itemsDataArrayRaw = jsonObj.itemArray;
    console.log("Item Array: " + itemsDataArrayRaw)

    //construct intial string line
    var emailString = "User '" + requestee + "' has requested items from the University of Portland Equipment Rental Request Website:\n\n";

    //check item count and assign vars accordingly
    if (itemCount < 1){
        console.error("Invalid number of items!");
        res.send(false);
        return;
    } else if (itemCount == 1){ //to avoid a single string being read as an array of characters
        itemsDataArray = [1];
        itemsDataArray[0] = itemsDataArrayRaw; 
    } else {
        itemsDataArray = itemsDataArrayRaw;
    }

    //convert data variables into email script lines
    var emailStringLines = [itemsDataArray.length];
    for (var i = 0; i < itemsDataArray.length; i++) {
        console.log("Item " + (i+1));
        splitString = itemsDataArray[i].split("*");
        key = splitString[0];
        console.log("\tKey: " + key);
        itemName = splitString[1];
        console.log("\tName: " + itemName);
        quant = splitString[2];
        console.log("\tQuantity: " + quant);
        comment = splitString[3];
        console.log("\tComment: " + comment);
        emailStringLine = "Item '" + itemName + "' with ID key number '" + key+ "'. Quantity: " + quant + ".\nComment: '" + comment + "'\n\n";
        emailStringLines[i] = emailStringLine;
    }

    //combine data script lines with opening lines and append closing line
    for (var j = 0; j < emailStringLines.length; j++){
        emailString = emailString + emailStringLines[j];
    }
    emailString = emailString + "\nSent automatically by the rental site. Please do not respond to this email."

    console.log("\nFinal email string:\n-");
    console.log(emailString);
    console.log("-");

    //send email
    sendEmailViaServer(emailString, requestee);

    //report success value
    res.send({"success": success});
});

module.exports = router;