spee.ch/server/models/blocked.js

82 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-04-29 19:31:26 +02:00
const logger = require('winston');
const BLOCKED_CLAIM = 'BLOCKED_CLAIM';
2018-04-28 00:17:09 +02:00
module.exports = (sequelize, { STRING }) => {
2018-04-29 19:31:26 +02:00
const Blocked = sequelize.define(
2018-04-28 00:17:09 +02:00
'Blocked',
{
outpoint: {
type : STRING,
allowNull: false,
},
},
{
freezeTableName: true,
}
);
2018-04-29 19:31:26 +02:00
2019-01-07 23:44:34 +01:00
Blocked.getBlockList = function () {
logger.debug('returning full block list');
return new Promise((resolve, reject) => {
this.findAll()
.then(list => { return resolve(list) });
});
};
Blocked.isNotBlocked = function (outpoint) {
logger.debug(`checking to see if ${outpoint} is not blocked`);
2018-04-29 19:31:26 +02:00
return new Promise((resolve, reject) => {
this.findOne({
where: {
outpoint,
2018-04-29 19:31:26 +02:00
},
})
.then(result => {
if (result) {
return reject(BLOCKED_CLAIM);
2018-04-29 19:31:26 +02:00
}
resolve(true);
})
.catch(error => {
logger.error(error);
reject(BLOCKED_CLAIM);
2018-04-29 19:31:26 +02:00
});
});
};
2019-01-07 23:44:34 +01:00
Blocked.refreshTable = function (blockEndpoint) {
let blockedList = [];
2019-01-07 23:44:34 +01:00
return fetch(blockEndpoint)
.then(response => {
return response.json();
})
.then(jsonResponse => {
if (!jsonResponse.data) {
throw new Error('no data in list_blocked response');
}
if (!jsonResponse.data.outpoints) {
throw new Error('no outpoints in list_blocked response');
}
2019-01-07 23:44:34 +01:00
let outpoints = jsonResponse.data.outpoints;
logger.debug('total outpoints:', outpoints.length);
// prep the records
for (let i = 0; i < outpoints.length; i++) {
blockedList.push({
outpoint: outpoints[i],
});
}
// clear the table
return this.destroy({
truncate: true,
});
})
.then(() => {
// fill the table
return this.bulkCreate(blockedList);
});
};
2018-04-29 19:31:26 +02:00
return Blocked;
2018-04-28 00:17:09 +02:00
};