<commit_before>'use strict';

module.exports = function (Event) {


  Event.disableRemoteMethodByName('upsert');
  Event.disableRemoteMethodByName('upsertWithWhere');
  Event.disableRemoteMethodByName('exists');
  Event.disableRemoteMethodByName('updateAll');
  Event.disableRemoteMethodByName('count');
  Event.disableRemoteMethodByName('createChangeStream');
  Event.disableRemoteMethodByName('replaceById');
  Event.disableRemoteMethodByName('replaceOrCreate');



  Event.listEvents = function (ctx, cb) {
    let configs = ctx.req.configs;
    Event.find({
      where: {
        startDate: { lt: Date.now() },
        status: 'approved'
      },
      order: 'startDate DESC',
      limit: configs[0].value
    }, function (err, pastInstances) {

      Event.find({
        where: {
          startDate: { gte: Date.now() },
          status: 'approved'
        },
        order: 'startDate DESC',
        limit: configs[1].value
      }, function (err, futureInstances) {

        let instances = [];
        instances = pastInstances.concat(futureInstances);

        cb(null, instances);
      });

    });
  };

  Event.beforeRemote('listEvents', function (context, unused, next) {

    Event.app.models.config.find({
      where: {
        or:
        [
          { name: 'no_of_past_events' },
          { name: 'no_of_future_events' }
        ]
      }
    },
      function (err, configs) {
        context.req.configs = configs;
        next();
      });
  });

  Event.remoteMethod('listEvents', {
    accepts: [
      { arg: 'ctx', type: 'object', http: { source: 'context' } }
    ],
    http: {
      path: '/list-events',
      verb: 'get'
    },
    returns: {
      arg: 'events',
      type: 'array'
    }
  }
  );

  Event.observe('before save', function (ctx, next) {
    if (ctx.instance) {
      console.log('updating Event status');
      ctx.instance.status = 'not approved';
    }
    next();
  });
  Event.observe('after save', function (ctx, next) {
    if (ctx.instance) {
      console.log('Saved %s#%s', ctx.Model.modelName, ctx.instance.id);
      Event.app.models.Task.create({ type: 'event', approvableId: ctx.instance.id, status: 'Pending' });
    } else {
      console.log('Updated Event %s matching %j',
        ctx.Model.pluralModelName,
        ctx.where);
    }
    next();
  });

}<commit_msg>modified to add a get event remote method<commit_after>'use strict';

module.exports = function (Event) {


  Event.disableRemoteMethodByName('upsert');
  Event.disableRemoteMethodByName('upsertWithWhere');
  Event.disableRemoteMethodByName('exists');
  Event.disableRemoteMethodByName('updateAll');
  Event.disableRemoteMethodByName('count');
  Event.disableRemoteMethodByName('createChangeStream');
  Event.disableRemoteMethodByName('replaceById');
  Event.disableRemoteMethodByName('replaceOrCreate');



  Event.listEvents = function (ctx, cb) {
    let configs = ctx.req.configs;
    Event.find({
      where: {
        startDate: { lt: Date.now() },
        status: 'approved'
      },
      order: 'startDate DESC',
      limit: configs[0].value
    }, function (err, pastInstances) {

      Event.find({
        where: {
          startDate: { gte: Date.now() },
          status: 'approved'
        },
        order: 'startDate DESC',
        limit: configs[1].value
      }, function (err, futureInstances) {

        let instances = [];
        instances = pastInstances.concat(futureInstances);

        cb(null, instances);
      });

    });
  };

  Event.beforeRemote('listEvents', function (context, unused, next) {

    Event.app.models.config.find({
      where: {
        or:
        [
          { name: 'no_of_past_events' },
          { name: 'no_of_future_events' }
        ]
      }
    },
      function (err, configs) {
        context.req.configs = configs;
        next();
      });
  });

  Event.remoteMethod('listEvents', {
    accepts: [
      { arg: 'ctx', type: 'object', http: { source: 'context' } }
    ],
    http: {
      path: '/list-events',
      verb: 'get'
    },
    returns: {
      arg: 'events',
      type: 'array'
    }
  }
  );

   Event.listEventsForUser = function (ctx, userId, cb) {
    console.log("userid from request" + userId);
    Event.find({
      where: {
        endDate: { gte: Date.now() },
        status: 'approved'
      },
      order: 'startDate DESC'
    }, function (err, pastInstances) {
      // this logic is to determin the list of non-approved events that are returned 
      // by this query. userId = 0 for an admin user and all non-approved events are returned
      if(userId == 0){
        Event.find({
        where: {
          endDate: { gte: Date.now() },
          status: 'not approved'          
        },
        order: 'startDate DESC'
      }, function (err, futureInstances) {

        let instances = [];
        instances = pastInstances.concat(futureInstances);

        cb(null, instances);
      });
      }else{
        Event.find({
        where: {
          endDate: { gte: Date.now() },
          status: 'not approved',
          createdBy: userId
        },
        order: 'startDate DESC'
      }, function (err, futureInstances) {

        let instances = [];
        instances = pastInstances.concat(futureInstances);

        cb(null, instances);
      });
      }

      

    });
  };
  
  Event.remoteMethod('listEventsForUser',{
    accepts: [
      { arg: 'ctx', type: 'object', http: { source: 'context' } },
      { arg: 'userId', type: 'number' }
    ],
    http: {
      path: '/list-events-for-user',
      verb: 'get'
    },
    returns: {
      arg: 'events',
      type: 'array'
    }
  });




  Event.observe('before save', function (ctx, next) {
    if (ctx.instance) {
      console.log('updating Event status');
      ctx.instance.status = 'not approved';
    }
    next();
  });
  Event.observe('after save', function (ctx, next) {
    if (ctx.instance) {
      console.log('Saved %s#%s', ctx.Model.modelName, ctx.instance.id);
      Event.app.models.Task.create({ type: 'event', approvableId: ctx.instance.id, status: 'Pending' });
    } else {
      console.log('Updated Event %s matching %j',
        ctx.Model.pluralModelName,
        ctx.where);
    }
    next();
  });

}
{'fraction_non_alphanumeric': 0.14990390775144138, 'fraction_numerical': 0.0009609224855861628, 'mean_word_length': 2.281660535995796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5431910', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1926, 'n_words': 393}