server/helpers/adminLogs.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createAdminLog = createAdminLog;
exports.getOrderedAdminLogs = getOrderedAdminLogs;
require("core-js/modules/es.date.to-json.js");
require("core-js/modules/web.url.to-json.js");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
/*
 * Copyright (C) 2023 Shivam Awasthi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
function createAdminLog(_x, _x2, _x3) {
  return _createAdminLog.apply(this, arguments);
}
/**
 * Fetches Admin logs for Show All Admin Logs page
 * Fetches the last 'size' number of admin logs with offset 'from'
 *
 * @param {number} from - the offset value
 * @param {number} size - no. of last logs required
 * @param {object} orm - the BookBrainz ORM, initialized during app setup
 * @returns {array} - orderedLogs
 */
function _createAdminLog() {
  _createAdminLog = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(logData, AdminLog, transacting) {
    var actionType, adminId, newPrivs, note, oldPrivs, targetUserId;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            actionType = logData.actionType, adminId = logData.adminId, newPrivs = logData.newPrivs, note = logData.note, oldPrivs = logData.oldPrivs, targetUserId = logData.targetUserId;
            _context.next = 3;
            return new AdminLog({
              actionType: actionType,
              adminId: adminId,
              newPrivs: newPrivs,
              note: note,
              oldPrivs: oldPrivs,
              targetUserId: targetUserId
            }).save(null, {
              method: 'insert',
              transacting: transacting
            });
          case 3:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _createAdminLog.apply(this, arguments);
}
function getOrderedAdminLogs(_x4, _x5, _x6) {
  return _getOrderedAdminLogs.apply(this, arguments);
}
function _getOrderedAdminLogs() {
  _getOrderedAdminLogs = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(from, size, orm) {
    var AdminLog, logs, logsJSON;
    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            AdminLog = orm.AdminLog;
            _context2.next = 3;
            return new AdminLog().orderBy('time', 'DESC').fetchPage({
              limit: size,
              offset: from,
              withRelated: ['admin', 'targetUser']
            });
          case 3:
            logs = _context2.sent;
            logsJSON = logs.toJSON();
            return _context2.abrupt("return", logsJSON);
          case 6:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _getOrderedAdminLogs.apply(this, arguments);
}
//# sourceMappingURL=adminLogs.js.map