all files / api/toJSON/ gallery.js

88.89% Statements 16/18
50% Branches 5/10
100% Functions 1/1
88.89% Lines 16/18
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                                                       
'use strict';
 
const R = require('ramda');
let getLastUpdated = require("./getLastUpdated");
let createGalleryContent = require('./galleryContent');
 
module.exports = function (imageDimensions, tuple) {
 
    let images = [];
 
    let gallery = tuple[1];
 
    const galleryObj = {
        data: {
            type: 'gallery',
            id: gallery.escenic_id,
            attributes: {
                title: gallery.title
            }
        }
    };
    
    Eif (R.has('gallery_contents', gallery)) {
        const contents = gallery.gallery_contents.map(contentItem => {
            return createGalleryContent(imageDimensions, contentItem);
        });
        images = R.reject(R.isNil, contents);
    }
 
    galleryObj.included = R.tail(images).map(R.last);
    galleryObj.data.attributes.leadMedia = R.isEmpty(images) ? null : R.head(images)[1];
    const meta = {
        "type": "gallery",
        "lastModifiedDate": getLastUpdated(gallery.last_modified_date || gallery.updated_at, images),
        "id": gallery.id
    };
 
    Iif(R.has('article_relations', gallery) && gallery.article_relations.length > 0){
        const attributes = R.merge(galleryObj.data.attributes, gallery.article_relations[0].static_content);
        return Array.of(meta, R.assocPath(['data', 'attributes'], attributes, galleryObj));
    } else {
        return Array.of(meta, galleryObj);
    }
};