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: use `replace_error` on `list.first`

Kacaii 791eb5d3 146e097c

+34 -42
+34 -42
src/app/domain/occurrence/register_new_occurrence.gleam
··· 47 47 use <- wisp.require_method(request, http.Post) 48 48 use body <- wisp.require_json(request) 49 49 50 - // Decode the request's body 51 50 case decode.run(body, body_decoder()) { 52 - // Handle possible errors 53 51 Error(err) -> web.handle_decode_error(err) 54 - // Process the parsed data 55 52 Ok(body) -> handle_body(request:, ctx:, body:) 56 53 } 57 54 } ··· 59 56 fn handle_body( 60 57 request request: wisp.Request, 61 58 ctx ctx: Context, 62 - body body: RegisterOccurrenceBody, 59 + body body: RequestBody, 63 60 ) -> wisp.Response { 64 - // Insert the occurrence on the DataBase 65 61 case insert_occurrence(request:, ctx:, body:) { 66 - //  Handle possible errors 67 62 Error(err) -> handle_error(err) 68 - //  Send a response to the client 69 - Ok(data) -> { 70 - wisp.json_response(data, 201) 71 - } 63 + Ok(data) -> wisp.json_response(data, 201) 72 64 } 73 65 } 74 66 75 - /// 󱐁 Form data submitted by the client 76 - pub opaque type RegisterOccurrenceBody { 77 - RegisterOccurrenceBody( 67 + pub opaque type RequestBody { 68 + RequestBody( 78 69 ///  Occurrence category 79 70 occurrence_category: category.Category, 80 71 ///  Occurrence subcategory ··· 92 83 ) 93 84 } 94 85 95 - /// Registering a new occurrence can fail 96 86 type RegisterNewOccurrenceError { 97 87 /// Failed to authenticate the user 98 88 AccessControl(user.AuthenticationError) ··· 108 98 case err { 109 99 AccessControl(err) -> user.handle_authentication_error(err) 110 100 DataBase(err) -> web.handle_database_error(err) 111 - FailedToAssignBrigade(id) -> 112 - wisp.internal_server_error() 113 - |> wisp.set_body(wisp.Text( 114 - "Não foi possível designar a equipe: " <> uuid.to_string(id), 115 - )) 101 + FailedToAssignBrigade(id) -> { 102 + let body = "Não foi possível designar a equipe: " <> uuid.to_string(id) 103 + wisp.Text(body) 104 + |> wisp.set_body(wisp.internal_server_error(), _) 105 + } 116 106 OccurrenceNotCreated -> 117 - wisp.internal_server_error() 118 - |> wisp.set_body(wisp.Text("A ocorrência não foi registrada")) 107 + "A ocorrência não foi registrada" 108 + |> wisp.Text 109 + |> wisp.set_body(wisp.internal_server_error(), _) 119 110 } 120 111 } 121 112 122 - fn body_decoder() -> decode.Decoder(RegisterOccurrenceBody) { 113 + fn body_decoder() -> decode.Decoder(RequestBody) { 123 114 let brigade_uuid_decoder = { 124 115 use maybe_uuid <- decode.then(decode.string) 125 116 case uuid.from_string(maybe_uuid) { ··· 138 129 decode.list(brigade_uuid_decoder), 139 130 ) 140 131 141 - decode.success(RegisterOccurrenceBody( 132 + decode.success(RequestBody( 142 133 occurrence_category: occ_category, 143 134 occurrence_subcategory: occ_subcategory, 144 135 priority: occ_priority, ··· 152 143 fn insert_occurrence( 153 144 request request: wisp.Request, 154 145 ctx ctx: Context, 155 - body body: RegisterOccurrenceBody, 146 + body body: RequestBody, 156 147 ) -> Result(String, RegisterNewOccurrenceError) { 157 148 use applicant_uuid <- result.try( 158 149 user.extract_uuid(request:, cookie_name: user.uuid_cookie_name) ··· 173 164 |> result.map_error(DataBase), 174 165 ) 175 166 176 - use row <- result.try(case list.first(returned.rows) { 177 - Error(_) -> Error(OccurrenceNotCreated) 178 - Ok(row) -> Ok(row) 179 - }) 167 + use row <- result.try( 168 + list.first(returned.rows) 169 + |> result.replace_error(OccurrenceNotCreated), 170 + ) 180 171 181 172 use assigned_brigades <- result.map(try_assign_brigades( 182 173 ctx:, ··· 208 199 let registry = group_registry.get_registry(ctx.registry_name) 209 200 occurrence.notify_new_occurrence(new: row.id, of: occ_category, registry:) 210 201 211 - json.to_string( 212 - json.object([ 213 - #("id", uuid.to_string(row.id) |> json.string), 214 - #("applicant_id", uuid.to_string(row.id) |> json.string), 215 - #("priority", json.string(occurrence_priority)), 216 - #("assigned_brigades", assigned_brigades_json), 217 - #("created_at", json.float(timestamp.to_unix_seconds(row.created_at))), 218 - ]), 219 - ) 202 + json.object([ 203 + #("id", uuid.to_string(row.id) |> json.string), 204 + #("applicant_id", uuid.to_string(row.id) |> json.string), 205 + #("priority", json.string(occurrence_priority)), 206 + #("assigned_brigades", assigned_brigades_json), 207 + #("created_at", json.float(timestamp.to_unix_seconds(row.created_at))), 208 + ]) 209 + |> json.to_string 220 210 } 221 211 222 212 fn try_assign_brigades( ··· 235 225 } 236 226 237 227 use assigned_users <- result.try({ 238 - use returned <- result.try( 228 + use returned <- result.map( 239 229 sql.query_participants(ctx.db, occurrence_id) 240 230 |> result.map_error(DataBase), 241 231 ) 242 232 243 - list.map(returned.rows, fn(row) { row.user_id }) 244 - |> Ok 233 + use row <- list.map(returned.rows) 234 + row.user_id 245 235 }) 246 236 247 237 //  BROADCAST -------------------------------------------------------------- ··· 263 253 } 264 254 } 265 255 266 - fn category_to_enum(category: category.Category) { 256 + fn category_to_enum(category: category.Category) -> sql.OccurrenceCategoryEnum { 267 257 case category { 268 258 category.Fire -> sql.Fire 269 259 category.MedicEmergency -> sql.MedicEmergency ··· 272 262 } 273 263 } 274 264 275 - fn subcategory_to_enum(subcategory: subcategory.Subcategory) { 265 + fn subcategory_to_enum( 266 + subcategory: subcategory.Subcategory, 267 + ) -> sql.OccurrenceSubcategoryEnum { 276 268 case subcategory { 277 269 subcategory.Collision -> sql.Collision 278 270 subcategory.Comercial -> sql.Comercial