const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;

const tools = require('../tools/general');

const { ObjectID } = require('mongodb');

mongoose.Promise = global.Promise;
mongoose.level = "F";


// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
  schema.options.usePushEach = true
});

const CashCategorySchema = new Schema({
  idapp: {
    type: String,
  },
  descr: {
    type: String,
  },
  notes: {
    type: String
  },
});

var CashCategory = module.exports = mongoose.model('CashCategory', CashCategorySchema);

module.exports.getFieldsForSearch = function () {
  return []
};

module.exports.executeQueryTable = function (idapp, params) {
  params.fieldsearch = this.getFieldsForSearch();
  return tools.executeQueryTable(this, idapp, params);
};

module.exports.findAllIdApp = async function (idapp) {
  const myfind = { idapp };

  return await CashCategory.find(myfind);
};

module.exports.getAllCashCategory = function (query, sort, callback) {
  CashCategory.find(query, null, sort, callback)
}

module.exports.getCashCategoryByUserId = function (userId, sort, callback) {
  CashCategory.find({ userId }, null, sort, callback)
}


module.exports.getCashCategoryByID = function (id, callback) {
  CashCategory.findById(id, callback);
}

module.exports.createCashCategory = async function (CashCategory) {
  const CashCategoryModel = new CashCategory(CashCategory);

  return await CashCategoryModel.save(CashCategory)
    .then((ris) => {
      if (!!ris)
        return ris._id;
      return null;
    });
}
