{"size": 1034, "ext": "js", "max_stars_repo_path": "src/api/controllers/target/targets.list.js", "max_stars_repo_name": "la-flute/faluche-troyenne-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/api/controllers/target/targets.list.js", "max_issues_repo_name": "la-flute/faluche-troyenne-api", "max_issues_repo_issues_event_min_datetime": "2019-02-28T14:08:28.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T06:49:25.000Z", "max_forks_repo_path": "src/api/controllers/target/targets.list.js", "max_forks_repo_name": "la-flute/faluche-troyenne-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 27.2105263158, "max_line_length": 77, "alphanum_fraction": 0.5473887814}
const errorHandler = require('../../utils/errorHandler')
const isAuth = require('../../middlewares/isAuth')
const isValide = require('../../middlewares/isValide')

/**
 * GET /targets
 *
 * Response:
 * [
 *    { id, name, firstname, lastname, email, paid, }, ...
 * ]
 */
module.exports = app => {
  app.get('/targets', [isAuth('targets-list'), isValide('targets-list')])
  app.get('/targets', async (req, res) => {
    try {
      const { User, Target } = req.app.locals.models
      let users = await User.findAll({
        attributes: ['id', 'firstName', 'nickName', 'town']
      })
      const targets = await Target.findAll({
        where: { userId: req.user.id },
        attributes: ['targetedId']
      })
      const actioned = targets.map(t => t.targetedId)
      users = users
        .filter(u => u.town)
        .filter(u => u.id !== req.user.id && !actioned.find(a => a === u.id))
      return res
        .status(200)
        .json(users)
        .end()
    } catch (err) {
      errorHandler(err, res)
    }
  })
}
