Last active
February 6, 2017 08:41
-
-
Save kulakowka/dc313f2a82a4d265c7a4458d1e4485f1 to your computer and use it in GitHub Desktop.
Service Objects in JavaScript.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original article: https://hackernoon.com/service-objects-in-ruby-on-rails-and-you-79ca8a1c946e