'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      * *******************************************************************************
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * Copyright (c) 2018 Edgeworx, Inc.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * This program and the accompanying materials are made available under the
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * terms of the Eclipse Public License v. 2.0 which is available at
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * http://www.eclipse.org/legal/epl-2.0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  * SPDX-License-Identifier: EPL-2.0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *  *******************************************************************************
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      */

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _sequelizeUtils = require('./../utils/sequelizeUtils');

var _sequelizeUtils2 = _interopRequireDefault(_sequelizeUtils);

var _errorUtils = require('./../utils/errorUtils');

var _errorUtils2 = _interopRequireDefault(_errorUtils);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var BaseManager = function () {
  function BaseManager() {
    _classCallCheck(this, BaseManager);
  }

  _createClass(BaseManager, [{
    key: 'getEntity',
    value: function getEntity() {
      return null;
    }
  }, {
    key: 'find',
    value: function find(object) {

      object = object || {};

      if (object.limit && object.limit == -1) delete object.limit;

      return this.getEntity().findAll(object);
    }
  }, {
    key: 'create',
    value: function create(object) {
      if (this.getEntity().attributes.createdAt) object.createdAt = _moment2.default.utc().valueOf();

      return this.getEntity().create(object);
    }
  }, {
    key: 'findById',
    value: function findById(id, include) {
      return this.getEntity().findById(id, {
        include: include
      });
    }
  }, {
    key: 'deleteById',
    value: function deleteById(id) {
      return this.findById(id).then(function (object) {
        if (null == object) return;

        return object.destroy();
      });
    }
  }, {
    key: 'update',
    value: function update(newObject) {
      //find the new content by id
      return this.findById(newObject.id).then(function (dbObject) {
        //if id is invalid throw error
        if (null == dbObject) throw new _errorUtils2.default.BadRequestError('Invalid id');

        //update the properties
        _sequelizeUtils2.default.updateObject(dbObject, newObject);

        //if there are any updated properites
        //save the changes, otherwise return the object as it is to next step
        if (!dbObject.changed()) return dbObject;

        return dbObject.save();
      });
    }
  }, {
    key: 'count',
    value: function count(where) {
      var queryObject = {};
      if (where) queryObject.where = where;

      return this.getEntity().count(queryObject);
    }
  }]);

  return BaseManager;
}();

exports.default = BaseManager;