updated layout and volunteer

change the ui and now able to remove assigned addresses from volunteer
This commit is contained in:
Mann Patel
2025-08-28 00:15:10 -06:00
parent d7f6c794b4
commit 9dd24e71e7
20 changed files with 302 additions and 536 deletions

View File

@@ -34,6 +34,7 @@ type PageNumber struct {
// AddressWithDetails extends AddressDatabase with appointment and user info
type AddressWithDetails struct {
models.AddressDatabase
UserID *int
UserName string
UserEmail string
AppointmentDate string
@@ -97,6 +98,7 @@ func AddressHandler(w http.ResponseWriter, r *http.Request) {
WHEN ap.sched_id IS NOT NULL THEN true
ELSE false
END as assigned,
ap.user_id,
COALESCE(u.first_name || ' ' || u.last_name, '') as user_name,
COALESCE(u.email, '') as user_email,
COALESCE(ap.appointment_date::text, '') as appointment_date,
@@ -133,6 +135,7 @@ func AddressHandler(w http.ResponseWriter, r *http.Request) {
&a.CreatedAt,
&a.UpdatedAt,
&a.Assigned,
&a.UserID,
&a.UserName,
&a.UserEmail,
&a.AppointmentDate,
@@ -324,4 +327,66 @@ func AssignAddressHandler(w http.ResponseWriter, r *http.Request) {
// Redirect back to addresses page with success
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)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form", http.StatusBadRequest)
return
}
userIDStr := r.FormValue("user_id")
addressIDStr := r.FormValue("address_id")
if userIDStr == "" || addressIDStr == "" {
http.Error(w, "User ID and Address ID are required", http.StatusBadRequest)
return
}
userID, err := strconv.Atoi(userIDStr)
if err != nil {
http.Error(w, "Invalid user ID", http.StatusBadRequest)
return
}
addressID, err := strconv.Atoi(addressIDStr)
if err != nil {
http.Error(w, "Invalid address ID", http.StatusBadRequest)
return
}
// Verify the user is managed by current admin
currentAdminID := r.Context().Value("user_id").(int)
var userExists int
err = models.DB.QueryRow(`
SELECT COUNT(*)
FROM admin_volunteers av
JOIN appointment ap ON av.volunteer_id = ap.user_id
WHERE av.admin_id = $1 AND ap.user_id = $2 AND ap.address_id = $3
`, currentAdminID, userID, addressID).Scan(&userExists)
if err != nil {
log.Println("Verification error:", err)
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
if userExists == 0 {
http.Error(w, "Unauthorized removal", http.StatusForbidden)
return
}
// Remove volunteer assignment
_, err = models.DB.Exec(`DELETE FROM appointment WHERE user_id = $1 AND address_id = $2`, userID, addressID)
if err != nil {
log.Println("Remove assignment error:", err)
http.Error(w, "Failed to remove assignment", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/addresses?success=removed", http.StatusSeeOther)
}