-
Notifications
You must be signed in to change notification settings - Fork 38
/
calendar.go
53 lines (43 loc) · 1.75 KB
/
calendar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package internalapi
import (
"time"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/dates"
calendarop "github.com/transcom/mymove/pkg/gen/internalapi/internaloperations/calendar"
"github.com/transcom/mymove/pkg/gen/internalmessages"
"github.com/transcom/mymove/pkg/handlers"
)
// ShowAvailableMoveDatesHandler returns the available move dates starting at a given date.
type ShowAvailableMoveDatesHandler struct {
handlers.HandlerConfig
}
// Handle returns the available move dates.
func (h ShowAvailableMoveDatesHandler) Handle(params calendarop.ShowAvailableMoveDatesParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(_ appcontext.AppContext) (middleware.Responder, error) {
startDate := time.Time(params.StartDate)
var availableMoveDatesPayload internalmessages.AvailableMoveDates
availableMoveDatesPayload.StartDate = handlers.FmtDate(startDate)
var datesPayload []strfmt.Date
const daysToCheckAfterStartDate = 90
const shortFuseTotalDays = 5
daysChecked := 0
shortFuseDaysFound := 0
usCalendar := dates.NewUSCalendar()
firstPossibleDate := startDate.AddDate(0, 0, 1) // We never include the start date.
for d := firstPossibleDate; daysChecked < daysToCheckAfterStartDate; d = d.AddDate(0, 0, 1) {
if usCalendar.IsWorkday(d) {
if shortFuseDaysFound < shortFuseTotalDays {
shortFuseDaysFound++
} else {
datesPayload = append(datesPayload, strfmt.Date(d))
}
}
daysChecked++
}
availableMoveDatesPayload.Available = datesPayload
return calendarop.NewShowAvailableMoveDatesOK().WithPayload(&availableMoveDatesPayload), nil
})
}