const uuid          = require('uuid');
const dateFormat    = require('dateformat');


// Retour arrière sur les enregistrements en BDD
function rollback(session, user) {
  db.query('DELETE from participation WHERE user=? AND session=?', [user, session], (error, result) => {
      if(error){ console.log(error) }
      if(result && result.affectedRows > 0) {
          db.query('DELETE from user WHERE id=?', [user], (error, result) => {
              if(error){ console.log(error) }
              if(result && result.affectedRows > 0) {
                  db.query('UPDATE session SET nb_of_participants = nb_of_participants - 1 WHERE ID=?', [session], (error, result) => {
                      if(error){ console.log(error) }
                      if(result && result.affectedRows > 0) {
                          console.log('Rollback effectué');
                      }
                  })
              }
          }) 
      }
  })
}

// Génère un UUID
function getUuid() {
    return uuid.v4();
}

// Récupère la date du jour (pour test des sessions à venir)
function getNowDate(format) {
    if (!format || format == '') { console.log('FormatInvalide'); exit; }
    return dateFormat(new Date(), format);
}

module.exports = { rollback, getUuid, getNowDate }