diff --git a/internal/models/monthlyReport.go b/internal/models/monthlyReport.go index c7cb11237831526f0946e38f275c3439390d550d..53273487ee833efd8462ab5e321d5eb17971ad05 100644 --- a/internal/models/monthlyReport.go +++ b/internal/models/monthlyReport.go @@ -87,7 +87,7 @@ func (dh *DataHandler) getMonthlyReport(year int, month int) (monthlyReport Mont err = dh.sqlClient.Where("year = ? AND month = ?", year, month).First(&mailSubject).Error if err != nil || mailSubject.Subject == "" { // Build default object - mailSubject.Subject = fmt.Sprintf("[Ecolyo] Votre bilan %s %v", getMonthNameWithPrep(month-1), year) + mailSubject.Subject = fmt.Sprintf("[Ecolyo] Votre bilan %s", getDateWithPrep(month-1, year)) } var monthlyNews MonthlyNews @@ -111,7 +111,7 @@ func (dh *DataHandler) getMonthlyReport(year int, month int) (monthlyReport Mont return monthlyReport, nil } -func getMonthNameWithPrep(month int) string { +func getDateWithPrep(month int, year int) string { frenchMonths := []string{ "de janvier", "de février", @@ -126,5 +126,14 @@ func getMonthNameWithPrep(month int) string { "de novembre", "de décembre", } - return frenchMonths[month] + + var prevMonth = month - 1 + var prevYear = year + // If month is January return December of the previous year + if month == 0 { + prevMonth = 11 + prevYear = year - 1 + } + + return fmt.Sprintf("%s %d", frenchMonths[prevMonth], prevYear) } diff --git a/internal/rootmux/rootmux_test.go b/internal/rootmux/rootmux_test.go index 07f4b332f99fd15612dddf324838da3a270eea74..21e05c53e5c1a8a890784e998e1caca1f6f5562b 100644 --- a/internal/rootmux/rootmux_test.go +++ b/internal/rootmux/rootmux_test.go @@ -123,7 +123,7 @@ func unloggedTests(t *testing.T) { // Try to get a monthlyReport (must fail because no parameters are given) do("GET", "/api/common/monthlyReport", noH, "", http.StatusBadRequest, "") // Try to get a monthlyReport (must pass empty) - do("GET", "/api/common/monthlyReport?year=2021&month=12", noH, "", http.StatusOK, `{"year":2021,"month":12,"subject":"[Ecolyo] Votre bilan de décembre 2021","info":"","image":"","newsTitle":"","newsContent":"","question":"","link":""}`) + do("GET", "/api/common/monthlyReport?year=2021&month=12", noH, "", http.StatusOK, `{"year":2021,"month":12,"subject":"[Ecolyo] Votre bilan de novembre 2021","info":"","image":"","newsTitle":"","newsContent":"","question":"","link":""}`) // Try to get partnersInfo (must pass) do("GET", "/api/common/partnersInfo", noH, "", http.StatusOK, `{"ID":1,"grdf_failure":false,"enedis_failure":false,"egl_failure":false,"notification_activated":false}`) } @@ -156,7 +156,7 @@ func adminTests(t *testing.T) { // Try to get the monthlyNews created (must pass) do("GET", "/api/admin/monthlyNews/2021/1", xsrfHeader, "", http.StatusOK, `{"year":2021,"month":1,"title":"Les nouveautés du service","content":"Nouvelles fonctionnalités"`) // Try to get the monthlyReport (must pass) - do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de janvier 2021","info":"","image":"","newsTitle":"Les nouveautés du service","newsContent":"Nouvelles fonctionnalités","question":"","link":""}`) + do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de décembre 2020","info":"","image":"","newsTitle":"Les nouveautés du service","newsContent":"Nouvelles fonctionnalités","question":"","link":""}`) // Try to create a poll without the XSRF-TOKEN (must fail) do("PUT", "/api/admin/poll", noH, newPollStr, http.StatusUnauthorized, "XSRF protection triggered") @@ -171,7 +171,7 @@ func adminTests(t *testing.T) { // Try to get the poll created (must pass) do("GET", "/api/admin/poll/2021/1", xsrfHeader, "", http.StatusOK, newPollStr) // Try to get the monthlyReport (must pass) - do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de janvier 2021","info":"","image":"","newsTitle":"Les nouveautés du service","newsContent":"Nouvelles fonctionnalités","question":"pollQuestion","link":"pollLink"}`) + do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de décembre 2020","info":"","image":"","newsTitle":"Les nouveautés du service","newsContent":"Nouvelles fonctionnalités","question":"pollQuestion","link":"pollLink"}`) // Try to create a monthlyInfo without the XSRF-TOKEN (must fail) do("PUT", "/api/admin/monthlyInfo", noH, monthlyInfoStr, http.StatusUnauthorized, "XSRF protection triggered") @@ -186,7 +186,7 @@ func adminTests(t *testing.T) { // Try to get the monthlyInfo created (must pass) do("GET", "/api/admin/monthlyInfo/2021/1", xsrfHeader, "", http.StatusOK, monthlyInfoStr) // Try to get the monthlyReport (must pass) - do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de janvier 2021","info":"Informations du mois","image":"imagebase64","newsTitle":"Les nouveautés du service","newsContent":"Nouvelles fonctionnalités","question":"pollQuestion","link":"pollLink"`) + do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de décembre 2020","info":"Informations du mois","image":"imagebase64","newsTitle":"Les nouveautés du service","newsContent":"Nouvelles fonctionnalités","question":"pollQuestion","link":"pollLink"`) // Try to update the partnersInfo (must pass) do("PUT", "/api/admin/partnersInfo", xsrfHeader, partnersInfoStr, http.StatusOK, partnersInfoStr) @@ -219,7 +219,7 @@ func adminTests(t *testing.T) { // Try to get a mail subject after it is deleted (must fail because not found) do("GET", "/api/admin/mailSubject/2021/1", xsrfHeader, "", http.StatusNotFound, "") // Try to get the monthlyReport (must pass) - do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de janvier 2021","info":"Informations du mois","image":"imagebase64","newsTitle":"","newsContent":"","question":"","link":""`) + do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de décembre 2020","info":"Informations du mois","image":"imagebase64","newsTitle":"","newsContent":"","question":"","link":""`) } // Try to login (must pass)