-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.go
101 lines (90 loc) · 3.05 KB
/
action.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package services
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/harborchurchla/harborbot/internal/entities"
"github.com/harborchurchla/harborbot/internal/utils"
"sort"
"time"
)
type ActionService struct {
*ScheduleService
}
func NewActionService(ss *ScheduleService) *ActionService {
return &ActionService{ss}
}
func (s *ActionService) ListActions() []*entities.Action {
return []*entities.Action{
{ID: entities.GetTeamScheduleAction, Description: ""},
{ID: entities.GetWhoIsServingThisWeekAction, Description: ""},
}
}
func (s *ActionService) ExecuteByID(id string, params map[string][]string) (*entities.ActionResult, error) {
switch id {
case entities.GetTeamScheduleAction:
if len(params["team"]) != 1 {
return nil, fmt.Errorf("one param 'team' is required for action %s", id)
}
return s.ExecuteGetTeamScheduleAction(params["team"][0])
case entities.GetWhoIsServingThisWeekAction:
if len(params["team"]) != 1 {
return nil, fmt.Errorf("one param 'team' is required for action %s", id)
}
return s.ExecuteGetWhoIsServingThisWeekAction(params["team"][0])
default:
return nil, fmt.Errorf("error executing unknown action %s", id)
}
}
func (s *ActionService) ExecuteGetTeamScheduleAction(team string) (*entities.ActionResult, error) {
schedule, err := s.ScheduleService.GetByTeam(team)
if err != nil {
return nil, fmt.Errorf("error while fetching %s team schedule: %v", team, err)
}
entries := schedule.GetFutureEntries()
if len(entries) == 0 {
return nil, errors.New("the schedule needs to be updated")
}
scheduleText := ""
for _, entry := range entries {
scheduleText += fmt.Sprintf("%s: *%s*\n", time.Time(entry.Date).Format("1/2/2006"), utils.ReplaceNameWithSlackMention(entry.TeamMembers))
}
sheetText := fmt.Sprintf("_Feel free to make changes on the google sheet:_ https://docs.google.com/spreadsheets/d/%s", s.ScheduleService.GetSheetID())
return &entities.ActionResult{
Message: fmt.Sprintf(
"_Here's the upcoming schedule for the %s team:_\n\n%s\n\n%s",
team,
scheduleText,
sheetText,
),
Metadata: gin.H{
"schedule": schedule,
},
}, nil
}
func (s *ActionService) ExecuteGetWhoIsServingThisWeekAction(team string) (*entities.ActionResult, error) {
schedule, err := s.ScheduleService.GetByTeam(team)
if err != nil {
return nil, fmt.Errorf("error while fetching %s team schedule: %v", team, err)
}
entries := schedule.GetFutureEntries()
if len(entries) == 0 {
return nil, errors.New("the schedule needs to be updated")
}
sort.SliceStable(entries, func(i, j int) bool {
return time.Time(entries[i].Date).Before(time.Time(entries[j].Date))
})
next := entries[0]
sheetText := fmt.Sprintf("_Feel free to make changes on the google sheet:_ https://docs.google.com/spreadsheets/d/%s", s.ScheduleService.GetSheetID())
return &entities.ActionResult{
Message: fmt.Sprintf(
"_Here's who's serving this %s:_\n\n*%s*\n\n%s",
time.Time(next.Date).Format("Mon Jan _2"),
utils.ReplaceNameWithSlackMention(next.TeamMembers),
sheetText,
),
Metadata: gin.H{
"schedule": schedule,
},
}, nil
}