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

:recycle: use priority decoders

+85 -96
+15 -7
src/app/domain/occurrence/priority.gleam
··· 15 15 } 16 16 } 17 17 18 - pub fn to_json(priority: Priority) -> json.Json { 19 - to_string(priority) 20 - |> json.string 21 - } 22 - 23 18 pub fn to_string_pt_br(value: Priority) { 24 19 case value { 25 20 High -> "alta" 26 21 Low -> "baixa" 27 22 Medium -> "média" 28 23 } 24 + } 25 + 26 + pub fn to_json(priority: Priority) -> json.Json { 27 + to_string(priority) 28 + |> json.string 29 29 } 30 30 31 31 pub fn from_string(maybe_priority: String) -> Result(Priority, Nil) { ··· 48 48 } 49 49 } 50 50 51 - pub fn decoder() { 51 + pub fn decoder() -> decode.Decoder(Priority) { 52 52 use prioriry_string <- decode.then(decode.string) 53 53 case from_string(prioriry_string) { 54 - Error(_) -> decode.failure(Low, "prioridade") 54 + Error(_) -> decode.failure(Low, "priority") 55 + Ok(value) -> decode.success(value) 56 + } 57 + } 58 + 59 + pub fn decoder_pt_br() -> decode.Decoder(Priority) { 60 + use prioriry_string <- decode.then(decode.string) 61 + case from_string_pt_br(prioriry_string) { 62 + Error(_) -> decode.failure(Low, "priority") 55 63 Ok(value) -> decode.success(value) 56 64 } 57 65 }
+25 -32
src/app/domain/occurrence/register_new_occurrence.gleam
··· 112 112 } 113 113 114 114 fn body_decoder() -> decode.Decoder(RequestBody) { 115 - let brigade_uuid_decoder = { 116 - use maybe_uuid <- decode.then(decode.string) 117 - case uuid.from_string(maybe_uuid) { 118 - Ok(value) -> decode.success(value) 119 - Error(_) -> decode.failure(uuid.v7(), "brigade_uuid") 120 - } 121 - } 122 - use occ_category <- decode.field("categoria", category.decoder()) 123 - use occ_subcategory <- decode.field("subcategoria", subcategory.decoder()) 124 - use occ_priority <- decode.field("prioridade", priority.decoder()) 125 - use occ_description <- decode.field("descricao", decode.string) 126 - use occ_location <- decode.field("gps", decode.list(decode.float)) 127 - use occ_reference_point <- decode.field("pontoDeReferencia", decode.string) 128 - use assigned_brigades_id <- decode.field( 129 - "idEquipes", 130 - decode.list(brigade_uuid_decoder), 115 + let brigade_list_decoder = decode.list(web.uuid_decoder("brigade_uuid")) 116 + 117 + use occurrence_category <- decode.field("categoria", category.decoder()) 118 + use occurrence_subcategory <- decode.field( 119 + "subcategoria", 120 + subcategory.decoder(), 131 121 ) 122 + use priority <- decode.field("prioridade", priority.decoder()) 123 + use description <- decode.field("descricao", decode.string) 124 + use location <- decode.field("gps", decode.list(decode.float)) 125 + use reference_point <- decode.field("pontoDeReferencia", decode.string) 126 + use brigade_list <- decode.field("idEquipes", brigade_list_decoder) 132 127 133 128 decode.success(RequestBody( 134 - occurrence_category: occ_category, 135 - occurrence_subcategory: occ_subcategory, 136 - priority: occ_priority, 137 - description: occ_description, 138 - location: occ_location, 139 - reference_point: occ_reference_point, 140 - brigade_list: assigned_brigades_id, 129 + occurrence_category:, 130 + occurrence_subcategory:, 131 + priority:, 132 + description:, 133 + location:, 134 + reference_point:, 135 + brigade_list:, 141 136 )) 142 137 } 143 138 144 139 fn insert_occurrence( 145 - request request: wisp.Request, 140 + request req: wisp.Request, 146 141 ctx ctx: Context, 147 142 body body: RequestBody, 148 143 ) -> Result(String, RegisterNewOccurrenceError) { 149 144 use token <- result.try( 150 - session.extract(request) 145 + session.extract(req) 151 146 |> result.map_error(Session), 152 147 ) 153 148 ··· 225 220 row.inserted_brigade_id 226 221 } 227 222 228 - use assigned_users <- result.try({ 223 + use assigned_users <- result.map({ 229 224 use returned <- result.map( 230 225 sql.query_participants(ctx.db, occurrence_id) 231 226 |> result.map_error(DataBase), 232 227 ) 233 228 234 - use row <- list.map(returned.rows) 235 - row.user_id 229 + list.map(returned.rows, fn(row) { row.user_id }) 236 230 }) 237 231 238 232 //  BROADCAST -------------------------------------------------------------- 239 233 let registry = group_registry.get_registry(ctx.registry_name) 240 234 list.each(assigned_users, fn(user_id) { 241 - let body = 242 - msg.Domain(msg.UserAssignedToOccurrence(user_id:, occurrence_id:)) 243 - user.broadcast(registry, user_id, body) 235 + msg.Domain(msg.UserAssignedToOccurrence(user_id:, occurrence_id:)) 236 + |> user.broadcast(registry, user_id, _) 244 237 }) 245 238 246 - Ok(assigned_brigades) 239 + assigned_brigades 247 240 } 248 241 249 242 fn priority_to_enum(priority: priority.Priority) -> sql.OccurrencePriorityEnum {
+31 -24
src/app/domain/role.gleam
··· 11 11 Admin 12 12 } 13 13 14 - pub fn decoder() { 14 + pub fn decoder() -> decode.Decoder(Role) { 15 15 use maybe_role <- decode.then(decode.string) 16 16 case from_string(maybe_role) { 17 17 Error(_) -> decode.failure(Firefighter, "user_role") ··· 19 19 } 20 20 } 21 21 22 - pub fn from_string_pt_br(role_name role_name: String) -> Result(Role, String) { 23 - case string.lowercase(role_name) { 24 - "administrador" -> Ok(Admin) 25 - "analista" -> Ok(Analyst) 26 - "bombeiro" -> Ok(Firefighter) 27 - "capitão" -> Ok(Captain) 28 - "desenvolvedor" -> Ok(Developer) 29 - "sargento" -> Ok(Sargeant) 30 - 31 - unknown -> Error(unknown) 32 - } 33 - } 34 - 35 - pub fn to_string_pt_br(user_role user_role: Role) -> String { 36 - case user_role { 37 - Admin -> "administrador" 38 - Analyst -> "analista" 39 - Captain -> "capitão" 40 - Developer -> "desenvolvedor" 41 - Firefighter -> "bombeiro" 42 - Sargeant -> "sargento" 22 + pub fn decoder_pt_br() -> decode.Decoder(Role) { 23 + use maybe_role <- decode.then(decode.string) 24 + case from_string_pt_br(maybe_role) { 25 + Error(_) -> decode.failure(Firefighter, "user_role") 26 + Ok(value) -> decode.success(value) 43 27 } 44 28 } 45 29 ··· 56 40 } 57 41 } 58 42 43 + pub fn from_string_pt_br(role_name role_name: String) -> Result(Role, String) { 44 + case string.lowercase(role_name) { 45 + "administrador" -> Ok(Admin) 46 + "analista" -> Ok(Analyst) 47 + "bombeiro" -> Ok(Firefighter) 48 + "capitão" -> Ok(Captain) 49 + "desenvolvedor" -> Ok(Developer) 50 + "sargento" -> Ok(Sargeant) 51 + 52 + unknown -> Error(unknown) 53 + } 54 + } 55 + 59 56 pub fn to_string(user_role user_role: Role) -> String { 60 57 case user_role { 61 58 Admin -> "admin" ··· 67 64 } 68 65 } 69 66 67 + pub fn to_string_pt_br(user_role user_role: Role) -> String { 68 + case user_role { 69 + Admin -> "administrador" 70 + Analyst -> "analista" 71 + Captain -> "capitão" 72 + Developer -> "desenvolvedor" 73 + Firefighter -> "bombeiro" 74 + Sargeant -> "sargento" 75 + } 76 + } 77 + 70 78 pub fn to_json(role: Role) -> json.Json { 71 - role 72 - |> to_string 79 + to_string(role) 73 80 |> json.string 74 81 }
+14 -33
test/occurrence_test.gleam
··· 41 41 let oc_subcategory = subcategory.to_string(dummy.random_subcategory()) 42 42 let oc_priority = priority.to_string(dummy.random_priority()) 43 43 let coords = [float.random() *. 100.0, float.random() *. 100.0] 44 + let oc_assigned_brigades = 45 + json.array([uuid.to_string(dummy_brigade_id)], json.string) 44 46 45 47 let req = 46 48 simulate.browser_request(http.Post, "/occurrence/new") ··· 52 54 #("descricao", json.string(wisp.random_string(33))), 53 55 #("gps", json.array(coords, json.float)), 54 56 #("pontoDeReferencia", json.string(wisp.random_string(33))), 55 - #( 56 - "idEquipes", 57 - json.array([uuid.to_string(dummy_brigade_id)], json.string), 58 - ), 57 + #("idEquipes", oc_assigned_brigades), 59 58 ]), 60 59 ) 61 60 ··· 72 71 let body = simulate.read_body(resp) 73 72 let assert Ok(dummy_occurrence_id) = 74 73 json.parse(body, { 75 - let priority_decoder = { 76 - use maybe_priority <- decode.then(decode.string) 77 - case priority.from_string_pt_br(maybe_priority) { 78 - Error(_) -> decode.failure(priority.Low, "priority") 79 - Ok(value) -> decode.success(value) 80 - } 81 - } 82 - 83 - use _ <- decode.field("priority", priority_decoder) 74 + use _ <- decode.field("priority", priority.decoder_pt_br()) 84 75 85 76 use dummy_occurrence_id <- decode.field( 86 77 "id", ··· 164 155 // JSON PARSING -------------------------------------------------------------- 165 156 let assert Ok(_) = 166 157 json.parse(body, { 167 - let priority_decoder = { 168 - use maybe_priority <- decode.then(decode.string) 169 - case priority.from_string_pt_br(maybe_priority) { 170 - Error(_) -> decode.failure(priority.Low, "prioridade") 171 - Ok(value) -> decode.success(value) 172 - } 173 - } 174 - 175 158 let call_decoder = { 176 159 use _ <- decode.field("tipo", category.decoder_pt_br()) 177 160 use _ <- decode.field("detalhes", decode.string) ··· 196 179 decode.success(Nil) 197 180 } 198 181 199 - let occurrence_brigade_decoder = { 200 - use _ <- decode.field("id", web.uuid_decoder("brigade_uuid")) 201 - use _ <- decode.field("nomeEquipe", decode.string) 202 - use _ <- decode.field("codigoViatura", decode.string) 203 - use _ <- decode.field("lider", decode.string) 204 - decode.success(Nil) 205 - } 182 + let occurrence_brigade_list_decoder = 183 + decode.list({ 184 + use _ <- decode.field("id", web.uuid_decoder("brigade_uuid")) 185 + use _ <- decode.field("nomeEquipe", decode.string) 186 + use _ <- decode.field("codigoViatura", decode.string) 187 + use _ <- decode.field("lider", decode.string) 188 + decode.success(Nil) 189 + }) 206 190 207 191 decode.list({ 208 192 use id <- decode.field("id", web.uuid_decoder("call_uuid?")) 209 193 use _ <- decode.field("status", decode.string) 210 - use _ <- decode.field("prioridade", priority_decoder) 194 + use _ <- decode.field("prioridade", priority.decoder_pt_br()) 211 195 use _ <- decode.field("chamado", call_decoder) 212 196 use _ <- decode.field( 213 197 "coordenadas", ··· 215 199 ) 216 200 use _ <- decode.field("timestamps", occurrence_timestamp_decoder) 217 201 use _ <- decode.field("metadata", occurrence_metadata_decoder) 218 - use _ <- decode.field( 219 - "equipes", 220 - decode.list(occurrence_brigade_decoder), 221 - ) 202 + use _ <- decode.field("equipes", occurrence_brigade_list_decoder) 222 203 223 204 assert id == dummy_occurrence 224 205 as "Occurence uuid should be the same as the dummy one"