wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
at main 176 lines 5.0 kB view raw
1//// This module contains the code to run the sql queries defined in 2//// `./src/app/domain/admin/sql`. 3//// > 🐿️ This module was generated automatically using v4.6.0 of 4//// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 5//// 6 7import gleam/dynamic/decode 8import gleam/time/timestamp.{type Timestamp} 9import pog 10import youid/uuid.{type Uuid} 11 12/// A row you get from running the `admin_update_user` query 13/// defined in `./src/app/domain/admin/sql/admin_update_user.sql`. 14/// 15/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 16/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 17/// 18pub type AdminUpdateUserRow { 19 AdminUpdateUserRow( 20 id: Uuid, 21 full_name: String, 22 email: String, 23 user_role: UserRoleEnum, 24 registration: String, 25 is_active: Bool, 26 updated_at: Timestamp, 27 ) 28} 29 30///  Update an user's information as admin 31/// 32/// > 🐿️ This function was generated automatically using v4.6.0 of 33/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 34/// 35pub fn admin_update_user( 36 db: pog.Connection, 37 arg_1: Uuid, 38 arg_2: String, 39 arg_3: String, 40 arg_4: UserRoleEnum, 41 arg_5: String, 42 arg_6: Bool, 43) -> Result(pog.Returned(AdminUpdateUserRow), pog.QueryError) { 44 let decoder = { 45 use id <- decode.field(0, uuid_decoder()) 46 use full_name <- decode.field(1, decode.string) 47 use email <- decode.field(2, decode.string) 48 use user_role <- decode.field(3, user_role_enum_decoder()) 49 use registration <- decode.field(4, decode.string) 50 use is_active <- decode.field(5, decode.bool) 51 use updated_at <- decode.field(6, pog.timestamp_decoder()) 52 decode.success(AdminUpdateUserRow( 53 id:, 54 full_name:, 55 email:, 56 user_role:, 57 registration:, 58 is_active:, 59 updated_at:, 60 )) 61 } 62 63 "--  Update an user's information as admin 64update public.user_account as u 65set 66 full_name = $2, 67 email = $3, 68 user_role = $4, 69 registration = $5, 70 is_active = $6, 71 updated_at = current_timestamp 72where u.id = $1 73returning 74 u.id, 75 u.full_name, 76 u.email, 77 u.user_role, 78 u.registration, 79 u.is_active, 80 u.updated_at; 81" 82 |> pog.query 83 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 84 |> pog.parameter(pog.text(arg_2)) 85 |> pog.parameter(pog.text(arg_3)) 86 |> pog.parameter(user_role_enum_encoder(arg_4)) 87 |> pog.parameter(pog.text(arg_5)) 88 |> pog.parameter(pog.bool(arg_6)) 89 |> pog.returning(decoder) 90 |> pog.execute(db) 91} 92 93/// A row you get from running the `count_total_users` query 94/// defined in `./src/app/domain/admin/sql/count_total_users.sql`. 95/// 96/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 97/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 98/// 99pub type CountTotalUsersRow { 100 CountTotalUsersRow(total: Int) 101} 102 103/// 󰆙 Count the total number of users in our system 104/// 105/// > 🐿️ This function was generated automatically using v4.6.0 of 106/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 107/// 108pub fn count_total_users( 109 db: pog.Connection, 110) -> Result(pog.Returned(CountTotalUsersRow), pog.QueryError) { 111 let decoder = { 112 use total <- decode.field(0, decode.int) 113 decode.success(CountTotalUsersRow(total:)) 114 } 115 116 "-- 󰆙 Count the total number of users in our system 117select count(u.id) as total 118from public.user_account as u; 119" 120 |> pog.query 121 |> pog.returning(decoder) 122 |> pog.execute(db) 123} 124 125// --- Enums ------------------------------------------------------------------- 126 127/// Corresponds to the Postgres `user_role_enum` enum. 128/// 129/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 130/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 131/// 132pub type UserRoleEnum { 133 Sargeant 134 Developer 135 Captain 136 Firefighter 137 Analyst 138 Admin 139} 140 141fn user_role_enum_decoder() -> decode.Decoder(UserRoleEnum) { 142 use user_role_enum <- decode.then(decode.string) 143 case user_role_enum { 144 "sargeant" -> decode.success(Sargeant) 145 "developer" -> decode.success(Developer) 146 "captain" -> decode.success(Captain) 147 "firefighter" -> decode.success(Firefighter) 148 "analyst" -> decode.success(Analyst) 149 "admin" -> decode.success(Admin) 150 _ -> decode.failure(Sargeant, "UserRoleEnum") 151 } 152} 153 154fn user_role_enum_encoder(user_role_enum) -> pog.Value { 155 case user_role_enum { 156 Sargeant -> "sargeant" 157 Developer -> "developer" 158 Captain -> "captain" 159 Firefighter -> "firefighter" 160 Analyst -> "analyst" 161 Admin -> "admin" 162 } 163 |> pog.text 164} 165 166// --- Encoding/decoding utils ------------------------------------------------- 167 168/// A decoder to decode `Uuid`s coming from a Postgres query. 169/// 170fn uuid_decoder() { 171 use bit_array <- decode.then(decode.bit_array) 172 case uuid.from_bit_array(bit_array) { 173 Ok(uuid) -> decode.success(uuid) 174 Error(_) -> decode.failure(uuid.v7(), "Uuid") 175 } 176}