text: import { audit } from '@core/services/routes/audit'
  import Refund from '@apps/finance/models/refund'
  import Credit from '@apps/finance/models/credit'
  
  export const refundCredit = async(req, { amount, order, payment, reason, registration }) => {
  
    const credit = await Credit.forge({
      team_id: req.team.get('id'),
      contact_id: payment.related('invoice').get('contact_id'),
      program_id: payment.related('invoice').get('program_id'),
      description: 'Refunded payment',
      amount
    }).save(null, {
      transacting: req.trx
    })
  
    const refund = await Refund.forge({
      team_id: req.team.get('id'),
      payment_id: payment.get('id'),
      credit_id: credit.get('id'),
      registration_id: registration ? registration.get('id') : null,
      order_id: order ? order.get('id') : null,
      status: 'credited',
      type: 'credit',
      reason,
      amount
    }).save(null, {
      transacting: req.trx
    })
  
    await audit(req, {
      story: 'created',
      auditable: credit
    })
  
    return refund
  
  }
  