|
| 1 | +const Router = require('express').Router; |
| 2 | +// A standard request and response handler used widely in most endpoints |
| 3 | +const handler = require('../../../utils/generic-handler'); |
| 4 | +// Get the database handler |
| 5 | +const getDatabase = require('../../../database'); |
| 6 | + |
| 7 | +const router = Router({ mergeParams: true }); |
| 8 | + |
| 9 | +// Get the versions of all data related to the requested project (references excluded) |
| 10 | +// This includes the project metadata, every MD metadata and every analysis |
| 11 | +router.route('/').get( |
| 12 | + handler({ |
| 13 | + async retriever(request) { |
| 14 | + // Stablish database connection and retrieve our custom handler |
| 15 | + const database = await getDatabase(request); |
| 16 | + // Get raw project data, which includes all MDs |
| 17 | + // Note that the MD number in the request (if any) is ignored since we return versions for all MDs |
| 18 | + const projectData = await database.getRawProjectData( |
| 19 | + { 'metadata.ver': true, 'mds.name': true, 'mds.ver': true }); |
| 20 | + // If something went wrong then return the error |
| 21 | + if (projectData.error) return projectData; |
| 22 | + // Get the version of every analysis, but not the analysis data itself |
| 23 | + const cursor = await database.analyses.find( |
| 24 | + { project: projectData._id }, |
| 25 | + { projection: { _id: false, name: true, md: true, 'value.version': true } }, |
| 26 | + ); |
| 27 | + const analyses = await cursor.toArray(); |
| 28 | + // Set the versions of project-wide analyses (i.e. those with no MD) |
| 29 | + const projectAnalyses = {}; |
| 30 | + // Set the versions of every MD, including its analyses |
| 31 | + const mds = (projectData.mds || []).map(md => ({ |
| 32 | + name: md.name, |
| 33 | + metadata: md.ver || null, |
| 34 | + analyses: {}, |
| 35 | + })); |
| 36 | + analyses.forEach(analysis => { |
| 37 | + const version = (analysis.value && analysis.value.version) || null; |
| 38 | + if (analysis.md === undefined || analysis.md === null) |
| 39 | + projectAnalyses[analysis.name] = version; |
| 40 | + // MDs may have been removed while their analyses remain, so make sure the MD exists |
| 41 | + else if (mds[analysis.md]) mds[analysis.md].analyses[analysis.name] = version; |
| 42 | + }); |
| 43 | + return { |
| 44 | + metadata: (projectData.metadata && projectData.metadata.ver) || null, |
| 45 | + analyses: projectAnalyses, |
| 46 | + mds, |
| 47 | + }; |
| 48 | + } |
| 49 | + }), |
| 50 | +); |
| 51 | + |
| 52 | +module.exports = router; |
0 commit comments