'use strict'

const Boom = require('boom')
const SQL = require('./../db/SQL')

function boomErrorWrapper (next) {
  return function wrapAsBadImplementation (err, result) {
    if (err && err.isBoom) {
      return next(err)
    }

    next(err ? Boom.badImplementation(err) : null, result)
  }
}

function isUniqueViolationError (err) {
  return err && err.code === '23505'
}

function isForeignKeyViolationError (err) {
  return err && err.code === '23503'
}

function checkPoliciesOrg (db, policies, organizationId, cb) {
  db.query(SQL`SELECT id FROM policies WHERE id = ANY (${policies}) AND org_id = ${organizationId}`, (err, result) => {
    if (err) return cb(Boom.badImplementation(err))

    if (result.rowCount !== policies.length) {
      return cb(Boom.badRequest(`Some policies [${policies.filter(p => result.rows.map(r => r.id).indexOf(p) < 0)}] were not found`))
    }

    cb()
  })
}

function checkUsersOrg (db, users, organizationId, cb) {
  db.query(SQL`SELECT id FROM users WHERE id = ANY (${users}) AND org_id = ${organizationId}`, (err, result) => {
    if (err) return cb(Boom.badImplementation(err))

    if (result.rowCount !== users.length) {
      return cb(Boom.badRequest(`Some users [${users.filter(p => result.rows.map(r => r.id).indexOf(p) < 0)}] were not found`))
    }

    cb()
  })
}

function checkTeamsOrg (db, teams, organizationId, cb) {
  db.query(SQL`SELECT id FROM teams WHERE id = ANY (${teams}) AND org_id = ${organizationId}`, (err, result) => {
    if (err) return cb(Boom.badImplementation(err))

    if (result.rowCount !== teams.length) {
      return cb(Boom.badRequest(`Some teams [${teams.filter(p => result.rows.map(r => r.id).indexOf(p) < 0)}] were not found`))
    }

    cb()
  })
}

function checkUserOrg (db, id, organizationId, cb) {
  db.query(SQL`SELECT id FROM users WHERE id = ${id} AND org_id = ${organizationId}`, (err, result) => {
    if (err) return cb(Boom.badImplementation(err))

    if (result.rowCount !== 1) {
      return cb(Boom.badRequest(`User ${id} not found`))
    }

    cb()
  })
}

module.exports = {
  boomErrorWrapper,
  isUniqueViolationError,
  isForeignKeyViolationError,
  checkPoliciesOrg,
  checkUsersOrg,
  checkTeamsOrg,
  checkUserOrg
}

@@ -7,16 +7,44 @@
 trict'%0A%0A
+const _ = require('lodash')%0A
 const Bo
@@ -64,16 +64,16 @@
 'boom')%0A
-
 const SQ
@@ -819,28 +819,30 @@
  %5B$%7B
-policies.filter(p =%3E
+_.difference(policies,
  res
@@ -860,39 +860,24 @@
 p(r =%3E r.id)
-.indexOf(p) %3C 0
 )%7D%5D were not
@@ -1229,25 +1229,27 @@
  %5B$%7B
-users.filter(p =%3E
+_.difference(users,
  res
@@ -1267,39 +1267,24 @@
 p(r =%3E r.id)
-.indexOf(p) %3C 0
 )%7D%5D were not
@@ -1636,25 +1636,27 @@
  %5B$%7B
-teams.filter(p =%3E
+_.difference(teams,
  res
@@ -1682,23 +1682,8 @@
 .id)
-.indexOf(p) %3C 0
 )%7D%5D 

replace array diff with lodash