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

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

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

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

const CategorySchema = new Schema({
  idapp: {
    type: String,
  },
  name: {
    type: String,
    unique: true,
    index: true,
  },
  img: {
    type: String,
  },
});

CategorySchema.statics.getAllCategories = function (callback) {
  Category.find(callback)
}

CategorySchema.statics.getCategoryById = function (id, callback) {
  Category.findById(id, callback);
}

CategorySchema.statics.getFieldsForSearch = function () {
  return [{ field: 'name', type: tools.FieldType.string }]
};

CategorySchema.statics.executeQueryTable = function (idapp, params) {
  return tools.executeQueryTable(this, idapp, params);
};

CategorySchema.statics.findAllIdApp = async function (idapp) {
  const myfind = { idapp };

  return await Category.find(myfind).lean();
};

const Category = mongoose.model('Category', CategorySchema);

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



module.exports = { Category };
