<commit_before>const config = require('../../../knexfile').extweb
const knex = require('knex')(config)

module.exports = function (reference, eligibiltyId, claimId) {
  return knex.raw(`SELECT * FROM [IntSchema].[getClaimExpenseByIdOrLastApproved] (?, ?, ?)`, [ reference, eligibiltyId, claimId ])
  .then(function (claimExpenses) {
    claimExpenses.forEach(function (expense) {
      if (!expense.Cost) {
        expense.Cost = '0'
      } else {
        expense.Cost = Number(expense.Cost).toFixed(2)
      }
    })

    return claimExpenses
  })
}
<commit_msg>Changed from stored function to knex query<commit_after>const config = require('../../../knexfile').extweb
const knex = require('knex')(config)
const log = require('../log')

module.exports = function (reference, eligibiltyId, claimId) {
  console.log(
    knex('IntSchema.ClaimExpense AS ClaimExpense')
      .select('ClaimExpense.ClaimExpenseId', 'ClaimExpense.ExpenseType', 'ClaimExpense.Cost AS RequestedCost', 'ClaimExpense.ApprovedCost AS Cost', 'ClaimExpense.TravelTime', 'ClaimExpense.From', 'ClaimExpense.To', 'ClaimExpense.IsReturn', 'ClaimExpense.DurationOfTravel', 'ClaimExpense.TicketType', 'ClaimExpense.TicketOwner', 'ClaimExpense.Status', 'ClaimExpense.FromPostCode', 'ClaimExpense.ToPostCode', 'ClaimExpense.Distance', 'ClaimExpense.ReturnTime')
      .where(function () {
        this.where('ClaimExpense.ClaimId', claimId)
        .andWhere('ClaimExpense.Reference', reference)
        .orWhere(function () {
          this.whereRaw(claimId + ' IS NULL')
          .whereIn('ClaimExpense.ClaimId', function () {
            this.from('IntSchema.Claim AS Claim')
            .select('Claim.ClaimId')
            .where('Claim.Reference', reference)
            .andWhere('Claim.EligibilityId', eligibiltyId)
            .andWhere('Claim.Status', 'APPROVED')
            .limit(1)
            .orderBy('Claim.DateSubmitted','DESC')
          })
        })
      })
    .andWhere('ClaimExpense.IsEnabled', 1)
    .andWhere(function () {
      this.whereNull('ClaimExpense.Status')
      .orWhere('ClaimExpense.Status', '!=', 'REJECTED')
    }).toString()
  )
  //return knex.raw(`SELECT * FROM [IntSchema].[getClaimExpenseByIdOrLastApproved] (?, ?, ?)`, [ reference, eligibiltyId, claimId ])
  return knex('IntSchema.ClaimExpense AS ClaimExpense')
  .select('ClaimExpense.ClaimExpenseId', 'ClaimExpense.ExpenseType','ClaimExpense.Cost AS RequestedCost', 'ClaimExpense.ApprovedCost AS Cost', 'ClaimExpense.TravelTime', 'ClaimExpense.From', 'ClaimExpense.To', 'ClaimExpense.IsReturn', 'ClaimExpense.DurationOfTravel', 'ClaimExpense.TicketType', 'ClaimExpense.TicketOwner', 'ClaimExpense.Status', 'ClaimExpense.FromPostCode', 'ClaimExpense.ToPostCode', 'ClaimExpense.Distance', 'ClaimExpense.ReturnTime')
  .where(function () {
    this.where('ClaimExpense.ClaimId', claimId)
    .andWhere('ClaimExpense.Reference', reference)
    .orWhere(function () {
      this.whereRaw(claimId + ' IS NULL')
      .whereIn('ClaimExpense.ClaimId', function () {
        this.from('IntSchema.Claim AS Claim')
        .select('Claim.ClaimId')
        .where('Claim.Reference', reference)
        .andWhere('Claim.EligibilityId', eligibiltyId)
        .andWhere('Claim.Status', 'APPROVED')
        .limit(1)
        .orderBy('Claim.DateSubmitted','DESC')
      })
    })
  })
  .andWhere('ClaimExpense.IsEnabled', 1)
  .andWhere(function () {
    this.whereNull('ClaimExpense.Status')
    .orWhere('ClaimExpense.Status', '!=', 'REJECTED')
  })
  .then(function (claimExpenses) {
    log.info(claimExpenses)
    claimExpenses.forEach(function (expense) {
      if (!expense.Cost) {
        expense.Cost = '0'
      } else {
        expense.Cost = Number(expense.Cost).toFixed(2)
      }
    })

    return claimExpenses
  })
}

{'fraction_non_alphanumeric': 0.1565883284922102, 'fraction_numerical': 0.002112490097702667, 'mean_word_length': 3.6535626535626538, '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': '2890582', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1192, 'n_words': 178}