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

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

const shared_consts = require('../tools/shared_nodejs');

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

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

// A1P

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

const inventariogmSchema = new Schema({
  idapp: {
    type: String,
  },
});

var Inventariogm = module.exports = mongoose.model('Inventariogm', inventariogmSchema);

inventariogmSchema.index({ idapp: 1 });

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

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

module.exports.getInventariogmByCode = function (idapp, code) {
  return Inventariogm.findAllIdApp(idapp, code);
}

module.exports.getInventariogmById = async function (id) {
  const arrris = await Inventariogm.findAllIdApp('', '', id);
  return arrris && arrris.length > 0 ? arrris[0] : null
}

module.exports.findAllIdApp = async function (idapp) {
  const Inventariogm = this;

  const myfind = { idapp, deleted: false };

  return await tools.findAllQueryIdApp(Inventariogm, myfind);

};