all files / api/lib/get/ index.js

96.97% Statements 32/33
78.57% Branches 11/14
100% Functions 2/2
96.97% Lines 32/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55      27× 27× 27× 27×               27× 27×       27×             27× 60×   60× 11× 11× 49× 12× 12× 37× 33×   31×    
'use strict';
 
const R = require('ramda');
const hl = require('highland');
const co = require('co');
let getAuthors = require('./authors');
let getTags = require('./tag');
let getGalleries = require('./gallery');
let getImages = require("./image");
let getHtmls = require("./html");
 
module.exports = R.curry((db, config, articleInstance) => {
    return hl(co(function* () {
        const articleId = articleInstance.get('id');
        const groupedArticleRelations = groupByArticleRelations(R.concat(articleInstance.article_relations || [], articleInstance.article_contents || []));
        const contentTypeInstances = yield [
            getGalleries(db, groupedArticleRelations.gallery),
            getImages(db, groupedArticleRelations.image),
            getHtmls(db, groupedArticleRelations.html),
            getTags(db, articleId),
            getAuthors(config, articleInstance.article_authors)
        ];
 
        const articleMeta = R.objOf('type', articleInstance.get('type'));
        return R.concat([[articleMeta, articleInstance]], R.unnest(contentTypeInstances));
    }));
});
 
function groupByArticleRelations(articleContents) {
    const memo = {
        image: [],
        video: [],
        gallery: [],
        html: []
    };
 
    return R.reduce((grouped, articleContent) => {
        Iif (R.isNil(articleContent)) {
            return grouped;
        } else if (!R.isNil(R.path(['content_item', 'dataValues', 'image_id'], articleContent))) {
            grouped.image.push(articleContent.content_item);
            return grouped;
        } else if (!R.isNil(R.path(['content_item', 'dataValues', 'video_id'], articleContent))) {
            grouped.video.push(articleContent.content_item);
            return grouped;
        } else if (!R.isNil(R.path(['content_item', 'dataValues', 'gallery_id'], articleContent))) {
            grouped.gallery.push(articleContent.content_item);
            return grouped;
        } else if (!R.isNil(R.path(['content_item', 'dataValues', 'html_id'], articleContent))) {
            grouped.html.push(articleContent.content_item);
            return grouped;
        }
        return grouped;
    }, memo, articleContents);
}