2017-06-28 05:25:36 +02:00
|
|
|
const logger = require('winston');
|
2018-03-19 23:46:49 +01:00
|
|
|
const db = require('../models');
|
2018-01-23 21:08:53 +01:00
|
|
|
|
2017-06-28 05:25:36 +02:00
|
|
|
module.exports = {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats (action, url, ipAddress, name, claimId, result) {
|
2017-09-07 20:46:06 +02:00
|
|
|
logger.debug('action:', action);
|
2017-06-30 02:52:37 +02:00
|
|
|
// make sure the result is a string
|
|
|
|
if (result && (typeof result !== 'string')) {
|
|
|
|
result = result.toString();
|
|
|
|
}
|
2017-07-20 00:42:56 +02:00
|
|
|
// make sure the ip address(es) are a string
|
2017-06-30 02:52:37 +02:00
|
|
|
if (ipAddress && (typeof ipAddress !== 'string')) {
|
|
|
|
ipAddress = ipAddress.toString();
|
|
|
|
}
|
2017-07-13 00:30:31 +02:00
|
|
|
db.File
|
|
|
|
.findOne({where: { name, claimId }})
|
|
|
|
.then(file => {
|
|
|
|
// create record in the db
|
|
|
|
let FileId;
|
|
|
|
if (file) {
|
|
|
|
FileId = file.dataValues.id;
|
|
|
|
} else {
|
|
|
|
FileId = null;
|
|
|
|
}
|
|
|
|
return db.Request
|
|
|
|
.create({
|
|
|
|
action,
|
|
|
|
url,
|
|
|
|
ipAddress,
|
|
|
|
result,
|
|
|
|
FileId,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-09-14 00:59:29 +02:00
|
|
|
logger.error('Sequelize error >>', error);
|
2017-07-13 00:30:31 +02:00
|
|
|
});
|
2017-06-30 02:52:37 +02:00
|
|
|
},
|
2017-06-28 05:25:36 +02:00
|
|
|
};
|