all files / src/ sections.js

100% Statements 18/18
50% Branches 3/6
100% Functions 0/0
100% Lines 17/17
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                                                                           
"use strict";
 
const hl = require("highland");
const utils = require("./utils");
const BeneLogger = require('bene-logger');
const tz = require('timezone');
 
module.exports = (config, db) => (req, res, next) => {
 
    const publicationId = req.params.publicationId;
    const escenicId = req.params.escenicId;
    const source = req.params.source;
 
    const logger = new BeneLogger({logging: config.logging.enabled});
 
    logger.setDefaults({
        section_id: `${source}.${escenicId}`,
        source: source,
        service_name: 'ListAPI'
    });
 
    logger.info('Processing Sections Request');
    logger.time('Finished Sections Request');
 
    const articlesEndpoint = !req.query.cache || req.query.cache !== "false" ? config.cacheEndpoint : config.articleEndpoint;
 
    const stream = hl(db.article_section.findAll({
        "where": {
            [`section_id`]: `${source}.${escenicId}`,
            "published_date": {
                "$and": {
                    "$ne": null,
                    "$lte": tz(new Date(), '%Y-%m-%dT%H:%M:%S.%3N%^z', 'UTC')
                }
            }
        },
        "limit": 30,
        "order": [[
            'published_date',
            'DESC'
        ]]
    }))
        .sequence()
        .invoke("get", {"plain": true})
        .pluck("article_id");
 
    const GetArticle = utils.makeArticleRequest(logger, articlesEndpoint, source, publicationId);
    const NotFound = utils.notFound(`${source}/${publicationId}/sections/${escenicId || ""} not available`);
 
    utils.getArticlesInParallel(logger, GetArticle, NotFound, stream)
        .map(utils.addMeta)
        .toCallback(utils.sendResponse(logger, res, next));
 
};