@speakr/speakr-module-services
Version:
SPEAKR Shared Service Module
110 lines (102 loc) • 2.25 kB
JavaScript
;
const db = require('../../db');
const historics = require('../accounts_historics');
module.exports = {
updateFairOffer,
updateMinOffer,
updateAccount,
updateEmail
};
function updateEmail(options) {
return db.accounts.update(
{
account_email: options.update.email
},
{
where: { influencer_id: options.influencers.id }
}
).then(accounts => {
if (!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.resolve(options);
}
})
}
function updateFairOffer(options) {
return db.accounts.update(
{
fair_offer_price: options.params.fair_offer_price,
},
{
where: { id: options.update.account_id },
returning: true
}
)
.then(accounts => {
if (!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.resolve(options);
}
}
);
}
function updateMinOffer(options) {
return db.accounts.update(
{
min_offer_price: options.params.min_offer_price,
},
{
where: { id: options.update.account_id },
returning: true
}
)
.then(accounts => {
if (!!accounts) {
options.accounts = accounts;
return Promise.resolve(options);
} else {
return Promise.resolve(options);
}
}
);
}
function updateAccount(options) {
const id = options.req.params.account_id;
const where = { id }
const body = options.req.body;
let oldAccount;
console.log('\n\n\n\n>>>>>',id, body)
return db.accounts.findOne({ where })
.then(account => {
if(!account) {
return Promise.reject({
code: 404,
message: 'Could not update that account',
purpse: `Could not find account with id :: ${id}`})
}
oldAccount = Object.assign({}, account.dataValues ? account.dataValues : account);
return db.accounts.update(body, {
where,
returning: true
}
)
})
.then(updatedAccount => {
return historics.create.newRow(oldAccount)
.then(() => {
options.accounts = updatedAccount;
return Promise.resolve(options);
})
})
.catch(err => {
console.log('\n\n ERROR UPDATING ACCOUNT >> ', err)
return Promise.reject({
message: 'Could not update Account',
purpose: err.message
})
})
}