Added attachments to campaigns

This commit is contained in:
Andris Reinman 2016-09-09 22:12:03 +03:00
parent 89715c56fc
commit bfc6983c93
6 changed files with 444 additions and 115 deletions

View file

@ -12,6 +12,7 @@ let feed = require('../feed');
let log = require('npmlog');
let mailer = require('../mailer');
let caches = require('../caches');
let humanize = require('humanize');
let allowedKeys = ['description', 'from', 'address', 'subject', 'template', 'source_url', 'list', 'segment', 'html', 'text', 'tracking_disabled'];
@ -362,6 +363,120 @@ module.exports.get = (id, withSegment, callback) => {
});
};
module.exports.getAttachments = (campaign, callback) => {
campaign = Number(campaign) || 0;
if (campaign < 1) {
return callback(new Error('Missing Campaign ID'));
}
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
connection.query('SELECT `id`, `filename`, `size`, `created` FROM `attachments` WHERE `campaign`=?', [campaign], (err, rows) => {
connection.release();
if (err) {
return callback(err);
}
if (!rows || !rows.length) {
return callback(null, []);
}
let attachments = rows.map((row, i) => {
row = tools.convertKeys(row);
row.index = i + 1;
row.size = humanize.filesize(Number(row.size) || 0);
return row;
});
return callback(null, attachments);
});
});
};
module.exports.addAttachment = (id, attachment, callback) => {
let size = attachment.content ? attachment.content.length : 0;
if (!size) {
return callback(new Error('Emtpy or too large attahcment'));
}
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
let keys = ['campaign', 'size'];
let values = [id, size];
Object.keys(attachment).forEach(key => {
let value;
if (Buffer.isBuffer(attachment[key])) {
value = attachment[key];
} else {
value = typeof attachment[key] === 'number' ? attachment[key] : (attachment[key] || '').toString().trim();
}
key = tools.toDbKey(key);
keys.push(key);
values.push(value);
});
let query = 'INSERT INTO `attachments` (`' + keys.join('`, `') + '`) VALUES (' + values.map(() => '?').join(',') + ')';
connection.query(query, values, (err, result) => {
connection.release();
if (err) {
return callback(err);
}
let attachmentId = result && result.insertId || false;
return callback(null, attachmentId);
});
});
};
module.exports.deleteAttachment = (id, attachment, callback) => {
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
let query = 'DELETE FROM `attachments` WHERE `id`=? AND `campaign`=? LIMIT 1';
connection.query(query, [attachment, id], (err, result) => {
connection.release();
if (err) {
return callback(err);
}
let deleted = result && result.affectedRows || false;
return callback(null, deleted);
});
});
};
module.exports.getAttachment = (id, attachment, callback) => {
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
let query = 'SELECT `filename`, `content_type`, `content` FROM `attachments` WHERE `id`=? AND `campaign`=? LIMIT 1';
connection.query(query, [attachment, id], (err, rows) => {
connection.release();
if (err) {
return callback(err);
}
if (!rows || !rows.length) {
return callback(null, false);
}
let attachment = tools.convertKeys(rows[0]);
return callback(null, attachment);
});
});
};
module.exports.getLinks = (id, linkId, callback) => {
if (!callback && typeof linkId === 'function') {
callback = linkId;