<commit_before>import { Observable, ReplaySubject, Subject } from 'rxjs';
import { Event } from 'services/event';
import { Attendee } from './attendee';

import { API_BASE, API_EVENTS, API_ATTEND, API_ATTENDEES, API_USERS } from 'common/constants';
import { isRfid } from 'common/utils';
import { http } from 'services/net';

/**
 * Status codes:
 * 50:
 *   des: no user with given username
 *   context: try to register rfid for a user
 *   http: status: 400 BAD_REQUEST
 * 40:
 *   des: no user with given rfid
 *   context: try to register with an rfid that is not bound to a user'
 *   http: status: 400 BAD_REQUEST
 * 30:
 *   des: Attendee is on waitinglist
 *   context: try to register an attendee when attendee is on waitinglist, force option = false'
 *   http: status: 100 CONTINUE
 * 20:
 *   des: Attendee has already registered
 *   context: try to register attendee when attendee is already registered
 *   http: status: 400 BAD_REQUEST
 * 10:
 *   des: Attendee successfuly registered
 *   context: try to register an attendee
 *   http: status: 200 OK
 */


class AttendeeServiceProvider {
  
  /**
   * @class AttendeeServiceProvider
   */
  constructor() {
    /** @private */
    this.cache = {};
  }
  
  /**
   * @method registerAttendee - Registers an attendee
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {Event} event - the event an attendee is being registerd for 
   * @param {String} rfid - the attendees rfid
   * @param {Boolean} approved - wether or not the attendee has been approved when on the waitinglist
   * @returns {Observable<{}>}
   */
  registerAttendee(event, rfid, approved = false) {
    return this.handleResponse(http.post(`${API_BASE}${API_ATTEND}`, {
      rfid: isRfid(rfid) ? rfid : null,
      username: isRfid(rfid) ? null : rfid,
      event: event.id,
      approved,
    }))
      .map((res) => {
        const ret = Object.assign({}, res, {
          event,
        });
        return ret;
      }); 
  }
  
  /**
   * @method getCached
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {Number} attendee_id - the attendee's id
   * @returns {Attendee}
   */
  getCached(attendee_id) {
    return this.cache[attendee_id];
  }

  /**
   * @method registerRfid - binds an rfid to a user
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {String} username 
   * @param {String} rfid 
   * @param {Event} event 
   */
  registerRfid(username, rfid, event) {
    if (username != null && rfid != null && rfid.length > 0) {
      return this.handleResponse(http.post(`${API_BASE}${API_ATTEND}`, {
        rfid,
        username,
        event: event.id,
      }));
    }
    return Observable.throw({error: "Rfid or username is null"});
  }

  /**
   * @method handleResponse - catches errors from an http response
   * @memberof AttendeeServiceProvider
   * @inner
   * @private
   * @param {Observable<{}>} r - Observable that resolves into an http/json response
   * @returns {Observable<{}>}
   */
  handleResponse(r) {
    return r.catch(error =>
      // if(error.status == 400 || error.status == 100)
       Observable.fromPromise(error.json ? error.json() : {message: "No json response in error"}).flatMap(r => Observable.throw(r))
        // return Observable.throw(Observable.fromPromise(error.json()))

    );
  }
  
