Skip to content

Instantly share code, notes, and snippets.

@horsey
Created December 11, 2015 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save horsey/96ce0a9db9e66090288d to your computer and use it in GitHub Desktop.
Save horsey/96ce0a9db9e66090288d to your computer and use it in GitHub Desktop.
Code snippet to demonstrate Promises.
'use strict';
var _ = require('lodash'),
async = require('async'),
moment = require('moment'),
messaging = require('../../messaging'),
patients = require('../patients'),
practitioners = require('../practitioners'),
dataProvider = require('../../database');
module.exports = function(Events) {
Events.cancel = function (data, options, cb) {
var eventIds,
partnerId,
practitionerId,
userId,
userType;
eventIds = data.eventIds;
partnerId = options.context.partnerId;
userId = options.context.userId;
userType = options.context.userType;
if(userType == "practitioner") {
practitionerId = userId;
}
else {
practitionerId = data.practitionerId;
}
async.each(eventIds, function(eventId, cb) {
var eventInfo;
async.waterfall([
function(next) {
dataProvider.events.exists(eventId, options, next);
},
function(event, next) {
eventInfo = event;
var filter = { 'userId': practitionerId,
'partnerId': partnerId
};
practitioners.exists(filter, options, next);
},
function(practitioner, next) {
eventInfo.practInfo = practitioner;
var status = {"status": "cancelled"};
dataProvider.events.edit(eventId, status, options, next);
},
function(status, next) {
patients.exists(eventInfo.patientId, options, next);
},
function(patientInfo, next) {
eventInfo.patientInfo = patientInfo;
next();
},
function(next) {
var message = "Dear " + eventInfo.patientInfo.firstName + "," + " " +
"your appointment with " +
"Dr. " + eventInfo.practInfo.practGivenName + " " +
"at " + eventInfo.location + " " +
"has been CANCELLED for " +
moment(eventInfo.start).utcOffset(+330).format('MMMM DD YYYY h:mm a') +
".";
var options = {
"message": message,
"number": "+" + eventInfo.patientInfo.phoneNumber
};
messaging.sendMessage(options, function(err, response) {
if (err) {
return next(err, null);
}
if (response.statusCode !== 200) {
return next(err, null);
}
next(null, eventInfo);
});
}
], cb);
}, function(err, result) {
if (err)
return cb(err);
return cb(null, {"message": "Marked event as cancelled"});
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment