Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajustes de padronizações #31

Merged
merged 5 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This rules are based on [this document](http://177.153.6.25/ercompany.com.br/bol
```elixir
def deps do
[
{:bran, "~> 0.2.0"}
{:bran, "~> 1.0.0"}
]
end
```
10 changes: 5 additions & 5 deletions lib/banks/c6.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ defmodule BRAN.Banks.C6 do
{:ok, :valid}
"""

@spec validate([Integer.t()], [Integer.t()], Integer.t() | String.t()) ::
@spec validate([integer()], [integer()], integer() | String.t()) ::
{:error, :not_valid} | {:ok, :valid}
def validate(agency_code, account_number, digit) do
agency_code_string =
agency_code
def validate(bank_branch, account_number, digit) do
bank_branch_string =
bank_branch
|> Enum.map(&Integer.to_string/1)
|> Enum.join()

with true <- Enum.all?(account_number, &validate_numeric_digit?/1) || {:error, :not_valid},
{:ok, :valid} <- validate_agency_by_example(agency_code_string),
{:ok, :valid} <- validate_branch_by_example(bank_branch_string),
{:ok, parsed_digit} <- validate_numeric_digit?(digit) do
account_number
|> fill_with_zeros()
Expand Down
14 changes: 7 additions & 7 deletions lib/banks/itau.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ defmodule BRAN.Banks.Itau do
@weigths [2, 1]

@doc """
Returns a tuple, after checking if the combination of agency_number, account_number and digit is valid
Returns a tuple, after checking if the combination of branch_number, account_number and digit is valid

##Examples
iex> BRAN.Itau.validate([2,5,4,5], [0,2,3,6,6], 1)
iex> BRAN.Banks.Itau.validate([2,5,4,5], [0,2,3,6,6], 1)
{:ok, :valid}
"""

@spec validate([Integer.t()], Integer.t(), Integer.t() | String.t()) ::
{:error, :invalid_account_number_length | :invalid_agency_code_length | :not_valid}
@spec validate([integer()], [integer()], integer() | String.t()) ::
{:error, :invalid_account_number_length | :invalid_bank_branch_length | :not_valid}
| {:ok, :valid}
def validate(agency_code, account_number, digit) do
with {:ok, :valid} <- validate_agency_code(agency_code),
def validate(bank_branch, account_number, digit) do
with {:ok, :valid} <- validate_bank_branch(bank_branch),
{:ok, :valid} <- validate_account_number(account_number),
{:ok, parsed_digit} <- validate_numeric_digit?(digit) do
(agency_code ++ account_number)
(bank_branch ++ account_number)
|> DigitCalculator.mod(10, @weigths, &sum_digits/1)
|> case do
^parsed_digit ->
Expand Down
16 changes: 8 additions & 8 deletions lib/banks/nubank.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ defmodule BRAN.Banks.Nubank do
"""

@doc """
Returns a tuple, after checking if the combination of agency_number, account_number and digit is valid
Returns a tuple, after checking if the combination of branch_number, account_number and digit is valid

##Examples
iex> BRAN.Banks.Nubank.validate([0,0,0,1], [5,2,1,6,1,2,5], 0)
{:ok, :valid}
"""

@spec validate([Integer.t()], [Integer.t()], Integer.t() | String.t()) ::
{:error, :invalid_account_number_length | :invalid_agency_code_length | :not_valid}
@spec validate([integer()], [integer()], integer() | String.t()) ::
{:error, :invalid_account_number_length | :invalid_bank_branch_length | :not_valid}
| {:ok, :valid}
def validate(agency_code, account_number, digit) do
def validate(bank_branch, account_number, digit) do
with true <- Enum.all?(account_number, &validate_numeric_digit?/1) || {:error, :not_valid},
{:ok, parsed_account_number} <- validate_account_number(account_number),
{:ok, :valid} <- validate_agency(agency_code),
{:ok, :valid} <- validate_branch(bank_branch),
{:ok, parsed_digit} <- validate_numeric_digit?(digit) do
account_with_digit = String.to_integer("#{parsed_account_number}#{parsed_digit}")

Expand All @@ -42,10 +42,10 @@ defmodule BRAN.Banks.Nubank do
|> validate_numeric_digit?()
end

defp validate_agency(agency_code) do
agency_code
defp validate_branch(bank_branch) do
bank_branch
|> Enum.map(&Integer.to_string/1)
|> Enum.join()
|> validate_agency_by_example()
|> validate_branch_by_example()
end
end
14 changes: 7 additions & 7 deletions lib/banks/santander.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ defmodule BRAN.Banks.Santander do
]

