2026-03-03 11:27:07 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-03 12:50:24 -06:00
|
|
|
"errors"
|
2026-03-03 11:27:07 -06:00
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// handleKioskGet returns the volunteer's profile, current shift assignments, and
|
2026-03-04 10:53:42 -06:00
|
|
|
// available open shifts in their department. Authenticated by ticket code only —
|
2026-03-03 11:27:07 -06:00
|
|
|
// no JWT required.
|
|
|
|
|
func (app *App) handleKioskGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := r.PathValue("token")
|
2026-03-04 10:53:42 -06:00
|
|
|
t, err := app.getTicketByCode(token)
|
|
|
|
|
if err != nil || t == nil {
|
2026-03-03 11:27:07 -06:00
|
|
|
writeError(w, "not found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 10:53:42 -06:00
|
|
|
var v *Volunteer
|
|
|
|
|
if t.ParticipantID != nil {
|
|
|
|
|
v, _ = app.getVolunteerByParticipantID(*t.ParticipantID)
|
|
|
|
|
}
|
2026-03-03 11:27:07 -06:00
|
|
|
if v == nil {
|
|
|
|
|
writeError(w, "no volunteer record linked to this token", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assigned, _ := app.listShiftsForVolunteer(v.ID)
|
|
|
|
|
if assigned == nil {
|
|
|
|
|
assigned = []Shift{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var available []Shift
|
|
|
|
|
if v.DepartmentID != nil {
|
|
|
|
|
available, _ = app.listOpenShiftsForDept(*v.DepartmentID)
|
|
|
|
|
}
|
|
|
|
|
if available == nil {
|
|
|
|
|
available = []Shift{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeJSON(w, map[string]any{
|
|
|
|
|
"volunteer": v,
|
|
|
|
|
"shifts": assigned,
|
|
|
|
|
"available": available,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleKioskClaim assigns the volunteer to a shift.
|
|
|
|
|
// Without ?force=true it returns 409 with conflicting shifts on overlap.
|
|
|
|
|
func (app *App) handleKioskClaim(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := r.PathValue("token")
|
|
|
|
|
shiftID, err := strconv.Atoi(r.PathValue("id"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, "invalid shift id", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 10:53:42 -06:00
|
|
|
t, err := app.getTicketByCode(token)
|
|
|
|
|
if err != nil || t == nil {
|
2026-03-03 11:27:07 -06:00
|
|
|
writeError(w, "not found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-04 10:53:42 -06:00
|
|
|
var v *Volunteer
|
|
|
|
|
if t.ParticipantID != nil {
|
|
|
|
|
v, _ = app.getVolunteerByParticipantID(*t.ParticipantID)
|
|
|
|
|
}
|
2026-03-03 11:27:07 -06:00
|
|
|
if v == nil {
|
|
|
|
|
writeError(w, "no volunteer linked to this token", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
force := r.URL.Query().Get("force") == "true"
|
|
|
|
|
|
|
|
|
|
if !force {
|
|
|
|
|
conflicts, err := app.checkShiftConflict(v.ID, shiftID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(conflicts) > 0 {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
|
writeJSON(w, map[string]any{
|
|
|
|
|
"conflict": true,
|
|
|
|
|
"conflicting_shifts": conflicts,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shift, err := app.getShift(shiftID)
|
|
|
|
|
if err != nil || shift == nil {
|
|
|
|
|
writeError(w, "shift not found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-03 12:50:24 -06:00
|
|
|
|
|
|
|
|
if err := app.assignShiftWithCapacity(v.ID, shiftID, shift.Capacity); err != nil {
|
|
|
|
|
if errors.Is(err, errShiftFull) {
|
2026-03-03 11:27:07 -06:00
|
|
|
writeError(w, "shift is full", http.StatusConflict)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.handleKioskGet(w, r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleKioskUnclaim removes the volunteer from a shift.
|
|
|
|
|
func (app *App) handleKioskUnclaim(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := r.PathValue("token")
|
|
|
|
|
shiftID, err := strconv.Atoi(r.PathValue("id"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, "invalid shift id", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 10:53:42 -06:00
|
|
|
t, err := app.getTicketByCode(token)
|
|
|
|
|
if err != nil || t == nil {
|
2026-03-03 11:27:07 -06:00
|
|
|
writeError(w, "not found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-04 10:53:42 -06:00
|
|
|
var v *Volunteer
|
|
|
|
|
if t.ParticipantID != nil {
|
|
|
|
|
v, _ = app.getVolunteerByParticipantID(*t.ParticipantID)
|
|
|
|
|
}
|
2026-03-03 11:27:07 -06:00
|
|
|
if v == nil {
|
|
|
|
|
writeError(w, "no volunteer linked to this token", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := app.unassignShift(v.ID, shiftID); err != nil {
|
|
|
|
|
writeError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.handleKioskGet(w, r)
|
|
|
|
|
}
|