-
Notifications
You must be signed in to change notification settings - Fork 4
/
calendar-resolver.go
77 lines (70 loc) · 2.52 KB
/
calendar-resolver.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
package graph
import (
"context"
"fmt"
"time"
cache "github.com/Code-Hex/go-generics-cache"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/graph/api/model"
"github.com/bcc-code/bcc-media-platform/backend/loaders"
"github.com/bcc-code/bcc-media-platform/backend/memorycache"
"github.com/bcc-code/bcc-media-platform/backend/utils"
"github.com/samber/lo"
)
func getForPeriod[k comparable, t any](ctx context.Context, loader *loaders.Loader[k, *t], factory func(ctx context.Context, from time.Time, to time.Time) ([]k, error), from time.Time, to time.Time) ([]*t, error) {
fromTrunc := from.Truncate(time.Hour * 1)
toTrunc := to.Truncate(time.Hour * 1)
ids, err := memorycache.GetOrSet(ctx, fmt.Sprintf("period-%s-%s", fromTrunc.Format(time.RFC3339), toTrunc.Format(time.RFC3339)), func(ctx context.Context) ([]k, error) {
return factory(ctx, from, to)
}, cache.WithExpiration(time.Minute*5))
if err != nil {
return nil, err
}
items, err := loader.GetMany(ctx, ids)
if err != nil {
return nil, err
}
return items, nil
}
func getActiveDays(entries []*common.CalendarEntry, sourceTime time.Time) []string {
zone, offset := sourceTime.Zone()
location := time.FixedZone(zone, offset)
var days []string
for _, entry := range entries {
date := entry.Start.In(location)
dateString := date.Format("2006-01-02")
if !lo.Contains(days, dateString) {
days = append(days, dateString)
}
//endDate := entry.End.In(location)
//endDateString := endDate.Format("2006-01-02")
//if !endDate.After(date) {
// continue
//}
//for endDateString != dateString {
// if !lo.Contains(days, endDateString) {
// days = append(days, endDateString)
// }
// endDate = endDate.Add(time.Hour * -24)
// endDateString = endDate.Format("2006-01-02")
//}
}
return lo.Map(days, func(date string, _ int) string {
t, _ := time.ParseInLocation("2006-01-02", date, location)
return t.Format(time.RFC3339)
})
}
func (r *calendarResolver) periodResolver(ctx context.Context, from time.Time, to time.Time) (*model.CalendarPeriod, error) {
events, err := getForPeriod(ctx, r.Loaders.EventLoader, r.Queries.GetEventsForPeriod, from, to)
if err != nil {
return nil, err
}
entries, err := getForPeriod(ctx, r.GetFilteredLoaders(ctx).CalendarEntryLoader, r.Queries.GetCalendarEntriesForPeriod, from, to)
if err != nil {
return nil, err
}
return &model.CalendarPeriod{
Events: utils.MapWithCtx(ctx, events, model.EventFrom),
ActiveDays: getActiveDays(entries, from),
}, nil
}