this repo has no description
1import birl
2import gleam/dict
3import gleam/dynamic/decode
4import gleam/json
5import gleam/option
6import gleam/result
7
8pub type PaymentRequest {
9 PaymentRequest(
10 correlation_id: String,
11 amount: Float,
12 requested_at: birl.Time,
13 provider: option.Option(String),
14 )
15}
16
17pub fn set_provider(
18 payment_request: PaymentRequest,
19 provider: String,
20) -> PaymentRequest {
21 PaymentRequest(..payment_request, provider: option.Some(provider))
22}
23
24pub fn to_dict(payment: PaymentRequest) -> dict.Dict(String, String) {
25 dict.new()
26 |> dict.insert(payment.correlation_id, payment |> to_json |> json.to_string)
27}
28
29pub fn to_json(payment: PaymentRequest) -> json.Json {
30 [
31 #("correlationId", json.string(payment.correlation_id)),
32 #("amount", json.float(payment.amount)),
33 #("requestedAt", json.string(payment.requested_at |> birl.to_iso8601)),
34 #("provider", json.nullable(payment.provider, of: fn(a) { json.string(a) })),
35 ]
36 |> json.object
37}
38
39pub fn from_json_string(payment: String) {
40 let parse = {
41 use amount <- decode.field("amount", decode.float)
42 use correlation_id <- decode.field("correlationId", decode.string)
43 use requested_at <- decode.field("requestedAt", decode.string)
44 use provider <- decode.field("provider", decode.optional(decode.string))
45
46 decode.success(PaymentRequest(
47 amount: amount,
48 correlation_id: correlation_id,
49 requested_at: requested_at
50 |> birl.parse
51 |> result.unwrap(birl.now()),
52 provider: provider,
53 ))
54 }
55
56 json.parse(payment, parse)
57}