2022-11-13 23:17:55 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-02-26 02:54:40 +00:00
|
|
|
"context"
|
2022-11-13 23:17:55 +00:00
|
|
|
"net/http"
|
|
|
|
|
2023-02-26 02:54:40 +00:00
|
|
|
"github.com/google/uuid"
|
2022-11-13 23:17:55 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/safeserve/errchain"
|
|
|
|
"github.com/hay-kot/safeserve/server"
|
2022-11-13 23:17:55 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2023-02-09 02:59:04 +00:00
|
|
|
type ActionAmountResult struct {
|
2022-11-13 23:17:55 +00:00
|
|
|
Completed int `json:"completed"`
|
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
func actionHandlerFactory(ref string, fn func(context.Context, uuid.UUID) (int, error)) errchain.HandlerFunc {
|
2022-11-13 23:17:55 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
|
2023-02-26 02:54:40 +00:00
|
|
|
totalCompleted, err := fn(ctx, ctx.GID)
|
2022-11-13 23:17:55 +00:00
|
|
|
if err != nil {
|
2023-02-26 02:54:40 +00:00
|
|
|
log.Err(err).Str("action_ref", ref).Msg("failed to run action")
|
2022-11-13 23:17:55 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, ActionAmountResult{Completed: totalCompleted})
|
2023-02-09 02:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// HandleEnsureAssetID godoc
|
|
|
|
//
|
|
|
|
// @Summary Ensure Asset IDs
|
|
|
|
// @Description Ensures all items in the database have an asset ID
|
|
|
|
// @Tags Actions
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} ActionAmountResult
|
|
|
|
// @Router /v1/actions/ensure-asset-ids [Post]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleEnsureAssetID() errchain.HandlerFunc {
|
2023-02-26 02:54:40 +00:00
|
|
|
return actionHandlerFactory("ensure asset IDs", ctrl.svc.Items.EnsureAssetID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleEnsureImportRefs godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Ensures Import Refs
|
|
|
|
// @Description Ensures all items in the database have an import ref
|
|
|
|
// @Tags Actions
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} ActionAmountResult
|
|
|
|
// @Router /v1/actions/ensure-import-refs [Post]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleEnsureImportRefs() errchain.HandlerFunc {
|
2023-02-26 02:54:40 +00:00
|
|
|
return actionHandlerFactory("ensure import refs", ctrl.svc.Items.EnsureImportRef)
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:59:04 +00:00
|
|
|
// HandleItemDateZeroOut godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Zero Out Time Fields
|
|
|
|
// @Description Resets all item date fields to the beginning of the day
|
|
|
|
// @Tags Actions
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} ActionAmountResult
|
|
|
|
// @Router /v1/actions/zero-item-time-fields [Post]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemDateZeroOut() errchain.HandlerFunc {
|
2023-02-26 02:54:40 +00:00
|
|
|
return actionHandlerFactory("zero out date time", ctrl.repo.Items.ZeroOutTimeFields)
|
2022-11-13 23:17:55 +00:00
|
|
|
}
|