@speakr/speakr-module-services
Version:
SPEAKR Shared Service Module
177 lines (156 loc) • 4.46 kB
JavaScript
;
const db = require('../../db');
module.exports = {
indexActive,
byNetworkIdHash,
byId,
byIdHash,
byCampaignIdHash,
fromArrayByCampaignId,
byInviteId
};
function indexActive(options) {
return db.networks.find({
where: { id_hash: options.req.headers.networkidhash },
attributes: ['id']
}).then(n => {
const network_id = n.dataValues.id;
const where = network_id === 1
? {initiative_status_id: 2}
: {initiative_status_id: 2, network_id: network_id};
return db.initiatives.findAll({ where: where})
.then(initiatives => {
if(!!initiatives) {
options.initiatives = initiatives;
return Promise.resolve(options)
}
else {
return Promise.resolve(options)
}
}).catch(err => {
return Promise.reject(err);
});
});
}
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.initiatives.findAll({
where: where,
include: [
{model: db.campaigns, attributes: ['id_hash', 'title'], required: true},
{model: db.networks, attributes: ['id_hash'], required: true}
]
})
.then(initiatives => {
if(!!initiatives) {
options.initiatives = initiatives;
return Promise.resolve(options)
}
else {
return Promise.resolve(options)
}
}).catch(err => {
return Promise.reject({
code: 400,
message: `Could not find any initiatives for the network id hash of ${options.req.params.network_id_hash}`,
purpose: "Network has no initiatives"
});
});
});
}
function byId(options) {
}
function byIdHash(options) {
let where = { id_hash: options.req.params.id_hash };
return db.initiatives.findOne({ where, include: [
db.platforms,
db.statuses
]})
.then(initiatives => {
if(initiatives) {
options.initiatives = initiatives;
return Promise.resolve(options);
} else {
return Promise.reject({
code: 404,
message: `Could not find initiative with id_hash ${options.req.params.id_hash}`,
purpose: 'initiative ID_HASH must be invalid'
});
}
})
.catch(err => {
return Promise.reject({
code: 404,
message: `Could not find initiative with id_hash ${options.req.params.id_hash}`,
purpose: 'initiative ID_HASH must be invalid'
});
});
}
function byCampaignIdHash(options) {
let where = { id_hash: options.req.params.id_hash };
return db.campaigns.findOne({where})
.then(campaigns => {
where = {campaign_id: campaigns.id};
return db.initiatives.findAll({where, include: [
db.platforms,
db.statuses
]})
.then(initiatives => {
if(!!initiatives) {
options.initiatives = initiatives;
return Promise.resolve(options);
} else {
return Promise.resolve(options);
}
}).catch(err => {
return Promise.reject(err);
})
}).catch(err => {
return Promise.reject(err);
})
}
function fromArrayByCampaignId(options) {
let where = {campaign_id: options.campaigns.id};
return db.initiatives.findAll({ where, include: [
db.platforms,
db.statuses
]} )
.then(initiatives => {
if(!!initiatives) {
options.initiatives = initiatives;
return Promise.resolve(options);
}
else {
return Promise.resolve(options);
}
}).catch(err => {
return Promise.reject(err);
})
}
// Get Inititaive with invite id
function byInviteId (options) {
const where = { id: options.invites.initiative_id };
return db.initiatives.findOne({ where })
.then(initiative => {
if(!initiative) throw new Error('Could not find Initiative From invite!')
options.initiatives = initiative.dataValues ?
initiative.dataValues :
initiative;
return options;
})
.catch(err => Promise.reject(err));
}