From e548cc09654a7a38e5f70debe6d7a69dd27388c9 Mon Sep 17 00:00:00 2001 From: felkerch Date: Fri, 21 Feb 2020 18:35:29 -0500 Subject: [PATCH] Publishes schedulePage and unschedulePage to event bus --- lib/services/bus.js | 7 +++++++ lib/services/bus.test.js | 15 +++++++++++++++ lib/services/init.js | 5 ++++- lib/services/init.test.js | 3 +++ lib/services/schedule.js | 13 +++++++++++-- lib/services/schedule.test.js | 8 ++++++-- 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 lib/services/bus.js create mode 100644 lib/services/bus.test.js diff --git a/lib/services/bus.js b/lib/services/bus.js new file mode 100644 index 0000000..68064b9 --- /dev/null +++ b/lib/services/bus.js @@ -0,0 +1,7 @@ +'use strict'; + +function setPublish(publish) { + module.exports.publish = publish; +} + +module.exports.setPublish = setPublish; diff --git a/lib/services/bus.test.js b/lib/services/bus.test.js new file mode 100644 index 0000000..a075169 --- /dev/null +++ b/lib/services/bus.test.js @@ -0,0 +1,15 @@ +'use strict'; + +const bus = require('./bus'); + +describe('services/bus', () => { + const fn = () => 'a setter'; + + describe('setPublish', () => { + test('if setPublish is a setter', () => { + bus.setPublish(fn); + expect(bus.publish).toBe(fn); + }); + }); + +}); diff --git a/lib/services/init.js b/lib/services/init.js index 9b22153..6cd13f8 100644 --- a/lib/services/init.js +++ b/lib/services/init.js @@ -2,15 +2,18 @@ const db = require('./db'), routes = require('./routes'), + bus = require('./bus'), schedule = require('./schedule'); /** * Initializes plugin. * @param {Object} router * @param {Object} storage + * @param {Function} publish to the event bus * @return {Promise} */ -function onInit(router, storage) { +function onInit(router, storage, publish) { + bus.setPublish(publish); return db(storage) .then(() => routes(router)) .then(() => { diff --git a/lib/services/init.test.js b/lib/services/init.test.js index b89443f..0c83737 100644 --- a/lib/services/init.test.js +++ b/lib/services/init.test.js @@ -2,12 +2,14 @@ const init = require('./init'), db = require('./db'), + bus = require('./bus'), routes = require('./routes'), schedule = require('./schedule'), router = {}, storage = {}; jest.mock('./db'); +jest.mock('./bus'); jest.mock('./routes'); jest.mock('./schedule'); @@ -32,6 +34,7 @@ describe('init', () => { return init(router, storage).then(() => { expect(db.mock.calls.length).toBe(1); + expect(bus.setPublish.mock.calls.length).toBe(1); expect(db.mock.calls[0][0]).toEqual(storage); expect(routes.mock.calls.length).toBe(1); expect(routes.mock.calls[0][0]).toEqual(router); diff --git a/lib/services/schedule.js b/lib/services/schedule.js index eb540bb..5fd1056 100644 --- a/lib/services/schedule.js +++ b/lib/services/schedule.js @@ -9,6 +9,7 @@ const _ = require('lodash'), db = require('./db'), references = require('./references'), buf = require('./buffer'), + bus = require('./bus'), rest = require('./rest'), publishProperty = 'publish', scheduledAtProperty = 'at', @@ -41,7 +42,7 @@ function publishExternally(url) { log('error', `failed to publish url from schedule: [${res.status}] ${res.statusText}`, { url, status: res.status }); } - return db.db.patchMeta(uri, { scheduled: false, scheduledTime: null }).then((meta) => res.json()); + return db.db.patchMeta(uri, { scheduled: false, scheduledTime: null }).then(() => res.json()); }); }); } @@ -105,6 +106,7 @@ function unschedule(uri, user) { return del(uri) .then((data) => { const publish = data[publishProperty]; + pageUri = publish.replace(/http:\/\/|https:\/\//g, ''); return db.db.getMeta(pageUri); @@ -120,7 +122,11 @@ function unschedule(uri, user) { }); return db.db.patchMeta(pageUri, meta); - }).catch((e) => { + }) + .then(() => { + return bus.publish('unschedulePage', { uri: pageUri, user }); + }) + .catch((e) => { errorLogger(e); return {}; @@ -154,6 +160,9 @@ function post(uri, data, user) { return db.db.patchMeta(pageUri, meta); }) + .then(() => { + return bus.publish('schedulePage', { uri: pageUri, data, user }); + }) .then(() => data) .catch(errorLogger); } diff --git a/lib/services/schedule.test.js b/lib/services/schedule.test.js index b597fa2..185de2e 100644 --- a/lib/services/schedule.test.js +++ b/lib/services/schedule.test.js @@ -20,11 +20,13 @@ const { } = require('./schedule'), rest = require('./rest'), db = require('./db'), + bus = require('./bus'), logger = jest.fn(); jest.mock('./rest'); jest.mock('./db'); setLog(logger); +bus.publish = jest.fn( () => Promise.resolve({}) ); describe('services/schedule', () => { describe('createScheduleObjectKey', () => { @@ -189,7 +191,7 @@ describe('services/schedule', () => { db.deleteItem.mockResolvedValue(Promise.resolve({})); return unschedule(uri, user).then(() => { - expect(db.deleteItem.mock.calls.length).toBe(1) + expect(db.deleteItem.mock.calls.length).toBe(1); }); }); @@ -215,8 +217,9 @@ describe('services/schedule', () => { expect(db.db.getMeta.mock.calls[0][0]).toBe('some.com/url'); expect(db.db.patchMeta.mock.calls.length).toBe(1); expect(db.db.patchMeta.mock.calls[0][1].scheduled).toBe(false); + expect(bus.publish.mock.calls.length).toBe(1); }); - }) + }); }); describe('post', () => { @@ -232,6 +235,7 @@ describe('services/schedule', () => { return post(uri, data, {}).then(() => { expect(db.insertItem.mock.calls.length).toBe(1); + expect(bus.publish.mock.calls.length).toBe(1); expect(db.insertItem.mock.calls[0][0]).toBe(newRef); expect(db.insertItem.mock.calls[0][1]).toBe(data); });