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

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

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

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

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

const searchSchema = new Schema({
  idapp: {
    type: String,
  },
  userId: {
    type: String,
  },
  date_created: {
    type: Date,
    default: Date.now,
  },
  text: {
    type: String,
  },
});

searchSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp, sall) {
  const Search = this;

  let myfind = {};
  if (sall === '1')
    myfind = { idapp };
  else
    myfind = { userId, idapp };

  return Search.find(myfind).lean();
};


const Search = mongoose.model('Search', searchSchema);

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


module.exports = { Search };
