From 4f10ef572b0293344051a00471039028fe1dcbcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Sat, 29 Oct 2022 05:24:52 +0000 Subject: [PATCH] feat: add a new field to ListGroupsParams - EnabledNotifications (#33) Reviewed-on: https://gitea.dwysokinski.me/twhelp/dcbot/pulls/33 --- internal/bundb/group.go | 11 ++++++ internal/bundb/group_test.go | 40 +++++++++++++++++++ internal/bundb/testdata/fixture.yml | 4 ++ internal/domain/group.go | 3 +- internal/service/group.go | 24 ++++-------- internal/service/monitor.go | 7 +++- internal/service/monitor_test.go | 61 ++++++++++++----------------- 7 files changed, 97 insertions(+), 53 deletions(-) diff --git a/internal/bundb/group.go b/internal/bundb/group.go index fc7263c..33cb1f2 100644 --- a/internal/bundb/group.go +++ b/internal/bundb/group.go @@ -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 } diff --git a/internal/bundb/group_test.go b/internal/bundb/group_test.go index f473176..5690622 100644 --- a/internal/bundb/group_test.go +++ b/internal/bundb/group_test.go @@ -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 { diff --git a/internal/bundb/testdata/fixture.yml b/internal/bundb/testdata/fixture.yml index 79ced71..1068143 100644 --- a/internal/bundb/testdata/fixture.yml +++ b/internal/bundb/testdata/fixture.yml @@ -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: diff --git a/internal/domain/group.go b/internal/domain/group.go index 6c90ee8..debb4f0 100644 --- a/internal/domain/group.go +++ b/internal/domain/group.go @@ -106,7 +106,8 @@ func (u UpdateGroupParams) IsZero() bool { } type ListGroupsParams struct { - ServerIDs []string + ServerIDs []string + EnabledNotifications NullBool // check if ChannelGains != null && ChannelLosses != null } type GroupLimitReachedError struct { diff --git a/internal/service/group.go b/internal/service/group.go index f826a31..21cd745 100644 --- a/internal/service/group.go +++ b/internal/service/group.go @@ -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) } diff --git a/internal/service/monitor.go b/internal/service/monitor.go index 3fda0b0..b239609 100644 --- a/internal/service/monitor.go +++ b/internal/service/monitor.go @@ -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) } diff --git a/internal/service/monitor_test.go b/internal/service/monitor_test.go index 86e8360..94e34bb 100644 --- a/internal/service/monitor_test.go +++ b/internal/service/monitor_test.go @@ -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]), }, }