/
store.go
57 lines (48 loc) · 1.93 KB
/
store.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
package db
import (
"context"
"time"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"github.com/nam-truong-le/lambda-utils-go/v3/pkg/aws/ssm"
"github.com/nam-truong-le/lambda-utils-go/v3/pkg/mongodb"
)
const colStores = "stores"
func CollectionStores(ctx context.Context) (*mongo.Collection, error) {
database, err := ssm.GetParameter(ctx, "/shop/mongo/database", false)
if err != nil {
return nil, err
}
return mongodb.Collection(ctx, database, colStores)
}
type Store struct {
ID primitive.ObjectID `bson:"_id" json:"id"`
Email string `bson:"email" json:"email"`
Telephone string `bson:"telephone" json:"telephone"`
Name string `bson:"name" json:"name"`
Key string `bson:"key" json:"key"`
Address string `bson:"address" json:"address"`
Owner string `bson:"owner" json:"owner"`
BusinessRegistration string `bson:"businessRegistration" json:"businessRegistration"`
TaxNumber string `bson:"taxNumber" json:"taxNumber"`
MBW map[string]string `bson:"mbw" json:"mbw"`
Slots []map[string]bool `bson:"slots" json:"slots"` // week starts with Sunday = index 0
Holidays []string `bson:"holidays" json:"holidays"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}
func (s *Store) GetSlots(date string) []string {
if lo.Contains(s.Holidays, date) {
return make([]string, 0)
}
d, err := time.Parse("2006-01-02", date)
if err != nil {
return make([]string, 0)
}
weekDay := int(d.Weekday())
slots := s.Slots[weekDay]
return lo.Filter(lo.Keys(slots), func(item string, index int) bool {
return slots[item]
})
}