Created
October 21, 2016 15:27
-
-
Save unscriptable/c006f1c8fdfb110ccb7cc789708b389d to your computer and use it in GitHub Desktop.
Code snippet used in an IoC blog post
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 _ 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