Skip to content

Instantly share code, notes, and snippets.

@alphanumeric0101
Last active September 22, 2017 18:03
Show Gist options
  • Select an option

  • Save alphanumeric0101/e6d81be19eb201c80bb3f0b4de12f136 to your computer and use it in GitHub Desktop.

Select an option

Save alphanumeric0101/e6d81be19eb201c80bb3f0b4de12f136 to your computer and use it in GitHub Desktop.

Revisions

  1. Alex revised this gist Sep 22, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions alertTaskers.js
    Original file line number Diff line number Diff line change
    @@ -49,3 +49,18 @@ this.alertTaskers = function (req, event) {
    })
    }))
    }
    this.shiftStartReminder = function (job, database, done) {
    let event = job.attrs.data.event
    database.events.findOne({eventid: event.eventid})
    .then(e => {
    console.log(`reminding an event: ${e.eventName}`)
    return Promise.all(e.all_accepted_users.map(userid => { return database.users.findOne({userid: userid}) }))
    .then(usersArray => {
    return Promise.all(usersArray.map(user => {
    let msgType = e.all_confirmed_users.includes(user.userid) ? 70 : 33
    sendText(user.phone_number, msgType, user, event, event.keyword)
    sendEmail(14, user, null, event, event.keyword)
    })).then(res => done())
    })
    })
    }
  2. Alex revised this gist Sep 22, 2017. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion alertTaskers.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,19 @@
    this.alertTaskers = function (req, event) {
    var textClient = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN)
    var sendText = textClient.messages.create({
    to: phone,
    from: taskiPhone,
    body: messageBody
    }, function (err, message) {
    if (err) {
    debug('Twilio error!')
    console.log('texting error: ' + messageBody.length + ' chars')
    console.log(err)
    } else {
    console.log(`sent a text to: ${phone}: ${messageBody}`)
    }
    })

    this.alertTaskers = function (req, event) {
    let sentTexts = []
    Promise.all(event.shifts.map((s, i, a) => { // s is the new shift
    let shift = Object.assign(s, {
  3. Alex created this gist Sep 22, 2017.
    36 changes: 36 additions & 0 deletions alertTaskers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    this.alertTaskers = function (req, event) {
    let sentTexts = []
    Promise.all(event.shifts.map((s, i, a) => { // s is the new shift
    let shift = Object.assign(s, {
    venueName: event.venueName || null,
    time: moment(s.startDate).format('ha'),
    endTime: moment(s.endDate).format('ha'),
    date: moment(s.startDate).format('dddd MMMM Do'),
    company: event.company
    })
    return req.db.jobs.find({date: s.date}, {fields: {'jobid': 1, '_id': 0}})
    .then(sameDayShifts => {
    let conflictingShifts = sameDayShifts.map(cs => cs.jobid)
    return req.db.users.find({
    skills: s.position,
    status: 'Accepted',
    account_type: 'worker',
    'notificationSettings.phone_job_match': true,
    matched_jobs: { $nin: conflictingShifts }
    }, { fields: { 'distance': 1, 'email': 1, 'location': 1, 'phone_number': 1, 'name': 1, 'firstName': 1, 'userid': 1 } })
    .then(users => {
    if (users.length) {
    return Promise.all(users.map(u => {
    if (u.distance && u.location && event.location) {
    if (calculateDistance(event, u) <= u.distance && u.phone_number.length > 3 && !sentTexts.includes(u.phone_number)) {
    sentTexts.push(u.phone_number)
    sendText(u.phone_number, 30, u, shift, null)
    return { email: u.email, alert: 'text sent' }
    } else { return { email: u.email, alert: 'distance was wrong or duplicate' } }
    } else { return { email: u.email, alert: 'missing fields in userdoc' } }
    }))
    } else { return { email: null, alert: 'no available users found' } }
    })
    })
    }))
    }