Skip to content

Commit

Permalink
create Core.print_invoices/3 and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryldev committed Feb 7, 2024
1 parent f81eb7c commit 2e06ac4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/telephony/core.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ defmodule Telephony.Core do
|> maybe_print_invoice(year, month)
end

def print_invoices(subscribers, year, month) do
Enum.reduce(subscribers, [], fn subscriber, acc ->
case maybe_print_invoice(subscriber, year, month) do
{:error, _message} -> acc
invoice -> acc ++ [invoice]
end
end)
end

defp maybe_print_invoice({:error, _message} = err, _year, _month) do
err
end
Expand Down
67 changes: 67 additions & 0 deletions test/telephony/core_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,71 @@ defmodule Telephony.CoreTest do
assert expected == result
end
end

describe "print_invoices/3" do
setup do
date = NaiveDateTime.utc_now()
last_month = NaiveDateTime.add(date, -31, :day)
two_months_ago = NaiveDateTime.add(last_month, -31, :day)

subscribers = [
%Subscriber{
full_name: "John Doe",
phone: "1234567890",
type: %Core.Prepaid{
credits: 213,
recharges: [
%Core.Recharge{value: 100, date: date},
%Core.Recharge{value: 100, date: last_month},
%Core.Recharge{value: 100, date: two_months_ago}
]
},
calls: [
%Core.Call{time_spent: 10, date: date},
%Core.Call{time_spent: 20, date: last_month},
%Core.Call{time_spent: 30, date: two_months_ago}
]
}
]

{:ok,
subscribers: subscribers,
date: date,
last_month: last_month,
two_months_ago: two_months_ago}
end

test "with valid params", %{
subscribers: subscribers,
date: date,
last_month: last_month,
two_months_ago: two_months_ago
} do
expected = [
%{
subscriber: %Telephony.Core.Subscriber{
full_name: "John Doe",
phone: "1234567890",
type: %Telephony.Core.Prepaid{
credits: 213,
recharges: [
%Telephony.Core.Recharge{value: 100, date: date},
%Telephony.Core.Recharge{value: 100, date: last_month},
%Telephony.Core.Recharge{value: 100, date: two_months_ago}
]
},
calls: [
%Telephony.Core.Call{time_spent: 10, date: date},
%Telephony.Core.Call{time_spent: 20, date: last_month},
%Telephony.Core.Call{time_spent: 30, date: two_months_ago}
]
},
invoice: %{calls: [], credits: 213, recharges: []}
}
]

result = Core.print_invoices(subscribers, 2021, 1)
assert expected == result
end
end
end

0 comments on commit 2e06ac4

Please sign in to comment.