'use strict';

// ONLY SEND USER IDs TO THESE FUNCTIONS!

async function getPointsAmount(db, channel, userID) {

  let amount = await db.users.find({ channel: channel, twitch_id: userID});
  return amount[0].points;

}

async function getPointsName(db, channel) {

  let name = await db.channel_settings.find({ channel: channel });
  return name[0].points_name;

}

async function modifyPoints(db, channel, userID, amount) {

  let currentAmount = await getPointsAmount(db, channel, userID);
  let newAmount = parseInt(currentAmount) + parseInt(amount);
  if (newAmount > 0) {
    await db.users.update({
      twitch_id: userID,
      channel: channel
    }, {
      points: newAmount
    })
  }
}

module.exports = {
  getPointsAmount: getPointsAmount,
  getPointsName: getPointsName,
  modifyPoints: modifyPoints
}

@@ -546,16 +546,57 @@
 amount);
+%0A%0A  // Do not allow points to go negative
 %0A  if (n
@@ -608,17 +608,17 @@
 unt 
-%3E
+%3C
  0) %7B%0A  
   aw
@@ -613,16 +613,81 @@
  0) %7B%0A  
+  newAmount = 0;%0A  %7D%0A%0A  // Update database with new point amount%0A
   await 
@@ -708,18 +708,16 @@
 e(%7B%0A    
-  
 twitch_i
@@ -727,18 +727,16 @@
 userID,%0A
-  
     chan
@@ -752,19 +752,15 @@
 nel%0A
-  
   %7D, %7B%0A
-  
     
@@ -781,18 +781,15 @@
 unt%0A
-  
   %7D)%0A  
-%7D
 %0A%7D%0A%0A

do not allow points to become negative