@speakr/speakr-module-services
Version:
SPEAKR Shared Service Module
312 lines (289 loc) • 8.12 kB
JavaScript
'use strict';
const db = require('../../db');
const _ = require('underscore');
module.exports = {
byInfluencerId,
manyByInfluencer,
byId,
byIdHash,
allByIdHash,
byPage,
findInvoiceIdByIdHash,
upsertInvoiceBy,
doesInvoiceExist,
countInvoices,
byInfluencer,
byInfluencerAndNetwork,
perYearByInfluencer
};
function perYearByInfluencer(options) {
let d = new Date();
d.setMonth(d.getMonth() - 12);
return db.invoices.findAll({
where: {due_at: {$gt: d}, influencer_id: options.influencers.id, network_id: {$not: null}}
})
.then((invoices) => {
options.invoices = invoices;
return Promise.resolve(options)
})
.then(options => {
return Promise.all(options.invoices.map(invoice => {
return db.line_items.findAll({
attributes: [[db.Sequelize.fn("MAX", db.Sequelize.col('id')), 'id']],
where: {invoice_id: invoice.id},
order: [[db.Sequelize.fn("MAX", db.Sequelize.col('updated_at')), 'DESC']],
group: 'id_hash'
})
.then(line_items => {
return db.line_items.findAll({where: {id:{$in: line_items.map(l=>l.id)}}});
})
.then(line_items => {
invoice.line_items = line_items;
return Promise.resolve(invoice);
});
}))
.then(invoices => {
options.invoices = invoices;
return Promise.resolve(options);
});
});
}
function byInfluencerAndNetwork(options) {
let where = options.networks ? {influencer_id: options.influencers.id, status: 'pending', network_id: options.networks.id} :
{influencer_id: options.influencers.id, status: 'pending', network_id: {$not: null}};
return db.invoices.findAndCountAll({
where: where,
include: [
db.line_items,
{model: db.influencers, include: [db.accounts] },
{model: db.networks, attributes: ['id','id_hash', 'name']}
]
})
.then((invoices) => {
options.invoices = invoices;
return Promise.resolve(options)
});
}
function byInfluencer(options) {
return db.invoices.findAndCountAll({
where: {influencer_id: options.influencers.id, status: 'pending', network_id: {$not: null}},
include: [
db.line_items,
{model: db.influencers, include: [db.accounts] },
{model: db.networks, attributes: ['id','id_hash', 'name']}
]
})
.then((invoices) => {
options.invoices = invoices;
return Promise.resolve(options)
});
}
function manyByInfluencer(influencerId) {
return db.invoices.findAndCountAll({
where: {influencer_id: influencerId, status: 'pending'},
include: [
db.line_items,
{model: db.influencers, include: [db.accounts] },
{model: db.networks, attributes: ['id','id_hash', 'name']}
]
});
}
function byInfluencerId(influencer_id, network_id) {
return db.invoices.findOne({where: {influencer_id: influencer_id, status: 'pending', network_id: network_id}});
}
function byId(id) {
return db.invoice.findOne({
where: {id: id},
include: [
db.line_items,
{model: db.influencers, include: [db.accounts] },
{model: db.networks, attributes: ['id','id_hash']}
]
});
}
function allByIdHash(options) {
return db.invoices.findAndCountAll({
where: {id_hash: {$in: options.req.body.id_hashes}},
distinct: true,
include: [
db.line_items,
{model: db.influencers, include: [{model: db.accounts, include: [db.platforms]}] },
{model: db.networks, attributes: ['id','id_hash', 'name']}
]
})
.then(invoices => {
if(!!invoices && invoices.count > 0) {
options.invoices = invoices;
return Promise.resolve(options)
} else {
return Promise.reject({
code: 404,
message: 'Could not find invoices',
purpose: 'feature invoice read.allByIdHash could not find invoices'
});
}
})
}
function findInvoiceIdByIdHash(id_hash) {
return db.invoices.findOne({
where: {id_hash: id_hash},
attributes: ['id'],
include: [
db.line_items,
{model: db.influencers, include: [db.accounts] },
{model: db.networks, attributes: ['id','id_hash']}
]
});
}
function byIdHash(options) {
return db.invoices.findOne({
where: {id_hash: options.req.params.id_hash || options.invoices.id_hash},
include: [
db.line_items,
{
model: db.influencers,
include: [
{
model: db.accounts,
include: [db.platforms]
}
]
},
{model: db.networks, attributes: ['id','id_hash']}
]
}).then(invoice => {
if(!!invoice) {
options.invoices = invoice.dataValues ? invoice.dataValues : invoice;
return db.paypal_ipn.findAll({
where: { unique_id: `${invoice.id}`}
}).then(ipn => {
options.ipn = ipn.dataValues ? ipn.dataValues : ipn;
return Promise.resolve(options);
}).catch(err => Promise.reject(err))
} else {
return Promise.reject({
code: 404,
message: 'Could not find invoices',
purpose: 'feature invoice read could not find invoices'
});
}
})
}
function countInvoices(options){
const user_network_hash = options.req.headers.usernetworkhash;
return db.networks.findOne({
where: { id_hash: user_network_hash}
})
.then(networkObj => {
return networkObj.dataValues ? networkObj.dataValues.id : networkObj.id;
})
.then(network_id => {
return db.invoices.findAndCountAll({
where: { network_id: network_id },
})
})
.then(invoices => {
if(!!invoices) {
options.invoices.count = invoices.count;
return Promise.resolve(options)
} else {
return Promise.reject({
code: 404,
message: 'Could not find invoices',
purpose: 'feature invoice read could not find invoices'
});
}
})
}
function byPage(options) {
const orderBy = (options.req.query || {}).order || 'requested_at';
const page = (options.req.query || {}).page || 1;
const limit = (options.req.query || {}).per_page || 10;
const user_network_hash = options.req.headers.usernetworkhash;
return db.networks.findOne({
where: { id_hash: user_network_hash}
})
.then(networkObj => {
return networkObj.dataValues ? networkObj.dataValues.id : networkObj.id;
})
.then(network_id => {
return db.invoices.findAndCountAll({
where: { network_id: network_id },
offset: (page - 1) * limit,
limit: limit,
distinct: true,
include: [
db.line_items,
{model: db.influencers,
include: [
{model: db.accounts, include:[db.platforms]}
]
},
{model: db.networks, attributes: ['id','id_hash']}
],
order: [['created_at', 'DESC']]
})
})
.then(invoices => {
if(!!invoices) {
options.invoices = invoices;
options.invoices.rows = invoices.rows.map(inv => inv.dataValues ? inv.dataValues : inv);
return options
} else {
return Promise.reject({
code: 404,
message: 'Could not find invoices',
purpose: 'feature invoice read could not find invoices'
});
}
})
.then(options => {
const itemPromises = options.invoices.rows.map(inv => {
return db.line_items.findAll({
where: { invoice_id: inv.id }
})
.then(lis => inv.line_items = lis)
})
return db.Sequelize.Promise.all(itemPromises)
.then(() => {
return Promise.resolve(options)
})
})
}
function upsertInvoiceBy(options) {
return db.invoices.findOne({
where: {
status: 'pending',
influencer_id: options.influencers.id,
network_id: options.network_id,
}
})
.then(invoice => {
if(invoice) {
options.invoices = invoice.dataValues ? invoice.dataValues : invoice;
return options
} else if(!invoice) {
return db.invoices.create({
status: 'pending',
influencer_id: options.influencers.id,
paypal_email: options.influencers.paypal_email,
created_by: options.req.body.user_id_hash,
network_id: options.network_id
}).then(() => upsertInvoiceBy(options))
}
})
}
function doesInvoiceExist(influencer, network_id) {
return db.invoices.findOne({
where: {
status: 'pending',
influencer_id: influencer.id,
network_id: network_id,
},
attributes: ['id_hash']
})
.then(invoice => {
if(invoice) return invoice;
if(!invoice) return null;
})
}