store users login
This commit is contained in:
125
scratch/seed_users.go
Normal file
125
scratch/seed_users.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
type UserSeed struct {
|
||||
Authname string
|
||||
Firstname string
|
||||
Lastname string
|
||||
Password string
|
||||
Email string
|
||||
Contactno string
|
||||
Roleid int
|
||||
Locationid int
|
||||
Status string
|
||||
}
|
||||
|
||||
func main() {
|
||||
dsn := "host=66.116.207.225 port=5433 user=admin password=Package@123# dbname=nearledb sslmode=disable"
|
||||
db, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening db connection: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
log.Fatalf("Error pinging db: %v", err)
|
||||
}
|
||||
fmt.Println("Connected successfully to PostgreSQL!")
|
||||
|
||||
seeds := []UserSeed{
|
||||
// Tenant-wide logins
|
||||
{Authname: "abhishek@suriya.com", Firstname: "Abhishek", Lastname: "Tenant Admin", Password: "Password123", Email: "abhishek@suriya.com", Contactno: "9000000001", Roleid: 1, Locationid: 0, Status: "Active"},
|
||||
{Authname: "dharaneesh@suriya.com", Firstname: "Dharaneesh", Lastname: "Tenant Admin", Password: "Password123", Email: "dharaneesh@suriya.com", Contactno: "9000000002", Roleid: 1, Locationid: 0, Status: "Active"},
|
||||
|
||||
// RS Puram branch (1166)
|
||||
{Authname: "abhishek.rspuram@suriya.com", Firstname: "Abhishek", Lastname: "RS Puram Staff", Password: "Password123", Email: "abhishek.rspuram@suriya.com", Contactno: "9000000003", Roleid: 0, Locationid: 1166, Status: "active"},
|
||||
{Authname: "dharaneesh.rspuram@suriya.com", Firstname: "Dharaneesh", Lastname: "RS Puram Staff", Password: "Password123", Email: "dharaneesh.rspuram@suriya.com", Contactno: "9000000004", Roleid: 0, Locationid: 1166, Status: "active"},
|
||||
|
||||
// Peelamedu branch (1170)
|
||||
{Authname: "abhishek.peelamedu@suriya.com", Firstname: "Abhishek", Lastname: "Peelamedu Staff", Password: "Password123", Email: "abhishek.peelamedu@suriya.com", Contactno: "9000000005", Roleid: 0, Locationid: 1170, Status: "active"},
|
||||
{Authname: "dharaneesh.peelamedu@suriya.com", Firstname: "Dharaneesh", Lastname: "Peelamedu Staff", Password: "Password123", Email: "dharaneesh.peelamedu@suriya.com", Contactno: "9000000006", Roleid: 0, Locationid: 1170, Status: "active"},
|
||||
|
||||
// Gandhipuram branch (1169)
|
||||
{Authname: "abhishek.gandhipuram@suriya.com", Firstname: "Abhishek", Lastname: "Gandhipuram Staff", Password: "Password123", Email: "abhishek.gandhipuram@suriya.com", Contactno: "9000000007", Roleid: 0, Locationid: 1169, Status: "active"},
|
||||
{Authname: "dharaneesh.gandhipuram@suriya.com", Firstname: "Dharaneesh", Lastname: "Gandhipuram Staff", Password: "Password123", Email: "dharaneesh.gandhipuram@suriya.com", Contactno: "9000000008", Roleid: 0, Locationid: 1169, Status: "active"},
|
||||
|
||||
// Ukkadam branch (1171)
|
||||
{Authname: "abhishek.ukkadam@suriya.com", Firstname: "Abhishek", Lastname: "Ukkadam Staff", Password: "Password123", Email: "abhishek.ukkadam@suriya.com", Contactno: "9000000009", Roleid: 0, Locationid: 1171, Status: "active"},
|
||||
{Authname: "dharaneesh.ukkadam@suriya.com", Firstname: "Dharaneesh", Lastname: "Ukkadam Staff", Password: "Password123", Email: "dharaneesh.ukkadam@suriya.com", Contactno: "9000000010", Roleid: 0, Locationid: 1171, Status: "active"},
|
||||
|
||||
// NSN branch (1172)
|
||||
{Authname: "abhishek.nsn@suriya.com", Firstname: "Abhishek", Lastname: "NSN Staff", Password: "Password123", Email: "abhishek.nsn@suriya.com", Contactno: "9000000011", Roleid: 0, Locationid: 1172, Status: "active"},
|
||||
{Authname: "dharaneesh.nsn@suriya.com", Firstname: "Dharaneesh", Lastname: "NSN Staff", Password: "Password123", Email: "dharaneesh.nsn@suriya.com", Contactno: "9000000012", Roleid: 0, Locationid: 1172, Status: "active"},
|
||||
}
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
log.Fatalf("Error starting transaction: %v", err)
|
||||
}
|
||||
|
||||
checkQuery := `SELECT userid FROM app_users WHERE authname = $1`
|
||||
insertQuery := `
|
||||
INSERT INTO app_users (
|
||||
authname, firstname, lastname, password, email, dialcode, contactno,
|
||||
configid, authmode, roleid, pin, deviceid, devicetype, userfcmtoken,
|
||||
address, suburb, city, state, postcode, partnerid, tenantid, locationid,
|
||||
applocationid, status, shiftid
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, '+91', $6,
|
||||
1, 0, $7, 0, '', '', '',
|
||||
'', '', '', '', '', 0, 1135, $8,
|
||||
1, $9, 0
|
||||
)
|
||||
`
|
||||
updateQuery := `
|
||||
UPDATE app_users SET
|
||||
firstname = $1,
|
||||
lastname = $2,
|
||||
password = $3,
|
||||
email = $4,
|
||||
contactno = $5,
|
||||
roleid = $6,
|
||||
locationid = $7,
|
||||
status = $8
|
||||
WHERE userid = $9
|
||||
`
|
||||
|
||||
for _, s := range seeds {
|
||||
var existingUserID int
|
||||
err := tx.QueryRow(checkQuery, s.Authname).Scan(&existingUserID)
|
||||
if err == sql.ErrNoRows {
|
||||
// Insert new record
|
||||
_, err = tx.Exec(insertQuery, s.Authname, s.Firstname, s.Lastname, s.Password, s.Email, s.Contactno, s.Roleid, s.Locationid, s.Status)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
log.Fatalf("Error inserting user %s: %v", s.Authname, err)
|
||||
}
|
||||
fmt.Printf("Successfully inserted new user: %s\n", s.Authname)
|
||||
} else if err != nil {
|
||||
tx.Rollback()
|
||||
log.Fatalf("Error checking existing user %s: %v", s.Authname, err)
|
||||
} else {
|
||||
// Update existing record
|
||||
_, err = tx.Exec(updateQuery, s.Firstname, s.Lastname, s.Password, s.Email, s.Contactno, s.Roleid, s.Locationid, s.Status, existingUserID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
log.Fatalf("Error updating user %s: %v", s.Authname, err)
|
||||
}
|
||||
fmt.Printf("Successfully updated existing user: %s\n", s.Authname)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Fatalf("Error committing transaction: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("All seed users successfully created/updated!")
|
||||
}
|
||||
Reference in New Issue
Block a user