<commit_before>const request = require('superagent')
const DataLoader = require('dataloader')
const wallet = {
  silver: 0,
  gold: 0,
  receipts: []
}

const init = (config, app) => {
  const api = config.e_wallet_api
  if (api === 'DEV') {
    global.NAP.Ewallet = {
      getEwallet: token => ({
        hasReceipt: async ({ refId, spendType }) => !!wallet.receipts.find(r => r === refId),
        getJelly: async () => ({ gold: wallet.gold, silver: wallet.silver }),
        spendJelly: async ({ refId, spendType, merchantId, merchantAliasId, amount, currencyType, commissionRate, payload }) => {
          console.log('Ewallet.DEV.spendJelly: ', { refId, spendType, merchantId, merchantAliasId, amount, currencyType, commissionRate, payload })
          if (currencyType === 'gold') {
            wallet.gold -= amount
          } else {
            wallet.silver -= amount
          }
          wallet.receipts.push(refId)
          return { gold: wallet.gold, silver: wallet.silver }
        }
        // addExchange: async () =>({token, amountIn, amountOut, conversionType, progressBarcode, status})
      })
    }
  } else {
    const chalk = require('chalk')
    global.NAP.Ewallet = {
      getEwallet: token => {
        const callApi = async (path, data = {}) => {
          console.log(chalk.yellow('Ewallet: ') + `External api call ${path}`, data)

          try {
            const result = await request
            .post(`${api}/v1/${path}`)
            .set('Content-Type', 'application/json')
            // add token to data
            .set('x-access-token', process.env.E_WALLET_ACCESS_TOKEN || 'undefined')
            .timeout({
              response: 5000
            })
            .send(Object.assign({
              token
            }, data))

            return result.body.data
          } catch (e) {
            console.log('init ewallet: error on call api')
            console.error(e)
          }
        }
        const callGetApi = async (path, data = {}) => {
          const result = await request
            .get(`${api}/v1/${path}`)
            .set('Content-Type', 'application/json')
            // add token to data
            .set('x-access-token', process.env.E_WALLET_ACCESS_TOKEN || '<PASSWORD>')
          return result.body.data
        }
        const hasReceipt = new DataLoader(keys => {
          return callApi('spend/hasReceipts', { receipts: keys })
        })
        return {
          hasReceipt: async ({ refId, spendType }) => {
            return hasReceipt.load({ refId, spendType })
          },
          hasCard: async ({ token }) => {
            const result = await callApi('creditcard/hasCard', { token })
            return result
          },
          deleteCard: async ({ token }) => {
            const result = await callApi('creditcard/deleteCard', { token })
            return result
          },
          getJelly: async () => {
            try {
              const result = await callApi('user/getJelly', { token })
              if (result.gold >= 0) { return result }
              return { gold: 0, silver: 0 }
            } catch (e) {
              console.log(e)
              return { gold: 0, silver: 0 }
            }
          },
          getMerchantEwallet: async () => {
            console.log('getMerchantEwallet')
            const result = await callApi('user/getMerchantEwallet', { token })
            try {<commit_msg>fix(server.initEwallet): dataloader apply cacheFn<commit_after>const request = require('superagent')
const DataLoader = require('dataloader')
const wallet = {
  silver: 0,
  gold: 0,
  receipts: []
}

const init = (config, app) => {
  const api = config.e_wallet_api
  if (api === 'DEV') {
    global.NAP.Ewallet = {
      getEwallet: token => ({
        hasReceipt: async ({ refId, spendType }) => !!wallet.receipts.find(r => r === refId),
        getJelly: async () => ({ gold: wallet.gold, silver: wallet.silver }),
        spendJelly: async ({ refId, spendType, merchantId, merchantAliasId, amount, currencyType, commissionRate, payload }) => {
          console.log('Ewallet.DEV.spendJelly: ', { refId, spendType, merchantId, merchantAliasId, amount, currencyType, commissionRate, payload })
          if (currencyType === 'gold') {
            wallet.gold -= amount
          } else {
            wallet.silver -= amount
          }
          wallet.receipts.push(refId)
          return { gold: wallet.gold, silver: wallet.silver }
        }
        // addExchange: async () =>({token, amountIn, amountOut, conversionType, progressBarcode, status})
      })
    }
  } else {
    const chalk = require('chalk')
    global.NAP.Ewallet = {
      getEwallet: token => {
        const callApi = async (path, data = {}) => {
          console.log(chalk.yellow('Ewallet: ') + `External api call ${path}`, data)

          try {
            const result = await request
            .post(`${api}/v1/${path}`)
            .set('Content-Type', 'application/json')
            // add token to data
            .set('x-access-token', process.env.E_WALLET_ACCESS_TOKEN || 'undefined')
            .timeout({
              response: 5000
            })
            .send(Object.assign({
              token
            }, data))

            return result.body.data
          } catch (e) {
            console.log('init ewallet: error on call api')
            console.error(e)
          }
        }
        const callGetApi = async (path, data = {}) => {
          const result = await request
            .get(`${api}/v1/${path}`)
            .set('Content-Type', 'application/json')
            // add token to data
            .set('x-access-token', process.env.E_WALLET_ACCESS_TOKEN || 'undefined')
          return result.body.data
        }
        const hasReceipt = new DataLoader(keys => {
          return callApi('spend/hasReceipts', { receipts: keys })
        }, {
          cacheKeyFn: ({ refId, spendType }) => `${spendType}/${refId}`
        })
        return {
          hasReceipt: async ({ refId, spendType }) => {
            return hasReceipt.load({ refId, spendType })
          },
          hasCard: async ({ token }) => {
            const result = await callApi('creditcard/hasCard', { token })
            return result
          },
          deleteCard: async ({ token }) => {
            const result = await callApi('creditcard/deleteCard', { token })
            return result
          },
          getJelly: async () => {
            try {
              const result = await callApi('user/getJelly', { token })
              if (result.gold >= 0) { return result }
              return { gold: 0, silver: 0 }
            } catch (e) {
              console.log(e)
              return { gold: 0, silver: 0 }
            }
          },
          getMerchantEwallet: async () => {
            console.log('getMerchantEwallet')
            const result = await callApi('user/getMerchantEwallet', { token })
            try {
{'fraction_non_alphanumeric': 0.13794092342988243, 'fraction_numerical': 0.003728133065672498, 'mean_word_length': 1.7888844462215114, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 39, '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': '5769351', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1980, 'n_words': 453}