···23 let handler = router.handle_request(_, ctx)
2425 let assert Ok(_) = supervision_tree.init(pog_config, handler, secret_key)
26- let assert Ok(_) = seed.germinate(ctx.db, ctx.secret_key)
2728 // All done!
29 process.sleep_forever()
···23 let handler = router.handle_request(_, ctx)
2425 let assert Ok(_) = supervision_tree.init(pog_config, handler, secret_key)
26+ let assert Ok(_) = seed.germinate(ctx, ctx.secret_key)
2728 // All done!
29 process.sleep_forever()
+31-18
server/src/server/seed.gleam
···2import gleam/result
3import pog
4import server/auth
05import server/seed/sql
0067pub type UserTableState {
8 Empty
9 Populated
10}
1100000012pub const full_name = "SIGO"
1314pub const password = "root"
···1718pub const phone = "0000000000"
1920-pub const role = sql.Admin
2122/// ๎ถง Generate a root user when starting the system for the first time
23-pub fn germinate(
24- db: pog.Connection,
25- salt: String,
26-) -> Result(Nil, pog.QueryError) {
27- use state <- result.try(guard(db))
28 use <- bool.guard(when: state == Populated, return: Ok(Nil))
02930- let hashed = auth.hash(value: password, salt:)
31- use _ <- result.map(sql.germinate(
32- db,
33- full_name,
34- role,
35- hashed,
36- email,
37- phone,
38- True,
39- ))
004041 Nil
42}
4344-fn guard(db: pog.Connection) -> Result(UserTableState, pog.QueryError) {
45- use returned <- result.map(sql.guard(db))
000046 case returned.count {
47 0 -> Empty
48 _ -> Populated
···2import gleam/result
3import pog
4import server/auth
5+import server/context
6import server/seed/sql
7+import server/user
8+import shared/role
910pub type UserTableState {
11 Empty
12 Populated
13}
1415+pub type SeedError {
16+ Auth(auth.AuthError)
17+ UserError(user.UserError)
18+ Database(pog.QueryError)
19+}
20+21pub const full_name = "SIGO"
2223pub const password = "root"
···2627pub const phone = "0000000000"
2829+pub const role = role.Admin
3031/// ๎ถง Generate a root user when starting the system for the first time
32+pub fn germinate(ctx: context.Context, salt: String) -> Result(Nil, SeedError) {
33+ use state <- result.try(guard(ctx.db))
00034 use <- bool.guard(when: state == Populated, return: Ok(Nil))
35+ let hashed = auth.hash(value: password, salt:)
3637+ use _ <- result.map(
38+ user.register(
39+ ctx: ctx,
40+ user_name: full_name,
41+ user_role: role,
42+ user_password: hashed,
43+ user_email: email,
44+ user_phone: phone,
45+ is_active: True,
46+ )
47+ |> result.map_error(UserError),
48+ )
4950 Nil
51}
5253+fn guard(db: pog.Connection) -> Result(UserTableState, SeedError) {
54+ use returned <- result.map(
55+ sql.guard(db)
56+ |> result.map_error(Database),
57+ )
58+59 case returned.count {
60 0 -> Empty
61 _ -> Populated
-65
server/src/server/seed/sql.gleam
···7import gleam/dynamic/decode
8import pog
910-/// ๎ถง creates a default user when starting the system
11-///
12-/// > ๐ฟ๏ธ This function was generated automatically using v4.6.0 of
13-/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
14-///
15-pub fn germinate(
16- db: pog.Connection,
17- arg_1: String,
18- arg_2: UserRoleEnum,
19- arg_3: String,
20- arg_4: String,
21- arg_5: String,
22- arg_6: Bool,
23-) -> Result(pog.Returned(Nil), pog.QueryError) {
24- let decoder = decode.map(decode.dynamic, fn(_) { Nil })
25-26- "-- ๎ถง creates a default user when starting the system
27-insert into public.user_account as u (
28- full_name,
29- user_role,
30- password_hash,
31- email,
32- phone,
33- is_active
34-) values ($1, $2, $3, $4, $5, $6);
35-"
36- |> pog.query
37- |> pog.parameter(pog.text(arg_1))
38- |> pog.parameter(user_role_enum_encoder(arg_2))
39- |> pog.parameter(pog.text(arg_3))
40- |> pog.parameter(pog.text(arg_4))
41- |> pog.parameter(pog.text(arg_5))
42- |> pog.parameter(pog.bool(arg_6))
43- |> pog.returning(decoder)
44- |> pog.execute(db)
45-}
46-47/// A row you get from running the `guard` query
48/// defined in `./src/server/seed/sql/guard.sql`.
49///
···76 |> pog.returning(decoder)
77 |> pog.execute(db)
78}
79-80-// --- Enums -------------------------------------------------------------------
81-82-/// Corresponds to the Postgres `user_role_enum` enum.
83-///
84-/// > ๐ฟ๏ธ This type definition was generated automatically using v4.6.0 of the
85-/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
86-///
87-pub type UserRoleEnum {
88- Sargeant
89- Firefighter
90- Developer
91- Captain
92- Analyst
93- Admin
94-}
95-96-fn user_role_enum_encoder(user_role_enum) -> pog.Value {
97- case user_role_enum {
98- Sargeant -> "sargeant"
99- Firefighter -> "firefighter"
100- Developer -> "developer"
101- Captain -> "captain"
102- Analyst -> "analyst"
103- Admin -> "admin"
104- }
105- |> pog.text
106-}
···7import gleam/dynamic/decode
8import pog
9000000000000000000000000000000000000010/// A row you get from running the `guard` query
11/// defined in `./src/server/seed/sql/guard.sql`.
12///
···39 |> pog.returning(decoder)
40 |> pog.execute(db)
41}
0000000000000000000000000000
-9
server/src/server/seed/sql/germinate.sql
···1--- ๎ถง creates a default user when starting the system
2-insert into public.user_account as u (
3- full_name,
4- user_role,
5- password_hash,
6- email,
7- phone,
8- is_active
9-) values ($1, $2, $3, $4, $5, $6);