Skip to content

Commit

Permalink
Merge pull request #3031 from AtlasOfLivingAustralia/feature/issue3004
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala authored Nov 16, 2023
2 parents b89408d + e24585b commit 346abe4
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function ManagementUnitReportSelectorViewModel(options) {
function EntityReportSelectorViewModel(options) {
var self = this;
var config = _.defaults(options);

Expand All @@ -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',
Expand All @@ -33,7 +33,7 @@ function ManagementUnitReportSelectorViewModel(options) {
var details = data['details']
var message = data['message']
var detailsIcon = ' <i class="fa fa-info-circle showDownloadDetailsIcon" data-toggle="collapse" href="#downloadDetails"></i>'
var detailsPanel = '<div class="collapse" id="downloadDetails"><a id="muReportDownloadLink" href='+config.generateMUReportInPeriodUrl +'/' + details+'>Try this link, if you cannot get an email confirmation</a></div>'
var detailsPanel = '<div class="collapse" id="downloadDetails"><a id="entityReportDownloadLink" href='+config.downloadUrl +'/' + details+'>Try this link, if you cannot get an email confirmation</a></div>'
bootbox.alert(message + detailsIcon + detailsPanel)
}
}).fail(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
44 changes: 39 additions & 5 deletions grails-app/controllers/au/org/ala/merit/ReportController.groovy
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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
}
}

}
21 changes: 0 additions & 21 deletions grails-app/services/au/org/ala/merit/ManagementUnitService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
22 changes: 22 additions & 0 deletions grails-app/services/au/org/ala/merit/ReportService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

}
44 changes: 36 additions & 8 deletions grails-app/views/admin/adminReports.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,39 @@
<div class="col-sm-2 pl-0 pr-1">
<label for="fromDate">Start date</label>
<div class="input-group input-append">
<fc:datePicker targetField="fromDate.date" bs4="bs4" class="form-control form-control-sm dateControl" name="fromDate" data-validation-engine="validate[date]" size="form-control form-control-sm dateControl"/>
<fc:datePicker targetField="fromDate.date" bs4="bs4" class="form-control form-control-sm dateControl" name="muFromDate" data-validation-engine="validate[date]" size="form-control form-control-sm dateControl"/>
</div>
</div>
<div class="col-sm-2 pl-0 pr-1">
<label for="toDate">End date</label>
<div class="input-group input-append">
<fc:datePicker targetField="toDate.date" bs4="bs4" class="form-control form-control-sm dateControl" name="toDate" data-validation-engine="validate[date]" size="form-control form-control-sm dateControl"/>
<fc:datePicker targetField="toDate.date" bs4="bs4" class="form-control form-control-sm dateControl" name="muToDate" data-validation-engine="validate[date]" size="form-control form-control-sm dateControl"/>
</div>
</div>
</div>
<a id="muReportDownload" data-bind="click:muReportDownload" class="btn btn-sm" href="#">Management Unit - Activities</a>
<a id="muReportDownloadSummary" data-bind="click:muReportDownloadSummary" class="btn btn-sm" href="#">Core Reports - Status</a>
<a id="muReportDownload" data-bind="click:entityReportDownload" class="btn btn-sm" href="#">Management Unit - Activities</a>
<a id="muReportDownloadSummary" data-bind="click:entityReportDownloadSummary" class="btn btn-sm" href="#">Core Reports - Status</a>
</form>

<h4>Organisation report download:</h4>
<form id="org-report-selector">
<label class="control-label">Select reporting period: </label>
<div class="row mb-2">
<div class="col-sm-2 pl-0 pr-1">
<label for="orgFromDate">Start date</label>
<div class="input-group input-append">
<fc:datePicker targetField="fromDate.date" bs4="bs4" class="form-control form-control-sm dateControl" name="orgFromDate" data-validation-engine="validate[date]" size="form-control form-control-sm dateControl"/>
</div>
</div>
<div class="col-sm-2 pl-0 pr-1">
<label for="orgToDate">End date</label>
<div class="input-group input-append">
<fc:datePicker targetField="toDate.date" bs4="bs4" class="form-control form-control-sm dateControl" name="orgToDate" data-validation-engine="validate[date]" size="form-control form-control-sm dateControl"/>
</div>
</div>
</div>
<a id="orgReportDownload" data-bind="click:entityReportDownload" class="btn btn-sm" href="#">Organisation - Activities</a>
<a id="orgReportDownloadSummary" data-bind="click:entityReportDownloadSummary" class="btn btn-sm" href="#">Core Reports - Status</a>
</form>


