Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Last active May 16, 2017 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronmcadam/06d7a8bee1415016ddf137a74cdc645e to your computer and use it in GitHub Desktop.
Save aaronmcadam/06d7a8bee1415016ddf137a74cdc645e to your computer and use it in GitHub Desktop.
/**
* 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'])
));
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));
});
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