Skip to content

Instantly share code, notes, and snippets.

@AlexRuptsov
Last active July 23, 2018 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexRuptsov/641f390d1b7da19519ac2d3d837222f5 to your computer and use it in GitHub Desktop.
Save AlexRuptsov/641f390d1b7da19519ac2d3d837222f5 to your computer and use it in GitHub Desktop.
You can just copy-paste the whole code into your own app.js file.
/*
* Advanced Cloud Code Example
*/
var express = require('express');
var app = express();
// This will be our route handler for the /unbilled Slash Command
app.post('/getUnbilledUsers', async function(req, res) {
const query = new Parse.Query(Parse.User);
query.equalTo('billed', false); // find all unbilled users
let unbilled;
try {
//.find() returns a Promise so we can await it
unbilled = await query.find();
} catch (e) {
res.status(403);
res.send({
text: 'An Error occured'
});
console.error('Error' + e.message);
return;
}
if (unbilled.length === 0) {
// If there are no unbilled users, send a different response
res.send({
text: 'No unbilled users!'
});
return;
}
// If there are unbilled users, format them and send them as a response
const usernames = unbilled
.map(user => {
return `Username: ${user.get('username')} | User ID: ${user.id}`;
})
.join('\n');
res.send({
text: 'Unbilled Users:',
attachments: [
{
text: usernames
}
]
});
console.error('Error' + e.message);
});
//This will be our route handler for the /bill Slash Command
app.post('/billUser', async (req, res) => {
const unbilled = await new Parse.Query(Parse.User)
.equalTo('billed', false)
.find();
for (let user of unbilled) {
try {
await user.save({ billed: true }, { useMasterKey: true });
} catch (e) {
res.status(403);
res.send({ error: `failed to bill user ${user.id}: ${e.message}` });
return;
}
}
res.send({ text: `${unbilled.length} users successfully billed!` });
});
/*
* Exporting of module.exports.app is required.
* we mount it automaticaly to the Parse Server Deployment.
*/
module.exports = app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment