-
Notifications
You must be signed in to change notification settings - Fork 2
Meal groups #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Meal groups #68
Changes from all commits
11fad22
5721ac1
3949818
3d7a167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "math/rand" | ||
| "net/http" | ||
|
|
||
| "github.com/go-chi/chi" | ||
|
|
@@ -29,6 +31,11 @@ type UpdateScanTypesPayload struct { | |
| ScanTypes []store.ScanType `json:"scan_types" validate:"required,dive"` | ||
| } | ||
|
|
||
| type CreateScanResponse struct { | ||
| *store.Scan | ||
| MealGroup *string `json:"meal_group"` | ||
| } | ||
|
|
||
| // getScanTypesHandler returns all configured scan types | ||
| // | ||
| // @Summary Get scan types (Admin) | ||
|
|
@@ -61,7 +68,7 @@ func (app *application) getScanTypesHandler(w http.ResponseWriter, r *http.Reque | |
| // @Accept json | ||
| // @Produce json | ||
| // @Param scan body CreateScanPayload true "Scan to create" | ||
| // @Success 201 {object} store.Scan | ||
| // @Success 201 {object} CreateScanResponse | ||
| // @Failure 400 {object} object{error=string} | ||
| // @Failure 401 {object} object{error=string} | ||
| // @Failure 403 {object} object{error=string} | ||
|
|
@@ -148,7 +155,23 @@ func (app *application) createScanHandler(w http.ResponseWriter, r *http.Request | |
| return | ||
| } | ||
|
|
||
| if err := app.jsonResponse(w, http.StatusCreated, scan); err != nil { | ||
| if found.Category == store.ScanCategoryCheckIn { | ||
| app.assignMealGroup(r.Context(), req.UserID) | ||
| } | ||
|
|
||
| // Fetch meal group for response (non-fatal) | ||
| mealGroup, err := app.store.Application.GetMealGroupByUserID(r.Context(), req.UserID) | ||
| if err != nil && !errors.Is(err, store.ErrNotFound) { | ||
| // We don't want to fail the scan if we can't get the meal group info | ||
| app.logger.Warnw("failed to fetch meal group for scan response", "user_id", req.UserID, "error", err) | ||
| } | ||
|
|
||
| response := CreateScanResponse{ | ||
| Scan: scan, | ||
| MealGroup: mealGroup, | ||
| } | ||
|
|
||
| if err := app.jsonResponse(w, http.StatusCreated, response); err != nil { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like the frontend wasn't updated to take in this new response format? |
||
| app.internalServerError(w, r, err) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a return here
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually its technically not needed, but include it for clarity |
||
| } | ||
| } | ||
|
|
@@ -185,6 +208,33 @@ func (app *application) getUserScansHandler(w http.ResponseWriter, r *http.Reque | |
| } | ||
| } | ||
|
|
||
| func (app *application) assignMealGroup(ctx context.Context, userID string) { | ||
| groups, err := app.store.Settings.GetMealGroups(ctx) | ||
| if err != nil { | ||
| app.logger.Warnw("failed to fetch meal groups for assignment", "error", err) | ||
| return | ||
| } | ||
|
|
||
| if len(groups) == 0 { | ||
| return | ||
| } | ||
|
|
||
| hackerApp, err := app.store.Application.GetByUserID(ctx, userID) | ||
| if err != nil { | ||
| app.logger.Warnw("failed to fetch application for meal group assignment", "user_id", userID, "error", err) | ||
| return | ||
| } | ||
|
|
||
| if hackerApp.MealGroup != nil { | ||
| return // Already assigned | ||
| } | ||
|
|
||
| selectedGroup := groups[rand.Intn(len(groups))] | ||
| if err := app.store.Application.SetMealGroup(ctx, hackerApp.ID, selectedGroup); err != nil { | ||
| app.logger.Warnw("failed to set meal group on application", "app_id", hackerApp.ID, "error", err) | ||
| } | ||
| } | ||
|
|
||
| // getScanStatsHandler returns aggregate scan counts grouped by scan type | ||
| // | ||
| // @Summary Get scan statistics (Admin) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in the future we should separate meal scans and check in scans to different functions, might be helpful for implementing other things based on whether a person is checked in or not.