wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
0
fork

Configure Feed

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

:recycle: remove wrong usage of `decode.optional_field`

Kacaii 07de37f1 1cb4db19

+47 -57
+33 -38
src/app/routes/occurrence/register_new_occurrence.gleam
··· 13 13 import gleam/http 14 14 import gleam/json 15 15 import gleam/list 16 - import gleam/result 16 + import gleam/result.{try} 17 17 import gleam/time/timestamp 18 18 import group_registry 19 19 import pog ··· 94 94 /// Registering a new occurrence can fail 95 95 type RegisterNewOccurrenceError { 96 96 /// Failed to authenticate the user 97 - AuthenticationFailed(user.AuthenticationError) 97 + AccessControl(user.AuthenticationError) 98 98 /// Failed to access the database 99 - DataBaseError(pog.QueryError) 99 + DataBase(pog.QueryError) 100 100 /// Database returned no results 101 101 OccurrenceNotCreated 102 102 /// Failed to assign a brigade to the giving occurrence ··· 105 105 106 106 fn handle_error(err: RegisterNewOccurrenceError) -> wisp.Response { 107 107 case err { 108 - AuthenticationFailed(err) -> user.handle_authentication_error(err) 109 - DataBaseError(err) -> web.handle_database_error(err) 108 + AccessControl(err) -> user.handle_authentication_error(err) 109 + DataBase(err) -> web.handle_database_error(err) 110 110 FailedToAssignBrigade(id) -> 111 111 wisp.internal_server_error() 112 112 |> wisp.set_body(wisp.Text( ··· 118 118 } 119 119 } 120 120 121 - fn body_decoder() { 121 + fn body_decoder() -> decode.Decoder(RegisterOccurrenceBody) { 122 122 let brigade_uuid_decoder = { 123 123 use maybe_uuid <- decode.then(decode.string) 124 124 case uuid.from_string(maybe_uuid) { 125 125 Ok(value) -> decode.success(value) 126 - Error(_) -> decode.failure(uuid.v7(), "uuid") 126 + Error(_) -> decode.failure(uuid.v7(), "brigade_uuid") 127 127 } 128 128 } 129 129 use occ_category <- decode.field("categoria", category.decoder()) ··· 153 153 ctx ctx: Context, 154 154 body body: RegisterOccurrenceBody, 155 155 ) -> Result(String, RegisterNewOccurrenceError) { 156 - use applicant_uuid <- result.try( 156 + use applicant_uuid <- try( 157 157 user.extract_uuid(request:, cookie_name: user.uuid_cookie_name) 158 - |> result.map_error(AuthenticationFailed), 158 + |> result.map_error(AccessControl), 159 159 ) 160 160 161 - use returned <- result.try( 161 + use returned <- try( 162 162 sql.insert_new_occurence( 163 163 ctx.db, 164 164 applicant_uuid, ··· 169 169 body.location, 170 170 body.reference_point, 171 171 ) 172 - |> result.map_error(DataBaseError), 172 + |> result.map_error(DataBase), 173 173 ) 174 174 175 - use row <- result.try(case list.first(returned.rows) { 175 + use row <- try(case list.first(returned.rows) { 176 176 Error(_) -> Error(OccurrenceNotCreated) 177 177 Ok(row) -> Ok(row) 178 178 }) 179 179 180 - use assigned_brigades <- result.try(try_assign_brigades( 180 + use assigned_brigades <- result.map(try_assign_brigades( 181 181 ctx:, 182 182 assign: body.brigade_list, 183 183 to: row.id, ··· 190 190 191 191 // RESPONSE ------------------------------------------------------------------ 192 192 let occurrence_priority = 193 - enum_to_priority(row.priority) 194 - |> priority.to_string_pt_br 193 + priority.to_string_pt_br(case row.priority { 194 + sql.High -> priority.High 195 + sql.Medium -> priority.Medium 196 + sql.Low -> priority.Low 197 + }) 195 198 196 199 let occ_category = case row.occurrence_category { 197 200 sql.Fire -> category.Fire ··· 200 203 sql.TrafficAccident -> category.TrafficAccident 201 204 } 202 205 203 - //  Broadcast new occurrence 206 + //  Broadcast new occurrence ----------------------------------------------- 204 207 let registry = group_registry.get_registry(ctx.registry_name) 205 208 occurrence.notify_new_occurrence(new: row.id, of: occ_category, registry:) 206 209 207 - json.object([ 208 - #("id", uuid.to_string(row.id) |> json.string), 209 - #("applicant_id", uuid.to_string(row.id) |> json.string), 210 - #("priority", json.string(occurrence_priority)), 211 - #("assigned_brigades", assigned_brigades_json), 212 - #("created_at", json.float(timestamp.to_unix_seconds(row.created_at))), 213 - ]) 214 - |> json.to_string 215 - |> Ok 210 + json.to_string( 211 + json.object([ 212 + #("id", uuid.to_string(row.id) |> json.string), 213 + #("applicant_id", uuid.to_string(row.id) |> json.string), 214 + #("priority", json.string(occurrence_priority)), 215 + #("assigned_brigades", assigned_brigades_json), 216 + #("created_at", json.float(timestamp.to_unix_seconds(row.created_at))), 217 + ]), 218 + ) 216 219 } 217 220 218 221 fn try_assign_brigades( ··· 220 223 assign brigades_id: List(uuid.Uuid), 221 224 to occurrence_id: uuid.Uuid, 222 225 ) -> Result(List(uuid.Uuid), RegisterNewOccurrenceError) { 223 - use returned <- result.try( 226 + use returned <- try( 224 227 sql.assign_brigades_to_occurrence(ctx.db, occurrence_id, brigades_id) 225 - |> result.map_error(DataBaseError), 228 + |> result.map_error(DataBase), 226 229 ) 227 230 228 231 let assigned_brigades = { ··· 230 233 row.inserted_brigade_id 231 234 } 232 235 233 - use assigned_users <- result.try({ 234 - use returned <- result.try( 236 + use assigned_users <- try({ 237 + use returned <- try( 235 238 sql.query_participants(ctx.db, occurrence_id) 236 - |> result.map_error(DataBaseError), 239 + |> result.map_error(DataBase), 237 240 ) 238 241 239 242 list.map(returned.rows, fn(row) { row.user_id }) ··· 256 259 priority.High -> sql.High 257 260 priority.Low -> sql.Low 258 261 priority.Medium -> sql.Medium 259 - } 260 - } 261 - 262 - fn enum_to_priority(enum: sql.OccurrencePriorityEnum) { 263 - case enum { 264 - sql.High -> priority.High 265 - sql.Medium -> priority.Medium 266 - sql.Low -> priority.Low 267 262 } 268 263 } 269 264
+1 -1
test/app_test.gleam
··· 9 9 import wisp 10 10 import wisp/simulate 11 11 12 - pub const n_tests = 25 12 + pub const n_tests = 10 13 13 14 14 pub fn main() -> Nil { 15 15 gleeunit.main()
+1 -1
test/brigade_test.gleam
··· 195 195 196 196 use brigade_uuid <- decode.field("id", uuid_decoder) 197 197 use _ <- decode.field("brigade_name", decode.string) 198 - use _ <- decode.optional_field("leader_name", "wibble", decode.string) 198 + use _ <- decode.field("leader_name", decode.optional(decode.string)) 199 199 use _ <- decode.field("is_active", decode.bool) 200 200 decode.success(brigade_uuid) 201 201 }),
+11 -16
test/occurence_test.gleam
··· 61 61 // RESPONSE ------------------------------------------------------------------ 62 62 let resp = http_router.handle_request(req, ctx) 63 63 assert resp.status != 422 as "Invalid request Payload" 64 - assert resp.status == 401 65 - as "Endpoint should only accessible to authenticated users" 64 + assert resp.status == 401 as "Endpoint restricted to authenticated users" 66 65 67 66 let with_auth = app_test.with_authorization(next: req) 68 67 let resp = http_router.handle_request(with_auth, ctx) 69 - assert resp.status == 201 as "Status should be HTTP 201 Created" 68 + assert resp.status == 201 as "HTTP 201 Created" 70 69 71 70 // JSON PARSING -------------------------------------------------------------- 72 71 let body = simulate.read_body(resp) ··· 92 91 93 92 use dummy_occurrence_id <- decode.field("id", uuid_decoder) 94 93 use _ <- decode.field("priority", priority_decoder) 95 - use _ <- decode.optional_field("applicant_id", uuid.v7(), uuid_decoder) 96 - use _ <- decode.optional_field( 97 - "brigade_list", 98 - [uuid.v7()], 99 - decode.list(uuid_decoder), 100 - ) 94 + use _ <- decode.field("applicant_id", decode.optional(uuid_decoder)) 95 + use _ <- decode.field("assigned_brigades", decode.list(uuid_decoder)) 101 96 use _ <- decode.field("created_at", decode.float) 97 + 102 98 decode.success(dummy_occurrence_id) 103 99 }) 104 - as "Response should contain valid JSON" 100 + as "Response contain valid JSON" 105 101 106 102 // Check if users were registered as participants ---------------------------- 107 103 let dummy_participants_set = set.from_list(dummy_participants_id) ··· 110 106 set.from_list({ 111 107 let assert Ok(returned) = 112 108 o_sql.query_participants(ctx.db, dummy_occurrence_id) 113 - as "Failed to query occurrence participants" 109 + as "Query occurrence participants" 114 110 115 111 use row <- list.map(returned.rows) 116 112 row.user_id ··· 119 115 assert set.difference(registered_participants_set, dummy_participants_set) 120 116 |> set.to_list() 121 117 == [] 122 - as "Registered members contain unexpected users" 118 + as "Registered members contain all expected users" 123 119 124 120 assert set.difference(dummy_participants_set, registered_participants_set) 125 121 |> set.to_list() 126 122 == [] 127 - as "Not all users were assigned" 123 + as "Participants were assigned correclty" 128 124 129 125 // 󰃢 CLEANUP ---------------------------------------------------------------- 130 126 let assert Ok(_) = dev_sql.truncate_occurrence(ctx.db) ··· 217 213 use _ <- decode.field("status", decode.string) 218 214 use _ <- decode.field("prioridade", priority_decoder) 219 215 use _ <- decode.field("chamado", call_decoder) 220 - use _ <- decode.optional_field( 216 + use _ <- decode.field( 221 217 "coordenadas", 222 - [], 223 - decode.list(decode.float), 218 + decode.optional(decode.list(decode.float)), 224 219 ) 225 220 use _ <- decode.field("timestamps", occurrence_timestamp_decoder) 226 221 use _ <- decode.field("metadata", occurrence_metadata_decoder)
+1 -1
test/user_test.gleam
··· 175 175 use _ <- decode.field("id", decode.string) 176 176 use _ <- decode.field("full_name", decode.string) 177 177 use _ <- decode.field("registration", decode.string) 178 - use _ <- decode.optional_field("email", "null", decode.string) 178 + use _ <- decode.field("email", decode.optional(decode.string)) 179 179 use _ <- decode.field("user_role", decode.string) 180 180 decode.success(Nil) 181 181 }),