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.
sending texts via twilio to taskers when a new shift is posted
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' } }
})
})
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment