Ruby set hash inside the Hash for the Array of Hashes

139 views Asked by At

I am working on Rails 6 API. This is what I get

 "data": [
    {
        "invoice_details": {
            "customer_name": "Dylan Sollfrank",
            "invoice_number": "1060",
            "invoice_status": "paid"
        }
    },
    {
        "transaction_number": "QB1589148496",
        "customer_name": "Freeman Sporting Goods:55 Twin Lane",
        "amount": {
            "amount_to_pay": 86.4,
            "payment_fee": 0.0
        },
        "created_time": "03:38 AM",
        "created_date": "May 11, 2020",
        "payment_method": "qb_payment",
        "payment_status": "completed"
    },

Following is my code

def get_payment_report_activity(invoice_transactions, timezone = Time.zone.name)
invoice_details = []
transaction_details = {}    
amount = {}
 invoice_transactions.group_by(&:paymentable_id).each do |key, transactions|          
  invoice = Invoice.find key
  invoice_details.push(invoice_details:{
  customer_name: invoice&.customer&.fully_qualified_name&.strip,
  invoice_number: invoice&.doc_number,
  invoice_status: invoice&.invoice_status
  })         
  transactions.each do |transaction|
    customer = transaction&.paymentable&.customer      
    amount[:amount_to_pay] = transaction&.amount_to_pay.to_f
    amount[:payment_fee] = transaction&.payment_fee.to_f                
    transaction_details[:transaction_number] = transaction&.transaction_number
    transaction_details[:customer_name] = customer&.fully_qualified_name&.strip
    transaction_details[:amount] = amount
    transaction_details[:created_time] = Customer.time_format(transaction.created_at.in_time_zone(timezone))
    transaction_details[:created_date] = Customer.date_format(transaction.created_at.in_time_zone(timezone))
    transaction_details[:payment_method] = transaction&.payment_method
    transaction_details[:payment_status] = transaction&.payment_status        
  end
  invoice_details << transaction_details
 end    
 invoice_details
end

Now I need the hash transaction details inside the invoice_details hash label as transaction_details and there can be multiple transaction details inside the invoice_details

"data": [
{
    "invoice_details": {
        "customer_name": "Dylan Sollfrank",
        "invoice_number": "1060",
        "invoice_status": "paid",
        "transaction_details: [{
           "transaction_number": "QB1589148496",
           "customer_name": "Freeman Sporting Goods:55 Twin Lane",
           "amount": {
               "amount_to_pay": 86.4,
               "payment_fee": 0.0
           },
           "created_time": "03:38 AM",
           "created_date": "May 11, 2020",
           "payment_method": "qb_payment",
           "payment_status": "completed"                               
        },
        {
           "transaction_number": "QB1589148496",
           "customer_name": "Freeman Sporting Goods:55 Twin Lane",
           "amount": {
               "amount_to_pay": 86.4,
               "payment_fee": 0.0
           },
           "created_time": "03:38 AM",
           "created_date": "May 11, 2020",
           "payment_method": "qb_payment",
           "payment_status": "completed"                               
        }]

    },
      "invoice_details": {
        "customer_name": "Dylan Sollfrank",
        "invoice_number": "1060",
        "invoice_status": "paid",
        "transaction_details : {
           "transaction_number": "QB1589148496",
           "customer_name": "Freeman Sporting Goods:55 Twin Lane",
           "amount": {
               "amount_to_pay": 86.4,
               "payment_fee": 0.0
           },
           "created_time": "03:38 AM",
           "created_date": "May 11, 2020",
           "payment_method": "qb_payment",
           "payment_status": "completed"
                           
     }
},
    
}
1

There are 1 answers

0
Palash Bera On

you can try like this:

def get_payment_report_activity(invoice_transactions, timezone = Time.zone.name)
  invoice_details = []

  invoice_transactions.group_by(&:paymentable_id).each do |key, transactions|
    invoice = Invoice.find key
    transaction_details = []

    transactions.each do |transaction|
      transaction_hash = {}
      amount_hash = {}

      customer = transaction&.paymentable&.customer

      amount_hash[:amount_to_pay] = transaction&.amount_to_pay.to_f
      amount_hash[:payment_fee] = transaction&.payment_fee.to_f
      transaction_hash[:transaction_number] = transaction&.transaction_number
      transaction_hash[:customer_name] = customer&.fully_qualified_name&.strip
      transaction_hash[:created_time] = Customer.time_format(transaction.created_at.in_time_zone(timezone))
      transaction_hash[:created_date] = Customer.date_format(transaction.created_at.in_time_zone(timezone))
      transaction_hash[:payment_method] = transaction&.payment_method
      transaction_hash[:payment_status] = transaction&.payment_status
      transaction_hash[:amount] = amount_hash

      transaction_details << transaction_hash
    end

    invoice_details.push(invoice_details: {
      customer_name: invoice&.customer&.fully_qualified_name&.strip,
      invoice_number: invoice&.doc_number,
      invoice_status: invoice&.invoice_status,
      transaction_details: transaction_details
    })
  end

  invoice_details
end