Skip to content

Commit

Permalink
Added priority and read/unread status to notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
goeh committed Sep 15, 2012
1 parent 98fc304 commit 40be313
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ import org.apache.commons.lang.StringUtils

class CrmNotification {

public static final int PRIORITY_LOW = -1
public static final int PRIORITY_NORMAL = 0
public static final int PRIORITY_HIGH = 1

Date dateCreated
Long tenantId
String username
String body
Date dateCreated
int priority
boolean read

static constraints = {
tenantId(nullable: true)
username(maxSize: 80, nullable: true)
username(maxSize: 80, blank: false)
body(maxSize: 2000, blank: false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,55 @@ class CrmNotificationService {

@Listener(namespace = "crm", topic = "notify")
def notify(event) {
new CrmNotification(tenantId: event.tenant, username: event.username, payload: event).save(failOnError: true)
new CrmNotification(tenantId: event.tenant, username: event.username, priority: event.priority ?: 0, payload: event).save(failOnError: true)
}

List<CrmNotification> getNotifications(String username = null, Long tenant = null, Map orderParams = [:]) {
List<CrmNotification> getNotifications(String username, Long tenant = null, Map orderParams = [:]) {
CrmNotification.createCriteria().list(orderParams) {
if(username) {
eq('username', username)
eq('username', username)
if (tenant != null) {
eq('tenantId', tenant)
} else {
isNull('tenantId')
}
}
}

List<CrmNotification> getUnreadNotifications(String username, Long tenant = null, Map orderParams = [:]) {
CrmNotification.createCriteria().list(orderParams) {
eq('username', username)
if (tenant != null) {
eq('tenantId', tenant)
} else {
isNull('tenantId')
}
eq('read', false)
}
}

int countUnreadNotifications(String username, Long tenant = null) {
CrmNotification.createCriteria().count() {
eq('username', username)
if (tenant != null) {
eq('tenantId', tenant)
} else {
isNull('tenantId')
}
eq('read', false)
}
}

void markAsRead(CrmNotification arg) {
arg.read = true
arg.save()
}

void markAsUnRead(CrmNotification arg) {
arg.read = false
arg.save()
}

void delete(CrmNotification arg) {
arg.delete()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class CrmNotificationServiceSpec extends grails.plugin.spock.IntegrationSpec {
Thread.sleep(2000L)

then: "event consumed by crmNotificationService"
crmNotificationService.getNotifications('test', 42L).size() == 1
crmNotificationService.countUnreadNotifications('test', 42L) == 1

when: "notification marked as read"
crmNotificationService.markAsRead(crmNotificationService.getUnreadNotifications('test', 42L).find{it})

then: "no more unread notifications"
crmNotificationService.countUnreadNotifications('test', 42L) == 0

when: "mark it as unread"
crmNotificationService.markAsUnRead(crmNotificationService.getNotifications('test', 42L).find{it})

then: "one unread notification again"
crmNotificationService.countUnreadNotifications('test', 42L) == 1
}
}

0 comments on commit 40be313

Please sign in to comment.