Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
sns stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
larryboymi committed Oct 22, 2016
1 parent 56d1227 commit 5ee9d71
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@google/maps": "^0.3.0",
"auth0-lock": "^10.4.1",
"aws-sdk": "^2.6.11",
"body-parser": "^1.15.2",
"express": "~4.14.0",
"font-awesome": "~4.6.3",
Expand Down
4 changes: 2 additions & 2 deletions services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const agent = require('../src/agent')
const moment = require('moment')
const clients = require('./clients')
const shelters = require('./shelters')
const texting = require('./texting')
const snsSubscribe = require('./snsSubscribe')

const router = express.Router()

Expand All @@ -17,7 +17,7 @@ router.get('/now', (req, res, next) => {
.catch(next)
})

router.get('/texting', texting)
router.post('/subscribe', snsSubscribe)

router.use('/clients', clients)

Expand Down
3 changes: 3 additions & 0 deletions services/aws/getSNS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const AWS = require('aws-sdk')

module.exports = () => new AWS.SNS({region: 'us-east-1'})
7 changes: 3 additions & 4 deletions services/texting.js → services/snsGetTopic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const express = require('express')
const mango = require('./mango')

const shelterMango = (shelterId) => ({
Expand All @@ -13,8 +12,8 @@ const shelterMango = (shelterId) => ({
fields: ['_id', 'sns-topic']
})

const getTopic = (req, res, next) =>
mango('shelters', shelterMango(req.query.shelterId))
.then((shelters) => res.json(shelters))
const getTopic = (shelterId) =>
mango('shelters', shelterMango(shelterId))
.then((shelters) => shelters[0]['sns-topic'])

module.exports = getTopic
16 changes: 16 additions & 0 deletions services/snsSend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const snsGetTopic = require('./snsGetTopic')
const getSNS = require('./aws/getSNS')

const publishToTopic = (TopicArn, Endpoint) =>
getSNS().subscribe({
TopicArn,
Endpoint,
Protocol: 'sms'
}).promise()

const send = (req, res, next) =>
snsGetTopic(req.query.shelterId)
.then((topic) => publishToTopic(topic))
.then((resp) => res.json(resp))

module.exports = send
16 changes: 16 additions & 0 deletions services/snsSubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const snsGetTopic = require('./snsGetTopic')
const getSNS = require('./aws/getSNS')

const subscribeToTopic = (TopicArn, Endpoint) =>
getSNS().subscribe({
TopicArn,
Endpoint,
Protocol: 'sms'
}).promise()

const subscribe = (req, res, next) =>
snsGetTopic(req.query.shelterId)
.then((topic) => subscribeToTopic(topic, req.query.phone))
.then((resp) => res.json(resp))

module.exports = subscribe

0 comments on commit 5ee9d71

Please sign in to comment.