{"size": 1976, "ext": "js", "max_stars_repo_path": "www/js/services/log-alerts/LogAlertsService.js", "max_stars_repo_name": "pmajoras/pgp-logs", "max_stars_repo_stars_event_min_datetime": "2016-06-07T12:19:11.000Z", "max_stars_repo_stars_event_max_datetime": "2016-11-18T00:17:59.000Z", "max_issues_repo_path": "www/js/services/log-alerts/LogAlertsService.js", "max_issues_repo_name": "pmajoras/pgp-logs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "www/js/services/log-alerts/LogAlertsService.js", "max_forks_repo_name": "pmajoras/pgp-logs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.2285714286, "max_line_length": 130, "alphanum_fraction": 0.6138663968}
'use strict';
var q = require('q');
var BaseService = require('../BaseService');
var client = require('../JqueryRestClientService').logAlerts;
const ApplicationsService = require('../applications/ApplicationsService');
const applicationService = new ApplicationsService();
const AuthenticationService = require('../authentication/AuthenticationService');
const authenticationService = new AuthenticationService();

class LogAlertsService extends BaseService {
  constructor() {
    super();
  }

  getLogAlertsByAlertId(alertId) {
    return this.handleApiPromise(client.read(authenticationService.getUserId(), alertId));
  }

  resolveLogAlerts(alertId, logAlertsIds) {
    return this.handleApiPromise(client.resolve.post(authenticationService.getUserId(), alertId, { logAlertsIds: logAlertsIds }));
  }

  getApplicationLogAlerts() {
    let deferred = q.defer();
    let loadedApplications = [];

    applicationService.getApplications()
      .then((applications) => {
        loadedApplications = applications;
        return client.read(authenticationService.getUserId());
      })
      .then((logAlerts) => {
        let data = [];
        loadedApplications.forEach((application) => {
          let appData = {
            id: application._id,
            appId: application.appId,
            alerts: []
          };

          application.alerts.forEach((alert) => {
            let alertData = {
              name: alert.name,
              quantity: 0
            };

            logAlerts.forEach((logAlert) => {
              if (logAlert.appId === application.appId && logAlert.name === alert.name) {
                alertData.quantity++;
              }
            });

            appData.alerts.push(alertData);
          });

          data.push(appData);
        });

        deferred.resolve(data);
      })
      .catch((err) => {
        return deferred.reject(err);
      });

    return deferred.promise;
  }
}

module.exports = LogAlertsService;
