@speakr/speakr-module-services
Version:
SPEAKR Shared Service Module
225 lines (209 loc) • 5.42 kB
JavaScript
"use strict";
const db = require('../../db/');
module.exports = {
byId,
byInfluencer,
byPlatform,
byInfluencerId,
byInviteId,
byNetworkIdHash,
byParams,
byPlatformUserId
};
function byId(options) {
let where = {id: options.id};
return db.accounts.findOne( {
where: where,
include: [db.networks, db.platforms]
} )
.then(accounts => {
if(!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.reject("Account not found.")
}
});
}
function byInfluencer(options){
let where = (options.include_pages) ? {
influencer_id: options.influencers.id,
deleted_at: { $eq: null }
} :
{ influencer_id: options.influencers.id,
parent_id: null,
deleted_at: { $eq: null }
};
return db.accounts.findAll({
where: where,
include: [db.networks, db.platforms]
})
.then(accounts => {
if(!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.reject(options)
}
});
}
function byPlatform(options){
let where = {
influencer_id: options.influencers.id,
parent_id: null,
platform_id: options.platforms.id,
deleted_at: { $eq: null }
};
return db.accounts.findOne({
where: where }).then(accounts => {
if(!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.reject(options)
}
});
}
function byInfluencerId(options){
const influencer_id = options.req.headers.influencer_id;
return db.accounts.findAll({
where: { influencer_id }
}).then(accounts => {
if(!accounts) throw new Error({code: 404,
message: 'Accounts NOT found'});
options.accounts = accounts;
return options;
})
.catch(err => {
return Promise.reject(err)
})
}
function byInviteId(options){
const id = options.invites.dataValues ?
options.invites.dataValues.account_id :
options.invites.account_id;
const where = { id };
return db.accounts.findOne({ where })
.then(account => {
if(!account) throw new Error('no account')
options.accounts = account.dataValues;
return options;
})
.catch(err => {
return Promise.reject({
code: 404,
message: 'Error finding account',
purpose: err.message
})
})
}
function byNetworkIdHash(options) {
const network_id_hash = options.req.params.network_id_hash;
return db.networks.findOne({
where: {id_hash: 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"
});
}
return network.dataValues ? network.dataValues.id : network.id;
})
.then(network_id => {
return db.accounts.findAll({
attributes: ['id', 'current_username', 'email'],
include: [
{model: db.accounts_networks,
attributes: ['network_id', 'external'],
where: {
deleted: false,
network_id: network_id
},
required: true},
{model: db.platforms, attributes:['id', 'name']}
]
})
})
.then(accounts => {
if(!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.resolve(options);
}
})
.catch(err => {
return Promise.reject({
code: 404,
message: `Could not find any accounts for the network id hash of ${options.req.params.network_id_hash}`,
purpose: "Network has no accounts"
});
})
}
function byParams(options) {
const network_id_hash = options.req.params.network_id_hash;
const platform_id = Number(options.req.body.platform_id);
const current_username_frag = options.req.body.current_username;
return db.networks.findOne({
where: {id_hash: 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 ? network.dataValues.id : network.id;
return db.accounts.findAll({
where: {
current_username: { $iLike: `${current_username_frag}%`},
platform_id: platform_id
},
include: [
{
model: db.accounts_networks,
where: {network_id: network_id, deleted: false},
required: true ,
include: [db.networks]
},
db.platforms
]
})
})
.then(accounts => {
if(!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.resolve(options);
}
})
.catch(err => {
return Promise.reject({
code: 404,
message: `Could not find any accounts with params of network: ${network_id_hash}, platform: ${platform_id}, handle text: ${current_username_frag}`,
purpose: "No accounts match the parameters provided"
});
})
}
function byPlatformUserId(options) {
return db.accounts.findOne({
where: { platform_user_id: options.platform_user_id },
include: [db.networks, db.platforms]
})
.then(account => {
if(account) {
options.accounts = account.dataValues ? account.dataValues : account
} else {
options.accounts = null;
}
return Promise.resolve(options);
})
}