···2323 let handler = router.handle_request(_, ctx)
24242525 let assert Ok(_) = supervision_tree.init(pog_config, handler, secret_key)
2626- let assert Ok(_) = seed.germinate(ctx.db, ctx.secret_key)
2626+ let assert Ok(_) = seed.germinate(ctx, ctx.secret_key)
27272828 // All done!
2929 process.sleep_forever()
+31-18
server/src/server/seed.gleam
···22import gleam/result
33import pog
44import server/auth
55+import server/context
56import server/seed/sql
77+import server/user
88+import shared/role
69710pub type UserTableState {
811 Empty
912 Populated
1013}
11141515+pub type SeedError {
1616+ Auth(auth.AuthError)
1717+ UserError(user.UserError)
1818+ Database(pog.QueryError)
1919+}
2020+1221pub const full_name = "SIGO"
13221423pub const password = "root"
···17261827pub const phone = "0000000000"
19282020-pub const role = sql.Admin
2929+pub const role = role.Admin
21302231/// ๎ถง Generate a root user when starting the system for the first time
2323-pub fn germinate(
2424- db: pog.Connection,
2525- salt: String,
2626-) -> Result(Nil, pog.QueryError) {
2727- use state <- result.try(guard(db))
3232+pub fn germinate(ctx: context.Context, salt: String) -> Result(Nil, SeedError) {
3333+ use state <- result.try(guard(ctx.db))
2834 use <- bool.guard(when: state == Populated, return: Ok(Nil))
3535+ let hashed = auth.hash(value: password, salt:)
29363030- let hashed = auth.hash(value: password, salt:)
3131- use _ <- result.map(sql.germinate(
3232- db,
3333- full_name,
3434- role,
3535- hashed,
3636- email,
3737- phone,
3838- True,
3939- ))
3737+ use _ <- result.map(
3838+ user.register(
3939+ ctx: ctx,
4040+ user_name: full_name,
4141+ user_role: role,
4242+ user_password: hashed,
4343+ user_email: email,
4444+ user_phone: phone,
4545+ is_active: True,
4646+ )
4747+ |> result.map_error(UserError),
4848+ )
40494150 Nil
4251}
43524444-fn guard(db: pog.Connection) -> Result(UserTableState, pog.QueryError) {
4545- use returned <- result.map(sql.guard(db))
5353+fn guard(db: pog.Connection) -> Result(UserTableState, SeedError) {
5454+ use returned <- result.map(
5555+ sql.guard(db)
5656+ |> result.map_error(Database),
5757+ )
5858+4659 case returned.count {
4760 0 -> Empty
4861 _ -> Populated
-65
server/src/server/seed/sql.gleam
···77import gleam/dynamic/decode
88import pog
991010-/// ๎ถง creates a default user when starting the system
1111-///
1212-/// > ๐ฟ๏ธ This function was generated automatically using v4.6.0 of
1313-/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
1414-///
1515-pub fn germinate(
1616- db: pog.Connection,
1717- arg_1: String,
1818- arg_2: UserRoleEnum,
1919- arg_3: String,
2020- arg_4: String,
2121- arg_5: String,
2222- arg_6: Bool,
2323-) -> Result(pog.Returned(Nil), pog.QueryError) {
2424- let decoder = decode.map(decode.dynamic, fn(_) { Nil })
2525-2626- "-- ๎ถง creates a default user when starting the system
2727-insert into public.user_account as u (
2828- full_name,
2929- user_role,
3030- password_hash,
3131- email,
3232- phone,
3333- is_active
3434-) values ($1, $2, $3, $4, $5, $6);
3535-"
3636- |> pog.query
3737- |> pog.parameter(pog.text(arg_1))
3838- |> pog.parameter(user_role_enum_encoder(arg_2))
3939- |> pog.parameter(pog.text(arg_3))
4040- |> pog.parameter(pog.text(arg_4))
4141- |> pog.parameter(pog.text(arg_5))
4242- |> pog.parameter(pog.bool(arg_6))
4343- |> pog.returning(decoder)
4444- |> pog.execute(db)
4545-}
4646-4710/// A row you get from running the `guard` query
4811/// defined in `./src/server/seed/sql/guard.sql`.
4912///
···7639 |> pog.returning(decoder)
7740 |> pog.execute(db)
7841}
7979-8080-// --- Enums -------------------------------------------------------------------
8181-8282-/// Corresponds to the Postgres `user_role_enum` enum.
8383-///
8484-/// > ๐ฟ๏ธ This type definition was generated automatically using v4.6.0 of the
8585-/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
8686-///
8787-pub type UserRoleEnum {
8888- Sargeant
8989- Firefighter
9090- Developer
9191- Captain
9292- Analyst
9393- Admin
9494-}
9595-9696-fn user_role_enum_encoder(user_role_enum) -> pog.Value {
9797- case user_role_enum {
9898- Sargeant -> "sargeant"
9999- Firefighter -> "firefighter"
100100- Developer -> "developer"
101101- Captain -> "captain"
102102- Analyst -> "analyst"
103103- Admin -> "admin"
104104- }
105105- |> pog.text
106106-}
-9
server/src/server/seed/sql/germinate.sql
···11--- ๎ถง creates a default user when starting the system
22-insert into public.user_account as u (
33- full_name,
44- user_role,
55- password_hash,
66- email,
77- phone,
88- is_active
99-) values ($1, $2, $3, $4, $5, $6);