fix: filter monthly reports by month range
This commit is contained in:
+28
-2
@@ -819,7 +819,23 @@ func (a *API) dailyReportReminders(w http.ResponseWriter, r *http.Request, p *au
|
||||
func (a *API) monthlyReports(w http.ResponseWriter, r *http.Request, p *auth.Principal, st *store.Store) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
items, next, err := st.MonthlyReports(r.Context(), p.UserID, r.URL.Query().Get("cursor"), limit(r, 50, 100))
|
||||
fromMonth := ""
|
||||
if rawMonth := strings.TrimSpace(r.URL.Query().Get("from_month")); rawMonth != "" {
|
||||
if !validReportMonth(rawMonth) {
|
||||
writeError(w, r, http.StatusBadRequest, 30000, reasonMalformedRequest, "malformed request", map[string]any{"field": "from_month"})
|
||||
return
|
||||
}
|
||||
fromMonth = rawMonth
|
||||
}
|
||||
toMonth := ""
|
||||
if rawMonth := strings.TrimSpace(r.URL.Query().Get("to_month")); rawMonth != "" {
|
||||
if !validReportMonth(rawMonth) {
|
||||
writeError(w, r, http.StatusBadRequest, 30000, reasonMalformedRequest, "malformed request", map[string]any{"field": "to_month"})
|
||||
return
|
||||
}
|
||||
toMonth = rawMonth
|
||||
}
|
||||
items, next, err := st.MonthlyReports(r.Context(), p.UserID, fromMonth, toMonth, r.URL.Query().Get("cursor"), limit(r, 50, 100))
|
||||
if err != nil {
|
||||
writeStoreError(w, r, err)
|
||||
return
|
||||
@@ -832,7 +848,12 @@ func (a *API) monthlyReports(w http.ResponseWriter, r *http.Request, p *auth.Pri
|
||||
if !decodeBody(w, r, &body) {
|
||||
return
|
||||
}
|
||||
report, err := st.CreateMonthlyDraft(r.Context(), p.UserID, "mr_"+randomToken(12), strings.TrimSpace(body.Month))
|
||||
month := strings.TrimSpace(body.Month)
|
||||
if !validReportMonth(month) {
|
||||
writeError(w, r, http.StatusBadRequest, 30000, reasonMalformedRequest, "malformed request", map[string]any{"field": "month"})
|
||||
return
|
||||
}
|
||||
report, err := st.CreateMonthlyDraft(r.Context(), p.UserID, "mr_"+randomToken(12), month)
|
||||
if err != nil {
|
||||
writeStoreError(w, r, err)
|
||||
return
|
||||
@@ -1091,6 +1112,11 @@ func parseDate(value string) (time.Time, error) {
|
||||
return time.ParseInLocation("2006-01-02", strings.TrimSpace(value), time.Local)
|
||||
}
|
||||
|
||||
func validReportMonth(value string) bool {
|
||||
_, err := time.ParseInLocation("2006-01", strings.TrimSpace(value), time.Local)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func parseTime(value string) (time.Time, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
|
||||
Reference in New Issue
Block a user