Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created October 21, 2016 15:27
Show Gist options
  • Save unscriptable/c006f1c8fdfb110ccb7cc789708b389d to your computer and use it in GitHub Desktop.
Save unscriptable/c006f1c8fdfb110ccb7cc789708b389d to your computer and use it in GitHub Desktop.
Code snippet used in an IoC blog post
import _ from "lodash"
import { isValidPhone } from "../validation/phone"
import template from "./template"
import { DbConn } from "../Db"
import SmsService from "../SmsService"
const createMessage = _.template(template)
export const sendSms = userId => {
const service = new SmsService()
return DbConn.User.byId(userId)
.then(throwIfInvalidPhone)
.then(createMessage)
.then(message => service.send(message, user.phone))
.then(() => ([ userId, message ]))
}
const throwIfInvalidPhone = user => {
if (!isValidPhone(user.phone)) {
throw new Error(`Invalid phone for user ${ userId }.`);
}
return user
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment