this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

:sparkles: saved processor in redis when saving data

+35 -5
+17 -1
src/model.gleam
··· 2 2 import gleam/dict 3 3 import gleam/dynamic/decode 4 4 import gleam/json 5 + import gleam/option 5 6 import gleam/result 6 7 7 8 pub type PaymentRequest { 8 - PaymentRequest(correlation_id: String, amount: Float, requested_at: birl.Time) 9 + PaymentRequest( 10 + correlation_id: String, 11 + amount: Float, 12 + requested_at: birl.Time, 13 + provider: option.Option(String), 14 + ) 15 + } 16 + 17 + pub fn set_provider( 18 + payment_request: PaymentRequest, 19 + provider: String, 20 + ) -> PaymentRequest { 21 + PaymentRequest(..payment_request, provider: option.Some(provider)) 9 22 } 10 23 11 24 pub fn to_dict(payment: PaymentRequest) -> dict.Dict(String, String) { ··· 18 31 #("correlationId", json.string(payment.correlation_id)), 19 32 #("amount", json.float(payment.amount)), 20 33 #("requestedAt", json.string(payment.requested_at |> birl.to_iso8601)), 34 + #("provider", json.nullable(payment.provider, of: fn(a) { json.string(a) })), 21 35 ] 22 36 |> json.object 23 37 } ··· 27 41 use amount <- decode.field("amount", decode.float) 28 42 use correlation_id <- decode.field("correlationId", decode.string) 29 43 use requested_at <- decode.field("requestedAt", decode.string) 44 + use provider <- decode.field("provider", decode.optional(decode.string)) 30 45 31 46 decode.success(PaymentRequest( 32 47 amount: amount, ··· 34 49 requested_at: requested_at 35 50 |> birl.parse 36 51 |> result.unwrap(birl.now()), 52 + provider: provider, 37 53 )) 38 54 } 39 55
+16 -4
src/processor.gleam
··· 2 2 import gleam/option 3 3 import gleam/otp/actor 4 4 import gleam/otp/supervision 5 + import gleam/string 5 6 import integrations/provider 6 7 import model 7 8 import redis ··· 79 80 fn integrations( 80 81 providers: List(provider.ProviderConfig), 81 82 body: String, 82 - ) -> Result(Bool, Bool) { 83 + ) -> Result(provider.ProviderConfig, Bool) { 83 84 case providers { 84 85 [] -> Error(False) 85 86 [provider, ..rest] -> { 86 87 echo "Trying provider -> " <> provider.url 87 88 case provider.send_request(provider, body) { 88 - Ok(_) -> Ok(True) 89 + Ok(_) -> Ok(provider) 89 90 _ -> integrations(rest, body) 90 91 } 91 92 } ··· 105 106 processor.redis_conn 106 107 |> redis.enqueue_payments([data]) 107 108 } 108 - Ok(_) -> { 109 + Ok(provider_processed) -> { 109 110 echo "Success, saving it" 110 111 112 + let save_provider = case 113 + string.contains(provider_processed.url, contain: "default") 114 + { 115 + True -> "default" 116 + _ -> "fallback" 117 + } 118 + 111 119 processor.redis_conn 112 - |> redis.save_data(message |> model.to_dict) 120 + |> redis.save_data( 121 + message 122 + |> model.set_provider(save_provider) 123 + |> model.to_dict, 124 + ) 113 125 } 114 126 } 115 127 }
+2
src/web/controllers/payment_controller.gleam
··· 5 5 import gleam/http/response 6 6 import gleam/json 7 7 import gleam/list 8 + import gleam/option 8 9 import gleam/string_tree 9 10 import model.{type PaymentRequest, PaymentRequest} 10 11 import redis ··· 24 25 correlation_id: correlation, 25 26 amount: amount, 26 27 requested_at: birl.now(), 28 + provider: option.None, 27 29 )) 28 30 } 29 31 case decode.run(body, decoder) {