wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
at main 692 lines 21 kB view raw
1//// This module contains the code to run the sql queries defined in 2//// `./src/app/domain/user/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/option.{type Option} 9import pog 10import youid/uuid.{type Uuid} 11 12/// A row you get from running the `delete_user_by_id` query 13/// defined in `./src/app/domain/user/sql/delete_user_by_id.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 DeleteUserByIdRow { 19 DeleteUserByIdRow(id: Uuid, full_name: String) 20} 21 22///  Remove and user from the database 23/// 24/// > 🐿️ This function was generated automatically using v4.6.0 of 25/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 26/// 27pub fn delete_user_by_id( 28 db: pog.Connection, 29 arg_1: Uuid, 30) -> Result(pog.Returned(DeleteUserByIdRow), pog.QueryError) { 31 let decoder = { 32 use id <- decode.field(0, uuid_decoder()) 33 use full_name <- decode.field(1, decode.string) 34 decode.success(DeleteUserByIdRow(id:, full_name:)) 35 } 36 37 "--  Remove and user from the database 38delete from public.user_account as u 39where u.id = $1 40returning u.id, u.full_name; 41" 42 |> pog.query 43 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 44 |> pog.returning(decoder) 45 |> pog.execute(db) 46} 47 48/// A row you get from running the `get_complete_user_profiles` query 49/// defined in `./src/app/domain/user/sql/get_complete_user_profiles.sql`. 50/// 51/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 52/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 53/// 54pub type GetCompleteUserProfilesRow { 55 GetCompleteUserProfilesRow( 56 id: Uuid, 57 full_name: String, 58 registration: String, 59 email: String, 60 user_role: UserRoleEnum, 61 is_active: Bool, 62 ) 63} 64 65/// 󰀖 Find all users on the database 66/// 67/// > 🐿️ This function was generated automatically using v4.6.0 of 68/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 69/// 70pub fn get_complete_user_profiles( 71 db: pog.Connection, 72) -> Result(pog.Returned(GetCompleteUserProfilesRow), pog.QueryError) { 73 let decoder = { 74 use id <- decode.field(0, uuid_decoder()) 75 use full_name <- decode.field(1, decode.string) 76 use registration <- decode.field(2, decode.string) 77 use email <- decode.field(3, decode.string) 78 use user_role <- decode.field(4, user_role_enum_decoder()) 79 use is_active <- decode.field(5, decode.bool) 80 decode.success(GetCompleteUserProfilesRow( 81 id:, 82 full_name:, 83 registration:, 84 email:, 85 user_role:, 86 is_active:, 87 )) 88 } 89 90 "-- 󰀖 Find all users on the database 91select 92 u.id, 93 u.full_name, 94 u.registration, 95 u.email, 96 u.user_role, 97 u.is_active 98from public.user_account as u; 99" 100 |> pog.query 101 |> pog.returning(decoder) 102 |> pog.execute(db) 103} 104 105/// A row you get from running the `insert_new_user` query 106/// defined in `./src/app/domain/user/sql/insert_new_user.sql`. 107/// 108/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 109/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 110/// 111pub type InsertNewUserRow { 112 InsertNewUserRow(id: Uuid) 113} 114 115///  Inserts a new user into the database 116/// 117/// > 🐿️ This function was generated automatically using v4.6.0 of 118/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 119/// 120pub fn insert_new_user( 121 db: pog.Connection, 122 arg_1: String, 123 arg_2: String, 124 arg_3: String, 125 arg_4: String, 126 arg_5: String, 127 arg_6: UserRoleEnum, 128) -> Result(pog.Returned(InsertNewUserRow), pog.QueryError) { 129 let decoder = { 130 use id <- decode.field(0, uuid_decoder()) 131 decode.success(InsertNewUserRow(id:)) 132 } 133 134 "--  Inserts a new user into the database 135insert into public.user_account as u 136( 137 full_name, 138 registration, 139 phone, 140 email, 141 password_hash, 142 user_role 143) 144values ($1, $2, $3, $4, $5, $6) 145returning u.id; 146" 147 |> pog.query 148 |> pog.parameter(pog.text(arg_1)) 149 |> pog.parameter(pog.text(arg_2)) 150 |> pog.parameter(pog.text(arg_3)) 151 |> pog.parameter(pog.text(arg_4)) 152 |> pog.parameter(pog.text(arg_5)) 153 |> pog.parameter(user_role_enum_encoder(arg_6)) 154 |> pog.returning(decoder) 155 |> pog.execute(db) 156} 157 158/// A row you get from running the `query_crew_members` query 159/// defined in `./src/app/domain/user/sql/query_crew_members.sql`. 160/// 161/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 162/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 163/// 164pub type QueryCrewMembersRow { 165 QueryCrewMembersRow( 166 id: Uuid, 167 full_name: String, 168 user_role: UserRoleEnum, 169 brigade_id: Uuid, 170 ) 171} 172 173/// 󰢫 Retrieves detailed information about fellow brigade members 174/// for a given user, including their names and role details. 175/// 176/// > 🐿️ This function was generated automatically using v4.6.0 of 177/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 178/// 179pub fn query_crew_members( 180 db: pog.Connection, 181 arg_1: Uuid, 182) -> Result(pog.Returned(QueryCrewMembersRow), pog.QueryError) { 183 let decoder = { 184 use id <- decode.field(0, uuid_decoder()) 185 use full_name <- decode.field(1, decode.string) 186 use user_role <- decode.field(2, user_role_enum_decoder()) 187 use brigade_id <- decode.field(3, uuid_decoder()) 188 decode.success(QueryCrewMembersRow(id:, full_name:, user_role:, brigade_id:)) 189 } 190 191 "-- 󰢫 Retrieves detailed information about fellow brigade members 192-- for a given user, including their names and role details. 193select 194 u.id, 195 u.full_name, 196 u.user_role, 197 crew.brigade_id 198from public.query_crew_members($1) as crew 199inner join public.user_account as u 200 on crew.member_id = u.id; 201" 202 |> pog.query 203 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 204 |> pog.returning(decoder) 205 |> pog.execute(db) 206} 207 208/// A row you get from running the `query_login_token` query 209/// defined in `./src/app/domain/user/sql/query_login_token.sql`. 210/// 211/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 212/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 213/// 214pub type QueryLoginTokenRow { 215 QueryLoginTokenRow(id: Uuid, password_hash: String, user_role: UserRoleEnum) 216} 217 218///  Retrieves a user's ID and password hash from their registration 219/// number for authentication purposes. 220/// 221/// > 🐿️ This function was generated automatically using v4.6.0 of 222/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 223/// 224pub fn query_login_token( 225 db: pog.Connection, 226 arg_1: String, 227) -> Result(pog.Returned(QueryLoginTokenRow), pog.QueryError) { 228 let decoder = { 229 use id <- decode.field(0, uuid_decoder()) 230 use password_hash <- decode.field(1, decode.string) 231 use user_role <- decode.field(2, user_role_enum_decoder()) 232 decode.success(QueryLoginTokenRow(id:, password_hash:, user_role:)) 233 } 234 235 "--  Retrieves a user's ID and password hash from their registration 236-- number for authentication purposes. 237select 238 u.id, 239 u.password_hash, 240 u.user_role 241from public.user_account as u 242where u.registration = $1; 243" 244 |> pog.query 245 |> pog.parameter(pog.text(arg_1)) 246 |> pog.returning(decoder) 247 |> pog.execute(db) 248} 249 250/// A row you get from running the `query_occurrences_by_participant` query 251/// defined in `./src/app/domain/user/sql/query_occurrences_by_participant.sql`. 252/// 253/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 254/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 255/// 256pub type QueryOccurrencesByParticipantRow { 257 QueryOccurrencesByParticipantRow(id: Uuid) 258} 259 260/// 󰡦 Find all occurrences a user participated in 261/// 262/// > 🐿️ This function was generated automatically using v4.6.0 of 263/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 264/// 265pub fn query_occurrences_by_participant( 266 db: pog.Connection, 267 arg_1: Uuid, 268) -> Result(pog.Returned(QueryOccurrencesByParticipantRow), pog.QueryError) { 269 let decoder = { 270 use id <- decode.field(0, uuid_decoder()) 271 decode.success(QueryOccurrencesByParticipantRow(id:)) 272 } 273 274 "-- 󰡦 Find all occurrences a user participated in 275select u.id 276from public.user_account as u 277inner join public.brigade_membership as bm 278 on u.id = bm.user_id 279inner join public.occurrence_brigade as ob 280 on bm.brigade_id = ob.brigade_id 281where ob.occurrence_id = $1; 282" 283 |> pog.query 284 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 285 |> pog.returning(decoder) 286 |> pog.execute(db) 287} 288 289/// A row you get from running the `query_user_brigades` query 290/// defined in `./src/app/domain/user/sql/query_user_brigades.sql`. 291/// 292/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 293/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 294/// 295pub type QueryUserBrigadesRow { 296 QueryUserBrigadesRow(brigade_id: Uuid) 297} 298 299///  Find all brigades an user is assigned to 300/// 301/// > 🐿️ This function was generated automatically using v4.6.0 of 302/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 303/// 304pub fn query_user_brigades( 305 db: pog.Connection, 306 arg_1: Uuid, 307) -> Result(pog.Returned(QueryUserBrigadesRow), pog.QueryError) { 308 let decoder = { 309 use brigade_id <- decode.field(0, uuid_decoder()) 310 decode.success(QueryUserBrigadesRow(brigade_id:)) 311 } 312 313 "--  Find all brigades an user is assigned to 314select bm.brigade_id 315from public.brigade_membership as bm 316where bm.user_id = $1; 317" 318 |> pog.query 319 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 320 |> pog.returning(decoder) 321 |> pog.execute(db) 322} 323 324/// A row you get from running the `query_user_id_by_registration` query 325/// defined in `./src/app/domain/user/sql/query_user_id_by_registration.sql`. 326/// 327/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 328/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 329/// 330pub type QueryUserIdByRegistrationRow { 331 QueryUserIdByRegistrationRow(id: Uuid) 332} 333 334///  Retrieves a user's ID from their registration number. 335/// 336/// > 🐿️ This function was generated automatically using v4.6.0 of 337/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 338/// 339pub fn query_user_id_by_registration( 340 db: pog.Connection, 341 arg_1: String, 342) -> Result(pog.Returned(QueryUserIdByRegistrationRow), pog.QueryError) { 343 let decoder = { 344 use id <- decode.field(0, uuid_decoder()) 345 decode.success(QueryUserIdByRegistrationRow(id:)) 346 } 347 348 "--  Retrieves a user's ID from their registration number. 349select u.id 350from public.user_account as u 351where u.registration = $1; 352" 353 |> pog.query 354 |> pog.parameter(pog.text(arg_1)) 355 |> pog.returning(decoder) 356 |> pog.execute(db) 357} 358 359/// A row you get from running the `query_user_name` query 360/// defined in `./src/app/domain/user/sql/query_user_name.sql`. 361/// 362/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 363/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 364/// 365pub type QueryUserNameRow { 366 QueryUserNameRow(full_name: String) 367} 368 369///  Retrieves a user's full name by their user ID. 370/// 371/// > 🐿️ This function was generated automatically using v4.6.0 of 372/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 373/// 374pub fn query_user_name( 375 db: pog.Connection, 376 arg_1: Uuid, 377) -> Result(pog.Returned(QueryUserNameRow), pog.QueryError) { 378 let decoder = { 379 use full_name <- decode.field(0, decode.string) 380 decode.success(QueryUserNameRow(full_name:)) 381 } 382 383 "--  Retrieves a user's full name by their user ID. 384select u.full_name 385from public.user_account as u 386where u.id = $1; 387" 388 |> pog.query 389 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 390 |> pog.returning(decoder) 391 |> pog.execute(db) 392} 393 394/// A row you get from running the `query_user_password` query 395/// defined in `./src/app/domain/user/sql/query_user_password.sql`. 396/// 397/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 398/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 399/// 400pub type QueryUserPasswordRow { 401 QueryUserPasswordRow(password_hash: String) 402} 403 404///  Find the password hash from an user 405/// 406/// > 🐿️ This function was generated automatically using v4.6.0 of 407/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 408/// 409pub fn query_user_password( 410 db: pog.Connection, 411 arg_1: Uuid, 412) -> Result(pog.Returned(QueryUserPasswordRow), pog.QueryError) { 413 let decoder = { 414 use password_hash <- decode.field(0, decode.string) 415 decode.success(QueryUserPasswordRow(password_hash:)) 416 } 417 418 "--  Find the password hash from an user 419select u.password_hash 420from public.user_account as u 421where u.id = $1; 422" 423 |> pog.query 424 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 425 |> pog.returning(decoder) 426 |> pog.execute(db) 427} 428 429/// A row you get from running the `query_user_profile` query 430/// defined in `./src/app/domain/user/sql/query_user_profile.sql`. 431/// 432/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 433/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 434/// 435pub type QueryUserProfileRow { 436 QueryUserProfileRow( 437 id: Uuid, 438 full_name: String, 439 registration: String, 440 user_role: UserRoleEnum, 441 email: String, 442 phone: Option(String), 443 ) 444} 445 446/// 󰀖 Find basic information about an user account 447/// 448/// > 🐿️ This function was generated automatically using v4.6.0 of 449/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 450/// 451pub fn query_user_profile( 452 db: pog.Connection, 453 arg_1: Uuid, 454) -> Result(pog.Returned(QueryUserProfileRow), pog.QueryError) { 455 let decoder = { 456 use id <- decode.field(0, uuid_decoder()) 457 use full_name <- decode.field(1, decode.string) 458 use registration <- decode.field(2, decode.string) 459 use user_role <- decode.field(3, user_role_enum_decoder()) 460 use email <- decode.field(4, decode.string) 461 use phone <- decode.field(5, decode.optional(decode.string)) 462 decode.success(QueryUserProfileRow( 463 id:, 464 full_name:, 465 registration:, 466 user_role:, 467 email:, 468 phone:, 469 )) 470 } 471 472 "-- 󰀖 Find basic information about an user account 473select 474 u.id, 475 u.full_name, 476 u.registration, 477 u.user_role, 478 u.email, 479 u.phone 480from public.user_account as u 481where u.id = $1; 482" 483 |> pog.query 484 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 485 |> pog.returning(decoder) 486 |> pog.execute(db) 487} 488 489/// A row you get from running the `query_user_role` query 490/// defined in `./src/app/domain/user/sql/query_user_role.sql`. 491/// 492/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 493/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 494/// 495pub type QueryUserRoleRow { 496 QueryUserRoleRow(user_role: UserRoleEnum) 497} 498 499/// 󰀖 Find user access level 500/// 501/// > 🐿️ This function was generated automatically using v4.6.0 of 502/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 503/// 504pub fn query_user_role( 505 db: pog.Connection, 506 arg_1: Uuid, 507) -> Result(pog.Returned(QueryUserRoleRow), pog.QueryError) { 508 let decoder = { 509 use user_role <- decode.field(0, user_role_enum_decoder()) 510 decode.success(QueryUserRoleRow(user_role:)) 511 } 512 513 "-- 󰀖 Find user access level 514select u.user_role 515from 516 public.user_account as u 517where u.id = $1; 518" 519 |> pog.query 520 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 521 |> pog.returning(decoder) 522 |> pog.execute(db) 523} 524 525///  Set an new value to the password of an user 526/// 527/// > 🐿️ This function was generated automatically using v4.6.0 of 528/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 529/// 530pub fn update_user_password( 531 db: pog.Connection, 532 arg_1: Uuid, 533 arg_2: String, 534) -> Result(pog.Returned(Nil), pog.QueryError) { 535 let decoder = decode.map(decode.dynamic, fn(_) { Nil }) 536 537 "--  Set an new value to the password of an user 538update public.user_account 539set 540 password_hash = $2, 541 updated_at = current_timestamp 542where id = $1; 543" 544 |> pog.query 545 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 546 |> pog.parameter(pog.text(arg_2)) 547 |> pog.returning(decoder) 548 |> pog.execute(db) 549} 550 551/// A row you get from running the `update_user_profile` query 552/// defined in `./src/app/domain/user/sql/update_user_profile.sql`. 553/// 554/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 555/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 556/// 557pub type UpdateUserProfileRow { 558 UpdateUserProfileRow(full_name: String, email: String, phone: Option(String)) 559} 560 561///  Update an authenticated user profile 562/// 563/// > 🐿️ This function was generated automatically using v4.6.0 of 564/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 565/// 566pub fn update_user_profile( 567 db: pog.Connection, 568 arg_1: Uuid, 569 arg_2: String, 570 arg_3: String, 571 arg_4: String, 572) -> Result(pog.Returned(UpdateUserProfileRow), pog.QueryError) { 573 let decoder = { 574 use full_name <- decode.field(0, decode.string) 575 use email <- decode.field(1, decode.string) 576 use phone <- decode.field(2, decode.optional(decode.string)) 577 decode.success(UpdateUserProfileRow(full_name:, email:, phone:)) 578 } 579 580 "--  Update an authenticated user profile 581update public.user_account as u set 582 full_name = $2, 583 email = $3, 584 phone = $4 585where u.id = $1 586returning 587 u.full_name, 588 u.email, 589 u.phone; 590" 591 |> pog.query 592 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 593 |> pog.parameter(pog.text(arg_2)) 594 |> pog.parameter(pog.text(arg_3)) 595 |> pog.parameter(pog.text(arg_4)) 596 |> pog.returning(decoder) 597 |> pog.execute(db) 598} 599 600/// A row you get from running the `update_user_status` query 601/// defined in `./src/app/domain/user/sql/update_user_status.sql`. 602/// 603/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 604/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 605/// 606pub type UpdateUserStatusRow { 607 UpdateUserStatusRow(id: Uuid, is_active: Bool) 608} 609 610/// 󰚰 Update an user `is_active` field 611/// 612/// > 🐿️ This function was generated automatically using v4.6.0 of 613/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 614/// 615pub fn update_user_status( 616 db: pog.Connection, 617 arg_1: Uuid, 618 arg_2: Bool, 619) -> Result(pog.Returned(UpdateUserStatusRow), pog.QueryError) { 620 let decoder = { 621 use id <- decode.field(0, uuid_decoder()) 622 use is_active <- decode.field(1, decode.bool) 623 decode.success(UpdateUserStatusRow(id:, is_active:)) 624 } 625 626 "-- 󰚰 Update an user `is_active` field 627update public.user_account as u 628set 629 is_active = $2, 630 updated_at = current_timestamp 631where u.id = $1 632returning u.id, u.is_active; 633" 634 |> pog.query 635 |> pog.parameter(pog.text(uuid.to_string(arg_1))) 636 |> pog.parameter(pog.bool(arg_2)) 637 |> pog.returning(decoder) 638 |> pog.execute(db) 639} 640 641// --- Enums ------------------------------------------------------------------- 642 643/// Corresponds to the Postgres `user_role_enum` enum. 644/// 645/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 646/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 647/// 648pub type UserRoleEnum { 649 Sargeant 650 Developer 651 Captain 652 Firefighter 653 Analyst 654 Admin 655} 656 657fn user_role_enum_decoder() -> decode.Decoder(UserRoleEnum) { 658 use user_role_enum <- decode.then(decode.string) 659 case user_role_enum { 660 "sargeant" -> decode.success(Sargeant) 661 "developer" -> decode.success(Developer) 662 "captain" -> decode.success(Captain) 663 "firefighter" -> decode.success(Firefighter) 664 "analyst" -> decode.success(Analyst) 665 "admin" -> decode.success(Admin) 666 _ -> decode.failure(Sargeant, "UserRoleEnum") 667 } 668} 669 670fn user_role_enum_encoder(user_role_enum) -> pog.Value { 671 case user_role_enum { 672 Sargeant -> "sargeant" 673 Developer -> "developer" 674 Captain -> "captain" 675 Firefighter -> "firefighter" 676 Analyst -> "analyst" 677 Admin -> "admin" 678 } 679 |> pog.text 680} 681 682// --- Encoding/decoding utils ------------------------------------------------- 683 684/// A decoder to decode `Uuid`s coming from a Postgres query. 685/// 686fn uuid_decoder() { 687 use bit_array <- decode.then(decode.bit_array) 688 case uuid.from_bit_array(bit_array) { 689 Ok(uuid) -> decode.success(uuid) 690 Error(_) -> decode.failure(uuid.v7(), "Uuid") 691 } 692}