Skip to content

Commit a22119b

Browse files
author
Leandro Santos
committed
Fix IndividualDocument create method
1 parent b649727 commit a22119b

File tree

8 files changed

+68
-76
lines changed

8 files changed

+68
-76
lines changed

lib/individual_document/individual_document.ex

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,9 @@ defmodule StarkInfra.IndividualDocument do
7070
{:ok, [IndividualDocument.t()]} |
7171
{:error, [Error.t()]}
7272
def create(documents, options \\ []) do
73-
document = documents |> hd
74-
payload = document |> Enum.into(Atom)
75-
if Atom.get(payload, :content_type) != nil do
76-
Atom.put(payload, :content, "data:" <> Atom.get(payload, :content_type) <> ";base64," <> Base.encode64(Atom.get(payload, :content)))
77-
Atom.delete(payload, :content_type)
78-
end
79-
Rest.post!(
73+
Rest.post(
8074
resource(),
81-
documents,
75+
set_content(documents),
8276
options
8377
)
8478
end
@@ -91,15 +85,9 @@ defmodule StarkInfra.IndividualDocument do
9185
user: Project.t() | Organization.t() | nil
9286
) :: any
9387
def create!(documents, options \\ []) do
94-
document = documents |> hd
95-
payload = document |> Enum.into(Atom)
96-
if Atom.get(payload, :content_type) != nil do
97-
Atom.put(payload, :content, "data:" <> Atom.get(payload, :content_type) <> ";base64," <> Base.encode64(Atom.get(payload, :content)))
98-
Atom.delete(payload, :content_type)
99-
end
10088
Rest.post!(
10189
resource(),
102-
documents,
90+
set_content(documents),
10391
options
10492
)
10593
end
@@ -248,6 +236,21 @@ defmodule StarkInfra.IndividualDocument do
248236
}
249237
end
250238