@doc """
Returns a tuple, after checking if the combination of agency_number, account_number and digit is valid
Returns a tuple, after checking if the combination of branch_number, account_number and digit is valid

##Examples
iex> BRAN.Santander.validate([2,5,4,5], [0,2,3,6,6,0,2,3], 1)
iex> BRAN.Banks.Santander.validate([0, 0, 9, 2], [4, 6, 5, 3, 5, 4, 9, 5], 0)
{:ok, :valid}
"""
@spec validate([Integer.t()], Integer.t(), Integer.t() | String.t()) ::
@spec validate([integer()], integer(), integer() | String.t()) ::
{:error,
:invalid_account_number_length
| :invalid_account_type
| :invalid_agency_code_length
| :invalid_bank_branch_length
| :not_valid}
| {:ok, :valid}
def validate(agency_code, account_number, digit) do
with {:ok, :valid} <- validate_agency_code(agency_code),
def validate(bank_branch, account_number, digit) do
with {:ok, :valid} <- validate_bank_branch(bank_branch),
{:ok, :valid} <- validate_account_number(account_number),
{:ok, :valid} <- validate_account_type(account_number),
{:ok, parsed_digit} <- validate_numeric_digit?(digit) do
(agency_code ++ [0, 0] ++ account_number)
(bank_branch ++ [0, 0] ++ account_number)
|> DigitCalculator.mod(10, @weigths)
|> case do
^parsed_digit ->
Expand Down
17 changes: 10 additions & 7 deletions lib/banks/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ defmodule BRAN.Banks.Util do
@moduledoc """
Documentation for `BRAN.Banks.Util`.
"""
def validate_agency_code(agency_code) do
if length(agency_code) == 4 do
@spec validate_bank_branch([integer()]) :: {:error, :invalid_bank_branch_length} | {:ok, :valid}
def validate_bank_branch(bank_branch) do
if length(bank_branch) == 4 do
{:ok, :valid}
else
{:error, :invalid_agency_code_length}
{:error, :invalid_bank_branch_length}
rafaelsiqueira marked this conversation as resolved.
Show resolved Hide resolved
end
end

def validate_agency_by_example(agency_code, agency_example \\ "0001") do
if agency_code == agency_example do
@spec validate_branch_by_example(String.t(), String.t()) ::
{:error, :invalid_bank_branch} | {:ok, :valid}
def validate_branch_by_example(bank_branch, branch_example \\ "0001") do
if bank_branch == branch_example do
{:ok, :valid}
else
{:error, :invalid_agency_code}
{:error, :invalid_bank_branch}
end
end

@spec validate_numeric_digit?(String.t() | Integer.t()) ::
@spec validate_numeric_digit?(String.t() | integer()) ::
{:not_valid, :non_numeric_digit} | {:ok, integer}
def validate_numeric_digit?(digit) when is_integer(digit), do: {:ok, digit}
def validate_numeric_digit?(nil), do: {:not_valid, :non_numeric_digit}
Expand Down
24 changes: 12 additions & 12 deletions lib/bran.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ defmodule BRAN do

@doc """
validate
Returns a tuple with {:ok, :valid} or {:error, :reason}, after checking if the combination of bank_code, agency_number, account_number and digit is valid
Returns a tuple with {:ok, :valid} or {:error, :reason}, after checking if the combination of bank_code, branch_number, account_number and digit is valid

## Examples
iex> BRAN.validate("341","2545", "02366", 1)
{:ok, :valid}
"""
@spec validate(String.t(), String.t(), String.t(), String.t() | Integer.t()) ::
@spec validate(String.t(), String.t(), String.t(), String.t() | integer()) ::
{:error,
:invalid_account_number_length
| :invalid_account_type
| :invalid_agency_code_length
| :invalid_bank_branch_length
| :not_supported
| :not_valid}
| {:ok, :valid}
def validate(bank_code, agency_code, account_number, digit) do
parsed_agency_code = parse_to_integer_list(agency_code)
def validate(bank_code, bank_branch, account_number, digit) do
parsed_bank_branch = parse_to_integer_list(bank_branch)

