Skip to content

Commit

Permalink
fix(expiry): nb days displayed in mail is incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaël Philippe authored and Gaël Philippe committed Oct 3, 2022
1 parent 269c8a4 commit 7a65ee4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions api/internal/emailer/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ The Keystone team
</p>
<p>
Some messages you haven't read yet will expire in {{.NbDays}} days.
Some messages you haven't read yet will expire soon.
</p>
<p>
Expand All @@ -155,7 +155,7 @@ The Keystone team
{{ range $groupedProject := .GroupedProjects}}
<p>
- Project: {{$groupedProject.Project.Name}}
- Project: {{$groupedProject.Project.Name}}, in {{$groupedProject.Project.DaysBeforeTTLExpiry}} days
</p>
<p>
Expand Down
17 changes: 10 additions & 7 deletions api/internal/emailer/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ func TestMessageWillExpireMail(t *testing.T) {
groupedProjects: map[uint]GroupedMessageProject{
1: {
Project: models.Project{
Name: "that-one",
Name: "that-one",
DaysBeforeTTLExpiry: 3,
},
Environments: map[string]models.Environment{
"dev": {
Expand All @@ -419,7 +420,8 @@ func TestMessageWillExpireMail(t *testing.T) {
},
2: {
Project: models.Project{
Name: "this-one",
Name: "this-one",
DaysBeforeTTLExpiry: 4,
},
Environments: map[string]models.Environment{
"dev": {
Expand All @@ -435,7 +437,8 @@ func TestMessageWillExpireMail(t *testing.T) {
},
3: {
Project: models.Project{
Name: "that-one-too",
Name: "that-one-too",
DaysBeforeTTLExpiry: 5,
},
Environments: map[string]models.Environment{
"dev": {
Expand All @@ -459,25 +462,25 @@ func TestMessageWillExpireMail(t *testing.T) {
HtmlBody: "",
TextBody: `Hello!
Some messages you haven't read yet will expire in 3 days.
Some messages you haven't read yet will expire soon.
Related projects:
- Project: that-one
- Project: that-one, in 3 days
Environments:
- dev - prod - staging
- Project: this-one
- Project: this-one, in 4 days
Environments:
- dev - prod - staging
- Project: that-one-too
- Project: that-one-too, in 5 days
Environments:
Expand Down
3 changes: 2 additions & 1 deletion api/pkg/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ func SendExpireMessageToUsers(
groupedMessageUser map[uint]emailer.GroupedMessagesUser,
errors *[]error,
) {

// For each recipients, send message.
for _, groupedMessagesUser := range groupedMessageUser {
email, err := emailer.MessageWillExpireMail(
5,
0,
groupedMessagesUser.Projects,
)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions api/pkg/repo/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ func (repo *Repo) GetGroupedMessagesWillExpireByUser(
Model(&models.Message{}).
Joins("inner join environments on messages.environment_id = environments.environment_id").
Joins("inner join projects on environments.project_id = projects.id").
Where("date_trunc('day', messages.created_at) < date_trunc('day', now() - (projects.ttl - projects.days_before_ttl_expiry - 1 || ' days')::interval)").
Where("date_trunc('day', messages.created_at) > date_trunc('day', now() - (projects.ttl - projects.days_before_ttl_expiry + 1 || ' days')::interval)").
Where("date_trunc('day', messages.created_at) = date_trunc('day', now() - (projects.ttl - projects.days_before_ttl_expiry || ' days')::interval)").
Preload("Sender").
Preload("Recipient").
Preload("Environment").
Expand All @@ -166,8 +165,7 @@ func (repo *Repo) GetGroupedMessagesWillExpireByUser(
Model(&models.Message{}).
Joins("inner join environments on messages.environment_id = environments.environment_id").
Joins("inner join projects on environments.project_id = projects.id").
Where("date(messages.created_at, 'start_of_day') < date('now', 'start_of_day', '-' || projects.ttl - projects.days_before_ttl_expiry - 1 || ' days')").
Where("date(messages.created_at, 'start_of_day') > date('now', 'start_of_day', '-' || projects.ttl - projects.days_before_ttl_expiry + 1 || ' days')").
Where("date(messages.created_at, 'start_of_day') = date('now', 'start_of_day', '-' || projects.ttl - projects.days_before_ttl_expiry || ' days')").
Preload("Sender").
Preload("Recipient").
Preload("Environment").
Expand Down

0 comments on commit 7a65ee4

Please sign in to comment.