Skip to content

Commit

Permalink
feat: gather only relevant events (no travels/events)
Browse files Browse the repository at this point in the history
  • Loading branch information
nce committed Nov 29, 2022
1 parent 448ae47 commit ded4540
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
38 changes: 22 additions & 16 deletions icsparser/daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,69 @@ import (
"fmt"
"time"
"errors"
"strings"

"github.com/nce/ics2mattermost/logger"
)

type DailyIngest struct {
EventsToday []Event
TravellingPersons string
AbsentPersons string
TravellingPersons []string
AbsentPersons []string
}

func (c *Calendar) PrepareDailyIngest() (map[string]string, error) {

logger.Info(fmt.Sprintf("amount of meetings: %d", len(c.Events)))
func (c *Calendar) gatherRelevantEvents() DailyIngest {

ingest := DailyIngest{
EventsToday: []Event{},
TravellingPersons: "*no one*",
AbsentPersons: "*no one*",
}
var ingest DailyIngest

for _, event := range c.Events {
travelers, err := event.GetPersonsByCategory("travel")

if err == nil {
ingest.TravellingPersons = travelers
continue
}

absents, err := event.GetPersonsByCategory("leaves")
if err == nil {
ingest.AbsentPersons = absents
continue
}

ingest.EventsToday = append(ingest.EventsToday, event)
}

return ingest
}

func (c *Calendar) PrepareDailyIngest() (map[string]string, error) {


var err error

//ingest.Daily, err = c.GetEventByName("DAILY (ALL)")
if len(c.Events) == 0 {
ingest := c.gatherRelevantEvents()
if len(ingest.EventsToday) == 0 {

logger.Error(err.Error())
return nil, errors.New("no events today")

} else {
logger.Info(fmt.Sprintf("amount of meetings: %d", len(ingest.EventsToday)))

loc := time.Local
var formattedEvents string

for _, e := range c.Events {
for _, e := range ingest.EventsToday {
formattedEvents = formattedEvents + ":calendar: " + e.Start.In(loc).Format("15:04") + " - " +
e.End.In(loc).Format("15:04 MST") + " :fire: [" + e.Summary + "](" + e.Location + ")\n"
}

dailyMessage := map[string]string{
"name": "Foobar",
"text": "#### Welcome to today's daily ingest\n" +
"text": "#### Welcome to VRP's daily ingest\n" +
formattedEvents +
":airplane: " + ingest.TravellingPersons + "\n" +
":palm_tree: " + ingest.AbsentPersons,
":airplane: " + strings.Join(ingest.TravellingPersons, ", ") + "\n" +
":palm_tree: " + strings.Join(ingest.AbsentPersons, ", "),
}

// logger.Info(
Expand Down
7 changes: 3 additions & 4 deletions icsparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package icsparser
import (
"bytes"
"io"
"strings"
"errors"

"github.com/apognu/gocal"
Expand Down Expand Up @@ -117,7 +116,7 @@ func (c *Calendar) GetEventByName(eventName string) (Event, error) {
errors.New("no event found")
}

func (e *Event) GetPersonsByCategory(calendarCategory string) (string, error) {
func (e *Event) GetPersonsByCategory(calendarCategory string) ([]string, error) {
var attendees []string

for _, category := range e.Categories {
Expand All @@ -132,10 +131,10 @@ func (e *Event) GetPersonsByCategory(calendarCategory string) (string, error) {
}

if len(attendees) == 0 {
return "", errors.New("no attendees")
return nil, errors.New("no attendees")
}

return strings.Join(attendees, ", "), nil
return attendees, nil
}


3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func main() {

s := gocron.NewScheduler(time.Local)

s.Every("1h").Do(func() {
//s.Every(1).Weeks().Monday().Tuesday().Wednesday().Thursday().At("8:30").Do(func() {
s.Every(20).Seconds().Do(func() {

cal := icsparser.Setup(
icsUrl,
Expand Down

0 comments on commit ded4540

Please sign in to comment.