<commit_before>import * as Promise from 'bluebird'
import sqs from '../../lib/sqs'
import { buildSuccessResponse, buildFailureResponse } from '../../lib/http/response'
import { ValidationError, NotFoundError, InternalServerError } from '../../lib/errors'
import * as boletoService from './service'
import { parse } from '../../lib/http/request'
import { createSchema, updateSchema } from './schema'
import { makeFromLogger } from '../../lib/logger'
import { defaultCuidValue } from '../../lib/database/schema'
import { BoletosToRegisterQueue } from './queues'
import { models } from '../../database'

const { Boleto } = models

const makeLogger = makeFromLogger('boleto/index')

const handleError = (err) => {
  if (err instanceof ValidationError) {
    return buildFailureResponse(400, err)
  }

  if (err instanceof NotFoundError) {
    return buildFailureResponse(404, err)
  }

  return buildFailureResponse(500, new InternalServerError())
}

export const create = (event, context, callback) => {
  const body = JSON.parse(event.body || JSON.stringify({}))
  const logger = makeLogger({ operation: 'create' }, { id: defaultCuidValue('req_')() })

  const shouldRegister = () => body.register !== 'false' && body.register !== false

  const registerBoletoConditionally = (boleto) => {
    if (shouldRegister()) {
      return boletoService.register(boleto)
    }

    return boleto
  }

  // eslint-disable-next-line
  const pushBoletoToQueueConditionally = (boleto) => {
    if (boleto.status === 'pending_registration') {
      logger.info({ subOperation: 'pushToQueue', status: 'started', metadata: { boleto_id: boleto.id } })
      return BoletosToRegisterQueue.push({
        boleto_id: boleto.id
      })
        .then(() => {
          logger.info({ subOperation: 'pushToQueue', status: 'succeeded', metadata: { boleto_id: boleto.id } })
        })
        .catch((err) => {
          logger.info({ subOperation: 'pushToQueue', status: 'failed', metadata: { boleto_id: boleto.id, err } })
          throw err
        })
    }
  }

  logger.info({ status: 'started', metadata: { body } })

  Promise.resolve(body)
    .then(parse(createSchema))
    .then(boletoService.create)
    .tap(registerBoletoConditionally)
    .tap(pushBoletoToQueueConditionally)
    .then(Boleto.buildResponse)
    .then(buildSuccessResponse(201))
    .tap((response) => {
      logger.info({
        status: 'succeeded',
        metadata: { body: response.body, statusCode: response.statusCode }
      })
    })
    .catch((err) => {
      logger.error({ status: 'failed', metadata: { err } })
      return handleError(err)
    })
    .then(response => callback(null, response))
}

export const register = (event, context, callback) => {
  const body = JSON.parse(event.body || JSON.stringify({}))
  const logger = makeLogger({ operation: 'register' }, { id: defaultCuidValue('req_')() })
  const { boleto_id, sqsMessage } = body

  // eslint-disable-next-line
  const removeBoletoFromQueueConditionally = (boleto) => {
    if (boleto.status === 'registered' || boleto.status === 'refused') {
      logger.info({ subOperation: 'removeFromQueue', status: 'started', metadata: { boleto_id: boleto.id } })
      return BoletosToRegisterQueue.remove(sqsMessage)
        .then(() => {
          logger.info({ subOperation: 'removeFromQueue', status: 'succeeded', metadata: { boleto_id: boleto.id } })
        })
        .catch((err) => {
          logger.info({ subOperation: 'removeFromQueue', status: 'failed', metadata: { boleto_id: boleto.id, err } })
          throw err
        })
    }
  }

  // eslint-disable-next-line
  const sendMessageToUserQueueConditionally = (boleto) => {
    if (boleto.status === 'registered' || boleto.status === 'refused') {
      logger.info({ subOperation: 'sendToUserQueue', status: 'started', metadata: { boleto_id: boleto.id } })

      const params = {
        MessageBody: JSON.stringify({
          boleto_id: boleto.id,
          status: boleto.status
        }),
        QueueUrl: boleto.queue_url
      }

      return sqs.sendMessage(params).promise()
        .then(() => {
          logger.info({ subOperation: 'sendToUserQueue', status: 'succeeded', metadata: { boleto_id: boleto.id } })
        })
        .catch((err) => {
          logger.info({ subOperation: 'sendToUserQueue', status: 'failed', metadata: { boleto_id: boleto.id, err } })
          throw err
        })
    }
  }

  logger.info({ status: 'started', metadata: { body } })

  boletoService.registerById(boleto_id)
    .tap(removeBoletoFromQueueConditionally)
    .tap(sendMessageToUserQueueConditionally)
    .tap((response) => {
      logger.info({
        status: 'succeeded',
        metadata: { body: response.body, statusCode: response.statusCode }
      })
    })
    .catch((err) => {
      logger.error({ status: 'failed', metadata: { err } })
      callback(err)
    })
    .then(boleto => callback(null, boleto))
}

