Skip to content

Commit

Permalink
streamlines topic endpoint to only require topic id and params
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmullen authored and davelandry committed May 30, 2019
1 parent 6a4d976 commit eb469c6
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/cms/src/api/mortarRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,21 +362,31 @@ module.exports = function(app) {
});

// Endpoint for when a user selects a new dropdown for a topic, requiring new variables
app.get("/api/topic/:pid/:topicId", async(req, res) => {
app.get("/api/topic/:topicId", async(req, res) => {
req.setTimeout(1000 * 60 * 30); // 30 minute timeout for non-cached cube queries
const {pid, topicId} = req.params;
const {topicId} = req.params;
const locale = req.query.locale || envLoc;
const localeString = `?locale=${locale}`;
const origin = `http${ req.connection.encrypted ? "s" : "" }://${ req.headers.host }`;

// Fetch the topic via the topicId
const where = {};
if (isNaN(parseInt(topicId, 10))) where.slug = topicId;
else where.id = topicId;
let topic = await db.topic.findOne({where, include: topicReq}).catch(catcher);

// Extract its parent profile id
const pid = topic.profile_id;

// Build the params query so we can make a variables request
const dims = collate(req.query);
for (let i = 0; i < dims.length; i++) {
const dim = dims[i];
const attr = await db.search.findOne({where: {[sequelize.Op.or]: {id: dim.id, slug: dim.id}}}).catch(catcher);
dim.id = attr.id;
}

const url = `${origin}/api/variables/${pid}${localeString}`;
let url = `${origin}/api/variables/${pid}${localeString}`;
dims.forEach((dim, i) => {
url += `&slug${i + 1}=${dim.slug}&id${i + 1}=${dim.id}`;
});
Expand All @@ -390,10 +400,7 @@ module.exports = function(app) {
const formatters = await db.formatter.findAll().catch(catcher);
const formatterFunctions = formatters4eval(formatters, locale);

const where = {};
if (isNaN(parseInt(topicId, 10))) where.slug = topicId;
else where.id = topicId;
let topic = await db.topic.findOne({where, include: topicReq}).catch(catcher);
// Prepare the topic for use by extracting its language content and swapping vars
topic = extractLocaleContent(topic, locale, "topic");
topic = varSwapRecursive(topic, formatterFunctions, variables, req.query);
if (topic.subtitles) topic.subtitles.sort(sorter);
Expand Down

0 comments on commit eb469c6

Please sign in to comment.