parsed_account_number = parse_to_integer_list(account_number)

case bank_code do
"033" ->
Santander.validate(parsed_agency_code, parsed_account_number, digit)
Santander.validate(parsed_bank_branch, parsed_account_number, digit)

"341" ->
Itau.validate(parsed_agency_code, parsed_account_number, digit)
Itau.validate(parsed_bank_branch, parsed_account_number, digit)

"336" ->
C6.validate(parsed_agency_code, parsed_account_number, digit)
C6.validate(parsed_bank_branch, parsed_account_number, digit)

"260" ->
Nubank.validate(parsed_agency_code, parsed_account_number, digit)
Nubank.validate(parsed_bank_branch, parsed_account_number, digit)

_ ->
{:error, :not_supported}
Expand All @@ -51,14 +51,14 @@ defmodule BRAN do
{:error,
:invalid_account_number_length
| :invalid_account_type
| :invalid_agency_code_length
| :invalid_bank_branch_length
| :not_supported
| :not_valid}
| {:ok, :valid}
def validate(bank_code, agency_code, account_with_digit) do
def validate(bank_code, bank_branch, account_with_digit) do
{account, digit} = split_account_and_digit(account_with_digit)

validate(bank_code, agency_code, account, digit)
validate(bank_code, bank_branch, account, digit)
end

defp split_account_and_digit(account_with_digit) do
Expand Down
2 changes: 1 addition & 1 deletion lib/digit_calculator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule BRAN.DigitCalculator do
@moduledoc """
Documentation for `BRAN.DigitCalculator`.
"""
@spec mod([Integer.t()], Integer.t(), [Integer.t()], Function.t()) :: Integer.t()
@spec mod([integer()], integer(), [integer()], function()) :: integer()
def mod(full_account_number, mod_factor, weights, sum_digits \\ fn stream -> stream end) do
full_account_number
|> calc_numbers(weights, sum_digits)
Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule BRAN.MixProject do
def project do
[
app: :bran,
version: "0.2.0",
version: "1.0.0",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand All @@ -26,11 +26,11 @@ defmodule BRAN.MixProject do
]
end

defp description() do
defp description do
"Elixir package to validate bank account numbers of Brazilian banks."
end

