onasty/cmd/seed/users.go (view raw)
Olexandr Smirnov
Olexandr Smirnov
ss2316544@gmail.com fix(seed): fix email address for not activated user (#161), 11 months ago
ss2316544@gmail.com fix(seed): fix email address for not activated user (#161), 11 months ago
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/jackc/pgx/v5/pgtype" |
| 8 | "github.com/olexsmir/onasty/internal/hasher" |
| 9 | "github.com/olexsmir/onasty/internal/store/psqlutil" |
| 10 | ) |
| 11 | |
| 12 | var usersData = []struct { |
| 13 | id string |
| 14 | email string |
| 15 | password string |
| 16 | activated bool |
| 17 | }{ |
| 18 | { //nolint:exhaustruct |
| 19 | email: "admin@onasty.local", |
| 20 | password: "adminadmin", |
| 21 | activated: true, |
| 22 | }, |
| 23 | { //nolint:exhaustruct |
| 24 | email: "user@onasty.local", |
| 25 | activated: false, |
| 26 | password: "qwerty123", |
| 27 | }, |
| 28 | } |
| 29 | |
| 30 | func seedUsers( |
| 31 | ctx context.Context, |
| 32 | hash hasher.Hasher, |
| 33 | db *psqlutil.DB, |
| 34 | ) error { |
| 35 | for i, user := range usersData { |
| 36 | passwrd, err := hash.Hash(user.password) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | |
| 41 | var id pgtype.UUID |
| 42 | err = db.QueryRow(ctx, ` |
| 43 | insert into users (email, password, activated, created_at, last_login_at) |
| 44 | values ($1, $2, $3, $4, $5) |
| 45 | on conflict (email) do update |
| 46 | set password = excluded.password |
| 47 | returning id |
| 48 | `, user.email, passwrd, user.activated, time.Now(), time.Now()). |
| 49 | Scan(&id) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | usersData[i].id = id.String() |
| 55 | } |
| 56 | |
| 57 | return nil |
| 58 | } |