From 72825aef8dacddc558569f15fe17c84908d8ba03 Mon Sep 17 00:00:00 2001 From: temi Date: Tue, 14 Nov 2023 19:16:19 +1100 Subject: [PATCH] #3004 - added organisation download - generalised download code - refactored code --- ...anagementUnitReport.js => entityReport.js} | 16 +++---- .../ala/merit/ManagementUnitController.groovy | 35 --------------- .../au/org/ala/merit/ReportController.groovy | 44 ++++++++++++++++--- .../ala/merit/ManagementUnitService.groovy | 21 --------- .../au/org/ala/merit/ReportService.groovy | 22 ++++++++++ grails-app/views/admin/adminReports.gsp | 44 +++++++++++++++---- .../au/org/ala/fieldcapture/AdminSpec.groovy | 22 ++++++++++ .../groovy/pages/AdminReportsPage.groovy | 22 ++++++++-- .../merit/ManagementUnitControllerSpec.groovy | 19 +------- .../merit/ManagementUnitServiceSpec.groovy | 13 ------ .../org/ala/merit/ReportControllerSpec.groovy | 32 ++++++++++++++ .../au/org/ala/merit/ReportServiceSpec.groovy | 16 ++++++- src/test/js/spec/AdminReportSpec.js | 12 ++--- 13 files changed, 199 insertions(+), 119 deletions(-) rename grails-app/assets/javascripts/{managementUnitReport.js => entityReport.js} (70%) create mode 100644 src/test/groovy/au/org/ala/merit/ReportControllerSpec.groovy diff --git a/grails-app/assets/javascripts/managementUnitReport.js b/grails-app/assets/javascripts/entityReport.js similarity index 70% rename from grails-app/assets/javascripts/managementUnitReport.js rename to grails-app/assets/javascripts/entityReport.js index 02c9cc771..fc35053da 100644 --- a/grails-app/assets/javascripts/managementUnitReport.js +++ b/grails-app/assets/javascripts/entityReport.js @@ -1,4 +1,4 @@ -function ManagementUnitReportSelectorViewModel(options) { +function EntityReportSelectorViewModel(options) { var self = this; var config = _.defaults(options); @@ -8,20 +8,20 @@ function ManagementUnitReportSelectorViewModel(options) { self.fromDate = ko.observable(d).extend({simpleDate:false}); self.toDate = ko.observable(now).extend({simpleDate:false}); - self.muReportDownload = function () { + self.entityReportDownload = function () { var summaryFlag = false; - return generateMuReport(summaryFlag) + return generateEntityReport(summaryFlag) }; - self.muReportDownloadSummary = function() { + self.entityReportDownloadSummary = function() { var summaryFlag = true; - generateMuReport(summaryFlag); + generateEntityReport(summaryFlag); }; - function generateMuReport(summaryFlag) { + function generateEntityReport(summaryFlag) { var json = {fromDate:self.fromDate(), toDate:self.toDate(), summaryFlag: summaryFlag} return $.ajax({ - url: config.generateMUReportInPeriodUrl, + url: config.generateEntityReportInPeriodUrl, type: 'GET', data: json, dataType:'json', @@ -33,7 +33,7 @@ function ManagementUnitReportSelectorViewModel(options) { var details = data['details'] var message = data['message'] var detailsIcon = ' ' - var detailsPanel = '
Try this link, if you cannot get an email confirmation
' + var detailsPanel = '
Try this link, if you cannot get an email confirmation
' bootbox.alert(message + detailsIcon + detailsPanel) } }).fail(function() { diff --git a/grails-app/controllers/au/org/ala/merit/ManagementUnitController.groovy b/grails-app/controllers/au/org/ala/merit/ManagementUnitController.groovy index e89512097..966297fd3 100644 --- a/grails-app/controllers/au/org/ala/merit/ManagementUnitController.groovy +++ b/grails-app/controllers/au/org/ala/merit/ManagementUnitController.groovy @@ -9,8 +9,6 @@ import grails.converters.JSON import grails.core.GrailsApplication import grails.web.mapping.LinkGenerator import org.apache.http.HttpStatus -import java.text.ParseException - /** * Processes requests relating to MUs */ @@ -352,39 +350,6 @@ class ManagementUnitController { render featureCollection as JSON } - @PreAuthorise(accessLevel='siteReadOnly') - def generateReportsInPeriod(){ - - String startDate = params.fromDate - String endDate = params.toDate - - try{ - - def user = userService.getUser() - def extras =[:] - extras.summaryFlag = params.summaryFlag - - String email = user.userName - extras.put("systemEmail", grailsApplication.config.getProperty('fieldcapture.system.email.address')) - extras.put("senderEmail", grailsApplication.config.getProperty('fieldcapture.system.email.address')) - extras.put("email", email) - - String reportDownloadBaseUrl= grailsLinkGenerator.link(controller:'download',action:'get', absolute: true) - extras.put("reportDownloadBaseUrl", reportDownloadBaseUrl) - - def resp = managementUnitService.generateReports(startDate, endDate,extras) - render resp as JSON - - }catch (ParseException e){ - def message = [message: 'Error: You need to provide startDate and endDate in the format of yyyy-MM-dd '] - response.setContentType("application/json") - render message as JSON - }catch(Exception e){ - def message = [message: 'Fatal: '+ e.message] - render message as JSON - } - } - /** * Star or unstar a management unit for a user * Action is determined by the URI endpoint, either: /add | /remove diff --git a/grails-app/controllers/au/org/ala/merit/ReportController.groovy b/grails-app/controllers/au/org/ala/merit/ReportController.groovy index 7427238ad..28615bdc2 100644 --- a/grails-app/controllers/au/org/ala/merit/ReportController.groovy +++ b/grails-app/controllers/au/org/ala/merit/ReportController.groovy @@ -1,17 +1,16 @@ package au.org.ala.merit -import au.ala.org.ws.security.RequireApiKey -import au.org.ala.merit.command.MeriPlanReportCommand -import au.org.ala.merit.command.ProjectSummaryReportCommand + import au.org.ala.merit.command.Reef2050PlanActionReportCommand import au.org.ala.merit.command.Reef2050PlanActionReportSummaryCommand import grails.converters.JSON -import org.apache.http.HttpStatus import org.joda.time.DateTime import org.joda.time.Interval import org.joda.time.Period -import static au.org.ala.merit.DashboardTagLib.* +import java.text.ParseException + +import static au.org.ala.merit.DashboardTagLib.estimateHeight class ReportController { @@ -518,4 +517,39 @@ class ReportController { render view: '/report/_reportingHistory', model: [reportingHistory: reportingHistory] } + @PreAuthorise(accessLevel='siteReadOnly') + def generateReportsInPeriod(){ + + String startDate = params.fromDate + String endDate = params.toDate + + try{ + + def user = userService.getUser() + def extras =[:] + extras.summaryFlag = params.summaryFlag + + String email = user.userName + extras.put("hubId", SettingService.hubConfig?.hubId) + extras.put("entity", params.entity) + extras.put("systemEmail", grailsApplication.config.getProperty('fieldcapture.system.email.address')) + extras.put("senderEmail", grailsApplication.config.getProperty('fieldcapture.system.email.address')) + extras.put("email", email) + + String reportDownloadBaseUrl= grailsLinkGenerator.link(controller:'download',action:'get', absolute: true) + extras.put("reportDownloadBaseUrl", reportDownloadBaseUrl) + + def resp = reportService.generateReports(startDate, endDate,extras) + render resp as JSON + + }catch (ParseException e){ + def message = [message: 'Error: You need to provide startDate and endDate in the format of yyyy-MM-dd '] + response.setContentType("application/json") + render message as JSON + }catch(Exception e){ + def message = [message: 'Fatal: '+ e.message] + render message as JSON + } + } + } diff --git a/grails-app/services/au/org/ala/merit/ManagementUnitService.groovy b/grails-app/services/au/org/ala/merit/ManagementUnitService.groovy index 6491b9bcc..bb2a0cfca 100644 --- a/grails-app/services/au/org/ala/merit/ManagementUnitService.groovy +++ b/grails-app/services/au/org/ala/merit/ManagementUnitService.groovy @@ -8,7 +8,6 @@ import grails.core.GrailsApplication import grails.plugin.cache.Cacheable import groovy.util.logging.Slf4j import org.grails.web.json.JSONArray -import org.joda.time.DateTimeZone @Slf4j class ManagementUnitService { @@ -263,24 +262,4 @@ class ManagementUnitService { String url = "${grailsApplication.config.getProperty('ecodata.baseUrl')}" + "managementunit/"+id+"/report" webService.getJson(url) } - /** - * Download [all] management unit reports in a given period - * @param startDate - * @param endDate - * @param emails for sending email to user - * @return - */ - def generateReports(String startDate, String endDate, Map extras = null){ - - // The end date is the last day of the period (e.g. 2020-06-30) but reports will end at midnight of the next day (e.g. 2020-07-01T00:00:00) - // so add a day or two to achieve this. - - String isoStartDate = DateUtils.format(DateUtils.parse(startDate).plusDays(1).withZone(DateTimeZone.UTC)) - String isoEndDate = DateUtils.format(DateUtils.parse(endDate).plusDays(1).withZone(DateTimeZone.UTC)) - - String url = "${grailsApplication.config.getProperty('ecodata.baseUrl')}" + "managementunit/generateReportsInPeriod?startDate=${isoStartDate}&endDate=${isoEndDate}" - url += '&' + extras.collect { k,v -> "$k=$v" }.join('&') - def resp = webService.getJson(url) - return resp - } } diff --git a/grails-app/services/au/org/ala/merit/ReportService.groovy b/grails-app/services/au/org/ala/merit/ReportService.groovy index 0a06c4274..c7e12e002 100644 --- a/grails-app/services/au/org/ala/merit/ReportService.groovy +++ b/grails-app/services/au/org/ala/merit/ReportService.groovy @@ -9,6 +9,7 @@ import grails.plugin.cache.Cacheable import groovy.util.logging.Slf4j import org.apache.commons.io.FilenameUtils import org.joda.time.DateTime +import org.joda.time.DateTimeZone import org.joda.time.Period @Slf4j @@ -981,4 +982,25 @@ class ReportService { history } + /** + * Download [all] management unit reports in a given period + * @param startDate + * @param endDate + * @param emails for sending email to user + * @return + */ + def generateReports(String startDate, String endDate, Map extras = null){ + + // The end date is the last day of the period (e.g. 2020-06-30) but reports will end at midnight of the next day (e.g. 2020-07-01T00:00:00) + // so add a day or two to achieve this. + + String isoStartDate = DateUtils.format(DateUtils.parse(startDate).plusDays(1).withZone(DateTimeZone.UTC)) + String isoEndDate = DateUtils.format(DateUtils.parse(endDate).plusDays(1).withZone(DateTimeZone.UTC)) + + String url = "${grailsApplication.config.getProperty('ecodata.baseUrl')}" + "report/generateReportsInPeriod?startDate=${isoStartDate}&endDate=${isoEndDate}" + url += '&' + extras.collect { k,v -> "$k=$v" }.join('&') + def resp = webService.getJson(url) + return resp + } + } diff --git a/grails-app/views/admin/adminReports.gsp b/grails-app/views/admin/adminReports.gsp index 1b695fc26..9d7e721bb 100644 --- a/grails-app/views/admin/adminReports.gsp +++ b/grails-app/views/admin/adminReports.gsp @@ -31,18 +31,39 @@
- +
- +
- Management Unit - Activities - Core Reports - Status + Management Unit - Activities + Core Reports - Status + + +