239+
@doc false
240+
def set_content(data) do
241+
Enum.map(data, fn (document) ->
242+
case document.content_type do
243+
nil -> document
244+
_ -> %{
245+
:content => "data:" <> document.content_type <> ";base64," <> (document.content |> Base.encode64() |> to_string()),
246+
:type => document.type,
247+
:identity_id => document.identity_id,
248+
:tags => document.tags
249+
}
250+
end
251+
end)
252+
end
253+
251254
@doc false
252255
def resource_maker(json) do
253256
%IndividualDocument{

test/individual_document_test.exs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@ defmodule StarkInfraTest.IndividualDocument do
3232
test "create individual selfie" do
3333
{:ok, documents} = StarkInfra.IndividualDocument.create([example_individual_document_selfie()])
3434
document = documents |> hd
35-
3635
assert !is_nil(document.id)
3736
end
3837

3938
@tag :individual_document
4039
test "create! individual selfie" do
41-
documents = StarkInfra.IndividualDocument.create!([example_individual_document_selfie()]) |> hd
42-
document = documents |> hd
43-
44-
40+
document = StarkInfra.IndividualDocument.create!([example_individual_document_selfie()]) |> hd
4541
assert !is_nil(document.id)
4642
end
4743

@@ -97,29 +93,29 @@ defmodule StarkInfraTest.IndividualDocument do
9793
{:ok, data} = File.read("./test/utils/identity/identity-front-face.png")
9894
%StarkInfra.IndividualDocument{
9995
type: "identity-front",
100-
content: Base.encode64(data),
96+
content: data,
10197
content_type: "image/png",
102-
identity_id: "5155165527080960",
98+
identity_id: "5732110983757824",
10399
tags: ["breaking", "bad"]
104100
}
105101
end
106102
def example_individual_document_back() do
107103
{:ok, data} = File.read("./test/utils/identity/identity-back-face.png")
108104
%StarkInfra.IndividualDocument{
109105
type: "identity-back",
110-
content: Base.encode64(data),
106+
content: data,
111107
content_type: "image/png",
112-
identity_id: "5155165527080960",
108+
identity_id: "5732110983757824",
113109
tags: ["breaking", "bad"]
114110
}
115111
end
116112
def example_individual_document_selfie() do
117113
{:ok, data} = File.read("./test/utils/identity/walter-white.png")
118114
%StarkInfra.IndividualDocument{
119115
type: "selfie",
120-
content: Base.encode64(data),
116+
content: data,
121117
content_type: "image/png",
122-
identity_id: "5155165527080960",
118+
identity_id: "5732110983757824",
123119
tags: ["breaking", "bad"]
124120
}
125121
end

test/issuing_embossing_request_test.exs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ defmodule StarkInfraTest.IssuingEmbossingRequest do
3737

3838
@tag :issuing_embossing_request
3939
test "query! issuing embossing request with filters" do
40-
requests = StarkInfra.IssuingEmbossingRequest.query!(status: "created")
40+
StarkInfra.IssuingEmbossingRequest.query!(status: "success")
4141
|> Enum.take(1)
42-
|> hd()
43-
44-
StarkInfra.IssuingEmbossingRequest.query!(limit: 1, requests_ids: [requests.id], types: ["created"])
45-
|> Enum.take(5)
46-
|> (fn list -> assert length(list) == 1 end).()
4742
end
4843

4944
@tag :issuing_embossing_request

test/pix_claim_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ defmodule StarkInfraTest.PixClaim do
4141
|> Enum.take(1)
4242
|> hd()
4343

44-
parameters = %{reason: "fraud"}
45-
46-
{:ok, claim} = StarkInfra.PixClaim.update(pix_claim.id, "canceled", parameters)
44+
{:ok, claim} = StarkInfra.PixClaim.update(pix_claim.id, "canceled", reason: "fraud")
4745

4846
assert !is_nil(claim.id)
4947
end
@@ -54,9 +52,7 @@ defmodule StarkInfraTest.PixClaim do
5452
|> Enum.take(1)
5553
|> hd
5654

57-
parameters = %{reason: "fraud"}
58-
59-
claim = StarkInfra.PixClaim.update!(pix_claim.id, "canceled", parameters)
55+
claim = StarkInfra.PixClaim.update!(pix_claim.id, "canceled", reason: "fraud")
6056

6157
assert !is_nil(claim.id)
6258
end

test/pix_infraction_test.exs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,21 @@ defmodule StarkInfraTest.PixInfraction do
111111
end)
112112
end
113113

114+
def get_end_to_end_id_to_infraction(pix_request, cursor) when length(pix_request) == 0 do
115+
{cursor, requests} = StarkInfra.PixRequest.page!(limit: 10, cursor: cursor, status: ["success"])
116+
pix_in = Enum.filter(requests, fn(request) -> request.flow == "out" end)
117+
|> Enum.filter(fn(request) -> request.amount > 10000 end)
118+
|> Enum.take(1)
119+
120+
get_end_to_end_id_to_infraction(pix_in, cursor)
121+
end
122+
123+
def get_end_to_end_id_to_infraction(pix_request, _cursor) do
124+
pix_request
125+
end
126+
114127
def example_pix_infraction() do
115-
pix = StarkInfraTest.Utils.EndToEndId.get_end_to_end_id_to_infraction([], nil) |> hd
128+
pix = get_end_to_end_id_to_infraction([], nil) |> hd
116129
%StarkInfra.PixInfraction
117130
{reference_id: pix.end_to_end_id, type: "fraud"}
118131
end

test/pix_key_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule StarkInfraTest.PixKey do
1515

1616
@tag :pix_key
1717
test "get pix key" do
18-
pix_key = StarkInfra.PixKey.query!(limit: 10, status: "registered", type: "phone")
18+
pix_key = StarkInfra.PixKey.query!(limit: 10, status: "registered")
1919
|> Enum.take(1)
2020
|> hd()
2121

@@ -29,7 +29,7 @@ defmodule StarkInfraTest.PixKey do
2929

3030
@tag :pix_key
3131
test "get! pix key" do
32-
pix_key = StarkInfra.PixKey.query!(limit: 10, status: "registered", type: "phone")
32+
pix_key = StarkInfra.PixKey.query!(limit: 10, status: "registered")
3333
|> Enum.take(1)
3434
|> hd()
3535