export const update = (event, context, callback) => {
  const body = JSON.parse(event.body || JSON.stringify({}))
  const { pathParameters = {} }: { pathParameters: any } = event

  const { id } = pathParameters
  const { bank_response_code, paid_amount } = body

  Promise.resolve({ id, bank_response_code, paid_amount })
    .then(parse(updateSchema))
    .then(boletoService.update)
    .then(Boleto.buildResponse)
    .then(buildSuccessResponse(200))
    .catch(handleError)
    .then(response => callback(null, response))
}

export const index = (event, context, callback) => {
  const { queryStringParameters = {} }: { queryStringParameters: any } = event
  const { page, count } = queryStringParameters

  Promise.resolve({ page, count })
    .then(boletoService.index)
    .then(buildSuccessResponse(200))
    .catch(handleError)
    .then(response => callback(null, response))
}

export const show = (event, context, callback) => {
  const { pathParameters = {} }: { pathParameters: any } = event
  const { id } = pathParameters

  Promise.resolve(id)
    .then(boletoService.show)
    .then(buildSuccessResponse(200))
    .catch(handleError)
    .then(response => callback(null, response))<commit_msg>handler: fix the way the handler gets parameters from event.<commit_after>import * as Promise from 'bluebird'
import { path } from 'ramda'
import sqs from '../../lib/sqs'
import { buildSuccessResponse, buildFailureResponse } from '../../lib/http/response'
import { ValidationError, NotFoundError, InternalServerError } from '../../lib/errors'
import * as boletoService from './service'
import { parse } from '../../lib/http/request'
import { createSchema, updateSchema } from './schema'
import { makeFromLogger } from '../../lib/logger'
import { defaultCuidValue } from '../../lib/database/schema'
import { BoletosToRegisterQueue } from './queues'
import { models } from '../../database'

const { Boleto } = models

const makeLogger = makeFromLogger('boleto/index')

const handleError = (err) => {
  if (err instanceof ValidationError) {
    return buildFailureResponse(400, err)
  }

  if (err instanceof NotFoundError) {
    return buildFailureResponse(404, err)
  }

  return buildFailureResponse(500, new InternalServerError())
}

export const create = (event, context, callback) => {
  const body = JSON.parse(event.body || JSON.stringify({}))
  const logger = makeLogger({ operation: 'create' }, { id: defaultCuidValue('req_')() })

  const shouldRegister = () => body.register !== 'false' && body.register !== false

  const registerBoletoConditionally = (boleto) => {
    if (shouldRegister()) {
      return boletoService.register(boleto)
    }

    return boleto
  }

  // eslint-disable-next-line
  const pushBoletoToQueueConditionally = (boleto) => {
    if (boleto.status === 'pending_registration') {
      logger.info({ subOperation: 'pushToQueue', status: 'started', metadata: { boleto_id: boleto.id } })
      return BoletosToRegisterQueue.push({
        boleto_id: boleto.id
      })
        .then(() => {
          logger.info({ subOperation: 'pushToQueue', status: 'succeeded', metadata: { boleto_id: boleto.id } })
        })
        .catch((err) => {
          logger.info({ subOperation: 'pushToQueue', status: 'failed', metadata: { boleto_id: boleto.id, err } })
          throw err
        })
    }
  }

  logger.info({ status: 'started', metadata: { body } })

  Promise.resolve(body)
    .then(parse(createSchema))
    .then(boletoService.create)
    .tap(registerBoletoConditionally)
    .tap(pushBoletoToQueueConditionally)
    .then(Boleto.buildResponse)
    .then(buildSuccessResponse(201))
    .tap((response) => {
      logger.info({
        status: 'succeeded',
        metadata: { body: response.body, statusCode: response.statusCode }
      })
    })
    .catch((err) => {
      logger.error({ status: 'failed', metadata: { err } })
      return handleError(err)
    })
    .then(response => callback(null, response))
}

export const register = (event, context, callback) => {
  const body = JSON.parse(event.body || JSON.stringify({}))
  const logger = makeLogger({ operation: 'register' }, { id: defaultCuidValue('req_')() })
  const { boleto_id, sqsMessage } = body

  // eslint-disable-next-line
  const removeBoletoFromQueueConditionally = (boleto) => {
    if (boleto.status === 'registered' || boleto.status === 'refused') {
      logger.info({ subOperation: 'removeFromQueue', status: 'started', metadata: { boleto_id: boleto.id } })
      return BoletosToRegisterQueue.remove(sqsMessage)
        .then(() => {
          logger.info({ subOperation: 'removeFromQueue', status: 'succeeded', metadata: { boleto_id: boleto.id } })
        })
        .catch((err) => {
          logger.info({ subOperation: 'removeFromQueue', status: 'failed', metadata: { boleto_id: boleto.id, err } })
          throw err
        })
    }
  }

  // eslint-disable-next-line
  const sendMessageToUserQueueConditionally = (boleto) => {
    if (boleto.status === 'registered' || boleto.status === 'refused') {
      logger.info({ subOperation: 'sendToUserQueue', status: 'started', metadata: { boleto_id: boleto.id } })

      const params = {
        MessageBody: JSON.stringify({
          boleto_id: boleto.id,
          status: boleto.status
        }),
        QueueUrl: boleto.queue_url
      }

      return sqs.sendMessage(params).promise()
        .then(() => {
          logger.info({ subOperation: 'sendToUserQueue', status: 'succeeded', metadata: { boleto_id: boleto.id } })
        })
        .catch((err) => {
          logger.info({ subOperation: 'sendToUserQueue', status: 'failed', metadata: { boleto_id: boleto.id, err } })
          throw err
        })
    }
  }

  logger.info({ status: 'started', metadata: { body } })

  boletoService.registerById(boleto_id)
    .tap(removeBoletoFromQueueConditionally)
    .tap(sendMessageToUserQueueConditionally)
    .tap((response) => {
      logger.info({
        status: 'succeeded',
        metadata: { body: response.body, statusCode: response.statusCode }
      })
    })
    .catch((err) => {
      logger.error({ status: 'failed', metadata: { err } })
      callback(err)
    })
    .then(boleto => callback(null, boleto))
}

export const update = (event, context, callback) => {
  const body = JSON.parse(event.body || JSON.stringify({}))
  const { bank_response_code, paid_amount } = body
  const id = path(['pathParameters', 'id'], event)

  Promise.resolve({ id, bank_response_code, paid_amount })
    .then(parse(updateSchema))
    .then(boletoService.update)
    .then(Boleto.buildResponse)
    .then(buildSuccessResponse(200))
    .catch(handleError)
    .then(response => callback(null, response))
}

export const index = (event, context, callback) => {
  const page = path(['queryStringParameters', 'page'], event)
  const count = path(['queryStringParameters', 'count'], event)

  Promise.resolve({ page, count })
    .then(boletoService.index)
    .then(buildSuccessResponse(200))
    .catch(handleError)
    .then(response => callback(null, response))
}

export const show = (event, context, callback) => {
  const id = path(['pathParameters', 'id'], event)

  Promise.resolve(id)
    .then(boletoService.show)
    .then(buildSuccessResponse(200))
    .catch(handleError)
    .then(response => callback(null, response))
{'fraction_non_alphanumeric': 0.15239027573829336, 'fraction_numerical': 0.0034263338228095936, 'mean_word_length': 3.2699407871821666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, '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': '4014268', 'n_tokens_mistral': 3868, 'n_tokens_neox': 3764, 'n_words': 803}