UNPKG

@speakr/speakr-module-services

Version:
108 lines (95 loc) 1.92 kB
"use strict"; const db = require('../../db/'); module.exports = { index, byId, byIdHash, byNetworkIdHash }; function index(options) { return db.groups.findAll({ where: { network_id: {$ne: null} }, include: [ { model: db.networks } ] }) .then(groups => { if(!!groups) { options.groups = groups; return Promise.resolve(options); } return Promise.reject(options); } ) } function byId(options) { } function byNetworkIdHash(options) { return db.networks.find({ where: { id_hash: options.req.params.network_id_hash }, attributes: ['id'] }).then(network => { if(!network) { return Promise.reject({ code: 404, message: `Could not find the network with the network id hash of ${options.req.params.network_id_hash}`, purpose: "Network id hash could not be found" }); } const network_id = network.dataValues.id; const where = network_id === 1 ? {} : {network_id: network_id}; return db.groups.findAll( { where: where, include: [db.networks] }) .then(groups => { if(!!groups) { options.groups = groups; return Promise.resolve(options); } }).catch(err => { return Promise.reject({ code: 400, message: `Could not find any groups for the network id of ${options.req.params.network_id_hash}`, purpose: "Network has no gropus" }); }); }); } function byIdHash(options) { let include = [ { model: db.accounts, include: [ db.influencers, { model: db.metrics, where: {network_id: options.req.params.network_id} }, db.accounts_networks, db.networks ] } ]; return db.groups.findOne({ where: { id_hash: options.req.params.id_hash }, include }) .then(groups => { if(!!groups) { options.groups = groups; return Promise.resolve(options); } }).catch(err => { return Promise.reject(err); }); }