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

Commit aa2a755

Browse files
author
Marcelo Amancio de Lima Santos
committed
WIP
1 parent 22b18a4 commit aa2a755

File tree

35 files changed

+779
-115
lines changed

35 files changed

+779
-115
lines changed

lib/account/websocket/channel/account.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ channel Helix.Account.Websocket.Channel.Account do
1414
alias Helix.Network.Websocket.Requests.Bounce.Remove, as: BounceRemoveRequest
1515
alias Helix.Software.Websocket.Requests.Virus.Collect, as: VirusCollectRequest
1616
alias Helix.Story.Websocket.Requests.Email.Reply, as: EmailReplyRequest
17-
alias Helix.Universe.Websocket.Channel.Requests.CreateAccount,
17+
alias Helix.Universe.Bank.Websocket.Requests.CreateAccount,
1818
as: BankAccountCreateRequest
1919

2020
@doc """

lib/core/validator/validator.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Helix.Core.Validator do
55
| :hostname
66
| :bounce_name
77
| :reply_id
8+
| :token
89

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

lib/event/notificable/notificable.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defprotocol Helix.Event.Notificable do
6363
action, also receive special treatment. Hence:
6464
6565
- third AT attack_target - Third party on victim's server
66-
- third AT attack_source - Third party on attacker's server.
66+
- third AT attack_source - Third party on attacker's server.
6767
- third AT random_server - Third party on unrelated (random) server.
6868
6969
Last but not least, in some cases we do not have an attacker/victim
@@ -98,11 +98,19 @@ defprotocol Helix.Event.Notificable do
9898

9999
alias Helix.Account.Model.Account
100100
alias Helix.Server.Model.Server
101+
alias Helix.Universe.Bank.Model.ATM
102+
alias Helix.Universe.Bank.Model.BankAccount
101103

102104
@type whom_to_notify ::
103105
%{
104106
optional(:server) => [Server.id],
105-
optional(:account) => [Account.id]
107+
optional(:account) => [Account.id],
108+
optional(:bank_account) => [
109+
%{
110+
atm_id: ATM.id,
111+
account_number: BankAccount.account
112+
}
113+
]
106114
}
107115

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

lib/event/notification_handler.ex

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ defmodule Helix.Event.NotificationHandler do
88
alias Helix.Entity.Model.Entity
99
alias Helix.Server.Model.Server
1010
alias Helix.Server.State.Websocket.Channel, as: ServerWebsocketChannelState
11+
alias Helix.Universe.Bank.Model.ATM
12+
alias Helix.Universe.Bank.Model.BankAccount
1113

1214
@type channel_account_id :: Account.id | Entity.id
15+
@type channel_bank_account_id ::
16+
%{
17+
atm_id: ATM.id,
18+
account_number: BankAccount.account
19+
}
1320

1421
@doc """
1522
Handler responsible for guiding the event through the Notificable flow. It
@@ -51,13 +58,35 @@ defmodule Helix.Event.NotificationHandler do
5158
accounts
5259
|> Utils.ensure_list()
5360
|> Enum.uniq()
54-
|> get_account_channels()
61+
|> get_bank_account_channels()
5562
|> List.flatten()
5663
|> Kernel.++(acc)
5764

5865
channel_mapper(Map.delete(notify, :account), acc)
5966
end
6067

68+
defp channel_mapper(notify =
69+
%{
70+
bank_account: bank_accs =
71+
%{
72+
atm_id: _atm_id = %Server.ID{},
73+
account_number: _
74+
}
75+
},
76+
acc
77+
)
78+
do
79+
acc =
80+
bank_accs
81+
|> Utils.ensure_list()
82+
|> Enum.uniq()
83+
|> get_bank_account_channels()
84+
|> List.flatten()
85+
|> Kernel.++(acc)
86+
87+
channel_mapper(Map.delete(notify, :bank_account), acc)
88+
end
89+
6190
defp channel_mapper(%{}, acc),
6291
do: acc
6392

@@ -94,6 +123,15 @@ defmodule Helix.Event.NotificationHandler do
94123
defp get_account_channels(account_id),
95124
do: ["account:" <> to_string(account_id)]
96125

126+
@spec get_bank_account_channels(
127+
[channel_bank_account_id] | channel_bank_account_id
128+
) ::
129+
channels :: [String.t]
130+
defp get_bank_account_channels(bank_accounts) when is_list(bank_accounts),
131+
do: Enum.map(bank_accounts, &get_bank_account_channels/1)
132+
defp get_bank_account_channels(%{atm_id: atm_id, account_number: acc_num}),
133+
do: ["bank:" <> to_string(acc_num) <> "@" <> to_string(atm_id)]
134+
97135
defp concat(a, b),
98136
do: a <> to_string(b)
99137
end

lib/universe/bank/action/bank.ex

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ defmodule Helix.Universe.Bank.Action.Bank do
2727
as: BankAccountPasswordChangedEvent
2828
alias Helix.Universe.Bank.Event.Bank.Account.Token.Acquired,
2929
as: BankAccountTokenAcquiredEvent
30+
alias Helix.Universe.Bank.Event.Bank.Account.Removed,
31+
as: BankAccountRemovedEvent
32+
alias Helix.Universe.Bank.Event.Bank.Transfer.Successful,
33+
as: BankTransferSuccessfulEvent
34+
alias Helix.Universe.Bank.Event.Bank.Transfer.Failed,
35+
as: BankTransferFailedEvent
3036

