From 135df9fd186be1e405fe7cdf9789dcaef486c242 Mon Sep 17 00:00:00 2001 From: Suriya Date: Tue, 7 Jul 2026 12:09:34 +0530 Subject: [PATCH] store users login --- controllers/userController.go | 26 +++++++ repositories/userRepository.go | 5 ++ routes/userroutes.go | 1 + scratch/query_stores.go | 61 ++++++++++++++++ scratch/query_stores.js | 34 +++++++++ scratch/seed_users.go | 125 +++++++++++++++++++++++++++++++++ scratch/test_delete.go | 70 ++++++++++++++++++ services/userService.go | 5 ++ 8 files changed, 327 insertions(+) create mode 100644 scratch/query_stores.go create mode 100644 scratch/query_stores.js create mode 100644 scratch/seed_users.go create mode 100644 scratch/test_delete.go diff --git a/controllers/userController.go b/controllers/userController.go index 776b79e..acce3dd 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -228,3 +228,29 @@ func (ctl *UserController) TenantWebLogin(c *fiber.Ctx) error { return c.Status(code).JSON(resp) } + +func (ctl *UserController) DeleteUser(c *fiber.Ctx) error { + uid, err := strconv.Atoi(c.Query("userid")) + if err != nil { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "code": http.StatusBadRequest, + "message": "Invalid user ID", + "status": false, + }) + } + + if err := ctl.userService.DeleteUser(uid); err != nil { + return c.Status(http.StatusInternalServerError).JSON(fiber.Map{ + "code": http.StatusInternalServerError, + "message": err.Error(), + "status": false, + }) + } + + return c.JSON(fiber.Map{ + "code": http.StatusOK, + "message": "User successfully deleted", + "status": true, + }) +} + diff --git a/repositories/userRepository.go b/repositories/userRepository.go index 7c62ac6..8533ea0 100644 --- a/repositories/userRepository.go +++ b/repositories/userRepository.go @@ -25,6 +25,7 @@ type UserRepository interface { GetUserLogin(field, value string, configid int) (int, string, string, int) UpdateUserFcmToken(uid int, token string) error GetLocationStatus(locationid int) string + DeleteUser(userid int) error } type userRepository struct { @@ -286,4 +287,8 @@ func (r *userRepository) GetLocationStatus(locationid int) string { return status } +func (r *userRepository) DeleteUser(userid int) error { + return r.db.Table("app_users").Where("userid = ?", userid).Delete(&models.User{}).Error +} + diff --git a/routes/userroutes.go b/routes/userroutes.go index 0e47066..7487158 100644 --- a/routes/userroutes.go +++ b/routes/userroutes.go @@ -15,6 +15,7 @@ func RegisterUserRoutes(api fiber.Router, f *facade.Facade) { users.Post("/create", f.UserController.CreateUser) users.Post("/tenant/weblogin", f.UserController.TenantWebLogin) users.Put("/update", f.UserController.UpdateStaff) + users.Delete("/delete", f.UserController.DeleteUser) users = api.Group("/v1/mob/users") users.Post("/tenant/login", f.UserController.Login) diff --git a/scratch/query_stores.go b/scratch/query_stores.go new file mode 100644 index 0000000..d1b063b --- /dev/null +++ b/scratch/query_stores.go @@ -0,0 +1,61 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/jackc/pgx/v5/stdlib" +) + +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!") + + // 1. Query locations + fmt.Println("\n--- SURIYA STORE LOCATIONS (tenantid = 1135) ---") + rows, err := db.Query(`SELECT locationid, tenantid, locationname, email, contactno, status FROM tenantlocations WHERE tenantid = 1135`) + if err != nil { + log.Fatalf("Error querying locations: %v", err) + } + defer rows.Close() + + for rows.Next() { + var locationid, tenantid int + var locationname, email, contactno, status sql.NullString + if err := rows.Scan(&locationid, &tenantid, &locationname, &email, &contactno, &status); err != nil { + log.Fatalf("Scan error: %v", err) + } + fmt.Printf("Loc ID: %d | Tenant ID: %d | Name: %s | Email: %s | Contact: %s | Status: %s\n", + locationid, tenantid, locationname.String, email.String, contactno.String, status.String) + } + + // 2. Query users + fmt.Println("\n--- USERS ASSOCIATED WITH tenantid = 1135 ---") + rows2, err := db.Query(`SELECT userid, authname, firstname, email, contactno, roleid, locationid, status FROM app_users WHERE tenantid = 1135`) + if err != nil { + log.Fatalf("Error querying users: %v", err) + } + defer rows2.Close() + + for rows2.Next() { + var userid int + var roleid, locationid sql.NullInt64 + var authname, firstname, email, contactno, status sql.NullString + if err := rows2.Scan(&userid, &authname, &firstname, &email, &contactno, &roleid, &locationid, &status); err != nil { + log.Fatalf("Scan error: %v", err) + } + fmt.Printf("User ID: %d | Auth: %s | Name: %s | Email: %s | Contact: %s | Role: %d | Loc: %d | Status: %s\n", + userid, authname.String, firstname.String, email.String, contactno.String, roleid.Int64, locationid.Int64, status.String) + } +} diff --git a/scratch/query_stores.js b/scratch/query_stores.js new file mode 100644 index 0000000..b1592fe --- /dev/null +++ b/scratch/query_stores.js @@ -0,0 +1,34 @@ +const { Client } = require('pg'); + +const client = new Client({ + host: '66.116.207.225', + port: 5433, + database: 'nearledb', + user: 'admin', + password: 'Package@123#', + ssl: false, +}); + +async function main() { + try { + await client.connect(); + console.log('Connected to PostgreSQL successfully!'); + + // Get Suriya Store locations + const locationsRes = await client.query('SELECT locationid, tenantid, locationname, email, contactno, status FROM tenantlocations WHERE tenantid = 1135'); + console.log('\n--- SURIYA STORE LOCATIONS (tenantid = 1135) ---'); + console.table(locationsRes.rows); + + // Get all users associated with tenantid = 1135 + const usersRes = await client.query('SELECT userid, authname, firstname, email, contactno, roleid, locationid, status FROM app_users WHERE tenantid = 1135'); + console.log('\n--- USERS ASSOCIATED WITH tenantid = 1135 ---'); + console.table(usersRes.rows); + + } catch (err) { + console.error('Error connecting:', err); + } finally { + await client.end(); + } +} + +main(); diff --git a/scratch/seed_users.go b/scratch/seed_users.go new file mode 100644 index 0000000..8f183c4 --- /dev/null +++ b/scratch/seed_users.go @@ -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!") +} diff --git a/scratch/test_delete.go b/scratch/test_delete.go new file mode 100644 index 0000000..6d05e4d --- /dev/null +++ b/scratch/test_delete.go @@ -0,0 +1,70 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/jackc/pgx/v5/stdlib" +) + +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!") + + // 1. Create a temp user for delete testing + authName := "temp.delete.test@suriya.com" + _, err = db.Exec(` + 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, 'Temp', 'Delete Test', 'Password123', $1, '+91', '9999999999', + 1, 0, 0, 0, '', '', '', + '', '', '', '', '', 0, 1135, 1166, + 1, 'Active', 0 + ) + `, authName) + if err != nil { + log.Fatalf("Error creating temp user: %v", err) + } + fmt.Println("Temp user created.") + + // 2. Fetch the created user ID + var userID int + err = db.QueryRow("SELECT userid FROM app_users WHERE authname = $1", authName).Scan(&userID) + if err != nil { + log.Fatalf("Error fetching user ID: %v", err) + } + fmt.Printf("Temp user ID is: %d\n", userID) + + // 3. Delete the user + _, err = db.Exec("DELETE FROM app_users WHERE userid = $1", userID) + if err != nil { + log.Fatalf("Error deleting user: %v", err) + } + fmt.Println("Temp user deleted successfully.") + + // 4. Verify no longer exists + var checkID int + err = db.QueryRow("SELECT userid FROM app_users WHERE userid = $1", userID).Scan(&checkID) + if err == sql.ErrNoRows { + fmt.Println("Verification Success: User no longer exists in DB!") + } else if err != nil { + log.Fatalf("Query check error: %v", err) + } else { + log.Fatalf("Error: User still exists in database!") + } +} diff --git a/services/userService.go b/services/userService.go index 932de38..b5bbd66 100644 --- a/services/userService.go +++ b/services/userService.go @@ -18,6 +18,7 @@ type UserService interface { AppLogin(user models.User) (models.TenantUserInfo, fiber.Map, error) CreateUser(user models.User) (models.UserInfo, error) TenantWebLogin(user models.User) (models.TenantUserInfo, map[string]interface{}) + DeleteUser(userid int) error } type userService struct { @@ -287,4 +288,8 @@ func (s *userService) TenantWebLogin(user models.User) (models.TenantUserInfo, m } } +func (s *userService) DeleteUser(userid int) error { + return s.repo.DeleteUser(userid) +} +