From 80fd7c98428e1e85bca708aafc30bdccb69c113f Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 27 May 2025 18:30:21 +0100 Subject: [PATCH] invite: add /count/used route counts the number of active invites which have been used. --- api-invites.go | 24 ++++++++++++++++++++++++ router.go | 1 + 2 files changed, 25 insertions(+) diff --git a/api-invites.go b/api-invites.go index ef6b2e1..7c7e372 100644 --- a/api-invites.go +++ b/api-invites.go @@ -275,6 +275,30 @@ func (app *appContext) GetInviteCount(gc *gin.Context) { gc.JSON(200, resp) } +// @Summary Get the number of invites stored in the database that have been used (but are still valid). +// @Produce json +// @Success 200 {object} PageCountDTO +// @Router /invites/count [get] +// @Security Bearer +// @tags Invites +func (app *appContext) GetInviteUsedCount(gc *gin.Context) { + resp := PageCountDTO{} + var err error + resp.Count, err = app.storage.db.Count(&Invite{}, badgerhold.Where("usedBy").MatchFunc(func(ra *badgerhold.RecordAccess) (bool, error) { + field := ra.Field() + switch usedBy := field.(type) { + case [][]string: + return len(usedBy) > 0, nil + default: + return false, nil + } + })) + if err != nil { + resp.Count = 0 + } + gc.JSON(200, resp) +} + // @Summary Get invites. // @Produce json // @Success 200 {object} getInvitesDTO diff --git a/router.go b/router.go index a7e3113..8720269 100644 --- a/router.go +++ b/router.go @@ -201,6 +201,7 @@ func (app *appContext) loadRoutes(router *gin.Engine) { api.POST(p+"/invites", app.GenerateInvite) api.GET(p+"/invites", app.GetInvites) api.GET(p+"/invites/count", app.GetInviteCount) + api.GET(p+"/invites/count/used", app.GetInviteUsedCount) api.DELETE(p+"/invites", app.DeleteInvite) api.POST(p+"/invites/profile", app.SetProfile) api.GET(p+"/profiles", app.GetProfiles)