3137
@spec start_transfer(BankAccount.t, BankAccount.t, pos_integer, Account.idt) ::
3238
{:ok, BankTransfer.t}
3339
| {:error, {:funds, :insufficient}}
34-
| {:error, {:account, :notfound}}
40+
| {:error, {:bank_account, :not_found}}
3541
| {:error, Ecto.Changeset.t}
3642
@doc """
3743
Starts a bank transfer.
@@ -51,9 +57,8 @@ defmodule Helix.Universe.Bank.Action.Bank do
5157
as: :start
5258

5359
@spec complete_transfer(BankTransfer.t) ::
54-
:ok
55-
| {:error, {:transfer, :notfound}}
56-
| {:error, :internal}
60+
{:ok, BankTransfer.t, [BankTransferSuccessfulEvent.t]}
61+
| {:error, term, [BankTransferFailedEvent.t]}
5762
@doc """
5863
Completes the transfer.
5964
@@ -66,9 +71,19 @@ defmodule Helix.Universe.Bank.Action.Bank do
6671
This function should not be called directly by Public. Instead, it must be
6772
triggered by the BankTransferCompletedEvent.
6873
"""
69-
defdelegate complete_transfer(transfer),
70-
to: BankTransferInternal,
71-
as: :complete
74+
def complete_transfer(transfer) do
75+
case BankTransferInternal.complete(transfer) do
76+
:ok ->
77+
{:ok, transfer, [BankTransferSuccessfulEvent.new(transfer)]}
78+
79+
{:error, reason} ->
80+
{
81+
:error,
82+
reason,
83+
[BankTransferFailedEvent.new(transfer, reason)]
84+
}
85+
end
86+
end
7287

7388
@spec abort_transfer(BankTransfer.t) ::
7489
:ok
@@ -91,7 +106,7 @@ defmodule Helix.Universe.Bank.Action.Bank do
91106
as: :abort
92107

93108
@spec open_account(Account.idt, ATM.id) ::
94-
{:ok, BankAccount.t}
109+
{:ok, BankAccount.t, [BankAccountUpdatedEvent.t]}
95110
| {:error, Ecto.Changeset.t}
96111
@doc """
97112
Opens a bank account.
@@ -103,23 +118,34 @@ defmodule Helix.Universe.Bank.Action.Bank do
103118
|> Map.get(:entity_id)
104119
|> NPCQuery.fetch()
105120

106-
%{owner_id: owner, atm_id: atm, bank_id: bank}
107-
|> BankAccountInternal.create()
121+
case BankAccountInternal.create(owner, atm, bank) do
122+
{:ok, bank_acc} ->
123+
{:ok, bank_acc, [BankAccountUpdatedEvent.new(bank_acc, :created)]}
124+
125+
error ->
126+
error
127+
end
108128
end
109129

110130
@spec close_account(BankAccount.t) ::
111131
:ok
112-
| {:error, {:account, :notfound}}
113-
| {:error, {:account, :notempty}}
132+
| {:error, {:bank_account, :not_found}}
133+
| {:error, {:bank_account, :not_empty}}
114134
@doc """
115135
Closes a bank account.
116136
117137
May fail if the account is invalid or not empty. In order to close an account,
118138
its balance must be empty.
119139
"""
120-
defdelegate close_account(account),
121-
to: BankAccountInternal,
122-
as: :close
140+
def close_account(account) do
141+
case BankAccountInternal.close(account) do
142+
:ok ->
143+
{:ok, [BankAccountRemovedEvent.new(account)]}
144+
145+
{:error, reason} ->
146+
{:error, reason}
147+
end
148+
end
123149

124150
@spec direct_deposit(BankAccount.t, BankAccount.amount) ::
125151
{:ok, BankAccount.t, [BankAccountUpdatedEvent.t]}

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
55
alias Helix.Event
66
alias Helix.Entity.Query.Entity, as: EntityQuery
77
alias Helix.Network.Action.Flow.Tunnel, as: TunnelFlow
8-
alias Helix.Network.Model.Connection
98
alias Helix.Network.Query.Network, as: NetworkQuery
109
alias Helix.Process.Model.Process
1110
alias Helix.Server.Model.Server
@@ -61,13 +60,32 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
6160
Opens a new BankAccount to given Account.id
6261
"""
6362
def open(account_id, atm_id) do
64-
bank_account =
65-
BankAction.open_account(account_id, atm_id)
66-
case bank_account do
67-
{:ok, bank_account} ->
68-
{:ok, bank_account}
69-
{:error, _} ->
70-
{:error, :internal}
63+
flowing do
64+
with \
65+
{:ok, bank_acc, events} <- BankAction.open_account(account_id, atm_id),
66+
on_success(fn -> Event.emit(events) end)
67+
do
68+
{:ok, bank_acc}
69+
else
70+
_ ->
71+
{:error, :internal}
72+
end
73+
end
74+
end
75+
76+
@spec close(BankAccount.t) ::
77+
:ok
78+
| {:error, {:bank_account, :not_found}}
79+
| {:error, {:bank_account, :not_empty}}
80+
def close(bank_account) do
81+
flowing do
82+
with \
83+
true <- not is_nil(bank_account),
84+
{:ok, events} <- BankAction.close_account(bank_account),
85+
on_success(fn -> Event.emit(events) end)
86+
do
87+
:ok
88+
end
7189
end
7290
end
7391

@@ -152,9 +170,9 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
152170
{:ok, _, events} <- BankAction.login_token(acc, token, entity),
153171
on_success(fn -> Event.emit(events) end),
154172

155-
{:ok, _, connection} <- start_connection.()
173+
{:ok, tunnel, connection} <- start_connection.()
156174
do
157-
{:ok, connection}
175+
{:ok, tunnel, connection}
158176
end
159177
end
160178
end

0 commit comments

Comments
 (0)