-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregister_user.ex
More file actions
62 lines (51 loc) · 1.4 KB
/
register_user.ex
File metadata and controls
62 lines (51 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
defmodule RegisterUser do
@moduledoc """
Example command that registers a user.
"""
import Commandex
command do
param :email, default: "test@test.com"
param :password
param :agree_tos
data :user
data :auth
pipeline :check_already_registered
pipeline :verify_tos
pipeline :create_user
pipeline :record_auth_attempt
pipeline &IO.inspect/1
end
@spec check_already_registered(t(), map(), map()) :: t()
def check_already_registered(command, %{email: email}, _data) do
case email do
"exists@test.com" ->
command
|> put_error(:user, :already_exists)
|> halt(success: true)
_other ->
command
end
end
@spec verify_tos(t(), map(), map()) :: t()
def verify_tos(command, %{agree_tos: true} = _params, _data) do
command
end
def verify_tos(command, %{agree_tos: false} = _params, _data) do
command
|> put_error(:tos, :not_accepted)
|> halt()
end
@spec create_user(t(), map(), map()) :: t()
def create_user(command, %{password: nil} = _params, _data) do
command
|> put_error(:password, :not_given)
|> halt()
end
def create_user(command, %{email: email} = _params, _data) do
put_data(command, :user, %{email: email})
end
@spec record_auth_attempt(t(), map(), map()) :: t()
def record_auth_attempt(command, _params, _data) do
put_data(command, :auth, true)
end
end