  /**
   * @method getAttendees
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {Event} event - the event to fetch attendees from
   * @param {Number} page - what page to start fetching from 
   * @returns {Observable<Array<Attendee>>} - an observable that resolves into a list of Attendees
   */ 
  getAttendees(event, page = 1) {
    const count = 0;
    
    return http.get(`${API_BASE}${API_ATTENDEES}`, { event: event.id, page })
      .map((result) => {
        let attendees = result.results;
        const a = [];<commit_msg>Made getAttendees load more than 10 per request, should make the app load faster<commit_after>import { Observable, ReplaySubject, Subject } from 'rxjs';
import { Event } from 'services/event';
import { Attendee } from './attendee';

import { API_BASE, API_EVENTS, API_ATTEND, API_ATTENDEES, API_USERS } from 'common/constants';
import { isRfid } from 'common/utils';
import { http } from 'services/net';

/**
 * Status codes:
 * 50:
 *   des: no user with given username
 *   context: try to register rfid for a user
 *   http: status: 400 BAD_REQUEST
 * 40:
 *   des: no user with given rfid
 *   context: try to register with an rfid that is not bound to a user'
 *   http: status: 400 BAD_REQUEST
 * 30:
 *   des: Attendee is on waitinglist
 *   context: try to register an attendee when attendee is on waitinglist, force option = false'
 *   http: status: 100 CONTINUE
 * 20:
 *   des: Attendee has already registered
 *   context: try to register attendee when attendee is already registered
 *   http: status: 400 BAD_REQUEST
 * 10:
 *   des: Attendee successfuly registered
 *   context: try to register an attendee
 *   http: status: 200 OK
 */


class AttendeeServiceProvider {
  
  /**
   * @class AttendeeServiceProvider
   */
  constructor() {
    /** @private */
    this.cache = {};
  }
  
  /**
   * @method registerAttendee - Registers an attendee
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {Event} event - the event an attendee is being registerd for 
   * @param {String} rfid - the attendees rfid
   * @param {Boolean} approved - wether or not the attendee has been approved when on the waitinglist
   * @returns {Observable<{}>}
   */
  registerAttendee(event, rfid, approved = false) {
    return this.handleResponse(http.post(`${API_BASE}${API_ATTEND}`, {
      rfid: isRfid(rfid) ? rfid : null,
      username: isRfid(rfid) ? null : rfid,
      event: event.id,
      approved,
    }))
      .map((res) => {
        const ret = Object.assign({}, res, {
          event,
        });
        return ret;
      }); 
  }
  
  /**
   * @method getCached
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {Number} attendee_id - the attendee's id
   * @returns {Attendee}
   */
  getCached(attendee_id) {
    return this.cache[attendee_id];
  }

  /**
   * @method registerRfid - binds an rfid to a user
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {String} username 
   * @param {String} rfid 
   * @param {Event} event 
   */
  registerRfid(username, rfid, event) {
    if (username != null && rfid != null && rfid.length > 0) {
      return this.handleResponse(http.post(`${API_BASE}${API_ATTEND}`, {
        rfid,
        username,
        event: event.id,
      }));
    }
    return Observable.throw({error: "Rfid or username is null"});
  }

  /**
   * @method handleResponse - catches errors from an http response
   * @memberof AttendeeServiceProvider
   * @inner
   * @private
   * @param {Observable<{}>} r - Observable that resolves into an http/json response
   * @returns {Observable<{}>}
   */
  handleResponse(r) {
    return r.catch(error =>
      // if(error.status == 400 || error.status == 100)
       Observable.fromPromise(error.json ? error.json() : {message: "No json response in error"}).flatMap(r => Observable.throw(r))
        // return Observable.throw(Observable.fromPromise(error.json()))

    );
  }
  
  /**
   * @method getAttendees
   * @memberof AttendeeServiceProvider
   * @inner
   * @public
   * @param {Event} event - the event to fetch attendees from
   * @param {Number} page - what page to start fetching from 
   * @returns {Observable<Array<Attendee>>} - an observable that resolves into a list of Attendees
   */ 
  getAttendees(event, page = 1, page_size = 30) {
    const count = 0;
    
    return http.get(`${API_BASE}${API_ATTENDEES}`, { event: event.id, page, page_size })
      .map((result) => {
        let attendees = result.results;
        const a = [];
{'fraction_non_alphanumeric': 0.12389826240241752, 'fraction_numerical': 0.009065726517250064, 'mean_word_length': 2.870857699805068, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '2002799', 'n_tokens_mistral': 2685, 'n_tokens_neox': 2563, 'n_words': 746}