Skip to content

Commit

Permalink
Added SendEmailScheduleService
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyKlein committed Sep 9, 2021
1 parent eaa87e0 commit bed4d6c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions grails-app/init/deep/dive/Application.groovy
Expand Up @@ -4,8 +4,12 @@ import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration

import groovy.transform.CompileStatic
import org.springframework.context.annotation.ComponentScan
import org.springframework.scheduling.annotation.EnableScheduling

@CompileStatic
@ComponentScan('deep.dive')
@EnableScheduling
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
Expand Down
25 changes: 25 additions & 0 deletions grails-app/services/deep/dive/SendEmailScheduleService.groovy
@@ -0,0 +1,25 @@
package deep.dive

import groovy.util.logging.Slf4j
import org.springframework.scheduling.annotation.Scheduled

@Slf4j
class SendEmailScheduleService {

static lazyInit = false

SubscriberEntityDataService subscriberEntityDataService
EmailService emailService

@Scheduled(cron = "0 0/1 * * * ?")
void sendEmailNewsletters() {
List<SubscriberEntity> subscribers = subscriberEntityDataService.findAllByVerified(true, [:])

log.info("Sending newsletter to {} verified subscribers", subscribers.size())

subscribers.each { subscriber ->
emailService.sendEmail(subscriber.email, EmailType.NEWSLETTER)
}

}
}
5 changes: 5 additions & 0 deletions grails-app/services/deep/dive/SimpleEmailService.groovy
@@ -1,10 +1,12 @@
package deep.dive

import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired

@Slf4j
class SimpleEmailService implements EmailService {

@Autowired
EmailComposer emailComposer

void sendEmail(String recipient, EmailType emailType) {
Expand All @@ -15,6 +17,9 @@ class SimpleEmailService implements EmailService {
case EmailType.WELCOME:
emailBody = emailComposer.composeWelcomeEmail(recipient)
break
case EmailType.NEWSLETTER:
emailBody = emailComposer.composeNewsletterEmail(recipient)
break
default:
emailBody = null
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/groovy/deep/dive/EmailComposer.groovy
Expand Up @@ -24,6 +24,11 @@ Please confirm your email address by clicking this link: ${serverUrl}/newsletter
}


String composeNewsletterEmail(String recipient) {
"Hello ${recipient}! We hope you enjoy the newsletter"
}


@Override
void setConfiguration(Config co) {
serverUrl = co.getProperty("grails.serverUrl")
Expand Down

0 comments on commit bed4d6c

Please sign in to comment.