wingbot-mongodb
Advanced tools
Comparing version 2.0.0-alpha.1 to 2.0.0-alpha.2
{ | ||
"name": "wingbot-mongodb", | ||
"version": "2.0.0-alpha.1", | ||
"version": "2.0.0-alpha.2", | ||
"description": "MongoDB storage for wingbot.ai", | ||
@@ -5,0 +5,0 @@ "main": "src/main.js", |
@@ -115,26 +115,44 @@ /** | ||
case this.taksCollection: | ||
await Promise.all([ | ||
this._insertIndexIfNotExists(collection, 'unique_task', { | ||
pageId: 1, senderId: 1, sent: 1, campaignId: 1 | ||
}, { unique: true }), | ||
this._insertIndexIfNotExists(collection, 'enqueue', { | ||
enqueue: 1 | ||
}) | ||
await this._ensureIndexes(collection, [ | ||
{ | ||
index: { | ||
pageId: 1, senderId: 1, campaignId: 1, sent: -1 | ||
}, | ||
options: { unique: true, name: 'unique_task' } | ||
}, { | ||
index: { enqueue: 1 }, | ||
options: { name: 'enqueue' } | ||
}, { | ||
index: { | ||
pageId: 1, senderId: 1, sent: -1, read: 1 | ||
}, | ||
options: { name: 'search_by_read' } | ||
}, { | ||
index: { | ||
pageId: 1, senderId: 1, sent: -1, delivery: 1 | ||
}, | ||
options: { name: 'search_by_delivery' } | ||
} | ||
]); | ||
break; | ||
case this.subscribtionsCollection: | ||
await Promise.all([ | ||
this._insertIndexIfNotExists(collection, 'subscriber', { | ||
pageId: 1, senderId: 1 | ||
}, { unique: true }), | ||
this._insertIndexIfNotExists(collection, 'subs', { | ||
subs: 1, pageId: 1 | ||
}) | ||
await this._ensureIndexes(collection, [ | ||
{ | ||
index: { pageId: 1, senderId: 1 }, | ||
options: { unique: true, name: 'subscriber' } | ||
}, { | ||
index: { subs: 1, pageId: 1 }, | ||
options: { name: 'subs' } | ||
} | ||
]); | ||
break; | ||
case this.campaignsCollection: | ||
await Promise.all([ | ||
this._insertIndexIfNotExists(collection, 'identifier', { | ||
id: 1 | ||
}, { unique: true }) | ||
await this._ensureIndexes(collection, [ | ||
{ | ||
index: { id: 1 }, | ||
options: { unique: true, name: 'identifier' } | ||
}, { | ||
index: { active: -1, startAt: -1 }, | ||
options: { name: 'startAt' } | ||
} | ||
]); | ||
@@ -149,13 +167,14 @@ break; | ||
async _insertIndexIfNotExists (collection, indexName, definition, options = {}) { | ||
let indexExists; | ||
async _ensureIndexes (collection, indexes) { | ||
let existing; | ||
try { | ||
indexExists = await collection.indexExists(indexName); | ||
existing = await collection.indexes(); | ||
} catch (e) { | ||
indexExists = false; | ||
existing = []; | ||
} | ||
if (!indexExists) { | ||
await collection | ||
.createIndex(definition, Object.assign({ name: indexName }, options)); | ||
} | ||
await Promise.all(indexes | ||
.filter(i => !existing.some(e => e.name === i.options.name)) | ||
.map(i => collection | ||
.createIndex(i.index, i.options))); | ||
} | ||
@@ -206,3 +225,3 @@ | ||
if (typeof res.upsertedIds[i] !== 'undefined') { | ||
return res; | ||
return arr; | ||
} | ||
@@ -312,4 +331,27 @@ arr.push({ | ||
/** | ||
* Get last sent task from campaign | ||
* | ||
* @param {string} pageId | ||
* @param {string} senderId | ||
* @param {string} campaignId | ||
* @returns {Promise<Task|null>} | ||
*/ | ||
async getSentTask (pageId, senderId, campaignId) { | ||
const c = await this._getCollection(this.taksCollection); | ||
const res = await c.findOne({ | ||
pageId, | ||
senderId, | ||
campaignId, | ||
sent: { $gte: 1 } | ||
}, { | ||
sort: { sent: -1 } | ||
}); | ||
return this._mapGenericObject(res); | ||
} | ||
/** | ||
* | ||
* @param {string} senderId | ||
* @param {string} pageId | ||
@@ -436,2 +478,22 @@ * @param {number} watermark | ||
* | ||
* @param {number} [now] | ||
* @returns {Promise<Campaign|null>} | ||
*/ | ||
async popCampaign (now = Date.now()) { | ||
const c = await this._getCollection(this.campaignsCollection); | ||
const res = await c.findOneAndUpdate({ | ||
startAt: { $ne: null, $lte: now }, | ||
active: true | ||
}, { | ||
$set: { startAt: null } | ||
}, { | ||
returnOriginal: true | ||
}); | ||
return this._mapCampaign(res.value); | ||
} | ||
/** | ||
* | ||
* @param {string} campaignId | ||
@@ -438,0 +500,0 @@ * @returns {Promise<null|Campaign>} |
112953
1196