/
sales-channel-location.go
129 lines (115 loc) · 3.52 KB
/
sales-channel-location.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package services
import (
"context"
"github.com/driver005/gateway/models"
"github.com/driver005/gateway/sql"
"github.com/driver005/gateway/types"
"github.com/driver005/gateway/utils"
"github.com/google/uuid"
"github.com/samber/lo"
)
type SalesChannelLocationService struct {
ctx context.Context
r Registry
}
func NewSalesChannelLocationService(
r Registry,
) *SalesChannelLocationService {
return &SalesChannelLocationService{
context.Background(),
r,
}
}
func (s *SalesChannelLocationService) SetContext(context context.Context) *SalesChannelLocationService {
s.ctx = context
return s
}
func (s *SalesChannelLocationService) RemoveLocation(salesChannelId uuid.UUID, locationId uuid.UUID) *utils.ApplictaionError {
var res []models.SalesChannelLocation
query := sql.BuildQuery(
models.SalesChannelLocation{
SalesChannelId: uuid.NullUUID{UUID: salesChannelId},
LocationId: uuid.NullUUID{UUID: locationId},
},
&sql.Options{},
)
if err := s.r.SalesChannelLocationRepository().Find(s.ctx, &res, query); err != nil {
return err
}
if len(res) > 0 {
if err := s.r.SalesChannelLocationRepository().RemoveSlice(s.ctx, res); err != nil {
return err
}
}
return nil
}
func (s *SalesChannelLocationService) AssociateLocation(salesChannelId uuid.UUID, locationId uuid.UUID) *utils.ApplictaionError {
salesChannel, err := s.r.SalesChannelService().SetContext(s.ctx).RetrieveById(salesChannelId, &sql.Options{})
if err != nil {
return err
}
if s.r.StockLocationService() != nil {
_, err = s.r.StockLocationService().Retrieve(s.ctx, locationId, &sql.Options{})
if err != nil {
return err
}
}
salesChannelLocation := &models.SalesChannelLocation{
SalesChannelId: uuid.NullUUID{UUID: salesChannel.Id},
LocationId: uuid.NullUUID{UUID: locationId},
}
if err := s.r.SalesChannelLocationRepository().Save(s.ctx, salesChannelLocation); err != nil {
return err
}
return nil
}
func (s *SalesChannelLocationService) ListLocationIds(salesChannelId uuid.UUIDs) (uuid.UUIDs, *utils.ApplictaionError) {
salesChannels, err := s.r.SalesChannelService().SetContext(s.ctx).List(&types.FilterableSalesChannel{}, &sql.Options{
Selects: []string{"id"},
Specification: []sql.Specification{sql.In("id", salesChannelId)},
})
if err != nil {
return nil, err
}
var locations []models.SalesChannelLocation
query := sql.BuildQuery(
models.SalesChannelLocation{},
&sql.Options{
Selects: []string{"location_id"},
Specification: []sql.Specification{sql.In("sales_channel_id", lo.Map(salesChannels, func(salesChannel models.SalesChannel, index int) uuid.UUID {
return salesChannel.Id
}))},
},
)
if err := s.r.SalesChannelLocationRepository().Find(s.ctx, &locations, query); err != nil {
return nil, err
}
var res uuid.UUIDs
for _, l := range locations {
res = append(res, l.LocationId.UUID)
}
return res, nil
}
func (s *SalesChannelLocationService) ListSalesChannelIds(locationId uuid.UUID) (uuid.UUIDs, *utils.ApplictaionError) {
location, err := s.r.StockLocationService().Retrieve(s.ctx, locationId, &sql.Options{})
if err != nil {
return nil, err
}
var locations []models.SalesChannelLocation
query := sql.BuildQuery(
models.SalesChannelLocation{
LocationId: uuid.NullUUID{UUID: location.Id},
},
&sql.Options{
Selects: []string{"sales_channel_id"},
},
)
if err := s.r.SalesChannelLocationRepository().Find(s.ctx, &locations, query); err != nil {
return nil, err
}
var res uuid.UUIDs
for _, l := range locations {
res = append(res, l.SalesChannelId.UUID)
}
return res, nil
}