AgileVentures/MetPlus_PETS

View on GitHub
app/assets/javascripts/pusher.js.erb

Summary

Maintainability
Test Coverage
var PusherControl = {
  get_pusher: (function () {
    var pusher = new Pusher('<%=ENV['PUSHER_APP_KEY']%>');
    return function () {return pusher;}
  })(),

  get_spinner: (function () {
    var saved_selector;
    var spinner;
    return function (selector) {
      if (selector !== saved_selector) {
        saved_selector = selector;
        spinner = PETS.spinner($(selector));
      }
      return spinner;
    }
  })(),

  setup: function () {
    var pusher = PusherControl.get_pusher();

    // pusher_control channel is used to send
    // server-originated events to clients

    var channel = pusher.channel('pusher_control');

    if (!channel) {
      channel = pusher.subscribe('pusher_control');
    }

    channel.bind('spinner_start', function(data) {
      if (parseInt(Cookies('user_id')) === data.user_id) {
        PusherControl.get_spinner(data.target).start();
      }
    });

    channel.bind('spinner_stop', function(data) {
      if (parseInt(Cookies('user_id')) === data.user_id) {
        PusherControl.get_spinner(data.target).stop();
      }
    });

    channel.bind('cp_interest_in_js', function(data) {
      if (parseInt(Cookies('user_id')) === data.jd_user_id) {
        Notification.info_notification('Company person ' +
            "<a href='/company_admin/company_people/" +
            data.cp_id + "' target='_blank'>" +
            data.cp_name + "</a> has expressed interest in job seeker " +
            "<a href='/job_seekers/" + data.js_id + "' target='_blank'>" +
            data.js_name + "</a> for this job: " +
            "<a href='/jobs/" + data.job_id + "' target='_blank'>" +
            data.job_title + "</a>.")
      }
    });

    channel.bind('js_registered', function(data) {
      // Process event if I am logged in and am an agency_person
      if (Cookies('person_type') === 'AgencyPerson') {
        Notification.success_notification('Job Seeker: ' +
            "<a href='/job_seekers/" + data.id +
            "' target='_blank'>" + data.name + "</a>" +
            ' has joined PETS.');
      }
    });

    channel.bind('company_registered', function(data) {
      // Process event if I am logged in and am an agency_person
      if (Cookies('person_type') === 'AgencyPerson') {
        Notification.info_notification('Company: ' +
            "<a href='/companies/" + data.id +
            " ' target='_blank'>" + data.name + "</a>" +
            ' has registered in PETS.');

      }
    });

    channel.bind('jobseeker_applied', function(data) {
      // Process event if I am logged in and I am in notify list
      if ($.inArray(parseInt(Cookies('user_id')), data.notify_list) != -1) {
        Notification.info_notification('Job Seeker: ' +
            "<a href='/job_seekers/" + data.js_id +
            "' target='_blank'>" + data.js_name + "</a>" +
            " has applied to <a href='/jobs/" + data.job_id +
            "' target='_blank'>this job</a>");
      }
    });

    channel.bind('job_applied_by_job_developer', function(data) {
      // Process event if I am logged in and I am in notify list
      if (parseInt(Cookies('user_id')) === data.js_user_id) {
        Notification.info_notification('Your job developer ' +
            " has applied to <a href='/jobs/" + data.job_id +
            "' target='_blank'>this job</a>" + ' for you.');
      }
    });

    channel.bind('job_applied_by_other_job_developer', function(data) {
      // Process event if I am logged in and I am in notify list
      if (parseInt(Cookies('user_id')) === data.js_user_id) {
        Notification.info_notification('Job developer ' +
            '<a href=/agency_people/"' + data.jd_id + '/home" target="_blank">'
            + data.jd_name + '</a>' +
            " has applied to <a href='/jobs/" + data.job_id +
            "' target='_blank'>this job</a>" + ' for you.');
      }
    });

    channel.bind('job_application_accepted', function(data) {
      if (parseInt(Cookies('user_id')) === data.ap_user_id) {
        Notification.info_notification("Job Application: " +
          "<a href='/job_applications/" + data.id +
          "' target='_blank'>" + data.job_title + " by " +
          data.js_name + "</a>" + " is accepted.");
      }
    });

    channel.bind('job_application_rejected', function(data) {
      if (parseInt(Cookies('user_id')) === data.ap_user_id) {
        Notification.info_notification("Job Application: " +
            "<a href='/job_applications/" + data.id +
            "' target='_blank'>" + data.job_title + " by " +
            data.js_name + "</a>" + " is rejected.");
      }
    });

    channel.bind('jobseeker_assigned_jd', function(data) {
      // Process event if I am logged in and I am the job developer
      if (parseInt(Cookies('user_id')) === data.jd_user_id) {
        Notification.info_notification('Job Seeker: ' +
            "<a href='/job_seekers/" + data.js_id +
            "' target='_blank'>" + data.js_name + "</a>" +
            " has been assigned to you as Job Developer");
      }

      PusherControl.notify_job_seeker(data.js_user_id, data.jd_name, data.agency_name,
                        ' Job Developer.');
    });

    channel.bind('jd_self_assigned_js', function(data) {
      PusherControl.notify_job_seeker(data.js_user_id, data.jd_name, data.agency_name,
                        ' Job Developer.');
    });

    channel.bind('jobseeker_assigned_cm', function(data) {
      // Process event if I am logged in and I am the case manager
      if (parseInt(Cookies('user_id')) === data.cm_user_id) {
        Notification.info_notification('Job Seeker: ' +
            "<a href='/job_seekers/" + data.js_id +
            "' target='_blank'>" + data.js_name + "</a>" +
            " has been assigned to you as Case Manager");
      }

      PusherControl.notify_job_seeker(data.js_user_id, data.cm_name, data.agency_name,
                        ' Case Manager.');
    });

    channel.bind('cm_self_assigned_js', function(data) {
      PusherControl.notify_job_seeker(data.js_user_id, data.cm_name, data.agency_name,
                        ' Case Manager.');
    });

    channel.bind('job_posted', function(data) {
      // Process event if I am logged in and I am in notify list
      if ($.inArray(parseInt(Cookies('user_id')), data.notify_list) != -1) {
        Notification.info_notification('New job (' +
            "<a href='/jobs/" + data.job_id +
            "' target='_blank'>" + data.job_title + '</a>)' +
            " posted for company: " + data.company_name);
      }
    });

    channel.bind('job_revoked', function(data) {
      // Process event if I am logged in and I am in notify list
      if ($.inArray(parseInt(Cookies('user_id')), data.notify_list) != -1) {
        Notification.info_notification('Job (' +
            "<a href='/jobs/" + data.job_id +
            "' target='_blank'>" + data.job_title + '</a>)' +
            " revoked for company: " + data.company_name);
      }
    });
  },
  notify_job_seeker: function (user_id, agency_person_name, agency, role_str) {
    // Process event if I am logged in and I am the job seeker
    if (parseInt(Cookies('user_id')) === user_id) {
      Notification.info_notification(agency_person_name +
          ' has been assigned to you as your ' + agency + role_str);
    }
  }
};

$(PusherControl.setup);