defp package() do
defp package do
[
name: "bran",
links: %{"GitHub" => "https://github.com/Xerpa/bran_checker"},
Expand Down
18 changes: 10 additions & 8 deletions test/bank_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ defmodule BRANTest do
use ExUnit.Case
use ExUnit.Parameterized

doctest BRAN

describe "validate/3" do
test_with_params "returns {:ok, :valid} for valid accounts and digits with hyphen",
fn bank_code, agency, account ->
assert BRAN.validate(bank_code, agency, account) == {:ok, :valid}
fn bank_code, branch, account ->
assert BRAN.validate(bank_code, branch, account) == {:ok, :valid}
end do
[
{"341", "7062", "14945-0"},
Expand All @@ -25,8 +27,8 @@ defmodule BRANTest do
end

test_with_params "returns {:ok, :valid} for valid accounts and digits with no hyphen",
fn bank_code, agency, account ->
assert BRAN.validate(bank_code, agency, account) == {:ok, :valid}
fn bank_code, branch, account ->
assert BRAN.validate(bank_code, branch, account) == {:ok, :valid}
end do
[
{"341", "7062", "149450"},
Expand All @@ -48,8 +50,8 @@ defmodule BRANTest do

describe "validate/4" do
test_with_params "returns {:ok, :valid} when account is valid",
fn bank_code, agency, account, digit ->
assert BRAN.validate(bank_code, agency, account, digit) == {:ok, :valid}
fn bank_code, branch, account, digit ->
assert BRAN.validate(bank_code, branch, account, digit) == {:ok, :valid}
end do
[
{"341", "7062", "14945", 0},
Expand All @@ -69,8 +71,8 @@ defmodule BRANTest do
end

test_with_params "returns {:error, :not_valid} when account isn't valid",
fn bank_code, agency, account, digit ->
assert BRAN.validate(bank_code, agency, account, digit) ==
fn bank_code, branch, account, digit ->
assert BRAN.validate(bank_code, branch, account, digit) ==
{:error, :not_valid}
end do
[
Expand Down
18 changes: 10 additions & 8 deletions test/banks/c6_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ defmodule BRAN.Banks.C6Test do
use ExUnit.Case
use ExUnit.Parameterized

doctest BRAN.Banks.C6

describe "BRAN.Banks.C6Test" do
test_with_params "should return valid tuple when the account is valid",
fn agency, account, digit ->
assert C6.validate(agency, account, digit) == {:ok, :valid}
fn branch, account, digit ->
assert C6.validate(branch, account, digit) == {:ok, :valid}
end do
[
{[0, 0, 0, 1], [1, 5, 3, 8, 5, 6, 8], 0},
Expand Down Expand Up @@ -60,10 +62,10 @@ defmodule BRAN.Banks.C6Test do
]
end

test_with_params "should return :invalid_agency_code tuple when the agency is not valid",
fn agency, account, digit ->
assert C6.validate(agency, account, digit) ==
{:error, :invalid_agency_code}
test_with_params "should return :invalid_bank_branch tuple when the branch is not valid",
fn branch, account, digit ->
assert C6.validate(branch, account, digit) ==
{:error, :invalid_bank_branch}
end do
[
{[4, 3, 1, 3], [4, 3, 1, 2, 9], 9},
Expand All @@ -80,8 +82,8 @@ defmodule BRAN.Banks.C6Test do
end

test_with_params "should return :not_valid tuple when the account is not valid",
fn agency, account, digit ->
assert C6.validate(agency, account, digit) == {:error, :not_valid}
fn branch, account, digit ->
assert C6.validate(branch, account, digit) == {:error, :not_valid}
end do
[
{[0, 0, 0, 1], [4, 3, 1, 2, 9], 9},
Expand Down
18 changes: 10 additions & 8 deletions test/banks/itau_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ defmodule BRAN.Banks.ItauTest do
use ExUnit.Case
use ExUnit.Parameterized

doctest BRAN.Banks.Itau

describe "BRAN.Banks.ItauTest" do
test_with_params "should return valid tuple when the account is valid",
fn agency, account, digit ->
assert Itau.validate(agency, account, digit) == {:ok, :valid}
fn branch, account, digit ->
assert Itau.validate(branch, account, digit) == {:ok, :valid}
end do
[
{[4, 3, 1, 3], [4, 3, 1, 2, 9], 0},
Expand All @@ -34,8 +36,8 @@ defmodule BRAN.Banks.ItauTest do
end

test_with_params "should return not_valid tuple when the account is valid",
fn agency, account, digit ->
assert Itau.validate(agency, account, digit) == {:error, :not_valid}
fn branch, account, digit ->
assert Itau.validate(branch, account, digit) == {:error, :not_valid}
end do
[
{[4, 3, 1, 3], [4, 3, 1, 2, 9], 9},
Expand All @@ -62,16 +64,16 @@ defmodule BRAN.Banks.ItauTest do
end

test(
"should return invalid_agency_code_length tuple when the agency number have less than 4 digits"
"should return invalid_bank_branch_length tuple when the branch number have less than 4 digits"
) do
assert Itau.validate([2, 5, 4], [0, 2, 3, 6, 6], 1) == {:error, :invalid_agency_code_length}
assert Itau.validate([2, 5, 4], [0, 2, 3, 6, 6], 1) == {:error, :invalid_bank_branch_length}
end

test(
"should return invalid_agency_code_length tuple when the agency number have more than 4 digits"
"should return invalid_bank_branch_length tuple when the branch number have more than 4 digits"
) do
assert Itau.validate([2, 5, 4, 5, 5], [0, 2, 3, 6, 6], 1) ==
{:error, :invalid_agency_code_length}
{:error, :invalid_bank_branch_length}
end

test(
Expand Down
Loading