Skip to content

Commit 90296a0

Browse files
committed
#175 Add mailing list CSV support
1 parent cd6d17a commit 90296a0

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

userdetails-cognito/src/main/groovy/au/org/ala/userdetails/CognitoUserService.groovy

+30
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,36 @@ class CognitoUserService implements IUserService<UserRecord, UserPropertyRecord,
464464
return null
465465
}
466466

467+
@Override
468+
List<String[]> emailList(Date startDate, Date endDate) {
469+
// Initialize list to hold all filtered users across paginated calls
470+
def users = new ArrayList<UserType>()
471+
472+
// Pagination logic
473+
def token = null
474+
while (true) {
475+
def response
476+
if (token) {
477+
response = cognitoIdp.listUsers(new ListUsersRequest().withUserPoolId(poolId).withPaginationToken(token))
478+
} else {
479+
response = cognitoIdp.listUsers(new ListUsersRequest().withUserPoolId(poolId))
480+
}
481+
482+
// Filter users based on creation or last modified date and add to filtered_users list
483+
users.addAll(response.getUsers().findAll {
484+
(it.userCreateDate.after(startDate) && it.userCreateDate.before(endDate)) ||
485+
(it.userLastModifiedDate.after(startDate) && it.userLastModifiedDate.before(endDate))
486+
})
487+
488+
token = response.paginationToken
489+
if (!token) {
490+
break
491+
}
492+
}
493+
494+
return users.collect { [it.attributes.find { it.name == 'email' }.value, it.userCreateDate, it.userLastModifiedDate].toArray(new String[0]) }
495+
}
496+
467497
@Override
468498
Collection<RoleRecord> listRoles() {
469499
ListGroupsResult result = cognitoIdp.listGroups(

userdetails-gorm/src/main/groovy/au/org/ala/userdetails/gorm/GormUserService.groovy

+24
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,30 @@ class GormUserService implements IUserService<User, UserProperty, Role, UserRole
519519
}
520520
}
521521

522+
@Override
523+
List<String[]> emailList(Date startDate, Date endDate) {
524+
def results = User.withCriteria {
525+
or {
526+
and {
527+
gt 'dateCreated' startDate
528+
lt 'dateCreated' endDate
529+
}
530+
and {
531+
gt 'lastUpdated' startDate
532+
lt 'lastUpdated' endDate
533+
}
534+
}
535+
projections {
536+
property 'email'
537+
property 'dateCreated'
538+
property 'lastUpdated'
539+
}
540+
}
541+
return results.collect {
542+
[it[0], it[1], it[2]].toArray(new String[0])
543+
}
544+
}
545+
522546
@Override
523547
Collection<Role> listRoles() {
524548
Role.list()

userdetails-plugin/grails-app/controllers/au/org/ala/userdetails/AdminController.groovy

+16-1
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,27 @@ class AdminController {
154154

155155
def surveyResults() {
156156
def results = userService.countByProfileAttribute('affiliation', null, request.locale)
157+
respondWithCsv(results, "user-survey-${new Date()}.csv")
158+
}
159+
160+
def emailListForm() {
161+
162+
}
163+
164+
def emailList() {
165+
def startDate = params.date('start_date')
166+
def endDate = params.date('end_date')
167+
def results = userService.emailList(startDate, endDate)
168+
respondWithCsv(results, "email-list-$startDate-to-${endDate}.csv")
169+
}
170+
171+
private def respondWithCsv(List<String[]> results, String filename) {
157172
def csvWriter = new CSVWriterBuilder(response.writer)
158173
.withParser(new RFC4180ParserBuilder().build())
159174
.build()
160175
response.status = 200
161176
response.contentType = 'text/csv'
162-
response.setHeader('Content-Disposition', "attachment; filename=user-survey-${new Date()}.csv")
177+
response.setHeader('Content-Disposition', "attachment; filename=$filename")
163178
csvWriter.writeAll(results)
164179
csvWriter.flush()
165180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
%{--
2+
- Copyright (C) 2023 Atlas of Living Australia
3+
- All Rights Reserved.
4+
-
5+
- The contents of this file are subject to the Mozilla Public
6+
- License Version 1.1 (the "License"); you may not use this file
7+
- except in compliance with the License. You may obtain a copy of
8+
- the License at http://www.mozilla.org/MPL/
9+
-
10+
- Software distributed under the License is distributed on an "AS
11+
- IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
12+
- implied. See the License for the specific language governing
13+
- rights and limitations under the License.
14+
--}%
15+
<!doctype html>
16+
<html>
17+
<head>
18+
<meta name="layout" content="${grailsApplication.config.getProperty('skin.layout')}"/>
19+
<meta name="section" content="home"/>
20+
<g:set var="title">Export Users to CSV</g:set>
21+
<title>${title} | ${grailsApplication.config.getProperty('skin.orgNameLong')}</title>
22+
<asset:stylesheet src="userdetails.css" />
23+
</head>
24+
<body>
25+
<g:if test="${flash.message}">
26+
<div class="alert alert-danger">
27+
${flash.message}
28+
</div>
29+
</g:if>
30+
31+
<div class="row">
32+
<div class="col-md-12" id="page-body" role="main">
33+
<g:form name="emailList" action="emailList" method="get" class="form-horizontal">
34+
<div class="form-group">
35+
<label for="start_date">Start Date</label>
36+
<input type="date" class="form-control" id="start_date" >
37+
%{-- <g:datePicker name="start_date" value="${new Date()}"/>--}%
38+
</div>
39+
<div class="form-group">
40+
<label for="end_date">End Date</label>
41+
<input type="date" class="form-control" id="end_date" >
42+
%{-- <g:datePicker name="end_date" value="${new Date()}"/>--}%
43+
</div>
44+
<button type="submit" class="btn btn-default">Submit</button>
45+
</g:form>
46+
</div>
47+
</div>
48+
</body>
49+
</html>

userdetails-plugin/src/main/groovy/au/org/ala/userdetails/IUserService.groovy

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ interface IUserService<U extends IUser<? extends Serializable>, P extends IUserP
117117

118118
List<String[]> countByProfileAttribute(String s, Date date, Locale locale)
119119

120+
List<String[]> emailList(Date startDate, Date endDate)
121+
120122
/***
121123
* This method can be used to get users by username
122124
* The following paging params are always supported:

0 commit comments

Comments
 (0)