Skip to content

Instantly share code, notes, and snippets.

@kulakowka
Last active February 6, 2017 08:41
Show Gist options
  • Save kulakowka/dc313f2a82a4d265c7a4458d1e4485f1 to your computer and use it in GitHub Desktop.
Save kulakowka/dc313f2a82a4d265c7a4458d1e4485f1 to your computer and use it in GitHub Desktop.
Service Objects in JavaScript.js
import WelcomeEmailMailer from './WelcomeEmailMailer'
import SlackNotifier from './SlackNotifier'
class NewRegistrationService {
initialize (params) {
this.user = params.user // Mongooose model: User
this.organization = params.organization // Mongooose model: Organisation
}
perform () {
return this.organizationCreate()
.then(this.postOrganizationSetup.bind(this))
.then(this.sendWelcomeEmail.bind(this))
.then(this.notifySlack.bind(this))
}
// @private
organizationCreate () {
return this.organization.save() // save organisation to Db
}
postOrganizationSetup () {
this.user.set('organizationId', this.organization.id)
return this.user.save()
}
sendWelcomeEmail () {
return WelcomeEmailMailer.welcomeEmail(@user).deliver_later
}
notifySlack () {
const notifier = new SlackNotifier('https://hooks.slack.com/services/89ypfhuiwquhfwfwef908wefoij')
return notifier.ping(`A New User has appeared! ${this.organization.name} - ${this.user.name}`)
}
}
@kulakowka
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment