const express = require('express');
const auth = require('../middleware/authenticator.js');
const mysql = require("../db/mysql.js");
const router = express.Router();
/**
* @typedef {object} PersonGetPackage
* @property {string} first_name The first name of the person
* @property {string} last_name The last name of the person
* @property {boolean} has_picture Whether the person has a picture on the firebase database
*/
/**
* Route for getting person information for the current user
* @name person/get
* @function
* @inner
* @param {*} req
* @param {*} res
* @returns {PersonGetPackage|string} On success, information about the person. Otherwise, an error message.
* @async
*/
router.get('/', auth.token, async (req, res) => {
const errorMessage = "Unable to get person";
if (res.locals.uid) {
let client = await mysql.getClient();
client.query("SELECT first_name, last_name, has_picture FROM tma.person WHERE person_id=?", [res.locals.uid], function(error, results, fields) {
if (error) {
res.status(503).send(errorMessage);
} else if (results.length === 0) {
res.status(208).send(errorMessage);
} else {
res.status(200).json(results[0]);
}
});
} else {
res.status(400).send(errorMessage);
}
});
/**
* Route for adding a person
* @name person/post
* @function
* @inner
* @param {*} req
* @param {string} req.body.firstName The first name of the Person to be created
* @param {string} req.body.lastName The last name of the Person to be created
* @param {boolean} req.body.hasPicture Whether the person uploaded a picture to Firebase during account creation
* @param {*} res
* @returns {string} An error message if an error occured
* @async
*/
router.post('/', auth.token, async (req,res) => {
let first = req.body.firstName;
let last = req.body.lastName;
let hasPicture = req.body.hasPicture;
const errorMessage = "Unable to create profile";
if (res.locals.uid && first && last && hasPicture !== undefined) {
let client = await mysql.getClient();
client.query("insert into tma.person (person_id, first_name, last_name, email, has_picture) values(?, ?, ?, ?, ?)",[res.locals.uid,first,last,res.locals.email,hasPicture], function (error, results, fields) {
if (error) {
res.status(503).send(errorMessage);
} else {
res.status(200).send();
}
});
} else {
res.status(400).send(errorMessage);
}
});
/**
* Route for updating a person's information
* @name person/put
* @function
* @inner
* @param {*} req
* @param {string} req.body.firstName Optional parameter. If provided, will set the first name of the Person to this value
* @param {string} req.body.lastName Optional parameter. If provided, will set the last name of the Person to this value
* @param {boolean} req.body.hasPicture Optional parameter. If provided, will set whether the Person has a picture to this value
* @param {*} res
* @returns {string} An error message if an error occured
* @async
*/
router.put('/', auth.token, async (req,res) => {
let setString = [];
let args = [];
let first = req.body.firstName;
if (first) {
setString.push("first_name=?");
args.push(first);
}
let last = req.body.lastName;
if (last) {
setString.push("last_name=?");
args.push(last);
}
let hasPicture = req.body.hasPicture;
if (hasPicture) {
setString.push("has_picture=?");
args.push(hasPicture);
}
const errorMessage = "Unable to update person";
args.push(res.locals.uid);
if (setString.length > 0) {
let client = await mysql.getClient();
client.query("UPDATE tma.person SET " + setString.join(",") + " WHERE person_id=?", args, function(error, results, fields) {
if (error) {
res.status(503).send(errorMessage);
} else {
res.status(200).send();
}
});
} else {
res.status(400).send(errorMessage);
}
});
module.exports = router;