UNPKG

@speakr/speakr-module-services

Version:
147 lines (120 loc) 3.22 kB
"use strict"; const db = require('../../db/'); module.exports = { byId, byName, index, byInitiativeNetworkId, byIdHash, byInfluencer, internalByInfluencer }; function internalByInfluencer(options) { return db.networks.findAll({ include: [{ model: db.influencers_networks, where: {influencer_id: options.influencers.id} }]}) .then(networks => { return db.networks.findAll({ include: [{ model: db.accounts_networks, include: [{model: db.accounts, where: {influencer_id: options.influencers.id}}], where: {external: true} }]}) .then(excludedNetworks => { var excludedIds = excludedNetworks.map(e => e.id); return networks.filter(n => { return (excludedIds.indexOf(n.id) == -1) }) }); }) .then(networks => { options.networks = networks; return Promise.resolve(options); }); } function byInfluencer(options) { return db.networks.findAll({ include: [{ model: db.influencers_networks, where: {influencer_id: options.influencers.id} }] }) .then(networks => { options.networks = networks; return Promise.resolve(options); }) } function byName(options) { if (options.network) { return db.networks.findOne({ where: { name: options.network } }) .then(network => { if (!!network) { options.networks = network.dataValues ? network.dataValues : network; return Promise.resolve(options); } else { return Promise.resolve(options); } }) } else { return Promise.resolve(options); } } function byId(options) { let where = { id: options.users.network_id }; return db.networks.findOne({where}) .then((networks) => { delete options.users.network_id; if(!!networks) { options.networks = networks; return Promise.resolve(options); } else { return Promise.resolve(options); } }) } function byInitiativeNetworkId(options) { let where = { id: options.initiatives.network_id }; return db.networks.findOne({where}) .then((networks) => { if(!!networks) { options.networks = networks; return Promise.resolve(options); } else { return Promise.reject({ code: 404, message: `Counld not find any neworks with the id of ${options.initiatives.network_id}`, purpose: 'The network id must be invalid' }); } }).catch((err) => { return Promise.reject({ code: 404, message: `Counld not find any neworks with the id of ${options.initiatives.network_id}`, purpose: 'The network id must be invalid' }); }) } function index(options) { return db.networks.findAll() .then(networks => { options.networks = networks; return Promise.resolve(options); }) } function byIdHash(options) { const id_hash = options.req.headers.usernetworkhash; const where = { id_hash }; return db.networks.findOne({ where }).then(networks => { if(!networks) throw new Error(`Could not find network with id_hash :: ${id_hash}`); options.networks = networks; return Promise.resolve(options) }).catch(err => Promise.reject(err)) }