Organisation report download:

+
+ +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ Organisation - Activities + Core Reports - Status
@@ -92,7 +113,7 @@ - + diff --git a/src/integration-test/groovy/au/org/ala/fieldcapture/AdminSpec.groovy b/src/integration-test/groovy/au/org/ala/fieldcapture/AdminSpec.groovy index b3f7e3157..303fd76fb 100644 --- a/src/integration-test/groovy/au/org/ala/fieldcapture/AdminSpec.groovy +++ b/src/integration-test/groovy/au/org/ala/fieldcapture/AdminSpec.groovy @@ -109,6 +109,28 @@ class AdminSpec extends StubbedCasSpec { messages[0].getSubject() == "Your download is ready" } + when:"I clicked the Organisation Activities Report button" + downloadOrgReport(fromDate, toDate) + okBootbox() + + then: + waitFor 20, { + MimeMessage[] messages = greenMail.getReceivedMessages() + messages?.length == 1 + messages[0].getSubject() == "Your download is ready" + } + + when:"I clicked the Organisation Report Status button" + downloadOrgReportSummary(fromDate, toDate) + okBootbox() + + then: + waitFor 20, { + MimeMessage[] messages = greenMail.getReceivedMessages() + messages?.length == 2 + messages[0].getSubject() == "Your download is ready" + } + } diff --git a/src/integration-test/groovy/pages/AdminReportsPage.groovy b/src/integration-test/groovy/pages/AdminReportsPage.groovy index 56ce57596..6016bce7c 100644 --- a/src/integration-test/groovy/pages/AdminReportsPage.groovy +++ b/src/integration-test/groovy/pages/AdminReportsPage.groovy @@ -12,10 +12,14 @@ class AdminReportsPage extends Page { period {$('select#reportPeriodOfManagementUnit',0)} downloadReportBtn(required:false) {$('a#muReportDownload')} downloadReportSummaryBtn(required:false) {$('a#muReportDownloadSummary')} + orgDownloadReportBtn(required:false) {$('a#muReportDownload')} + orgDownloadReportSummaryBtn(required:false) {$('a#muReportDownloadSummary')} showDownloadDetailsIcon(required:false) { $('i.showDownloadDetailsIcon')} - muReportDownloadLink(required:false) {$('a#muReportDownloadLink')} - startDate{$("#fromDate")} - endDate{$("#toDate")} + muReportDownloadLink(required:false) {$('a#entityReportDownloadLink')} + startDate{$("#muFromDate")} + endDate{$("#muToDate")} + orgStartDate{$("#orgFromDate")} + orgEndDate{$("#orgToDate")} reportFormats { $('#reportFormats')} viewReportBtn(required:false) {$('#viewReportBtn')} @@ -37,6 +41,18 @@ class AdminReportsPage extends Page { downloadReportSummaryBtn.click() } + def downloadOrgReport(String fromDate, String toDate) { + orgStartDate = fromDate + orgEndDate = toDate + orgDownloadReportBtn.click() + } + + def downloadOrgReportSummary(String fromDate, String toDate) { + orgStartDate = fromDate + orgEndDate = toDate + orgDownloadReportSummaryBtn.click() + } + def generateReef2050Pdf() { reportFormats = "pdf" viewReportBtn.click() diff --git a/src/test/groovy/au/org/ala/merit/ManagementUnitControllerSpec.groovy b/src/test/groovy/au/org/ala/merit/ManagementUnitControllerSpec.groovy index 1820efdc6..2092ce925 100644 --- a/src/test/groovy/au/org/ala/merit/ManagementUnitControllerSpec.groovy +++ b/src/test/groovy/au/org/ala/merit/ManagementUnitControllerSpec.groovy @@ -1,11 +1,10 @@ package au.org.ala.merit import au.org.ala.merit.command.SaveReportDataCommand -import au.org.ala.merit.hub.HubSettings import au.org.ala.merit.util.ProjectGroupingHelper +import grails.testing.web.controllers.ControllerUnitTest import org.apache.http.HttpStatus import spock.lang.Specification -import grails.testing.web.controllers.ControllerUnitTest class ManagementUnitControllerSpec extends Specification implements ControllerUnitTest{ @@ -444,22 +443,6 @@ class ManagementUnitControllerSpec extends Specification implements ControllerUn return program } - def "The controller delegates to the managementUnitService to produce reports"() { - setup: - String fromDate = '01-07-2020' - String toDate = '31-12-2020' - - when: - params.fromDate = fromDate - params.toDate = toDate - controller.generateReportsInPeriod() - - then: - 1 * userService.getUser() >> [userName:'test@test.com'] - 1 * managementUnitService.generateReports(fromDate, toDate, _) >> [status:HttpStatus.SC_OK] - response.json == [status:HttpStatus.SC_OK] - } - def "User adds star to the management unit "() { setup: String managementUnitId = 'p1' diff --git a/src/test/groovy/au/org/ala/merit/ManagementUnitServiceSpec.groovy b/src/test/groovy/au/org/ala/merit/ManagementUnitServiceSpec.groovy index 8fa2fb6e0..a5753f699 100644 --- a/src/test/groovy/au/org/ala/merit/ManagementUnitServiceSpec.groovy +++ b/src/test/groovy/au/org/ala/merit/ManagementUnitServiceSpec.groovy @@ -175,19 +175,6 @@ class ManagementUnitServiceSpec extends Specification implements ServiceUnitTest 0 * reportService.findReportsForManagementUnit(muId) } - def "The server delegates to the ecodata to produce reports"() { - setup: - String startDate = '2020-07-01' - String endDate = '2020-12-31' - Map extras = [test:'test'] - - when: - service.generateReports(startDate, endDate, extras) - - then: - 1 * webService.getJson({it.endsWith('/managementunit/generateReportsInPeriod?startDate=2020-07-02T00:00:00Z&endDate=2021-01-01T00:00:00Z&test=test')}) - } - def "Users with the MERIT siteReadOnly role can retrieve management unit reports and documents"() { setup: String muId = 'mu1' diff --git a/src/test/groovy/au/org/ala/merit/ReportControllerSpec.groovy b/src/test/groovy/au/org/ala/merit/ReportControllerSpec.groovy new file mode 100644 index 000000000..b9ff3ee60 --- /dev/null +++ b/src/test/groovy/au/org/ala/merit/ReportControllerSpec.groovy @@ -0,0 +1,32 @@ +package au.org.ala.merit + +import grails.testing.web.controllers.ControllerUnitTest +import org.apache.http.HttpStatus +import spock.lang.Specification + +class ReportControllerSpec extends Specification implements ControllerUnitTest { + ReportService reportService = Mock(ReportService) + UserService userService = Mock(UserService) + + def setup() { + controller.reportService = reportService + controller.userService = userService + } + + def "The controller delegates to the reportService to produce reports"() { + setup: + String fromDate = '01-07-2020' + String toDate = '31-12-2020' + + when: + params.fromDate = fromDate + params.toDate = toDate + controller.generateReportsInPeriod() + + then: + 1 * userService.getUser() >> [userName:'test@test.com'] + 1 * reportService.generateReports(fromDate, toDate, _) >> [status: HttpStatus.SC_OK] + response.json == [status:HttpStatus.SC_OK] + } + +} diff --git a/src/test/groovy/au/org/ala/merit/ReportServiceSpec.groovy b/src/test/groovy/au/org/ala/merit/ReportServiceSpec.groovy index 552f21e2c..88adb8168 100644 --- a/src/test/groovy/au/org/ala/merit/ReportServiceSpec.groovy +++ b/src/test/groovy/au/org/ala/merit/ReportServiceSpec.groovy @@ -2,13 +2,11 @@ package au.org.ala.merit import au.org.ala.merit.config.EmailTemplate import au.org.ala.merit.config.ProgramConfig -import au.org.ala.merit.PublicationStatus import au.org.ala.merit.config.ReportConfig import au.org.ala.merit.reports.ReportOwner import au.org.ala.web.AuthService import grails.testing.services.ServiceUnitTest import spock.lang.Specification - /** * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions */ @@ -490,4 +488,18 @@ class ReportServiceSpec extends Specification implements ServiceUnitTest