test/pix_reversal_test.exs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ defmodule StarkInfraTest.PixReversal do
1111

1212
@tag :pix_reversal
1313
test "create! pix reversal" do
14-
pix_reversal = StarkInfra.PixReversal.create!([generate_example_pix_reversal()]) |> hd
15-
assert !is_nil(pix_reversal.id)
14+
reversal = StarkInfra.PixReversal.create!([generate_example_pix_reversal()]) |> hd
15+
assert !is_nil(reversal.id)
1616
end
1717

1818
@tag :pix_reversal
@@ -62,8 +62,28 @@ defmodule StarkInfraTest.PixReversal do
6262
assert length(ids) <= 10
6363
end
6464

65+
def get_end_to_end_id_to_reverse(pix_request, cursor \\ nil)
66+
67+
def get_end_to_end_id_to_reverse(pix_request, cursor) when length(pix_request) == 0 do
68+
{cursor, requests} = StarkInfra.PixRequest.page!(limit: 10, cursor: cursor, status: ["success"])
69+
pix_in = Enum.filter(requests, fn(request) -> request.flow == "in" end)
70+
|> Enum.filter(fn(request) -> request.amount > 10000 end)
71+
|> Enum.take(1)
72+
73+
get_end_to_end_id_to_reverse(pix_in, cursor)
74+
end
75+
76+
def get_end_to_end_id_to_reverse(pix_request, _cursor) do
77+
pix_request
78+
end
79+
80+
def get_end_to_end_id() do
81+
pix_request = StarkInfra.PixRequest.query!(limit: 1, status: ["success"]) |> Enum.take(1) |> hd
82+
pix_request.end_to_end_id
83+
end
84+
6585
def generate_example_pix_reversal() do
66-
pix = StarkInfraTest.Utils.EndToEndId.get_end_to_end_id_to_reverse([], nil) |> hd
86+
pix = get_end_to_end_id_to_reverse([], nil) |> hd
6787
%StarkInfra.PixReversal{
6888
amount: Enum.random(1..10),
6989
end_to_end_id: pix.end_to_end_id,

test/utils/end_to_end_id.exs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,5 @@
11
defmodule StarkInfraTest.Utils.EndToEndId do
22
use ExUnit.Case
33

4-
def get_end_to_end_id_to_reverse(pix_request, cursor \\ nil)
5-
6-
def get_end_to_end_id_to_reverse(pix_request, cursor) when length(pix_request) == 0 do
7-
{cursor, requests} = StarkInfra.PixRequest.page!(limit: 10, cursor: cursor, status: ["success"])
8-
pix_in = Enum.filter(requests, fn(request) -> request.flow == "in" end)
9-
|> Enum.filter(fn(request) -> request.amount > 10000 end)
10-
|> Enum.take(1)
11-
12-
get_end_to_end_id_to_reverse(pix_in, cursor)
13-
end
14-
15-
def get_end_to_end_id_to_reverse(pix_request, _cursor) do
16-
pix_request
17-
end
18-
19-
def get_end_to_end_id_to_infraction(pix_request, cursor) when length(pix_request) == 0 do
20-
{cursor, requests} = StarkInfra.PixRequest.page!(limit: 10, cursor: cursor, status: ["success"])
21-
pix_in = Enum.filter(requests, fn(request) -> request.flow == "out" end)
22-
|> Enum.filter(fn(request) -> request.amount > 10000 end)
23-
|> Enum.take(1)
24-
25-
get_end_to_end_id_to_infraction(pix_in, cursor)
26-
end
27-
28-
def get_end_to_end_id_to_infraction(pix_request, _cursor) do
29-
pix_request
30-
end
31-
32-
def get_end_to_end_id() do
33-
pix_request = StarkInfra.PixRequest.query!(limit: 1, status: ["success"]) |> Enum.take(1) |> hd
34-
pix_request.end_to_end_id
35-
end
4+
365
end

0 commit comments

Comments
 (0)