Update: Few issues to resolve see readme
this push will conclude the majority of pulls. this repos will now, not be actively be managed or any further code pushes will not be frequent.
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"fmt"
|
||||
|
||||
"github.com/patel-mann/poll-system/app/internal/models"
|
||||
"github.com/patel-mann/poll-system/app/internal/utils"
|
||||
@@ -36,19 +35,18 @@ type PageNumber struct {
|
||||
// AddressWithDetails extends AddressDatabase with appointment and user info
|
||||
type AddressWithDetails struct {
|
||||
models.AddressDatabase
|
||||
UserID *int
|
||||
UserName string
|
||||
UserEmail string
|
||||
AppointmentDate string
|
||||
AppointmentTime string
|
||||
UserID *int
|
||||
UserName string
|
||||
UserEmail string
|
||||
AppointmentDate string
|
||||
AppointmentTime string
|
||||
}
|
||||
|
||||
func AddressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Get pagination parameters from query string
|
||||
pageStr := r.URL.Query().Get("page")
|
||||
pageSizeStr := r.URL.Query().Get("pageSize")
|
||||
username,_ := models.GetCurrentUserName(r)
|
||||
|
||||
username, _ := models.GetCurrentUserName(r)
|
||||
|
||||
page := 1
|
||||
pageSize := 20
|
||||
@@ -157,7 +155,7 @@ func AddressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Get users associated with this admin
|
||||
currentAdminID := r.Context().Value("user_id").(int)
|
||||
currentAdminID := models.GetCurrentUserID(w, r)
|
||||
userRows, err := models.DB.Query(`
|
||||
SELECT u.user_id, u.first_name || ' ' || u.last_name AS name
|
||||
FROM users u
|
||||
@@ -216,7 +214,7 @@ func AddressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
"ActiveSection": "address",
|
||||
"Addresses": addresses,
|
||||
"Users": users,
|
||||
"UserName": username,
|
||||
"UserName": username,
|
||||
"Role": "admin",
|
||||
"Pagination": pagination,
|
||||
})
|
||||
@@ -267,13 +265,9 @@ func AssignAddressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
appointmentDate := r.FormValue("appointment_date")
|
||||
startTime := r.FormValue("time")
|
||||
|
||||
if userIDStr == "" || addressIDStr == "" {
|
||||
http.Error(w, "User ID and Address ID are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if appointmentDate == "" || startTime == "" {
|
||||
http.Error(w, "Appointment date and start time are required", http.StatusBadRequest)
|
||||
// Basic validation
|
||||
if userIDStr == "" || addressIDStr == "" || appointmentDate == "" || startTime == "" {
|
||||
http.Error(w, "All fields are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -289,54 +283,30 @@ func AssignAddressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Parse and validate the appointment date
|
||||
// Parse date
|
||||
parsedDate, err := time.Parse("2006-01-02", appointmentDate)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid appointment date format", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate that the appointment date is not in the past
|
||||
today := time.Now().Truncate(24 * time.Hour)
|
||||
if parsedDate.Before(today) {
|
||||
http.Error(w, "Appointment date cannot be in the past", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse and validate the start time
|
||||
// Parse time
|
||||
parsedTime, err := time.Parse("15:04", startTime)
|
||||
is_valid := ValidatedFreeTime(parsedDate, parsedTime, userID)
|
||||
if is_valid != true {
|
||||
http.Error(w, "User is not availabile", http.StatusBadRequest)
|
||||
return
|
||||
}else{
|
||||
fmt.Print("hello")
|
||||
}
|
||||
|
||||
// Verify the user exists and is associated with the current admin
|
||||
currentAdminID := r.Context().Value("user_id").(int)
|
||||
var userExists int
|
||||
err = models.DB.QueryRow(`
|
||||
SELECT COUNT(*) FROM admin_volunteers av
|
||||
JOIN users u ON av.volunteer_id = u.user_id
|
||||
WHERE av.admin_id = $1 AND u.user_id = $2 AND av.is_active = true
|
||||
`, currentAdminID, userID).Scan(&userExists)
|
||||
if err != nil {
|
||||
log.Println("User verification error:", err)
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if userExists == 0 {
|
||||
http.Error(w, "Invalid user selection", http.StatusBadRequest)
|
||||
http.Error(w, "Invalid appointment time format", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if this address is already assigned to any user
|
||||
// --- Availability Check (non-blocking) ---
|
||||
isValid := ValidateAvailability(parsedDate, parsedTime, userID)
|
||||
if !isValid {
|
||||
// Instead of blocking, just log it
|
||||
log.Printf("⚠️ User %d is not available on %s at %s", userID, appointmentDate, startTime)
|
||||
}
|
||||
|
||||
// Check if this address is already assigned
|
||||
var exists int
|
||||
err = models.DB.QueryRow(`
|
||||
SELECT COUNT(*) FROM appointment
|
||||
WHERE address_id = $1
|
||||
`, addressID).Scan(&exists)
|
||||
err = models.DB.QueryRow(`SELECT COUNT(*) FROM appointment WHERE address_id = $1`, addressID).Scan(&exists)
|
||||
if err != nil {
|
||||
log.Println("Assignment check error:", err)
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
@@ -347,38 +317,39 @@ func AssignAddressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the user already has an appointment at the same date and time
|
||||
var timeConflict int
|
||||
// Check for conflicting appointment for the user
|
||||
var conflict int
|
||||
err = models.DB.QueryRow(`
|
||||
SELECT COUNT(*) FROM appointment
|
||||
WHERE user_id = $1 AND appointment_date = $2 AND appointment_time = $3
|
||||
`, userID, appointmentDate, startTime).Scan(&timeConflict)
|
||||
WHERE user_id = $1 AND appointment_date = $2 AND appointment_time = $3`,
|
||||
userID, appointmentDate, startTime).Scan(&conflict)
|
||||
if err != nil {
|
||||
log.Println("Time conflict check error:", err)
|
||||
log.Println("Conflict check error:", err)
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if timeConflict > 0 {
|
||||
if conflict > 0 {
|
||||
http.Error(w, "User already has an appointment at this date and time", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Assign the address - create appointment with specific date and time
|
||||
// Insert the appointment anyway
|
||||
_, err = models.DB.Exec(`
|
||||
INSERT INTO appointment (user_id, address_id, appointment_date, appointment_time, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, NOW(), NOW())
|
||||
`, userID, addressID, appointmentDate, startTime)
|
||||
VALUES ($1, $2, $3, $4, NOW(), NOW())`,
|
||||
userID, addressID, appointmentDate, startTime)
|
||||
if err != nil {
|
||||
log.Println("Assignment error:", err)
|
||||
log.Println("Insert appointment error:", err)
|
||||
http.Error(w, "Failed to assign address", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect back to addresses page with success
|
||||
// ✅ Later: you can pass `UserNotAvailable: !isValid` to utils.Render instead of redirect
|
||||
log.Printf("✅ Address %d assigned to user %d for %s at %s (Available: %v)",
|
||||
addressID, userID, appointmentDate, startTime, isValid)
|
||||
|
||||
http.Redirect(w, r, "/addresses?success=assigned", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
|
||||
func RemoveAssignedAddressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Redirect(w, r, "/addresses", http.StatusSeeOther)
|
||||
|
||||
Reference in New Issue
Block a user