Skip to content
This repository was archived by the owner on Jun 11, 2023. It is now read-only.

Commit 23580b0

Browse files
Marcelo Amancio de Lima Santosthemaxhero
authored andcommitted
update BankAccount open and close tests
1 parent 1a2e58e commit 23580b0

24 files changed

Lines changed: 340 additions & 208 deletions

File tree

lib/core/validator/validator.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Helix.Core.Validator do
99

1010
@regex_hostname ~r/^[a-zA-Z0-9-_.@#]{1,20}$/
1111

12-
@regex_token ~r/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
12+
@uuid ~r/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
1313

1414
@spec validate_input(input :: String.t, input_type, opts :: term) ::
1515
{:ok, validated_input :: String.t}
@@ -55,7 +55,7 @@ defmodule Helix.Core.Validator do
5555
defp validate_token(v) when not is_binary(v),
5656
do: :error
5757
defp validate_token(v) do
58-
if Regex.match?(@regex_token, v) do
58+
if Regex.match?(@uuid, v) do
5959
{:ok, v}
6060
else
6161
:error
@@ -73,4 +73,5 @@ defmodule Helix.Core.Validator do
7373

7474
defp validate_money(v),
7575
do: validate_hostname(v)
76+
7677
end

lib/event/notificable/notificable.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ defprotocol Helix.Event.Notificable do
104104
@type whom_to_notify ::
105105
%{
106106
optional(:server) => [Server.id],
107-
optional(:account) => [Account.id]
107+
optional(:account) => [Account.id],
108+
optional(:bank_acc) => [term]
108109
}
109110

110111
@spec generate_payload(event :: struct, Socket.t) ::

lib/event/notification_handler.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Helix.Event.NotificationHandler do
1212
alias Helix.Universe.Bank.Model.BankAccount
1313

1414
@type channel_account_id :: Account.id | Entity.id
15+
@type channel_bank_id :: {ATM.id, BankAccount.account}
1516

1617
@doc """
1718
Handler responsible for guiding the event through the Notificable flow. It
@@ -36,6 +37,18 @@ defmodule Helix.Event.NotificationHandler do
3637
@spec channel_mapper(Notificable.whom_to_notify) ::
3738
channels :: [String.t]
3839
defp channel_mapper(whom_to_notify, acc \\ [])
40+
defp channel_mapper(notify = %{bank_acc: bank_accs}, acc) do
41+
acc =
42+
bank_accs
43+
|> Utils.ensure_list()
44+
|> Enum.uniq()
45+
|> get_bank_channels()
46+
|> List.flatten()
47+
|> Kernel.++(acc)
48+
49+
channel_mapper(Map.delete(notify, :bank_acc), acc)
50+
end
51+
3952
defp channel_mapper(notify = %{server: servers}, acc) do
4053
acc =
4154
servers
@@ -96,6 +109,13 @@ defmodule Helix.Event.NotificationHandler do
96109
defp get_account_channels(account_id),
97110
do: ["account:" <> to_string(account_id)]
98111

112+
@spec get_bank_channels([channel_bank_id] | channel_bank_id) ::
113+
channels :: [String.t]
114+
defp get_bank_channels(bank_accs) when is_list(bank_accs),
115+
do: Enum.map(bank_accs, &get_bank_channels/1)
116+
defp get_bank_channels({atm_id, account_number}),
117+
do: ["bank:" <> to_string(account_number) <> "@" <> to_string(atm_id)]
118+
99119
defp concat(a, b),
100120
do: a <> to_string(b)
101121
end

lib/universe/bank/action/bank.ex

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,26 @@ defmodule Helix.Universe.Bank.Action.Bank do
105105
to: BankTransferInternal,
106106
as: :abort
107107

108-
@spec open_account(Account.idt, ATM.id) ::
108+
@spec open_account(Account.id, ATM.id) ::
109109
{:ok, BankAccount.t, [BankAccountUpdatedEvent.t]}
110-
| {:error, Ecto.Changeset.t}
110+
| {:error, :internal}
111111
@doc """
112112
Opens a bank account.
113113
"""
114-
def open_account(owner, atm) do
115-
bank =
116-
atm
114+
def open_account(owner, atm_id) do
115+
bank_id =
116+
atm_id
117117
|> EntityQuery.fetch_by_server()
118118
|> Map.get(:entity_id)
119119
|> NPCQuery.fetch()
120+
|> Map.get(:npc_id)
120121

121-
case BankAccountInternal.create(owner, atm, bank) do
122+
case BankAccountInternal.create(owner, atm_id, bank_id) do
122123
{:ok, bank_acc} ->
123124
{:ok, bank_acc, [BankAccountUpdatedEvent.new(bank_acc, :created)]}
124125

125-
error ->
126-
error
126+
{:error, _} ->
127+
{:error, :internal}
127128
end
128129
end
129130

@@ -225,12 +226,12 @@ defmodule Helix.Universe.Bank.Action.Bank do
225226
end
226227
end
227228

228-
@spec change_password(BankAccount.t, Entity.id) ::
229+
@spec change_password(BankAccount.t) ::
229230
{:ok, BankAccount.t, [BankAccountPasswordChangedEvent.t]}
230231
| {:error, :internal}
231-
def change_password(account, changed_by) do
232+
def change_password(account) do
232233
with {:ok, account} <- update_password(account) do
233-
event = BankAccountPasswordChangedEvent.new(account, changed_by)
234+
event = BankAccountPasswordChangedEvent.new(account)
234235

235236
{:ok, account, [event]}
236237
else

lib/universe/bank/action/flow/bank_account.ex

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
33
import HELF.Flow
44

55
alias Helix.Event
6+
alias Helix.Entity.Model.Entity
67
alias Helix.Entity.Query.Entity, as: EntityQuery
78
alias Helix.Network.Action.Flow.Tunnel, as: TunnelFlow
89
alias Helix.Network.Query.Network, as: NetworkQuery
@@ -15,6 +16,8 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
1516

1617
alias Helix.Universe.Bank.Process.Bank.Account.AccountCreate,
1718
as: BankAccountCreateProcess
19+
alias Helix.Universe.Bank.Process.Bank.Account.AccountClose,
20+
as: BankAccountCloseProcess
1821
alias Helix.Universe.Bank.Process.Bank.Account.RevealPassword,
1922
as: BankAccountRevealPasswordProcess
2023
alias Helix.Universe.Bank.Process.Bank.Account.ChangePassword,
@@ -62,29 +65,36 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
6265
Starts the `bank_account_create` process.
6366
"""
6467
def open(gateway, account_id, atm, relay) do
65-
entity_id = Entity.ID.cast!(to_string(account_id.id))
68+
entity_id = Entity.ID.cast!(to_string(account_id))
69+
atm_id = atm.server_id
6670

6771
meta = %{
68-
src_atm_id: atm.server_id,
72+
network_id: NetworkQuery.internet().network_id,
73+
bounce: nil,
6974
source_entity_id: entity_id
7075
}
7176

72-
BankAccountCreateProcess.execute(gateway, atm, %{}, meta, relay)
77+
BankAccountCreateProcess.execute(gateway, atm, %{atm_id: atm_id}, meta, relay)
7378
end
7479

75-
@spec close(BankAccount.t) ::
80+
@spec close(Server.t, BankAccount.t, Server.t, relay) ::
7681
{:ok, Process.t}
7782
| BankAccountCloseProcess.executable_error
7883
@doc """
7984
Starts the `bank_account_close` process.
8085
"""
8186
def close(gateway, bank_account, atm, relay) do
8287
meta = %{
83-
src_atm_id: bank_account.server_id,
84-
src_account_number: bank_account.account_number
88+
network_id: NetworkQuery.internet().network_id,
89+
bounce: nil
90+
}
91+
92+
params = %{
93+
atm_id: bank_account.atm_id,
94+
account_number: bank_account.account_number
8595
}
8696

87-
BankAccountCloseProcess.execute(gateway, atm, %{}, meta, relay)
97+
BankAccountCloseProcess.execute(gateway, atm, params, meta, relay)
8898
end
8999

90100
@spec change_password(BankAccount.t, Server.t, Server.t, relay) ::

lib/universe/bank/event/account_close.ex

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ defmodule Helix.Universe.Bank.Event.AccountClose do
1717

1818
event_struct [:atm_id, :account_number]
1919

20-
@spec new(ATM.id, BankAccount.account) :: t
21-
22-
def new(atm_id, account_number) do
20+
@spec new(Process.t, AccountCloseProcess.t) :: t
21+
def new(process = %Process{}, data = %AccountCloseProcess{}) do
2322
%__MODULE__{
24-
atm_id: atm_id,
25-
account_number: account_number
23+
atm_id: data.atm_id,
24+
account_number: data.account_number
2625
}
2726
end
2827

29-
@spec new(Process.t, AccountCloseProcess.t) :: t
30-
def new(process = %Process{}, data = %AccountCloseProcess{}) do
28+
@spec new(ATM.id, BankAccount.account):: t
29+
30+
def new(atm_id, account_number) do
3131
%__MODULE__{
32-
atm_id: process.src_atm_id,
33-
account_number: process.src_acc_number
32+
atm_id: atm_id,
33+
account_number: account_number
3434
}
3535
end
3636
end
37+
end

lib/universe/bank/event/account_create.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ defmodule Helix.Universe.Bank.Event.AccountCreate do
3232
@spec new(Process.t, AccountCreateProcess.t) :: t
3333
def new(process = %Process{}, data = %AccountCreateProcess{}) do
3434
%__MODULE__{
35-
atm_id: process.src_atm_id,
35+
atm_id: process.data.atm_id,
3636
requester: process.source_entity_id
3737
}
3838
end
3939
end
40+
end

lib/universe/bank/event/bank/account.ex

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Helix.Universe.Bank.Event.Bank.Account do
1+
defmodule Helix.Universe.Bank.Event.Bank.Account do
22

33
import Helix.Event
44

@@ -101,25 +101,28 @@ defmodule Helix.Universe.Bank.Event.Bank.Account do
101101

102102
event Login do
103103

104+
alias Helix.Account.Model.Account
104105
alias Helix.Entity.Model.Entity
106+
alias Helix.Entity.Query.Entity, as: EntityQuery
105107
alias Helix.Universe.Bank.Model.BankAccount
106108
alias Helix.Universe.Bank.Model.BankToken
107109

108-
event_struct [:entity_id, :account, :token_id]
110+
event_struct [:entity_id, :notify_owner, :account, :token_id]
109111

110112
@type t :: %__MODULE__{
111113
entity_id: Entity.id,
112114
account: BankAccount.t,
113115
token_id: BankToken.id | nil
114116
}
115117

116-
@spec new(BankAccount.t, Entity.id) ::
118+
@spec new(BankAccount.t, Entity.idt) ::
117119
t
118120
def new(account = %BankAccount{}, entity_id, token_id \\ nil) do
119121
%__MODULE__{
120122
entity_id: entity_id,
121123
account: account,
122-
token_id: token_id
124+
token_id: token_id,
125+
notify_owner: (EntityQuery.fetch(entity_id).entity_type == :account)
123126
}
124127
end
125128

@@ -129,7 +132,7 @@ defmodule Helix.Universe.Bank.Event.Bank.Account do
129132
the local data.
130133
"""
131134

132-
@event :bank_login_event
135+
@event :bank_login
133136

134137
@doc false
135138
def generate_payload(event, _socket) do
@@ -145,9 +148,19 @@ defmodule Helix.Universe.Bank.Event.Bank.Account do
145148
end
146149

147150
def whom_to_notify(event) do
148-
%{
149-
account: event.entity_id
150-
}
151+
atm_id = event.account.atm_id
152+
bank_account = event.account.account_number
153+
154+
base_map =
155+
%{
156+
bank_acc: [{atm_id, bank_account}]
157+
}
158+
159+
if event.notify_owner do
160+
Map.merge(%{account: event.entity_id}, base_map)
161+
else
162+
base_map
163+
end
151164
end
152165
end
153166
end
@@ -175,7 +188,7 @@ defmodule Helix.Universe.Bank.Event.Bank.Account do
175188

176189
notify do
177190

178-
@event :bank_logout_event
191+
@event :bank_logout
179192

180193
def generate_payload(event, _socket) do
181194
data =

lib/universe/bank/event/bank/account/password.ex

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,61 @@ defmodule Helix.Universe.Bank.Event.Bank.Account.Password do
2222
account: account
2323
}
2424
end
25+
26+
notify do
27+
28+
@event :bank_password_revealed
29+
30+
@doc false
31+
def generate_payload(event, _socket) do
32+
data =
33+
%{
34+
atm_id: event.account.atm_id,
35+
account_number: event.account.account_number,
36+
password: event.account.password
37+
}
38+
end
39+
40+
def whom_to_notify(event),
41+
do: %{account: event.entity_id}
42+
end
2543
end
2644

2745
event Changed do
2846

29-
alias Helix.Entity.Model.Entity
30-
alias Helix.Universe.Bank.Model.BankAccount
47+
alias Helix.Entity.Model.Entity
48+
alias Helix.Universe.Bank.Model.BankAccount
3149

32-
@type t :: %__MODULE__{
33-
entity_id: Entity.id,
34-
account: BankAccount.t
35-
}
50+
@type t :: %__MODULE__{
51+
account: BankAccount.t
52+
}
3653

37-
event_struct [:entity_id, :account]
54+
event_struct [:account]
3855

39-
@spec new(BankAccount.t, Entity.id) ::
40-
t
41-
def new(account = %BankAccount{}, entity_id) do
42-
%__MODULE__{
43-
entity_id: entity_id,
44-
account: account
45-
}
46-
end
56+
@spec new(BankAccount.t) ::
57+
t
58+
def new(account = %BankAccount{}) do
59+
%__MODULE__{
60+
account: account
61+
}
62+
end
63+
64+
notify do
65+
66+
@event :bank_password_changed
67+
68+
@doc false
69+
def generate_payload(event, _socket) do
70+
data =
71+
%{
72+
atm_id: event.account.atm_id,
73+
account_number: event.account.account_number,
74+
password: event.account.password
75+
}
76+
end
77+
78+
def whom_to_notify(event),
79+
do: %{account: event.account.owner_id}
80+
end
4781
end
4882
end

0 commit comments

Comments
 (0)