diff --git a/scratch/seed_pin_customer.go b/scratch/seed_pin_customer.go new file mode 100644 index 0000000..fcad367 --- /dev/null +++ b/scratch/seed_pin_customer.go @@ -0,0 +1,70 @@ +//go:build ignore + +// seed_pin_customer.go — one ready-to-use customer for testing the interim PIN +// login flow: phone +919000001234, PIN 1234, already set. Idempotent (re-running +// just re-asserts the PIN to 1234). +// +// The phone is stored in NORMALISED +91 form, because CxPinLogin/CxVerifyPin +// normalise the entered number to +91 before the DB lookup — a bare 10-digit +// row would never be found. +// +// Run: go run scratch/seed_pin_customer.go + +package main + +import ( + "fmt" + "log" + + "doormile/config" + "doormile/db" + "doormile/models" + "doormile/utils" + + "github.com/joho/godotenv" +) + +func main() { + _ = godotenv.Load() + cfg := config.Load() + db.Connect(cfg) + if db.DB == nil { + log.Fatal("DB connection is nil") + } + + const phone = "+919000001234" // customer types 9000001234; it normalises to this + pinHash, err := utils.HashPassword("1234") + if err != nil { + log.Fatalf("hash: %v", err) + } + + var c models.AppCustomer + if db.DB.Where("phone = ?", phone).First(&c).Error != nil { + c = models.AppCustomer{ + Firstname: "Test", + Lastname: "PinCustomer", + Phone: phone, + Email: "test.pin@doormile.in", + Loginpinhash: pinHash, + Defaultlatitude: 11.0168, + Defaultlongitude: 76.9558, + Defaultpincode: "641012", + Status: "Active", + Configid: 1001, + } + if err := db.DB.Create(&c).Error; err != nil { + log.Fatalf("create: %v", err) + } + fmt.Println("created customer with PIN 1234") + } else { + c.Loginpinhash = pinHash + c.Status = "Active" + if err := db.DB.Save(&c).Error; err != nil { + log.Fatalf("update: %v", err) + } + fmt.Println("customer already existed — PIN re-asserted to 1234") + } + + fmt.Printf("\nāœ… ready: appcustomerid=%d phone=%s login-as=9000001234 pin=1234\n", + c.Appcustomerid, c.Phone) +}