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 CashSubCategorySchema = new Schema({
  idapp: {
    type: String,
  },
  idCashCategory: {
    type: String,
  },
  descr: {
    type: String,
  },
  notes: {
    type: String
  },
});

var CashSubCategory = module.exports = mongoose.model('CashSubCategory', CashSubCategorySchema);

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 CashSubCategory.find(myfind).lean();
};

module.exports.getAllCashSubCategory = function (query, sort, callback) {
  CashSubCategory.find(query, null, sort, callback)
}

module.exports.getCashSubCategoryByUserId = function (userId, sort, callback) {
  CashSubCategory.find({ userId }, null, sort, callback)
}


module.exports.getCashSubCategoryByID = function (id, callback) {
  CashSubCategory.findById(id, callback);
}

module.exports.createCashSubCategory = async function (CashSubCategory) {
  const CashSubCategoryModel = new CashSubCategory(CashSubCategory);

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

module.exports.createIndexes()
  .then(() => { })
  .catch((err) => { throw err; });

