{"size": 1984, "ext": "js", "max_stars_repo_path": "src/modules/audit-logs/audit-logs.controller.js", "max_stars_repo_name": "sammler/sammler-log-service", "max_stars_repo_stars_event_min_datetime": "2021-02-24T16:45:52.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-21T14:49:25.000Z", "max_issues_repo_path": "src/modules/audit-logs/audit-logs.controller.js", "max_issues_repo_name": "sammler/sammler-log-service", "max_issues_repo_issues_event_min_datetime": "2020-12-01T05:00:09.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-02T05:01:44.000Z", "max_forks_repo_path": "src/modules/audit-logs/audit-logs.controller.js", "max_forks_repo_name": "stefanwalther/audit-log-service", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 107, "max_forks_count": null, "avg_line_length": 24.4938271605, "max_line_length": 59, "alphanum_fraction": 0.6078629032}
const AuditLogsModel = require('./audit-logs.model').Model;
const ExpressResult = require('express-result');

class AuditLogsController {

  // Todo: Test
  // Todo: Auth, RBAC
  static post(req, res) {
    return AuditLogsModel
      .create(req.body)
      .then(result => ExpressResult.created(res, result))
      .catch(err => ExpressResult.error(res, err));
  }

  // Todo: Test
  // Todo: Auth, RBAC
  static get(req, res) {
    return AuditLogsModel
      .find()
      .sort({ts: -1})
      .exec()
      .then(result => ExpressResult.ok(res, result))
      .catch(err => ExpressResult.error(res, err));
  }

  // Todo: Test
  static getByDomain(req, res) {
    return AuditLogsModel
      .find({
        event_domain: req.body.event_domain,
        user_id: req.body.user_id
      })
      .exec()
      .then(result => ExpressResult.ok(res, result))
      .catch(err => ExpressResult.error(res, err));
  }

  // Todo: Test
  // Todo: Auth
  static getById(req, res) {
    return AuditLogsModel
      .findById(req.params.id)
      .exec()
      .then(result => ExpressResult.ok(res, result))
      .catch(err => ExpressResult.error(res, err));
  }

  // Todo: Test
  // Todo: Auth (only tenant_admin)
  static deleteById(req, res) {
    return AuditLogsModel
      .findByIdAndRemove(req.params.id)
      .exec()
      .then(result => ExpressResult.ok(res, result))
      .catch(err => ExpressResult.error(res, err));
  }

  // Todo: Test
  // Todo: Auth (only admins)
  static delete(req, res) {
    return AuditLogsModel
      .deleteMany({})
      .exec()
      .then(result => ExpressResult.ok(res, result))
      .catch(err => ExpressResult.error(res, err));
  }

  // Todo: Test
  // Todo: Auth (only admins)
  static generate(req, res) {
    return AuditLogsModel
      .generate({
        amount: 100
      })
      .then(result => ExpressResult.created(res, result))
      .catch(err => ExpressResult.error(res, err));
  }
}

module.exports = AuditLogsController;
