Work in progress on subscriptions
This commit is contained in:
parent
eecb3cd067
commit
b22a87e712
18 changed files with 1729 additions and 884 deletions
51
models/confirmations.js
Normal file
51
models/confirmations.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
'use strict';
|
||||
|
||||
const knex = require('../lib/knex');
|
||||
const shortid = require('shortid');
|
||||
|
||||
async function addConfirmation(listId, action, ip, data) {
|
||||
const cid = shortid.generate();
|
||||
await knex('confirmations').insert({
|
||||
cid,
|
||||
list: listId,
|
||||
action,
|
||||
ip,
|
||||
data: JSON.stringify(data || {})
|
||||
});
|
||||
|
||||
return cid;
|
||||
}
|
||||
|
||||
/*
|
||||
Atomically retrieves confirmation from the database, removes it from the database and returns it.
|
||||
*/
|
||||
async function takeConfirmation(cid) {
|
||||
return await knex.transaction(async tx => {
|
||||
const entry = await tx('confirmations').select(['cid', 'list', 'action', 'ip', 'data']).where('cid', cid).first();
|
||||
|
||||
if (!entry) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await tx('confirmations').where('cid', cid).del();
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(entry.data);
|
||||
} catch (err) {
|
||||
data = {};
|
||||
}
|
||||
|
||||
return {
|
||||
list: entry.list,
|
||||
action: entry.action,
|
||||
ip: entry.ip,
|
||||
data
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addConfirmation,
|
||||
takeConfirmation
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue