-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into production
- Loading branch information
Showing
24 changed files
with
548 additions
and
133 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
|
||
const getBroadcastsMiddleware = require('../../../lib/middleware/broadcasts/index/broadcasts-get'); | ||
|
||
const router = express.Router(); | ||
|
||
router.use(getBroadcastsMiddleware()); | ||
|
||
module.exports = router; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
|
||
const getBroadcastMiddleware = require('../../../lib/middleware/broadcasts/single/broadcast-get'); | ||
|
||
const router = express.Router({ mergeParams: true }); | ||
|
||
router.use(getBroadcastMiddleware()); | ||
|
||
module.exports = router; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
broadcastContentTypes: [ | ||
'broadcast', | ||
], | ||
}; |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
|
||
const logger = require('winston'); | ||
const contentful = require('../contentful'); | ||
const helpers = require('../helpers'); | ||
const config = require('../../config/lib/helpers/broadcast'); | ||
|
||
/** | ||
* @return {Promise} | ||
*/ | ||
function fetchAll() { | ||
// TODO: Loop through all pages of query results - currently returns 100. | ||
return contentful.fetchByContentTypes(config.broadcastContentTypes) | ||
.then(contentfulEntries => Promise.all(contentfulEntries.map(module.exports | ||
.parseBroadcastFromContentfulEntry))); | ||
} | ||
|
||
/** | ||
* @param {String} broadcastId | ||
* @return {Promise} | ||
*/ | ||
function fetchById(broadcastId) { | ||
return contentful.fetchByContentfulId(broadcastId) | ||
.then(contentfulEntry => module.exports.parseBroadcastFromContentfulEntry(contentfulEntry)); | ||
} | ||
|
||
/** | ||
* @param {String} topicId | ||
* @return {Promise} | ||
*/ | ||
function getById(broadcastId) { | ||
return helpers.cache.broadcasts.get(broadcastId) | ||
.then((data) => { | ||
if (data) { | ||
logger.debug('Broadcast cache hit', { broadcastId }); | ||
return data; | ||
} | ||
logger.debug('Broadcast cache miss', { broadcastId }); | ||
return module.exports.fetchById(broadcastId) | ||
.then(broadcast => helpers.cache.broadcasts.set(broadcastId, broadcast)); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {Object} contentfulEntry | ||
* @return {Promise} | ||
*/ | ||
function parseBroadcastFromContentfulEntry(contentfulEntry) { | ||
const data = { | ||
id: contentful.getContentfulIdFromContentfulEntry(contentfulEntry), | ||
name: contentful.getNameTextFromContentfulEntry(contentfulEntry), | ||
createdAt: contentfulEntry.sys.createdAt, | ||
updatedAt: contentfulEntry.sys.updatedAt, | ||
message: { | ||
text: contentfulEntry.fields.message, | ||
attachments: contentful.getAttachmentsFromContentfulEntry(contentfulEntry), | ||
}, | ||
}; | ||
// Note: We plan to split broadcast content type into two separate content types, so editors | ||
// don't need to remember which fields to include/exclude in order to create a specific type. | ||
const hardcodedTopic = contentfulEntry.fields.topic; | ||
if (hardcodedTopic) { | ||
// Another note: the new broadcast content types will reference a topic Contentful entry, not | ||
// a campaign Contentful entry -- eventually topic will be returned as a nested object. | ||
data.campaignId = null; | ||
data.topic = hardcodedTopic; | ||
data.message.template = 'rivescript'; | ||
} else { | ||
const campaignConfigEntry = contentfulEntry.fields.campaign; | ||
data.campaignId = Number(contentful.getCampaignIdFromContentfulEntry(campaignConfigEntry)); | ||
data.topic = null; | ||
data.message.template = contentfulEntry.fields.template || 'askSignup'; | ||
} | ||
return data; | ||
} | ||
|
||
module.exports = { | ||
fetchAll, | ||
fetchById, | ||
getById, | ||
parseBroadcastFromContentfulEntry, | ||
}; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const helpers = require('../../../helpers'); | ||
|
||
module.exports = function getBroadcasts() { | ||
return (req, res) => helpers.broadcast.fetchAll() | ||
.then((broadcasts) => { | ||
req.data = broadcasts; | ||
return res.send({ data: req.data }); | ||
}) | ||
.catch(err => helpers.sendErrorResponse(res, err)); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const helpers = require('../../../helpers'); | ||
|
||
module.exports = function getBroadcast() { | ||
return (req, res) => helpers.broadcast.getById(req.params.broadcastId) | ||
.then((broadcast) => { | ||
req.data = broadcast; | ||
return res.send({ data: req.data }); | ||
}) | ||
.catch(err => helpers.sendErrorResponse(res, err)); | ||
}; |
Oops, something went wrong.