Created
January 7, 2016 22:24
-
-
Save sjehutch/c9d3cc28042166c9056e to your computer and use it in GitHub Desktop.
parse cloud code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Update location after appointment is saved | |
Parse.Cloud.afterSave("appointments", function(request, response) { | |
var locationClass = Parse.Object.extend("location"); | |
var location = new locationClass; | |
location.set("location", request.object.get("location")); | |
location.set("client", request.object.get("client")); | |
location.set("trainer", request.object.get("trainer")); | |
location.save(null, { | |
success: function(location) { | |
response.success(location); | |
}, | |
error: function(error) { | |
response.error(error); | |
} | |
}); | |
}); | |
var user = Parse.Object.extend("User"); | |
Parse.Cloud.define("genreflinktrainer", function(request, response) { | |
Parse.Cloud.useMasterKey(); | |
var number = Math.floor((Math.random() * 50000) + 1000000); | |
var query = new Parse.Query(user); | |
query.equalTo("trainer_referral_key", number); | |
query.first({ | |
error: function(error) { | |
response.error("Nope"); | |
}, | |
success: function(object) { | |
response.success(number); | |
} | |
}); | |
}); | |
// Include the Twilio Cloud Module and initialize it | |
var twilio = require("twilio"); | |
twilio.initialize("ACd38ae2d31f51d26d647c199a71b9a8a5", "03b94b6514133bef03c3d7a877cd55eb"); | |
// Create the Cloud Function | |
Parse.Cloud.define("twilliofitz", function(request, response) { | |
// Use the Twilio Cloud Module to send an SMS | |
twilio.sendSMS({ | |
From: "19729921958", | |
To: request.params.number, | |
Body: request.params.message | |
}, { | |
success: function(httpResponse) { | |
response.success("SMS sent!"); | |
}, | |
error: function(httpResponse) { | |
response.error("Uh oh, something went wrong"); | |
} | |
}); | |
}); | |
//This will add a trainer key to the user table for type of trainer ... | |
Parse.Cloud.define("updatetrainerkey", function(request, response) { | |
Parse.Cloud.useMasterKey(); | |
var query = new Parse.Query(Parse.User); | |
query.equalTo("username", request.params.username); | |
query.first({ | |
success: function(object) { | |
object.set("trainer_referral_key", request.params.trainer_key); | |
object.save(); | |
response.success("Yep"); | |
}, | |
error: function(error) { | |
response.error("Nope"); | |
} | |
}); | |
}); | |
//Check for existing username in data table if exists stop id dead ... | |
var user = Parse.Object.extend("User"); | |
// Check if username is set, and enforce uniqueness based on the username column. | |
Parse.Cloud.beforeSave("User", function(request, response) { | |
if (!request.object.get("username")) { | |
response.error('A Use must have a Username.'); | |
} else { | |
var query = new Parse.Query(User); | |
query.equalTo("username", request.object.get("username")); | |
query.first({ | |
success: function(object) { | |
if (object) { | |
response.error("A username with this username already exists."); | |
} else { | |
response.success(); | |
} | |
}, | |
error: function(error) { | |
response.error("Could not validate uniqueness for this usern object."); | |
} | |
}); | |
} | |
}); | |
// Send notification after registration | |
Parse.Cloud.afterSave(Parse.User, function(request) { | |
Parse.Cloud.useMasterKey(); | |
//Get value from User Object | |
var installationId = request.object.get("installationId"); | |
//Set push query | |
var pushQuery = new Parse.Query(Parse.Installation); | |
pushQuery.equalTo("installationId", installationId); | |
Parse.Push.send({ | |
where: pushQuery, | |
data: { | |
alert: "You have successfully registered a member", | |
sound: "default" | |
} | |
}, { | |
success: function() {}, | |
error: function(error) {} | |
}); | |
if (request.object.get("email")) { | |
Parse.Push.send({ | |
where: pushQuery, | |
data: { | |
alert: "Please verify your e-mail for fitfitz", | |
sound: "default" | |
} | |
}, { | |
success: function() {}, | |
error: function(error) {} | |
}); | |
} | |
}); | |
// send notification request a training session | |
Parse.Cloud.define("requesttraining", function(request, response) { | |
Parse.Cloud.useMasterKey(); | |
var trainerId = request.params.trainerId; | |
var clientName = request.params.username; | |
var clientInstallationId = request.params.installationId; | |
var trainerQuery = new Parse.Query("_User"); | |
trainerQuery.equalTo("objectId", trainerId); | |
trainerQuery.find({ | |
success: function(results) { | |
var installationId = results[0].get("installationId"); | |
var pushQuery = new Parse.Query("_Installation"); | |
pushQuery.equalTo("installationId", installationId); | |
Parse.Push.send({ | |
where: pushQuery, | |
data: { | |
alert: "New training request from client.", | |
sound: "default" | |
} | |
}, { | |
success: function(result) { | |
if (result) { | |
var pushQuery = new Parse.Query("_Installation"); | |
pushQuery.equalTo("installationId", clientInstallationId); | |
Parse.Push.send({ | |
where: pushQuery, | |
data: { | |
alert: "Training request has been sent successfully.", | |
sound: "default" | |
} | |
}, { | |
success: function(result) { | |
response.success(result); | |
}, | |
error: function() { | |
response.error("Training request not send"); | |
} | |
}); | |
} | |
}, | |
error: function() { | |
response.error("Training request not send"); | |
} | |
}); | |
}, | |
error: function() { | |
response.error("Uh oh, something went wrong"); | |
} | |
}); | |
}); | |
// accepts or declines invitation | |
Parse.Cloud.define("invitationstatus", function(request, response) { | |
Parse.Cloud.useMasterKey(); | |
var invitationStatus = request.params.invitationStatus; | |
var clientID = request.params.clientID; | |
var cliendQuery = new Parse.Query("_User"); | |
cliendQuery.equalTo("objectId", clientID); | |
cliendQuery.find({ | |
success: function(results) { | |
var installationId = results[0].get("installationId"); | |
var pushQuery = new Parse.Query("_Installation"); | |
pushQuery.equalTo("installationId", installationId); | |
var pushMessage = "Invitation declined by trainer."; | |
if (invitationStatus) { | |
pushMessage = "Invitation accepted by trainer."; | |
} | |
var pushQuery = new Parse.Query("_Installation"); | |
pushQuery.equalTo("installationId", installationId); | |
Parse.Push.send({ | |
where: pushQuery, | |
data: { | |
alert: pushMessage, | |
sound: "default" | |
} | |
}, { | |
success: function(result) { | |
response.success(result); | |
}, | |
error: function() { | |
response.error("Uh oh, something went wrong"); | |
} | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment