✨ Seed settings for new users and auto-stop stale entries
This commit is contained in:
@@ -4,6 +4,8 @@ package tracking
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase/apis"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
@@ -24,9 +26,12 @@ var (
|
||||
ErrInvalidWeekKey = errors.New("invalid week key, expected YYYY-Www")
|
||||
)
|
||||
|
||||
// Register wires the tracking routes.
|
||||
// Register wires the tracking routes, the default settings hook and the midnight cron.
|
||||
func Register(app core.App) {
|
||||
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
|
||||
se.Router.GET("/healthz", func(e *core.RequestEvent) error {
|
||||
return e.JSON(http.StatusOK, map[string]string{"status": "ok"})
|
||||
})
|
||||
g := se.Router.Group("/api")
|
||||
g.Bind(apis.RequireAuth("users"))
|
||||
g.POST("/days/{dayKey}/close", handleCloseDay)
|
||||
@@ -36,6 +41,12 @@ func Register(app core.App) {
|
||||
g.DELETE("/weeks/{weekKey}/close", handleReopenWeek)
|
||||
return se.Next()
|
||||
})
|
||||
app.OnRecordAfterCreateSuccess("users").BindFunc(seedDefaultSettings)
|
||||
app.Cron().MustAdd("wotra_midnight", "* * * * *", func() {
|
||||
if _, err := AutoStopStalled(app, time.Now()); err != nil {
|
||||
app.Logger().Error("midnight auto-stop failed", "error", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func writeError(e *core.RequestEvent, status int, err error) error {
|
||||
|
||||
Reference in New Issue
Block a user