Expand Down Expand Up @@ -92,7 +113,7 @@

<asset:javascript src="common-bs4.js"/>
<asset:javascript src="reef2050Report.js"/>
<asset:javascript src="managementUnitReport.js"/>
<asset:javascript src="entityReport.js"/>
<script>
$(function () {
var SELECTED_REPORT_KEY = 'selectedAdminReport';
Expand Down Expand Up @@ -143,11 +164,18 @@
});
var generateMUReportInPeriodUrl = "${g.createLink(controller:'managementUnit', action:'generateReportsInPeriod')}";
var generateMUReportInPeriodUrl = "${g.createLink(controller:'report', action:'generateReportsInPeriod', params: [entity: 'managementUnit'])}";
var optionsReport = {
generateMUReportInPeriodUrl: generateMUReportInPeriodUrl
generateEntityReportInPeriodUrl: generateMUReportInPeriodUrl,
downloadUrl: "${g.createLink(controller:"download",action:"get")}"
};
ko.applyBindings(new EntityReportSelectorViewModel(optionsReport), document.getElementById('mu-report-selector'));
var optionsOrgReport = {
generateEntityReportInPeriodUrl: "${g.createLink(controller:'report', action:'generateReportsInPeriod', params: [entity: 'organisation'])}",
downloadUrl: "${g.createLink(controller:"download",action:"get")}"
};
ko.applyBindings(new ManagementUnitReportSelectorViewModel(optionsReport), document.getElementById('mu-report-selector'));
ko.applyBindings(new EntityReportSelectorViewModel(optionsOrgReport), document.getElementById('org-report-selector'));
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AdminSpec extends StubbedCasSpec {
def "Admin Reports"() {
setup:
login([userId:'1', role:"ROLE_ADMIN", email:'[email protected]', firstName: "ALA", lastName:'Admin'], browser)
String fromDate = "10/10/2020"
String fromDate = "10/01/2020"
String toDate = "10/10/2021"

when:
Expand Down Expand Up @@ -106,7 +106,29 @@ class AdminSpec extends StubbedCasSpec {
waitFor 20, {
MimeMessage[] messages = greenMail.getReceivedMessages()
messages?.length == 2
messages[0].getSubject() == "Your download is ready"
messages[1].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 == 3
messages[2].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 == 4
messages[3].getSubject() == "Your download is ready"
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class StubbedCasSpec extends FieldcaptureFunctionalTest {
}
/** Convenience method to stub the login of a user with the CAS ROLE_ALA_ADMIN role */
def loginAsAlaAdmin(Browser browser) {
login([userId:ALA_ADMIN_USER_ID, role:"ROLE_ADMIN", email: '[email protected]', firstName:"ALA", lastName:"Administrator"], browser)
login([userId:ALA_ADMIN_USER_ID, role:"ROLE_ADMIN", userName: '[email protected]', email: '[email protected]', firstName:"ALA", lastName:"Administrator"], browser)
}
/** Convenience method to stub the login of a user no special roles */
def loginAsUser(String userId, Browser browser) {
Expand Down
22 changes: 19 additions & 3 deletions src/integration-test/groovy/pages/AdminReportsPage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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#orgReportDownload')}
orgDownloadReportSummaryBtn(required:false) {$('a#orgReportDownloadSummary')}
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')}

Expand All @@ -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()
Expand Down
Loading

0 comments on commit 346abe4

Please sign in to comment.