const shortid = require('shortid');
const query = require('./query.js');
/**
* Generates and returns a unique access code for a Chior
* @returns {string} A 6 long unique access code
* @async
*/
const getAccessCode = async () => {
let code;
do {
code = shortid.generate().substring(0,6);
try {
const obj = await query("SELECT COUNT(*) FROM tma.choir WHERE access_code=?", [code]);
if (obj.results[0]["COUNT(*)"] === 0) {
break;
}
} catch(error) {
throw new Error(error);
}
} while (true);
return code;
}
module.exports = {getAccessCode};