<commit_before>const express = require('express')
const app = express()
const sendSMS = require('./lib/Twilio.js')
require('dotenv').config()
const bodyParser = require('body-parser')
const methodOverride = require('method-override')

const {
  createTimer,
  startTimer,
  stopTimer,
  resetTimer,
  decrementTimer
} = require('./timers.js')

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())

app.use(methodOverride());

let timersArray = []
let alertsArray = []

const checkAlerts = (timerId, time) => {
  alertsArray = alertsArray.map(alert => {
    if(alert.timerId === timerId && !alert.activated && alert.activationTime === time){
      sendSMS(alert.description)
      return Object.assign({}, alert, {activated: true})
    }else{
      return alert
    }
  })
}

const updateTimer = timerId => {
  const timer = timersArray.filter(timer => timer.id === timerId)[0]
  let updatedTimer = decrementTimer(timer)
  checkAlerts(updatedTimer.id, updatedTimer.currentTime)
  if(updatedTimer.currentTime === "0:00"){
    clearInterval(updatedTimer)
    updatedTimer = stopTimer(updatedTimer)
    updatedTimer.interval = null
  }
  console.log(updatedTimer.currentTime)
  timersArray = timersArray.map(t => {
    return (t.id === updatedTimer.id)
    ? updatedTimer
    : t
  })
}

// ## CORS middleware
// 
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
const allowCrossDomain = (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
      
    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
      res.sendStatus(200);
    }
    else {
      next();
    }
};
app.use(allowCrossDomain);

app.get('/alert/:id', (req, res) => {
  const alert = alertsArray.filter(alert => alert.id === req.params.id)[0]
  res.send(alert)
})

app.post('/alert/:id/edit', (req, res) => {
  if (req.params.id !== req.body.alert.id) sendStatus(500)
  const editedAlert = req.body.alert
  alertsArray = alertsArray.map(a => {
    return (a.id === editedAlert.id)
    ? editedAlert
    : a
  })
  console.log(editedAlert)
  res.send(editedAlert)
})

app.get('/alerts', (req, res) => {
  res.send(alertsArray)
})

app.post('/alerts/new', (req, res) => {
  const alert = req.body.alert
  alertsArray = [...alertsArray, alert]
  console.log(alert)
  res.send(alert)
})

app.get('/timer/:id', (req, res) => {
 const timer = timersArray.filter(timer => timer.id === req.params.id)[0] 
 res.send(timer)
})

app.post('/timer/:id/update', (req, res) => {
  const timer = timersArray.filter(timer => timer.id === req.params.id)[0]
  const updatedTimer = req.body.timer
  if (timer.id !== updatedTimer.id) sendStatus(500)
  timersArray = timersArray.map(t => {
    return (t.id === updatedTimer.id)<commit_msg>Use update instead of edit for alert endpoint<commit_after>const express = require('express')
const app = express()
const sendSMS = require('./lib/Twilio.js')
require('dotenv').config()
const bodyParser = require('body-parser')
const methodOverride = require('method-override')

const {
  createTimer,
  startTimer,
  stopTimer,
  resetTimer,
  decrementTimer
} = require('./timers.js')

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())

app.use(methodOverride());

let timersArray = []
let alertsArray = []

const checkAlerts = (timerId, time) => {
  alertsArray = alertsArray.map(alert => {
    if(alert.timerId === timerId && !alert.activated && alert.activationTime === time){
      sendSMS(alert.description)
      return Object.assign({}, alert, {activated: true})
    }else{
      return alert
    }
  })
}

const updateTimer = timerId => {
  const timer = timersArray.filter(timer => timer.id === timerId)[0]
  let updatedTimer = decrementTimer(timer)
  checkAlerts(updatedTimer.id, updatedTimer.currentTime)
  if(updatedTimer.currentTime === "0:00"){
    clearInterval(updatedTimer)
    updatedTimer = stopTimer(updatedTimer)
    updatedTimer.interval = null
  }
  console.log(updatedTimer.currentTime)
  timersArray = timersArray.map(t => {
    return (t.id === updatedTimer.id)
    ? updatedTimer
    : t
  })
}

// ## CORS middleware
// 
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
const allowCrossDomain = (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
      
    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
      res.sendStatus(200);
    }
    else {
      next();
    }
};
app.use(allowCrossDomain);

app.get('/alert/:id', (req, res) => {
  const alert = alertsArray.filter(alert => alert.id === req.params.id)[0]
  res.send(alert)
})

app.post('/alert/:id/update', (req, res) => {
  if (req.params.id !== req.body.alert.id) sendStatus(500)
  const updatedAlert = req.body.alert
  alertsArray = alertsArray.map(a => {
    return (a.id === updatedAlert.id)
    ? updatedAlert
    : a
  })
  console.log(updatedAlert)
  res.send(updatedAlert)
})

app.get('/alerts', (req, res) => {
  res.send(alertsArray)
})

app.post('/alerts/new', (req, res) => {
  const alert = req.body.alert
  alertsArray = [...alertsArray, alert]
  console.log(alert)
  res.send(alert)
})

app.get('/timer/:id', (req, res) => {
 const timer = timersArray.filter(timer => timer.id === req.params.id)[0] 
 res.send(timer)
})

app.post('/timer/:id/update', (req, res) => {
  const timer = timersArray.filter(timer => timer.id === req.params.id)[0]
  const updatedTimer = req.body.timer
  if (timer.id !== updatedTimer.id) sendStatus(500)
  timersArray = timersArray.map(t => {
    return (t.id === updatedTimer.id)
{'fraction_non_alphanumeric': 0.17472434266327397, 'fraction_numerical': 0.007803223070398643, 'mean_word_length': 3.809135399673736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1299748', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1988, 'n_words': 360}