Source: routers/person.js

  1. const express = require('express');
  2. const auth = require('../middleware/authenticator.js');
  3. const mysql = require("../db/mysql.js");
  4. const router = express.Router();
  5. /**
  6. * @typedef {object} PersonGetPackage
  7. * @property {string} first_name The first name of the person
  8. * @property {string} last_name The last name of the person
  9. * @property {boolean} has_picture Whether the person has a picture on the firebase database
  10. */
  11. /**
  12. * Route for getting person information for the current user
  13. * @name person/get
  14. * @function
  15. * @inner
  16. * @param {*} req
  17. * @param {*} res
  18. * @returns {PersonGetPackage|string} On success, information about the person. Otherwise, an error message.
  19. * @async
  20. */
  21. router.get('/', auth.token, async (req, res) => {
  22. const errorMessage = "Unable to get person";
  23. if (res.locals.uid) {
  24. let client = await mysql.getClient();
  25. client.query("SELECT first_name, last_name, has_picture FROM tma.person WHERE person_id=?", [res.locals.uid], function(error, results, fields) {
  26. if (error) {
  27. res.status(503).send(errorMessage);
  28. } else if (results.length === 0) {
  29. res.status(208).send(errorMessage);
  30. } else {
  31. res.status(200).json(results[0]);
  32. }
  33. });
  34. } else {
  35. res.status(400).send(errorMessage);
  36. }
  37. });
  38. /**
  39. * Route for adding a person
  40. * @name person/post
  41. * @function
  42. * @inner
  43. * @param {*} req
  44. * @param {string} req.body.firstName The first name of the Person to be created
  45. * @param {string} req.body.lastName The last name of the Person to be created
  46. * @param {boolean} req.body.hasPicture Whether the person uploaded a picture to Firebase during account creation
  47. * @param {*} res
  48. * @returns {string} An error message if an error occured
  49. * @async
  50. */
  51. router.post('/', auth.token, async (req,res) => {
  52. let first = req.body.firstName;
  53. let last = req.body.lastName;
  54. let hasPicture = req.body.hasPicture;
  55. const errorMessage = "Unable to create profile";
  56. if (res.locals.uid && first && last && hasPicture !== undefined) {
  57. let client = await mysql.getClient();
  58. 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) {
  59. if (error) {
  60. res.status(503).send(errorMessage);
  61. } else {
  62. res.status(200).send();
  63. }
  64. });
  65. } else {
  66. res.status(400).send(errorMessage);
  67. }
  68. });
  69. /**
  70. * Route for updating a person's information
  71. * @name person/put
  72. * @function
  73. * @inner
  74. * @param {*} req
  75. * @param {string} req.body.firstName Optional parameter. If provided, will set the first name of the Person to this value
  76. * @param {string} req.body.lastName Optional parameter. If provided, will set the last name of the Person to this value
  77. * @param {boolean} req.body.hasPicture Optional parameter. If provided, will set whether the Person has a picture to this value
  78. * @param {*} res
  79. * @returns {string} An error message if an error occured
  80. * @async
  81. */
  82. router.put('/', auth.token, async (req,res) => {
  83. let setString = [];
  84. let args = [];
  85. let first = req.body.firstName;
  86. if (first) {
  87. setString.push("first_name=?");
  88. args.push(first);
  89. }
  90. let last = req.body.lastName;
  91. if (last) {
  92. setString.push("last_name=?");
  93. args.push(last);
  94. }
  95. let hasPicture = req.body.hasPicture;
  96. if (hasPicture) {
  97. setString.push("has_picture=?");
  98. args.push(hasPicture);
  99. }
  100. const errorMessage = "Unable to update person";
  101. args.push(res.locals.uid);
  102. if (setString.length > 0) {
  103. let client = await mysql.getClient();
  104. client.query("UPDATE tma.person SET " + setString.join(",") + " WHERE person_id=?", args, function(error, results, fields) {
  105. if (error) {
  106. res.status(503).send(errorMessage);
  107. } else {
  108. res.status(200).send();
  109. }
  110. });
  111. } else {
  112. res.status(400).send(errorMessage);
  113. }
  114. });
  115. module.exports = router;