Skip to content

Instantly share code, notes, and snippets.

@lyzadanger
Last active March 27, 2018 15:30
Show Gist options
  • Save lyzadanger/93303be17ad8a484fc234bd8aa6c5cd5 to your computer and use it in GitHub Desktop.
Save lyzadanger/93303be17ad8a484fc234bd8aa6c5cd5 to your computer and use it in GitHub Desktop.
Spike: Translate groups API response to structure by organizations

A little spike demonstrating how the client might go about transforming the groups response into one organized by organization for the purposes of display in the UI.

The data here represents the current structure of the groups API response with the addition of minimal ("expanded") organization objects—presumably this would be analogous to what is available to components through groups.all().

The code does not mutate the original groups data.

[
{
"urls": {
"html": "http://localhost:5000/groups/MamPRxn8/lyzaactuallyrestricted"
},
"name": "LyzaActuallyRestricted",
"links": {
"html": "http://localhost:5000/groups/MamPRxn8/lyzaactuallyrestricted"
},
"organization": {
"name": "Lyza's Organization",
"id": "lyzaorg"
},
"url": "http://localhost:5000/groups/MamPRxn8/lyzaactuallyrestricted",
"id": "MamPRxn8",
"scoped": true,
"type": "restricted",
"public": true
},
{
"urls": {
"html": "http://localhost:5000/groups/BQg9ywB1/lyzarestricted"
},
"name": "LyzaOpen",
"links": {
"html": "http://localhost:5000/groups/BQg9ywB1/lyzarestricted"
},
"organization": {
"name": "Lyza's Organization",
"id": "lyzaorg"
},
"url": "http://localhost:5000/groups/BQg9ywB1/lyzarestricted",
"id": "BQg9ywB1",
"scoped": true,
"type": "open",
"public": true
},
{
"urls": {
"html": "http://localhost:5000/groups/Q2VaM9wQ/this-one-is-charmed"
},
"name": "This One is Charmed",
"organization": {
"name": "Hypothesis",
"id": "hypothesis"
},
"links": {
"html": "http://localhost:5000/groups/Q2VaM9wQ/this-one-is-charmed"
},
"url": "http://localhost:5000/groups/Q2VaM9wQ/this-one-is-charmed",
"id": "Q2VaM9wQ",
"scoped": true,
"type": "open",
"public": true
},
{
"urls": {
"html": "http://localhost:5000/groups/__world__/public"
},
"name": "Public",
"links": {
"html": "http://localhost:5000/groups/__world__/public"
},
"organization": {
"name": "Hypothesis",
"id": "hypothesis"
},
"url": "http://localhost:5000/groups/__world__/public",
"id": "__world__",
"scoped": false,
"type": "open",
"public": true
},
{
"urls": {
"html": "http://localhost:5000/groups/MXgB75MN/aardvarktest"
},
"name": "aardvarktest",
"links": {
"html": "http://localhost:5000/groups/MXgB75MN/aardvarktest"
},
"organization": {
"name": "Arcane",
"id": "arcane"
},
"url": "http://localhost:5000/groups/MXgB75MN/aardvarktest",
"id": "MXgB75MN",
"scoped": false,
"type": "private",
"public": false
},
{
"urls": {
"html": "http://localhost:5000/groups/V1e22Mgz/foobar"
},
"name": "foobar",
"links": {
"html": "http://localhost:5000/groups/V1e22Mgz/foobar"
},
"organization": {
"name": "Hypothesis",
"id": "hypothesis"
},
"url": "http://localhost:5000/groups/V1e22Mgz/foobar",
"id": "V1e22Mgz",
"scoped": false,
"type": "private",
"public": false
},
{
"urls": {
"html": "http://localhost:5000/groups/38ZNqr53/how-can-this-work"
},
"name": "How Can This Work?",
"links": {
"html": "http://localhost:5000/groups/38ZNqr53/how-can-this-work"
},
"organization": {
"name": "Hypothesis",
"id": "hypothesis"
},
"url": "http://localhost:5000/groups/38ZNqr53/how-can-this-work",
"id": "38ZNqr53",
"scoped": false,
"type": "private",
"public": false
},
{
"urls": {
"html": "http://localhost:5000/groups/dxp7MRpZ/lilac"
},
"name": "Lilac",
"links": {
"html": "http://localhost:5000/groups/dxp7MRpZ/lilac"
},
"organization": {
"name": "Lyza's Organization",
"id": "lyzaorg"
},
"url": "http://localhost:5000/groups/dxp7MRpZ/lilac",
"id": "dxp7MRpZ",
"scoped": false,
"type": "private",
"public": false
},
{
"urls": {
"html": "http://localhost:5000/groups/bG8e2DL1/moribund"
},
"name": "Moribund",
"links": {
"html": "http://localhost:5000/groups/bG8e2DL1/moribund"
},
"organization": {
"name": "Hypothesis",
"id": "hypothesis"
},
"url": "http://localhost:5000/groups/bG8e2DL1/moribund",
"id": "bG8e2DL1",
"scoped": false,
"type": "private",
"public": false
}
]
const groups = require('./data');
const _ = require('lodash');
/**
* Create a sorted Array of organization objects based on organization
* sub-resources from an Array of group objects
*/
const _sortedOrganizationsFromGroups = function (groups) {
const orgs = _(groups) // Start a lodash "chain"
.map('organization').uniqBy('id').sortBy('name')
.value(); // Exit a lodash chain
// Pluck the default (hypothesis) org and put it at the end of the Array
orgs.push(_.remove(orgs, org => org.id === 'hypothesis')[0]);
return orgs;
};
/**
* Translate an Array of group objects to a sorted Array of organization
* objects containing those groups.
*/
const groupsByOrganization = function (groups) {
const groupsByOrg = _sortedOrganizationsFromGroups(groups).map(org => {
org.groups = groups.filter(group => group.organization.id === org.id);
return org;
});
return groupsByOrg;
};
console.log(groupsByOrganization(groups));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment