<commit_before>'use strict'

var _ = require('lodash');
var Promise = require('bluebird');
var moment = require('moment');
var actions = require('../helpers/actions');

var model = require('../models/formBirds');

exports.formBirdsAdd = {
  name: 'formBirds:create',
  description: 'formBirds:create',
  middleware: ['auth'],
  inputs: model.insertInputs,

  run: actions.getInsert('formBirds')
};

exports.formBirdsEdit = {
  name: 'formBirds:edit',
  description: 'formBirds:edit',
  outputExample: {},
  middleware: ['auth'],

  inputs: model.editInputs,

  run: actions.getEdit('formBirds')
};

exports.formBirdsView = {
  name: 'formBirds:view',
  description: 'formBirds:view',
  middleware: ['auth'],
  inputs: {id: {required: true}},

  run: actions.getView('formBirds')
};

exports.formBirdsDelete = {
  name: 'formBirds:delete',
  description: 'formBirds:delete',
  middleware: ['auth'],
  inputs: {id: {required: true}},

  run: actions.getDelete('formBirds')
};


function prepareQuery(api, data) {
  return Promise.resolve({})
    .then(function (q) {
      var limit = parseInt(data.params.limit) || 20;
      // if (!data.session.user.isAdmin) {
      //   limit = Math.max(1, Math.min(1000, limit));
      // }
      var offset = data.params.offset || 0;

      q = {
        order: [
          ['updatedAt', 'DESC'],
          ['id', 'DESC']
        ],
        offset: offset
      };
      if (limit !== -1)
        q.limit = limit;

      if (!data.session.user.isAdmin) {
        q.where = _.extend(q.where || {}, {
          userId: data.session.userId
        });
      } else {
        if (data.params.user) {
          q.where = _.extend(q.where || {}, {
            userId: data.params.user
          });
        }
      }
      if (data.params.location) {
        q.where = _.extend(q.where || {}, {
          location: api.sequelize.sequelize.options.dialect === 'postgres'?{ilike: data.params.location}:data.params.location
        });
      }
      if (data.params.species) {
        q.where = _.extend(q.where || {}, {
          species: data.params.species
        });
      }
      if (data.params.from_date) {
        q.where = q.where || {};
        q.where.observationDateTime = _.extend(q.where.observationDateTime || {}, {
          $gte: moment(data.params.from_date).toDate()
        });
      }
      if (data.params.to_date) {
        q.where = q.where || {};
        q.where.observationDateTime = _.extend(q.where.observationDateTime || {}, {
          $lte: moment(data.params.to_date).toDate()
        });
      }
      return q;
    })
}

exports.formBirdsList = {
  name: 'formBirds:list',
  description: 'formBirds:list',
  middleware: ['auth'],
  //location&user&year&month&species&limit&offset
  inputs: {
    location: {},
    user: {},
    species: {},
    from_date: {},
    to_date: {},
    limit: {required: false, default: 20},
    offset: {required: false, default: 0}
  },

  run: actions.getSelect('formBirds', prepareQuery)
};<commit_msg>Include birds euring code in export<commit_after>'use strict'

var _ = require('lodash');
var Promise = require('bluebird');
var moment = require('moment');
var actions = require('../helpers/actions');

var model = require('../models/formBirds');

exports.formBirdsAdd = {
  name: 'formBirds:create',
  description: 'formBirds:create',
  middleware: ['auth'],
  inputs: model.insertInputs,

  run: actions.getInsert('formBirds')
};

exports.formBirdsEdit = {
  name: 'formBirds:edit',
  description: 'formBirds:edit',
  outputExample: {},
  middleware: ['auth'],

  inputs: model.editInputs,

  run: actions.getEdit('formBirds')
};

exports.formBirdsView = {
  name: 'formBirds:view',
  description: 'formBirds:view',
  middleware: ['auth'],
  inputs: {id: {required: true}},

  run: actions.getView('formBirds')
};

exports.formBirdsDelete = {
  name: 'formBirds:delete',
  description: 'formBirds:delete',
  middleware: ['auth'],
  inputs: {id: {required: true}},

  run: actions.getDelete('formBirds')
};


function prepareQuery(api, data) {
  return Promise.resolve({})
    .then(function (q) {
      var limit = parseInt(data.params.limit) || 20;
      // if (!data.session.user.isAdmin) {
      //   limit = Math.max(1, Math.min(1000, limit));
      // }
      var offset = data.params.offset || 0;

      q = {
        order: [
          ['updatedAt', 'DESC'],
          ['id', 'DESC']
        ],
        offset: offset
      };
      if (limit !== -1)
        q.limit = limit;

      if (!data.session.user.isAdmin) {
        q.where = _.extend(q.where || {}, {
          userId: data.session.userId
        });
      } else {
        if (data.params.user) {
          q.where = _.extend(q.where || {}, {
            userId: data.params.user
          });
        }
      }
      if (data.params.location) {
        q.where = _.extend(q.where || {}, {
          location: api.sequelize.sequelize.options.dialect === 'postgres'?{ilike: data.params.location}:data.params.location
        });
      }
      if (data.params.species) {
        q.where = _.extend(q.where || {}, {
          species: data.params.species
        });
      }
      if (data.params.from_date) {
        q.where = q.where || {};
        q.where.observationDateTime = _.extend(q.where.observationDateTime || {}, {
          $gte: moment(data.params.from_date).toDate()
        });
      }
      if (data.params.to_date) {
        q.where = q.where || {};
        q.where.observationDateTime = _.extend(q.where.observationDateTime || {}, {
          $lte: moment(data.params.to_date).toDate()
        });
      }
      return q;
    })
}

exports.formBirdsList = {
  name: 'formBirds:list',
  description: 'formBirds:list',
  middleware: ['auth'],
  //location&user&year&month&species&limit&offset
  inputs: {
    location: {},
    user: {},
    species: {},
    from_date: {},
    to_date: {},
    limit: {required: false, default: 20},
    offset: {required: false, default: 0}
  },

  run: actions.getSelect('formBirds', prepareQuery, function (api, record) {
    return {
      speciesEuringCode: record['speciesInfo.euring'],
    }
  })
};
{'fraction_non_alphanumeric': 0.1753267973856209, 'fraction_numerical': 0.00392156862745098, 'mean_word_length': 2.5320253894979805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '331170', 'n_tokens_mistral': 2078, 'n_tokens_neox': 2002, 'n_words': 353}