- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core) - Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token - Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers - Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/gofrs/uuid/v5"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/jmoiron/sqlx"
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/saveinmed/backend-go/internal/config"
|
|
"github.com/saveinmed/backend-go/internal/domain"
|
|
)
|
|
|
|
func main() {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("failed to load config: %v", err)
|
|
}
|
|
|
|
db, err := sqlx.Open("pgx", cfg.DatabaseURL)
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect to DB: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
if err := db.Ping(); err != nil {
|
|
log.Fatalf("Failed to ping DB: %v", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
log.Println("🌱 Restoring Admin and creating new Lojista...")
|
|
|
|
// 1. Restore Admin Account
|
|
adminUser := &domain.User{
|
|
Role: "admin",
|
|
Name: "Administrador",
|
|
Username: "admin",
|
|
Email: "admin@saveinmed.com",
|
|
}
|
|
createUser(ctx, db, adminUser, "teste123", cfg.PasswordPepper, uuid.Nil)
|
|
log.Println("✅ Admin account restored (admin / teste123)")
|
|
|
|
// 2. Create Second Lojista (Company + User)
|
|
lojista2CompanyID := uuid.Must(uuid.NewV7())
|
|
lojista2Company := &domain.Company{
|
|
ID: lojista2CompanyID,
|
|
CNPJ: "98765432000188",
|
|
CorporateName: "Farma Central Distribuidora",
|
|
Category: "distribuidora",
|
|
LicenseNumber: "LIC-987654",
|
|
IsVerified: true,
|
|
Latitude: -23.5611,
|
|
Longitude: -46.6559, // Near Paulista, SP
|
|
City: "São Paulo",
|
|
State: "SP",
|
|
}
|
|
createCompany(ctx, db, lojista2Company)
|
|
|
|
lojista2User := &domain.User{
|
|
Role: "Dono",
|
|
Name: "Ricardo Lojista",
|
|
Username: "ricardo_farma",
|
|
Email: "ricardo@farmacentral.com",
|
|
}
|
|
createUser(ctx, db, lojista2User, "password123", cfg.PasswordPepper, lojista2CompanyID)
|
|
log.Println("✅ Second Lojista created (ricardo@farmacentral.com / password123)")
|
|
|
|
log.Println("✨ All operations complete!")
|
|
}
|
|
|
|
func createCompany(ctx context.Context, db *sqlx.DB, c *domain.Company) {
|
|
now := time.Now().UTC()
|
|
c.CreatedAt = now
|
|
c.UpdatedAt = now
|
|
_, err := db.NamedExecContext(ctx, `
|
|
INSERT INTO companies (id, cnpj, corporate_name, category, license_number, is_verified, latitude, longitude, city, state, created_at, updated_at)
|
|
VALUES (:id, :cnpj, :corporate_name, :category, :license_number, :is_verified, :latitude, :longitude, :city, :state, :created_at, :updated_at)
|
|
ON CONFLICT (cnpj) DO UPDATE SET
|
|
corporate_name = EXCLUDED.corporate_name,
|
|
updated_at = EXCLUDED.updated_at
|
|
`, c)
|
|
if err != nil {
|
|
log.Printf("Error creating company %s: %v", c.CorporateName, err)
|
|
} else {
|
|
log.Printf("Company %s created/updated successfully", c.CorporateName)
|
|
}
|
|
}
|
|
|
|
func createUser(ctx context.Context, db *sqlx.DB, u *domain.User, password, pepper string, companyID uuid.UUID) {
|
|
hashed, _ := bcrypt.GenerateFromPassword([]byte(password+pepper), bcrypt.DefaultCost)
|
|
u.ID = uuid.Must(uuid.NewV7())
|
|
u.PasswordHash = string(hashed)
|
|
u.CreatedAt = time.Now().UTC()
|
|
u.UpdatedAt = time.Now().UTC()
|
|
u.EmailVerified = true
|
|
|
|
if companyID != uuid.Nil {
|
|
u.CompanyID = companyID
|
|
}
|
|
|
|
_, err := db.NamedExecContext(ctx, `
|
|
INSERT INTO users (id, company_id, role, name, username, email, password_hash, email_verified, created_at, updated_at)
|
|
VALUES (:id, :company_id, :role, :name, :username, :email, :password_hash, :email_verified, :created_at, :updated_at)
|
|
ON CONFLICT (email) DO UPDATE SET
|
|
password_hash = EXCLUDED.password_hash,
|
|
company_id = COALESCE(EXCLUDED.company_id, users.company_id),
|
|
role = EXCLUDED.role,
|
|
updated_at = EXCLUDED.updated_at
|
|
`, u)
|
|
if err != nil {
|
|
log.Printf("Error creating user %s: %v", u.Email, err)
|
|
} else {
|
|
log.Printf("User %s created/updated successfully", u.Email)
|
|
}
|
|
}
|