1
1
defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
2
+ @ moduledoc """
3
+ Handles data transformations between the API `Stripe.Event` struct and maps of
4
+ attributes suitable for work with our own `StripeEvent` database objects.
5
+ """
2
6
import CodeCorps.MapUtils , only: [ keys_to_string: 1 ]
3
7
import CodeCorps.StripeService.Util , only: [ transform_map: 2 ]
4
8
@@ -14,6 +18,7 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
14
18
Transforms a `%Stripe.Event{}` and a set of local attributes into a
15
19
map of parameters used to create or update a `StripeEvent` record.
16
20
"""
21
+ @ spec to_params ( Stripe.Event . t , map ) :: { :ok , map }
17
22
def to_params ( % Stripe.Event { } = stripe_event , % { } = attributes ) do
18
23
result =
19
24
stripe_event
@@ -45,12 +50,11 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
45
50
params |> Map . merge ( attributes )
46
51
end
47
52
48
- defp add_object_type ( params , stripe_event ) do
49
- object_type = stripe_event . data . object . object
50
- params |> Map . put ( :object_type , object_type )
51
- end
53
+ # NOTE: unlike object_id, object_type should never be nil
54
+ # Due to that, we do not have a catch-all clause for the nil case
55
+ # If it ever is nil, it should fail and we should know about it
56
+ defp add_object_type ( params , % { data: % { object: % { object: object } } } ) , do: params |> Map . put ( :object_type , object )
52
57
53
- defp add_object_id ( params , stripe_event ) do
54
- params |> Map . put ( :object_id , stripe_event . data . object . id )
55
- end
58
+ defp add_object_id ( params , % { data: % { object: % { id: id } } } ) , do: params |> Map . put ( :object_id , id )
59
+ defp add_object_id ( params , _stripe_event ) , do: params |> Map . put ( :object_id , nil )
56
60
end
0 commit comments