2021-02-03 23:28:02 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2021-02-03 23:28:02 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LoadUserSummary(ss services.ISummaryService, r *http.Request) (*models.Summary, error, int) {
|
2022-12-01 12:57:07 +03:00
|
|
|
summaryParams, err := helpers.ParseSummaryParams(r)
|
2021-02-03 23:28:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusBadRequest
|
|
|
|
}
|
2022-04-18 12:39:26 +03:00
|
|
|
return LoadUserSummaryByParams(ss, summaryParams)
|
|
|
|
}
|
2021-02-03 23:28:02 +03:00
|
|
|
|
2022-04-18 12:39:26 +03:00
|
|
|
func LoadUserSummaryByParams(ss services.ISummaryService, params *models.SummaryParams) (*models.Summary, error, int) {
|
2021-02-03 23:28:02 +03:00
|
|
|
var retrieveSummary services.SummaryRetriever = ss.Retrieve
|
2022-04-18 12:39:26 +03:00
|
|
|
if params.Recompute {
|
2021-02-03 23:28:02 +03:00
|
|
|
retrieveSummary = ss.Summarize
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:39:26 +03:00
|
|
|
summary, err := ss.Aliased(
|
|
|
|
params.From,
|
|
|
|
params.To,
|
|
|
|
params.User,
|
|
|
|
retrieveSummary,
|
|
|
|
params.Filters,
|
|
|
|
params.Recompute,
|
|
|
|
)
|
2021-02-03 23:28:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:39:26 +03:00
|
|
|
summary.FromTime = models.CustomTime(summary.FromTime.T().In(params.User.TZ()))
|
|
|
|
summary.ToTime = models.CustomTime(summary.ToTime.T().In(params.User.TZ()))
|
2021-04-25 15:15:18 +03:00
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
return summary, nil, http.StatusOK
|
|
|
|
}
|