Skip to content

Instantly share code, notes, and snippets.

@clesauln
Created January 17, 2016 14:44
Show Gist options
  • Save clesauln/e479a21897ce38589018 to your computer and use it in GitHub Desktop.
Save clesauln/e479a21897ce38589018 to your computer and use it in GitHub Desktop.
parse jobs ex
Parse.Cloud.job("Truncate_Multiple_Ad", function(request, response) {
Parse.Cloud.useMasterKey()
var adQuery = new Parse.Query(AdObj)
var tmpAd= [];
var toRemove = []
return adQuery.find().then(function(ads){
var byFbid = _.groupBy(ads, function(a){return a.get('datePublish')})
console.log(_.keys(byFbid).length + ' by date publish')
// filter where there is more than one profile with the same uid
var dupes = _.filter(byFbid, function(val) { return val.length > 1 })
console.log(dupes.length + ' duplicates');
var promises = []
var i, dupe
var fixDupes = function(orig, dupes) {
console.log('updating ad id to ' + orig.id)
var ad
var adQuery = new Parse.Query(AdObj)
return adQuery.get(orig.id).then(function(result) {
ad = result
console.log('updating ad ' + ad.id)
return ad.save()
}).then(function(matches) {
console.log('deleting duplicate profiles')
return Parse.Object.destroyAll(dupes)
})
}
for(i=0; i<dupes.length; i++) {
dupe = dupes[i]
dupe = _.sortBy(dupe, 'createdAt')
promises.push(fixDupes(dupe[0], dupe.slice(1, dupe.length)))
}
return Parse.Promise.when(promises)
}).then(function(result) {
response.success('job complete')
}, function(error) {
response.error('job error ' + JSON.stringify(error))
})
})
/**
* function to set public ACL to an object
**/
Parse.Cloud.job("PublicAccess_Ad", function(request,response){
Parse.Cloud.useMasterKey()
var ads_public = new Array();
var Ad_Query = new Parse.Query(AdObj)
return Ad_Query.find().then(function(ads){
_.each(ads, function(ad){
var acl = new Parse.ACL()
acl.setPublicReadAccess( true );
acl.setPublicWriteAccess( false );
ad.setACL(acl)
ads_public.push(ad);
});
}).then(function(){
Parse.Object.saveAll(ads_public,{
success: function(list) {
// All the objects were saved.
response.success("ok " ); //saveAll is now finished and we can properly exit with confidence :-)
},
error: function(error) {
// An error occurred while saving one of the objects.
response.error("failure on saving ads "+JSON.stringify(error));
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment