feat: seed secure demo merchant identity
This commit is contained in:
39
internal/identity/seed.go
Normal file
39
internal/identity/seed.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type SeedResult struct {
|
||||
Username string
|
||||
MerchantSourceKey string
|
||||
MerchantName string
|
||||
}
|
||||
|
||||
func SeedDemo(ctx context.Context, pool *pgxpool.Pool, username, password string) (SeedResult, error) {
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return SeedResult{}, err
|
||||
}
|
||||
var result SeedResult
|
||||
var merchantID int64
|
||||
err = pool.QueryRow(ctx, `SELECT m.id,m.source_key,m.display_name
|
||||
FROM merchants m JOIN payment_sessions ps ON ps.merchant_id=m.id
|
||||
GROUP BY m.id ORDER BY count(*) DESC,m.source_key LIMIT 1`).Scan(
|
||||
&merchantID, &result.MerchantSourceKey, &result.MerchantName,
|
||||
)
|
||||
if err != nil {
|
||||
return SeedResult{}, fmt.Errorf("selecting demo merchant: %w", err)
|
||||
}
|
||||
_, err = pool.Exec(ctx, `INSERT INTO merchant_users(merchant_id,username,password_hash)
|
||||
VALUES ($1,$2,$3) ON CONFLICT (username) DO UPDATE
|
||||
SET merchant_id=excluded.merchant_id,password_hash=excluded.password_hash,is_active=true`, merchantID, username, hash)
|
||||
if err != nil {
|
||||
return SeedResult{}, fmt.Errorf("seeding demo merchant user: %w", err)
|
||||
}
|
||||
result.Username = username
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user