feat: add a new field to ListGroupsParams - EnabledNotifications (#33)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #33
This commit is contained in:
parent
ddf7efe273
commit
4f10ef572b
|
@ -171,5 +171,16 @@ func (l listGroupsParamsApplier) apply(q *bun.SelectQuery) *bun.SelectQuery {
|
|||
q = q.Where("server_id IN (?)", bun.In(l.params.ServerIDs))
|
||||
}
|
||||
|
||||
if l.params.EnabledNotifications.Valid {
|
||||
if l.params.EnabledNotifications.Bool {
|
||||
q = q.WhereGroup(" AND ", func(query *bun.SelectQuery) *bun.SelectQuery {
|
||||
return q.WhereOr("channel_gains IS NOT NULL").
|
||||
WhereOr("channel_losses IS NOT NULL")
|
||||
})
|
||||
} else {
|
||||
q = q.Where("channel_gains IS NULL").Where("channel_losses IS NULL")
|
||||
}
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
|
|
|
@ -171,6 +171,46 @@ func TestGroup_List(t *testing.T) {
|
|||
"429b790e-7186-4106-b531-4cc4931ce2ba",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ServerIDs=[server-1],EnabledNotifications=true",
|
||||
params: domain.ListGroupsParams{
|
||||
ServerIDs: []string{"server-1"},
|
||||
EnabledNotifications: domain.NullBool{
|
||||
Bool: true,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
expectedGroups: []string{
|
||||
"d56ad37f-2637-48ea-98f8-79627f3fcc96",
|
||||
"429b790e-7186-4106-b531-4cc4931ce2ba",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ServerIDs=[server-2],EnabledNotifications=true",
|
||||
params: domain.ListGroupsParams{
|
||||
ServerIDs: []string{"server-2"},
|
||||
EnabledNotifications: domain.NullBool{
|
||||
Bool: true,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
expectedGroups: []string{
|
||||
"0be82203-4ca3-4b4c-a0c8-3a70099d88f7",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ServerIDs=[server-2],EnabledNotifications=false",
|
||||
params: domain.ListGroupsParams{
|
||||
ServerIDs: []string{"server-2"},
|
||||
EnabledNotifications: domain.NullBool{
|
||||
Bool: false,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
expectedGroups: []string{
|
||||
"abeb6c8e-70b6-445c-989f-890cd2a1f87a",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
4
internal/bundb/testdata/fixture.yml
vendored
4
internal/bundb/testdata/fixture.yml
vendored
|
@ -5,12 +5,14 @@
|
|||
server_id: server-1
|
||||
server_key: pl181
|
||||
version_code: pl
|
||||
channel_losses: 125
|
||||
created_at: 2022-03-15T15:00:10.000Z
|
||||
- _id: group-2-server-1
|
||||
id: 429b790e-7186-4106-b531-4cc4931ce2ba
|
||||
server_id: server-1
|
||||
server_key: pl181
|
||||
version_code: pl
|
||||
channel_gains: 125
|
||||
created_at: 2022-03-15T15:03:10.000Z
|
||||
- _id: group-1-server-2
|
||||
id: abeb6c8e-70b6-445c-989f-890cd2a1f87a
|
||||
|
@ -23,6 +25,8 @@
|
|||
server_id: server-2
|
||||
server_key: pl181
|
||||
version_code: pl
|
||||
channel_gains: 1555
|
||||
channel_losses: 1235
|
||||
created_at: 2022-03-19T15:03:10.000Z
|
||||
- model: Monitor
|
||||
rows:
|
||||
|
|
|
@ -107,6 +107,7 @@ func (u UpdateGroupParams) IsZero() bool {
|
|||
|
||||
type ListGroupsParams struct {
|
||||
ServerIDs []string
|
||||
EnabledNotifications NullBool // check if ChannelGains != null && ChannelLosses != null
|
||||
}
|
||||
|
||||
type GroupLimitReachedError struct {
|
||||
|
|
|
@ -60,51 +60,43 @@ func (g *Group) Create(ctx context.Context, params domain.CreateGroupParams) (do
|
|||
}
|
||||
|
||||
func (g *Group) SetChannelGains(ctx context.Context, id, serverID, channel string) (domain.Group, error) {
|
||||
group, err := g.repo.Update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
return g.update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
ChannelGains: domain.NullString{
|
||||
String: channel,
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Group{}, fmt.Errorf("GroupRepository.Update: %w", err)
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (g *Group) SetChannelLosses(ctx context.Context, id, serverID, channel string) (domain.Group, error) {
|
||||
group, err := g.repo.Update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
return g.update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
ChannelLosses: domain.NullString{
|
||||
String: channel,
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Group{}, fmt.Errorf("GroupRepository.Update: %w", err)
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (g *Group) SetInternals(ctx context.Context, id, serverID string, internals bool) (domain.Group, error) {
|
||||
group, err := g.repo.Update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
return g.update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
Internals: domain.NullBool{
|
||||
Bool: internals,
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Group{}, fmt.Errorf("GroupRepository.Update: %w", err)
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (g *Group) SetBarbarians(ctx context.Context, id, serverID string, barbarians bool) (domain.Group, error) {
|
||||
group, err := g.repo.Update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
return g.update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
Barbarians: domain.NullBool{
|
||||
Bool: barbarians,
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (g *Group) update(ctx context.Context, id, serverID string, params domain.UpdateGroupParams) (domain.Group, error) {
|
||||
group, err := g.repo.Update(ctx, id, serverID, params)
|
||||
if err != nil {
|
||||
return domain.Group{}, fmt.Errorf("GroupRepository.Update: %w", err)
|
||||
}
|
||||
|
|
|
@ -190,7 +190,12 @@ func (m *Monitor) Delete(ctx context.Context, id, serverID string) error {
|
|||
}
|
||||
|
||||
func (m *Monitor) Execute(ctx context.Context) ([]domain.EnnoblementNotification, error) {
|
||||
groups, err := m.groupSvc.List(ctx, domain.ListGroupsParams{})
|
||||
groups, err := m.groupSvc.List(ctx, domain.ListGroupsParams{
|
||||
EnabledNotifications: domain.NullBool{
|
||||
Bool: true,
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GroupService.List: %w", err)
|
||||
}
|
||||
|
|
|
@ -669,15 +669,6 @@ func TestMonitor_Execute(t *testing.T) {
|
|||
|
||||
groupSvc := &mock.FakeGroupReader{}
|
||||
groups := []domain.Group{
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
ServerID: uuid.NewString(),
|
||||
ChannelGains: "", // should be skipped - ChannelGains and ChannelLosses are empty strings
|
||||
ChannelLosses: "",
|
||||
ServerKey: "pl181",
|
||||
VersionCode: "pl",
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
ServerID: uuid.NewString(),
|
||||
|
@ -732,16 +723,24 @@ func TestMonitor_Execute(t *testing.T) {
|
|||
|
||||
repo := &mock.FakeMonitorRepository{}
|
||||
monitors := map[string][]domain.Monitor{
|
||||
groups[1].ID: {
|
||||
groups[0].ID: {
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["pl:pl181"][0].ID,
|
||||
GroupID: groups[1].ID,
|
||||
GroupID: groups[0].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["pl:pl181"][2].ID,
|
||||
GroupID: groups[0].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
},
|
||||
groups[1].ID: {
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["pl:pl181"][1].ID,
|
||||
GroupID: groups[1].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
|
@ -749,7 +748,7 @@ func TestMonitor_Execute(t *testing.T) {
|
|||
groups[2].ID: {
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["pl:pl181"][1].ID,
|
||||
TribeID: tribes["en:en130"][1].ID,
|
||||
GroupID: groups[2].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
|
@ -757,30 +756,22 @@ func TestMonitor_Execute(t *testing.T) {
|
|||
groups[3].ID: {
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["en:en130"][1].ID,
|
||||
TribeID: tribes["pl:pl180"][0].ID,
|
||||
GroupID: groups[3].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
},
|
||||
groups[4].ID: {
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["pl:pl180"][0].ID,
|
||||
GroupID: groups[4].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
},
|
||||
groups[5].ID: {
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["de:de200"][0].ID,
|
||||
GroupID: groups[5].ID,
|
||||
GroupID: groups[4].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
{
|
||||
ID: uuid.NewString(),
|
||||
TribeID: tribes["de:de200"][1].ID,
|
||||
GroupID: groups[5].ID,
|
||||
GroupID: groups[4].ID,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
},
|
||||
|
@ -795,38 +786,38 @@ func TestMonitor_Execute(t *testing.T) {
|
|||
expectedNotifications := []domain.EnnoblementNotification{
|
||||
{
|
||||
Type: domain.EnnoblementNotificationTypeGain,
|
||||
ServerID: groups[1].ServerID,
|
||||
ChannelID: groups[1].ChannelGains,
|
||||
ServerID: groups[0].ServerID,
|
||||
ChannelID: groups[0].ChannelGains,
|
||||
Ennoblement: ennoblementToDomainModel(ennoblements["pl:pl181"][0]),
|
||||
},
|
||||
{
|
||||
Type: domain.EnnoblementNotificationTypeGain,
|
||||
ServerID: groups[1].ServerID,
|
||||
ChannelID: groups[1].ChannelGains,
|
||||
ServerID: groups[0].ServerID,
|
||||
ChannelID: groups[0].ChannelGains,
|
||||
Ennoblement: ennoblementToDomainModel(ennoblements["pl:pl181"][3]),
|
||||
},
|
||||
{
|
||||
Type: domain.EnnoblementNotificationTypeLoss,
|
||||
ServerID: groups[2].ServerID,
|
||||
ChannelID: groups[2].ChannelLosses,
|
||||
ServerID: groups[1].ServerID,
|
||||
ChannelID: groups[1].ChannelLosses,
|
||||
Ennoblement: ennoblementToDomainModel(ennoblements["pl:pl181"][0]),
|
||||
},
|
||||
{
|
||||
Type: domain.EnnoblementNotificationTypeGain,
|
||||
ServerID: groups[3].ServerID,
|
||||
ChannelID: groups[3].ChannelGains,
|
||||
ServerID: groups[2].ServerID,
|
||||
ChannelID: groups[2].ChannelGains,
|
||||
Ennoblement: ennoblementToDomainModel(ennoblements["en:en130"][2]),
|
||||
},
|
||||
{
|
||||
Type: domain.EnnoblementNotificationTypeGain,
|
||||
ServerID: groups[5].ServerID,
|
||||
ChannelID: groups[5].ChannelGains,
|
||||
ServerID: groups[4].ServerID,
|
||||
ChannelID: groups[4].ChannelGains,
|
||||
Ennoblement: ennoblementToDomainModel(ennoblements["de:de200"][1]),
|
||||
},
|
||||
{
|
||||
Type: domain.EnnoblementNotificationTypeGain,
|
||||
ServerID: groups[5].ServerID,
|
||||
ChannelID: groups[5].ChannelGains,
|
||||
ServerID: groups[4].ServerID,
|
||||
ChannelID: groups[4].ChannelGains,
|
||||
Ennoblement: ennoblementToDomainModel(ennoblements["de:de200"][2]),
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user