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