invite: add /count/used route

counts the number of active invites which have been used.
This commit is contained in:
Harvey Tindall
2025-05-27 18:30:21 +01:00
parent 9409370984
commit 80fd7c9842
2 changed files with 25 additions and 0 deletions
+24
View File
@@ -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
+1
View File
@@ -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)