Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/cc_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (app *App) UpdateAgent(ctx context.Context, agent *model.Agent, limits mode
if limits.Extra != nil {
oldAgent.ExtraEmailCount = *limits.Extra
}
if limits.NoPostponedLimit {
oldAgent.EmailPostponedCount = nil
} else if limits.Postponed != nil {
oldAgent.EmailPostponedCount = model.NewUint32(*limits.Postponed)
}
oldAgent.Supervisor = agent.Supervisor
oldAgent.Team = agent.Team
oldAgent.Region = agent.Region
Expand Down Expand Up @@ -111,7 +116,8 @@ func (a *App) PatchAgent(ctx context.Context, domainId, id int64, patch *model.A
}

oldAgent, err = a.Store.Agent().Update(ctx, oldAgent, model.AgentEmailLimits{
Base: patch.EmailCount, Extra: patch.ExtraEmailCount,
Base: patch.EmailCount, Extra: patch.ExtraEmailCount, Postponed: patch.EmailPostponedCount,
NoPostponedLimit: patch.NoPostponedLimit,
})
if err != nil {
return nil, err
Expand Down
54 changes: 37 additions & 17 deletions app/cc_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ type emailLimitsAgentStore struct {
updated bool
}

func u32(v uint32) *uint32 { return &v }

func sameLimit(a, b *uint32) bool {
if a == nil || b == nil {
return a == b
}
return *a == *b
}

func (s *emailLimitsAgentStore) Get(context.Context, int64, int64) (*model.Agent, model.AppError) {
return &model.Agent{ChatCount: 1, TaskCount: 1, EmailCount: 2, ExtraEmailCount: 3}, nil
return &model.Agent{ChatCount: 1, TaskCount: 1, EmailCount: 2, ExtraEmailCount: 3, EmailPostponedCount: u32(4)}, nil
}

func (s *emailLimitsAgentStore) Update(_ context.Context, a *model.Agent, limits model.AgentEmailLimits) (*model.Agent, model.AppError) {
Expand All @@ -32,18 +41,23 @@ func (s *emailLimitsAgentStore) Update(_ context.Context, a *model.Agent, limits
s.t.Fatal("Update lost Email-limit presence")
}
// Model a newer database value; this checks forwarding, not PostgreSQL locking.
wantBase, wantExtra := uint32(2), uint32(3)
currentBase, currentExtra := uint32(9), uint32(8)
wantBase, wantExtra, wantPostponed := uint32(2), uint32(3), u32(4)
currentBase, currentExtra, currentPostponed := uint32(9), uint32(8), u32(7)
if limits.Base != nil {
wantBase, currentBase = *limits.Base, *limits.Base
}
if limits.Extra != nil {
wantExtra, currentExtra = *limits.Extra, *limits.Extra
}
if a.EmailCount != wantBase || a.ExtraEmailCount != wantExtra {
if limits.NoPostponedLimit {
wantPostponed, currentPostponed = nil, nil
} else if limits.Postponed != nil {
wantPostponed, currentPostponed = limits.Postponed, u32(*limits.Postponed)
}
if a.EmailCount != wantBase || a.ExtraEmailCount != wantExtra || !sameLimit(a.EmailPostponedCount, wantPostponed) {
s.t.Fatal("Email limits were not applied before validation and persistence")
}
a.EmailCount, a.ExtraEmailCount = currentBase, currentExtra
a.EmailCount, a.ExtraEmailCount, a.EmailPostponedCount = currentBase, currentExtra, currentPostponed
s.updated = true
return a, nil
}
Expand All @@ -56,25 +70,30 @@ func TestAgentUpdateAndPatchPreserveEmailLimitPresence(t *testing.T) {
method = "Patch"
}
for _, tc := range []struct {
name string
base, extra *uint32
wantBase uint32
wantExtra uint32
name string
base, extra, postponed *uint32
clearPostponed bool
wantBase uint32
wantExtra uint32
wantPostponed *uint32
}{
{name: "omitted", wantBase: 9, wantExtra: 8},
{name: "zero base", base: &zero, wantBase: 0, wantExtra: 8},
{name: "zero Extra", extra: &zero, wantBase: 9, wantExtra: 0},
{name: "both", base: &five, extra: &zero, wantBase: 5, wantExtra: 0},
{name: "omitted", wantBase: 9, wantExtra: 8, wantPostponed: u32(7)},
{name: "zero base", base: &zero, wantBase: 0, wantExtra: 8, wantPostponed: u32(7)},
{name: "zero Extra", extra: &zero, wantBase: 9, wantExtra: 0, wantPostponed: u32(7)},
{name: "zero postponed", postponed: &zero, wantBase: 9, wantExtra: 8, wantPostponed: u32(0)},
{name: "clear postponed", clearPostponed: true, wantBase: 9, wantExtra: 8, wantPostponed: nil},
{name: "all", base: &five, extra: &zero, postponed: &five, wantBase: 5, wantExtra: 0, wantPostponed: u32(5)},
} {
t.Run(method+"/"+tc.name, func(t *testing.T) {
limits := model.AgentEmailLimits{Base: tc.base, Extra: tc.extra}
limits := model.AgentEmailLimits{Base: tc.base, Extra: tc.extra, Postponed: tc.postponed, NoPostponedLimit: tc.clearPostponed}
agents := &emailLimitsAgentStore{t: t, limits: limits}
a := &App{Store: emailLimitsStore{agent: agents}}
var got *model.Agent
var err model.AppError
if patch {
got, err = a.PatchAgent(context.Background(), 1, 2, &model.AgentPatch{
EmailCount: tc.base, ExtraEmailCount: tc.extra,
EmailCount: tc.base, ExtraEmailCount: tc.extra, EmailPostponedCount: tc.postponed,
NoPostponedLimit: tc.clearPostponed,
})
} else {
got, err = a.UpdateAgent(context.Background(), &model.Agent{
Expand All @@ -84,8 +103,9 @@ func TestAgentUpdateAndPatchPreserveEmailLimitPresence(t *testing.T) {
if err != nil || got == nil || !agents.updated {
t.Fatalf("update failed: %v", err)
}
if got.EmailCount != tc.wantBase || got.ExtraEmailCount != tc.wantExtra {
t.Fatalf("Email limits = %d/%d, want %d/%d", got.EmailCount, got.ExtraEmailCount, tc.wantBase, tc.wantExtra)
if got.EmailCount != tc.wantBase || got.ExtraEmailCount != tc.wantExtra || !sameLimit(got.EmailPostponedCount, tc.wantPostponed) {
t.Fatalf("Email limits = %d/%d/%v, want %d/%d/%v", got.EmailCount, got.ExtraEmailCount, got.EmailPostponedCount,
tc.wantBase, tc.wantExtra, tc.wantPostponed)
}
})
}
Expand Down
Loading
Loading