<commit_before><commit_msg>apvs-353 - added persistence for approving/rejecting claims<commit_after>const config = require('../../../knexfile').intweb
const knex = require('knex')(config)
const claimDecisionEnum = require('../../constants/claim-decision-enum')
const tasksEnum = require('../../constants/tasks-enum')
const insertTaskSendClaimNotification = require('./insert-task-send-claim-notification')

module.exports = function (claimId, claimResponse) {
  return knex('Claim').where('ClaimId', claimId).first('EligibilityId').then(function (result) {
    var eligibilityId = result.EligibilityId
    var reference = result.Reference
    var decision = claimResponse.decision
    var reason = claimResponse.reason
    var note = claimResponse.note

    return Promise.all([updateEligibility(eligibilityId, decision),
                        updateClaim(claimId, decision, reason, note),
                        sendClaimNotification(reference, eligibilityId, claimId, decision)])
  })
}

function updateEligibility (eligibilityId, decision) {
  return knex('Eligibility').where('EligibilityId', eligibilityId).update({
    'Status': decision
  })
}

function updateClaim (claimId, decision, reason, note) {
  return knex('Claim').where('ClaimId', claimId).update({
    'Status': decision,
    'Reason': reason,
    'Note': note
  })
}

function sendClaimNotification (reference, eligibilityId, claimId, decision) {
  var notificationType
  if (decision === claimDecisionEnum.APPROVED) {
    notificationType = tasksEnum.ACCEPT_CLAIM_NOTIFICATION
  } else if (decision === claimDecisionEnum.REJECTED) {
    notificationType = tasksEnum.REJECT_CLAIM_NOTIFICATION
  }

  return insertTaskSendClaimNotification(notificationType, reference, eligibilityId, claimId)
}
{'fraction_non_alphanumeric': 0.12231030577576443, 'fraction_numerical': 0.0016987542468856172, 'mean_word_length': 4.663461538461538, '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': '3301445', 'n_tokens_mistral': 501, 'n_tokens_neox': 475, 'n_words': 102}