update to volunteer
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
// Add this to your handlers package (create volunteer_posts.go or add to existing file)
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
@@ -12,7 +11,18 @@ import (
|
||||
"github.com/patel-mann/poll-system/app/internal/utils"
|
||||
)
|
||||
|
||||
// VolunteerPostsHandler - Read-only posts view for volunteers
|
||||
type VolunteerStatistics struct {
|
||||
AppointmentsToday int
|
||||
AppointmentsThisWeek int
|
||||
TotalAppointments int
|
||||
PollsCompleted int
|
||||
PollsRemaining int
|
||||
LawnSignsRequested int
|
||||
BannerSignsRequested int
|
||||
PollCompletionPercent int
|
||||
}
|
||||
|
||||
// VolunteerPostsHandler - Dashboard view for volunteers with posts and statistics
|
||||
func VolunteerPostsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow GET requests for volunteers
|
||||
if r.Method != http.MethodGet {
|
||||
@@ -23,7 +33,7 @@ func VolunteerPostsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Get user info from context
|
||||
role := r.Context().Value("user_role").(int)
|
||||
CurrentUserID := models.GetCurrentUserID(w, r)
|
||||
username,_ := models.GetCurrentUserName(r)
|
||||
username, _ := models.GetCurrentUserName(r)
|
||||
|
||||
// Fetch posts from database
|
||||
rows, err := models.DB.Query(`
|
||||
@@ -34,7 +44,7 @@ func VolunteerPostsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
JOIN admin_volunteers x ON u.user_id = x.admin_id
|
||||
WHERE x.volunteer_id = $1
|
||||
ORDER BY p.created_at DESC
|
||||
`,CurrentUserID)
|
||||
`, CurrentUserID)
|
||||
if err != nil {
|
||||
fmt.Printf("Database query error: %v\n", err)
|
||||
http.Error(w, "Failed to fetch posts", http.StatusInternalServerError)
|
||||
@@ -60,20 +70,107 @@ func VolunteerPostsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get volunteer statistics
|
||||
stats, err := getVolunteerStatistics(CurrentUserID)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to fetch statistics: %v\n", err)
|
||||
// Continue with empty stats rather than failing
|
||||
stats = &VolunteerStatistics{}
|
||||
}
|
||||
|
||||
// Get navigation flags
|
||||
showAdminNav, showVolunteerNav := getNavFlags(role)
|
||||
|
||||
fmt.Printf("Volunteer viewing %d posts\n", len(posts))
|
||||
|
||||
|
||||
utils.Render(w, "dashboard/volunteer_dashboard.html", map[string]interface{}{
|
||||
"Title": "Community Posts",
|
||||
"Title": "Volunteer Dashboard",
|
||||
"IsAuthenticated": true,
|
||||
"ShowAdminNav": showAdminNav,
|
||||
"ShowVolunteerNav": showVolunteerNav,
|
||||
"UserName": username,
|
||||
"UserName": username,
|
||||
"Posts": posts,
|
||||
"ActiveSection": "posts",
|
||||
"IsVolunteer": true, // Flag to indicate this is volunteer view
|
||||
"Statistics": stats,
|
||||
"ActiveSection": "dashboard",
|
||||
"IsVolunteer": true,
|
||||
})
|
||||
}
|
||||
|
||||
func getVolunteerStatistics(userID int) (*VolunteerStatistics, error) {
|
||||
stats := &VolunteerStatistics{}
|
||||
today := time.Now().Format("2006-01-02")
|
||||
|
||||
// Get start of current week (Monday)
|
||||
now := time.Now()
|
||||
weekday := now.Weekday()
|
||||
if weekday == time.Sunday {
|
||||
weekday = 7
|
||||
}
|
||||
weekStart := now.AddDate(0, 0, -int(weekday)+1).Format("2006-01-02")
|
||||
|
||||
// Appointments today
|
||||
err := models.DB.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM appointment
|
||||
WHERE user_id = $1 AND DATE(appointment_date) = $2
|
||||
`, userID, today).Scan(&stats.AppointmentsToday)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Appointments this week
|
||||
err = models.DB.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM appointment
|
||||
WHERE user_id = $1 AND DATE(appointment_date) >= $2 AND DATE(appointment_date) <= $3
|
||||
`, userID, weekStart, today).Scan(&stats.AppointmentsThisWeek)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Total appointments
|
||||
err = models.DB.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM appointment
|
||||
WHERE user_id = $1
|
||||
`, userID).Scan(&stats.TotalAppointments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Polls completed
|
||||
err = models.DB.QueryRow(`
|
||||
SELECT COUNT(DISTINCT pr.poll_response_id)
|
||||
FROM poll p
|
||||
JOIN poll_response pr ON p.poll_id = pr.poll_id
|
||||
WHERE p.user_id = $1
|
||||
`, userID).Scan(&stats.PollsCompleted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Polls remaining (appointments without poll responses)
|
||||
stats.PollsRemaining = stats.TotalAppointments - stats.PollsCompleted
|
||||
|
||||
// Calculate completion percentage
|
||||
if stats.TotalAppointments > 0 {
|
||||
stats.PollCompletionPercent = (stats.PollsCompleted * 100) / stats.TotalAppointments
|
||||
} else {
|
||||
stats.PollCompletionPercent = 0
|
||||
}
|
||||
|
||||
// Signs requested
|
||||
err = models.DB.QueryRow(`
|
||||
SELECT
|
||||
COALESCE(SUM(pr.question3_lawn_signs), 0),
|
||||
COALESCE(SUM(pr.question4_banner_signs), 0)
|
||||
FROM poll p
|
||||
JOIN poll_response pr ON p.poll_id = pr.poll_id
|
||||
WHERE p.user_id = $1
|
||||
`, userID).Scan(&stats.LawnSignsRequested, &stats.BannerSignsRequested)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
Reference in New Issue
Block a user