javascriptparse-platformparse-cloud-codecron-taskback4app

Parse Cloud Code Job Function Optimization


I have a job on cloud code that I am running. It seems to work when I manually run the job. I think scheduling is posing an issue with its setup an frequency of running so I think it's unrelated to the actual code. Perhaps I'm wrong, but curious if there's a more efficient way to poll my parse class. I have an app where I am trying to find bookings that are coming up within the next hour from now(), and if there are send a push notification to the users within that class. Again, this runs but I think I may be able to optimize my query to only get items within that time frame vs more items that have a certain status.

Parse.Cloud.job("updateReviews", async (request) => {

var resultCount = 0;

// Query all bookings with a status of confirmed
var query = new Parse.Query("bookings");
query.equalTo("status", "confirmed");
const results = await query.find({useMasterKey:true});

results.forEach(object => {

    var users = [];
    users.push(object.get("buyerId"));
    users.push(object.get("placeOwner"));

    var today = new moment();
    var hourFrom = moment().add(1, 'hours');
    var startTime = moment(object.get("startTime"));

    if (startTime.isBetween(today, hourFrom)) {
    
        console.log("BETWEEN THE TIMEFRAME");
        resultCount += 1;

        users.forEach(sendPush);

    } else {
        
        console.log("NOT BETWEEN THE TIME FRAME, PASS OVER");
    }
});


return ("Successfully sent " + resultCount + " new notifications!");

});

function sendPush(value, index, array) {

var pushType = "bookingConfirmed";

let query = new Parse.Query(Parse.Installation);
query.equalTo("userId", value);
return Parse.Push.send({
    where: query,
    data: {
        title: "New Booking Coming Up",
        alert: "You have a booking coming up soon!",
        pushType
    }
},{useMasterKey: true});

}


Solution

  • Yes. It could be much better. I'd try something like this:

    Parse.Cloud.job('updateReviews', async () => {
      // Query all bookings with a status of confirmed
      const query = new Parse.Query('bookings');
      query.equalTo('status', 'confirmed');
      const now = new Date();
      query.greaterThanOrEqualTo('startTime', now);
      query.lessThanOrEqualTo('startTime', new Date(now.getTime() + 60 * 60 * 1000));
      const results = await query.find({ useMasterKey: true });
    
      const pushType = "bookingConfirmed";
    
      const pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.containedIn("userId", results.map(result => result.get('buyerId')).concat(results.map(result => result.get('placeOwner'))));
      await Parse.Push.send({
        where: pushQuery,
        data: {
          title: 'New Booking Coming Up',
          alert: 'You have a booking coming up soon!',
          pushType
        }
      }, { useMasterKey: true });
    
      return (`Successfully sent ${results.length} new notifications!`);
    });