all files / src/ articlesByAuthor.js

100% Statements 21/21
50% Branches 3/6
100% Functions 0/0
100% Lines 19/19
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 56 57 58      20×                                                                        
"use strict";
 
const hl = require("highland");
const utils = require("./utils");
const R = require("ramda");
const BeneLogger = require('bene-logger');
 
const isFuture = x => new Date(x.published_date) > new Date();
 
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({
        tag_id: `${source}.${escenicId}`,
        source: source,
        service_name: 'list-api'
    });
 
    logger.info('Processing Authors Request');
    logger.time('Finished Authors Request');
 
    const articlesEndpoint = !req.query.cache || req.query.cache !== "false" ? config.cacheEndpoint : config.articleEndpoint;
 
    const query = {
        where:{
            author_id: `${source}.${escenicId}`,
            publication_id:`${source}.${publicationId}`
        },
        order: [[
            'published_date',
            'DESC'
        ]],
        limit:20
    };
 
    const stream = hl(db.article_author.findAll(query))
        .reject(R.isNil)
        .sequence()
        .invoke("get", {"plain": true})
        .reject(isFuture)
        .pluck("article_id");
    
    const GetArticle = utils.makeArticleRequest(logger, articlesEndpoint, source, publicationId);
    const NotFound = utils.notFound(`${source}/${publicationId}/authors/${escenicId || ""} not available`);
 
    utils.getArticlesInParallel(logger, GetArticle, NotFound, stream)
        .sequence()
        .reject(utils.shouldBeExcluded)
        .collect()
        .map(utils.addMeta)
        .toCallback(utils.sendResponse(logger, res, next));
};