Source: routers/performanceProgress.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. const verify = require("../middleware/verifier.js");
  6. /**
  7. * @typedef {object} PerformanceProgressGetPackage
  8. * @property {number[]} averagePerformance The average score of each measure of the provided sheet music of the user's part. The ith index is the score for the i+1 measure number
  9. */
  10. /**
  11. * Route for getting the average of the stored graded performance information for the current user of the specified piece of sheet music
  12. * @name performance-progress/get
  13. * @function
  14. * @inner
  15. * @param {*} req
  16. * @param {string} req.body.sheetMusicId The sheet music ID to be accessed. NOTE: Only need to pass sheetMusicId in body or query not both
  17. * @param {string} req.query.sheetMusicId The sheet music ID to be accessed. NOTE: Only need to pass sheetMusicId in body or query not both
  18. * @param {*} res
  19. * @returns {PerformanceProgressGetPackage|string} On success, the average score on each measure for this user's part in the given piece of sheet music. Otherwise, an error message.
  20. * @async
  21. */
  22. router.get('/', auth.token, async (req, res) => {
  23. verify.memberOfSheetMusic(req, res).then((choirId) => {
  24. if (choirId === null) {
  25. res.status(403).send('Unauthorized');
  26. } else {
  27. const sheetMusicId = req.query.sheetMusicId;
  28. if (!sheetMusicId) {
  29. sheetMusicId = req.body.sheetMusicId;
  30. }
  31. const errorMessage = "Unable to get performance progress";
  32. if (sheetMusicId) {
  33. mysql.getClient().then((client) => {
  34. client.query("SELECT hex(member_id) AS member_id FROM tma.member WHERE person_id=? AND choir_id=unhex(?)", [res.locals.uid, choirId], function(error, results, fields) {
  35. if (error) {
  36. res.status(503).send(errorMessage);
  37. } else if (results.length === 0) {
  38. res.status(400).send(errorMessage);
  39. } else {
  40. let memberId = results[0]["member_id"];
  41. client.query("SELECT progress_data FROM tma.performance_progress WHERE member_id=unhex(?) AND sheet_music_id=unhex(?)", [memberId, sheetMusicId], function(error, results, fields) {
  42. if (error) {
  43. res.status(503).send(errorMessage);
  44. } else if (results.length === 0) {
  45. res.status(400).send(errorMessage);
  46. } else {
  47. let data = JSON.parse(results[0]["progress_data"]).data;
  48. let average = [];
  49. for (let i = 0; i < data[0].length; i++) {
  50. average.push(data[0][i]);
  51. for (let j = 1; j < data.length; j++) {
  52. average[i] += data[j][i];
  53. }
  54. average[i] = average[i] / data.length;
  55. }
  56. res.status(200).json({ averagePerformance : average });
  57. }
  58. });
  59. }
  60. });
  61. });
  62. } else {
  63. res.status(400).send(errorMessage);
  64. }
  65. }
  66. });
  67. });
  68. module.exports = router;