<commit_before>const { Action, api } = require('actionhero')

module.exports = class ListSettlement extends Action {
  constructor () {
    super()
    this.name = 'autocomplete'
    this.description = this.name
    this.middleware = ['auth']
    this.inputs = {
      q: { required: true },
      type: { required: true },
      limit: {
        default: 10,
        formatter: function (value) {
          return Math.max(1, Math.min(100, parseInt(value) || 100))
        }
      }
    }
  }

  async run ({ params: { q, type, limit }, response }) {
    response.data = []
    switch (type) {
      case 'settlements':
        response.data = response.data.concat(
          (await api.models.settlement.findAll({
            attributes: ['nameEn'],
            where: {
              nameEn: {
                [api.sequelize.sequelize.options.dialect === 'postgres' ? '$ilike' : '$like']:
                  `${q}%`
              }
            },
            limit
          })).map(({ nameEn }) => nameEn),
          (await api.models.settlement.findAll({
            attributes: ['nameLocal'],
            where: {
              nameLocal: {
                [api.sequelize.sequelize.options.dialect === 'postgres' ? '$ilike' : '$like']:
                  `${q}%`
              }
            },
            limit
          })).map(({ nameLocal }) => nameLocal)
        )
        break
    }
  }
}<commit_msg>feat: add poi to autocomplete search<commit_after>const { Action, api } = require('actionhero')
const capitalizeFirstLetter = require('../utils/capitalizeFirstLetter')

module.exports = class ListSettlement extends Action {
  constructor () {
    super()
    this.name = 'autocomplete'
    this.description = this.name
    this.middleware = ['auth']
    this.inputs = {
      q: { required: true },
      type: { required: true },
      limit: {
        default: 10,
        formatter: function (value) {
          return Math.max(1, Math.min(100, parseInt(value) || 100))
        }
      }
    }
  }

  async run ({ connection, params: { q, type, limit }, response }) {
    response.data = []
    if (!Array.isArray(type)) {
      type = [type]
    }

    const fetches = []

    if (type.includes('settlements')) {
      fetches.push(Promise.resolve(api.models.settlement.findAll({
        attributes: ['nameEn'],
        where: {
          nameEn: {
            [api.sequelize.sequelize.options.dialect === 'postgres' ? '$ilike' : '$like']:
              `${q}%`
          }
        },
        limit
      })).then(settlements => settlements.map(settlement => settlement.nameEn)))

      fetches.push(Promise.resolve(api.models.settlement.findAll({
        attributes: ['nameLocal'],
        where: {
          nameLocal: {
            [api.sequelize.sequelize.options.dialect === 'postgres' ? '$ilike' : '$like']:
              `${q}%`
          }
        },
        limit
      })).then(settlements => settlements.map(settlement => settlement.nameLocal)))
    }

    if (type.includes('pois')) {
      fetches.push(Promise.resolve(api.models.poi.findAll({
        attributes: ['labelEn'],
        where: {
          type,
          labelEn: {
            [api.sequelize.sequelize.options.dialect === 'postgres' ? '$ilike' : '$like']:
                `${q}%`
          }
        },
        limit
      })).then(pois => pois.map(poi => poi.labelEn)))

      if (connection.locale !== 'en') {
        const label = `label${capitalizeFirstLetter(connection.locale)}`
        fetches.push(Promise.resolve(api.models.poi.findAll({
          attributes: [label],
          where: {
            type,
            [label]: {
              [api.sequelize.sequelize.options.dialect === 'postgres' ? '$ilike' : '$like']:
                `${q}%`
            }
          },
          limit
        })).then(pois => pois.map(poi => poi[label])))
      }
    }

    const results = await Promise.all(fetches)
    response.data = [].concat(...results)
  }
}
{'fraction_non_alphanumeric': 0.1595045500505561, 'fraction_numerical': 0.00455005055611729, 'mean_word_length': 1.8063829787234043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '7462440', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1180, 'n_words': 212}