Search code examples
elixirphoenix-frameworkecto

How to get Phoenix to render multiple views in one call to render_many, for non related schemas


Good Afternoon, I have an Elixir Phoenix API and I receive a Json with multiple data from two tables, Quota and clientsQuota, I managed to insert both tables using Ecto.Repo.insert_many/3 function. but I need to render both data in one json in one view, the data is as follows:

    [
  %{
    balance_sub_code: "123456789ABCDEF",
    bill_number: "0",
    delayed_quota_history_days: 0,
    due_date: ~U[2020-03-18 00:00:00Z],
    inte_adjust: "A",
    loan_code: "999-399-06-00-00001",
    payment_date: ~U[1970-01-01 00:00:00Z],
    plan_number: 0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_quota_state: "UNKNOWN",
    previous_state_sub_code: "UNKNOWN",
    prevision_amount: 0,
    quota_delayed_days: 0,
    quota_sec: 1,
    quota_state: "VIGENTE",
    start_date: ~U[2020-03-06 00:00:00Z]
  },
  %{
    balance_sub_code: "123456789ABCDEF",
    bill_number: "0",
    delayed_quota_history_days: 0,
    due_date: ~U[2020-03-25 00:00:00Z],
    inte_adjust: "A",
    loan_code: "999-399-06-00-00001",
    payment_date: ~U[1970-01-01 00:00:00Z],
    plan_number: 0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_quota_state: "UNKNOWN",
    previous_state_sub_code: "UNKNOWN",
    prevision_amount: 0,
    quota_delayed_days: 0,
    quota_sec: 2,
    quota_state: "APROBADO",
    start_date: ~U[2020-03-18 00:00:00Z]
  },
  %{
    balance_sub_code: "123456789ABCDEF",
    bill_number: "0",
    delayed_quota_history_days: 0,
    due_date: ~U[2020-03-18 00:00:00Z],
    inte_adjust: "A",
    loan_code: "999-399-06-00-00002",
    payment_date: ~U[1970-01-01 00:00:00Z],
    plan_number: 0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_quota_state: "UNKNOWN",
    previous_state_sub_code: "UNKNOWN",
    prevision_amount: 0,
    quota_delayed_days: 0,
    quota_sec: 1,
    quota_state: "VIGENTE",
    start_date: ~U[2020-03-06 00:00:00Z]
  },
  %{
    balance_sub_code: "123456789ABCDEF",
    bill_number: "0",
    delayed_quota_history_days: 0,
    due_date: ~U[2020-03-25 00:00:00Z],
    inte_adjust: "A",
    loan_code: "999-399-06-00-00002",
    payment_date: ~U[1970-01-01 00:00:00Z],
    plan_number: 0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_quota_state: "UNKNOWN",
    previous_state_sub_code: "UNKNOWN",
    prevision_amount: 0,
    quota_delayed_days: 0,
    quota_sec: 2,
    quota_state: "APROBADO",
    start_date: ~U[2020-03-18 00:00:00Z]
  }
]

and for ClientsQuota is as follows:

    [
  %{
    calc_number_days: 12,
    concept_code: "CAPI",
    concept_state: "VIGENTE",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 22.77,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00001",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 22.77,
    quota_sec: 1,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 12,
    concept_code: "INTE",
    concept_state: "VIGENTE",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00001",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 5.63,
    quota_sec: 1,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 12,
    concept_code: "IVAIT",
    concept_state: "VIGENTE",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00001",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 0.9,
    quota_sec: 1,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 7,
    concept_code: "CAPI",
    concept_state: "APROBADO",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 27.23,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00001",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 27.23,
    quota_sec: 2,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 7,
    concept_code: "INTE",
    concept_state: "APROBADO",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00001",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 1.79,
    quota_sec: 2,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 7,
    concept_code: "IVAIT",
    concept_state: "APROBADO",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00001",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 0.29,
    quota_sec: 2,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 12,
    concept_code: "CAPI",
    concept_state: "VIGENTE",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 22.77,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00002",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 22.77,
    quota_sec: 1,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 12,
    concept_code: "INTE",
    concept_state: "VIGENTE",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00002",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 5.63,
    quota_sec: 1,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 12,
    concept_code: "IVAIT",
    concept_state: "VIGENTE",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00002",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 0.9,
    quota_sec: 1,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 7,
    concept_code: "CAPI",
    concept_state: "APROBADO",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 27.23,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00002",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 27.23,
    quota_sec: 2,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 7,
    concept_code: "INTE",
    concept_state: "APROBADO",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00002",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 1.79,
    quota_sec: 2,
    user_code: "123456789ABCDEF"
  },
  %{
    calc_number_days: 7,
    concept_code: "IVAIT",
    concept_state: "APROBADO",
    condoned_amount: 0.0,
    debt_balance: 0.0,
    earned_amount: 0.0,
    last_payment_penalty: "A",
    loan_code: "999-399-06-00-00002",
    office_code: "Miio",
    paid_amount: 0.0,
    payment_date: ~U[1970-01-01 00:00:00Z],
    payment_sec: 0,
    plan_number: 0,
    previous_concept_state: "UNKNOWN",
    previous_earned_amount: 0.0,
    previous_paid_amount: 0.0,
    previous_payment_date: ~U[1970-01-01 00:00:01Z],
    previous_sec_payment: 0,
    quota_amount: 0.29,
    quota_sec: 2,
    user_code: "123456789ABCDEF"
  }
]

I tried this in View code but got Errors:

can not render function render_many.json in Cabbot.CreditsView.ex.

def render("render_many.json", [
        %{quota: quota},
        %{quota: quota},
        %{clients_quota: clients_quota},
        %{clients_quota: clients_quota},
        %{clients_quota: clients_quota},
        %{clients_quota: clients_quota},
        %{clients_quota: clients_quota},
        %{clients_quota: clients_quota}
      ]) do
    %{data: render_many(quota, CreditsView, "credits_quota.json")}
    %{data: render_many(clients_quota, CreditsView, "credits_quota.json")}
  end

Any help will be deeply apreciated, Thanks in advance.


Solution

  • A repetitive argument in the function clause results in pattern matching on this argument.

    defmodule Test do
      def foo(a, a), do: :same
      def foo(_, _), do: :diff
    end
    
    Test.foo(42, 42) #⇒ :same
    Test.foo(41, 42) #⇒ :diff
    

    That said, your function is unlikely to be ever called. What you need instead, would be to split the input with Enum.split_with/2 somewhat like below

    def render("render_many.json", input) do
      {quota, clients_quota} = Enum.split_with(input, fn
        %{quota: _} -> true
        %{clients_quota: _} -> false
      end)
    
      # do whatever you need with the outcome here
    end