diff --git a/app/index.js b/app/index.js index 75c690d..03b35de 100644 --- a/app/index.js +++ b/app/index.js @@ -51,6 +51,9 @@ routeInterruptionPages.route(app) const routeIntroPages = require('./routeIntroPages') routeIntroPages.route(app) +const frameworkRoutes = require('./routeFrameworks') +frameworkRoutes.route(app) + app.locals.db = require('./dbTree/db')({ connectionString: process.env.MONGO_READONLY, docStatus: process.env.DOC_STATUS || 'LIVE' diff --git a/app/routeFrameworks.js b/app/routeFrameworks.js new file mode 100644 index 0000000..1dd38eb --- /dev/null +++ b/app/routeFrameworks.js @@ -0,0 +1,35 @@ +const route = (app) => { + const populateFrameworkReferences = (frameworks, categories, providers) => { + const populatedFrameworks = frameworks.map(f => { + if (f.cat) { + const matchingCategory = categories.filter(c => c._id.toString() === f.cat.toString())[0]; + if (matchingCategory) { + f.cat = JSON.parse(JSON.stringify(matchingCategory)); + delete f.cat._id; + } + } + if (f.provider) { + const matchingProvider = providers.filter(p => p._id.toString() === f.provider.toString())[0]; + if (matchingProvider) { + f.provider = JSON.parse(JSON.stringify(matchingProvider)); + delete f.provider._id; + } + } + delete f._id; + return f; + }) + return populatedFrameworks; + }; + + app.get("/frameworks", (req, res, next) => { + const db = app.locals.db; + db.getRecord().then((doc) => { + populatedFrameworks = populateFrameworkReferences(doc.framework, doc.category, doc.provider); + res.send(populatedFrameworks); + }); + }); +}; + +module.exports = { + route +};