chore: build
This commit is contained in:
@@ -12,14 +12,23 @@ import (
|
||||
"backend/internal/platform/db"
|
||||
)
|
||||
|
||||
// alwaysAvailableGate satisfies users.AccountsGate for the seed CLI, which
|
||||
// only ever creates the initial admin account (never a customer), so the
|
||||
// gate is never actually consulted.
|
||||
type alwaysAvailableGate struct{}
|
||||
|
||||
func (alwaysAvailableGate) CustomerAccountsAvailable(context.Context) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("load config: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Seed.AdminEmail == "" || cfg.Seed.AdminPassword == "" {
|
||||
log.Fatal("SEED_ADMIN_EMAIL and SEED_ADMIN_PASSWORD must be set")
|
||||
if cfg.Seed.AdminUsername == "" || cfg.Seed.AdminPassword == "" {
|
||||
log.Fatal("SEED_ADMIN_USERNAME and SEED_ADMIN_PASSWORD must be set")
|
||||
}
|
||||
if len(cfg.Seed.AdminPassword) < 12 {
|
||||
log.Fatal("SEED_ADMIN_PASSWORD must be at least 12 characters")
|
||||
@@ -31,20 +40,20 @@ func main() {
|
||||
}
|
||||
|
||||
repo := users.NewRepository(database)
|
||||
service := users.NewService(repo)
|
||||
service := users.NewService(repo, alwaysAvailableGate{}, cfg.Seed)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
if existing, err := repo.FindByEmail(ctx, cfg.Seed.AdminEmail); err == nil && existing != nil {
|
||||
log.Fatalf("an account with email %q already exists (id=%s)", cfg.Seed.AdminEmail, existing.ID)
|
||||
if existing, err := repo.FindByUsername(ctx, cfg.Seed.AdminUsername); err == nil && existing != nil {
|
||||
log.Fatalf("an account with username %q already exists (id=%s)", cfg.Seed.AdminUsername, existing.ID)
|
||||
} else if err != nil && !errors.Is(err, users.ErrNotFound) {
|
||||
log.Fatalf("check existing admin: %v", err)
|
||||
}
|
||||
|
||||
admin, err := service.Create(ctx, cfg.Seed.AdminEmail, cfg.Seed.AdminPassword, users.RoleAdmin)
|
||||
admin, err := service.Create(ctx, cfg.Seed.AdminUsername, cfg.Seed.AdminPassword, users.RoleAdmin)
|
||||
if err != nil {
|
||||
log.Fatalf("create admin: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("admin account created: email=%s id=%s", admin.Email, admin.ID)
|
||||
log.Printf("admin account created: username=%s id=%s", admin.Username, admin.ID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user