Last active
May 16, 2017 23:55
-
-
Save aaronmcadam/06d7a8bee1415016ddf137a74cdc645e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Returns a Comparison of 2 Tasks | |
* @param {string} projectId the Project ID | |
* @param {Array<string>} taskIds a list of Task IDs | |
* @returns {Promise<Object>} | |
*/ | |
const fetchComparison = (projectId, leftTaskId, rightTaskId) => ( | |
new Promise(resolve => ( | |
R.composeP( | |
resolve, | |
buildComparisonTable | |
)(projectId, [leftTaskId, rightTaskId]) | |
)) | |
); | |
const buildComparisonTable = (projectId, taskIds) => ( | |
R.composeP( | |
buildTable, | |
R.map(mapComparisonTask), | |
() => Promise.all(getImages(projectId, taskIds)) | |
)() | |
); | |
const getImages = (projectId, taskIds) => R.map(fetchData(projectId), taskIds); | |
const fetchData = R.curry((projectId, taskId) => ( | |
R.composeP( | |
addImagesToTask(projectId, taskId), | |
callBizApi | |
)([`/tasks/${taskId}`]) | |
)); | |
const addImagesToTask = R.curry((projectId, taskId, task) => ( | |
fetchImages(projectId, taskId).then(setImages(task)) | |
)); | |
const setImages = R.flip(R.compose( | |
R.prop('task'), | |
R.assocPath(['task', 'images']) | |
)); |
This file contains hidden or 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
import test from 'ava'; | |
import { get, validate } from '../tests/support'; | |
import { webSchemas } from '../tests/schemas'; | |
test('the Web API payload matches its schema', async t => { | |
const PROJECT_ID = 175; // Fancy IAT | |
const TASK_LEFT = 688; // Brand X | |
const TASK_RIGHT = 689; // Your Colour | |
const payload = await get( | |
`/projects/${PROJECT_ID}/comparison?taskLeftId=${TASK_LEFT}&taskRightId=${TASK_RIGHT}` | |
); | |
t.true(validate(webSchemas.comparisonTable, payload.body)); | |
}); |
This file contains hidden or 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
import R from 'ramda'; | |
import express from 'express'; | |
const router = express.Router(); | |
router.route('/projects/:projectId/comparison') | |
.get((req, res) => { | |
fetchComparison( | |
req.params.projectId, | |
...R.props(['leftTaskId', 'rightTaskId'], req.query) | |
) | |
.then(comparison => res.json(comparison)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment