Source: routers/performanceProgress.js

const express = require('express');
const auth = require('../middleware/authenticator.js');
const mysql = require("../db/mysql.js");
const router = express.Router();
const verify = require("../middleware/verifier.js");

/**
 * @typedef {object} PerformanceProgressGetPackage
 * @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
 */

/**
 * Route for getting the average of the stored graded performance information for the current user of the specified piece of sheet music
 * @name performance-progress/get
 * @function
 * @inner
 * @param {*} req
 * @param {string} req.body.sheetMusicId The sheet music ID to be accessed. NOTE: Only need to pass sheetMusicId in body or query not both
 * @param {string} req.query.sheetMusicId The sheet music ID to be accessed. NOTE: Only need to pass sheetMusicId in body or query not both
 * @param {*} res
 * @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.
 * @async
 */
router.get('/', auth.token, async (req, res) => {
    verify.memberOfSheetMusic(req, res).then((choirId) => {
        if (choirId === null) {
            res.status(403).send('Unauthorized');
        } else {
            const sheetMusicId = req.query.sheetMusicId;
            if (!sheetMusicId) {
                sheetMusicId = req.body.sheetMusicId;
            }
            const errorMessage = "Unable to get performance progress";
            if (sheetMusicId) {
                mysql.getClient().then((client) => {
                    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) {
                        if (error) {
                            res.status(503).send(errorMessage);
                        } else if (results.length === 0) {
                            res.status(400).send(errorMessage);
                        } else {
                            let memberId = results[0]["member_id"];

                            client.query("SELECT progress_data FROM tma.performance_progress WHERE member_id=unhex(?) AND sheet_music_id=unhex(?)", [memberId, sheetMusicId], function(error, results, fields) {
                                if (error) {
                                    res.status(503).send(errorMessage);
                                } else if (results.length === 0) {
                                    res.status(400).send(errorMessage);
                                } else {
                                    let data = JSON.parse(results[0]["progress_data"]).data;
                                    let average = [];
                                    for (let i = 0; i < data[0].length; i++) {
                                        average.push(data[0][i]);
                                        for (let j = 1; j < data.length; j++) {
                                            average[i] += data[j][i];
                                        }
                                        average[i] = average[i] / data.length;
                                    }
                                    res.status(200).json({ averagePerformance : average });
                                }
                            });
                        }
                    });
                });
            } else {
                res.status(400).send(errorMessage);
            }
        }
    });
});

module.exports = router;