mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Implemented a version notification data service
refs https://github.com/TryGhost/Toolbox/issues/292 - The version notification data service serves as data manger for version compatibility service - There's not much logic mostly fetching data from proficed services and filtering/serializing it into a needed format
This commit is contained in:
parent
38dc003258
commit
48c7c9b73a
3 changed files with 171 additions and 8 deletions
|
@ -0,0 +1,49 @@
|
|||
const internalContext = {
|
||||
internal: true
|
||||
};
|
||||
|
||||
class VersionNotificationsDataService {
|
||||
constructor({UserModel, settingsService}) {
|
||||
this.UserModel = UserModel;
|
||||
this.settingsService = settingsService;
|
||||
}
|
||||
|
||||
async fetchNotification(acceptVersion) {
|
||||
const setting = await this.settingsService.read('version_notifications', internalContext);
|
||||
const versionNotifications = JSON.parse(setting.version_notifications.value);
|
||||
|
||||
return versionNotifications.find(version => version === acceptVersion);
|
||||
}
|
||||
|
||||
async saveNotification(acceptVersion) {
|
||||
const setting = await this.settingsService.read('version_notifications', internalContext);
|
||||
const versionNotifications = JSON.parse(setting.version_notifications.value);
|
||||
|
||||
if (!versionNotifications.find(version => version === acceptVersion)) {
|
||||
versionNotifications.push(acceptVersion);
|
||||
|
||||
return this.settingsService.edit([{
|
||||
key: 'version_notifications',
|
||||
value: JSON.stringify(versionNotifications)
|
||||
}], {
|
||||
context: internalContext
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getNotificationEmails() {
|
||||
const data = await this.UserModel.findAll(Object.assign({
|
||||
withRelated: ['roles'],
|
||||
filter: 'status:active'
|
||||
}, internalContext));
|
||||
|
||||
const adminEmails = data
|
||||
.toJSON()
|
||||
.filter(user => ['Owner', 'Administrator'].includes(user.roles[0].name))
|
||||
.map(user => user.email);
|
||||
|
||||
return adminEmails;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VersionNotificationsDataService;
|
|
@ -1,8 +0,0 @@
|
|||
const assert = require('assert');
|
||||
|
||||
describe('Version Notification Data Service', function () {
|
||||
it('Runs a test', function () {
|
||||
// TODO: Write me!
|
||||
assert.equal('hello', 'hello');
|
||||
});
|
||||
});
|
|
@ -0,0 +1,122 @@
|
|||
const assert = require('assert');
|
||||
const sinon = require('sinon');
|
||||
const VersionNotificationsDataService = require('..');
|
||||
|
||||
describe('Version Notification Data Service', function () {
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe('fetchNotification', function () {
|
||||
it('parses and filters out version notifications', async function () {
|
||||
const settingsService = {
|
||||
read: sinon.stub().resolves({
|
||||
version_notifications: {
|
||||
value: JSON.stringify([
|
||||
'v3.4',
|
||||
'v4.1',
|
||||
'v5.0',
|
||||
'v0.99'
|
||||
])
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel: {},
|
||||
settingsService
|
||||
});
|
||||
|
||||
assert.equal(await versionNotificationsDataService.fetchNotification('v4.1'), 'v4.1');
|
||||
assert.equal(await versionNotificationsDataService.fetchNotification('v9999.1'), undefined);
|
||||
assert.equal(await versionNotificationsDataService.fetchNotification('v5'), undefined);
|
||||
assert.equal(await versionNotificationsDataService.fetchNotification(), undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveNotification', function () {
|
||||
it('parses and filters out version notifications', async function () {
|
||||
const settingsService = {
|
||||
read: sinon.stub().resolves({
|
||||
version_notifications: {
|
||||
value: JSON.stringify([
|
||||
'v3.4',
|
||||
'v4.1',
|
||||
'v5.0',
|
||||
'v0.99'
|
||||
])
|
||||
}
|
||||
}),
|
||||
edit: sinon.stub().resolves()
|
||||
};
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel: {},
|
||||
settingsService
|
||||
});
|
||||
|
||||
await versionNotificationsDataService.saveNotification('v5.0');
|
||||
assert.equal(settingsService.edit.called, false);
|
||||
|
||||
await versionNotificationsDataService.saveNotification('v4.0');
|
||||
assert.equal(settingsService.edit.called, true);
|
||||
assert.deepEqual(settingsService.edit.firstCall.args, [[{
|
||||
key: 'version_notifications',
|
||||
value: JSON.stringify([
|
||||
'v3.4',
|
||||
'v4.1',
|
||||
'v5.0',
|
||||
'v0.99',
|
||||
'v4.0'
|
||||
])
|
||||
}], {
|
||||
context: {
|
||||
internal: true
|
||||
}
|
||||
}]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNotificationEmails', function () {
|
||||
it('parses and filters out version notifications', async function () {
|
||||
const UserModel = {
|
||||
findAll: sinon
|
||||
.stub()
|
||||
.withArgs({
|
||||
withRelated: ['roles'],
|
||||
filter: 'status:active'
|
||||
}, {
|
||||
internal: true
|
||||
})
|
||||
.resolves({
|
||||
toJSON: () => [{
|
||||
email: 'simon@example.com',
|
||||
roles: [{
|
||||
name: 'Administrator'
|
||||
}]
|
||||
}, {
|
||||
email: 'bob@example.com',
|
||||
roles: [{
|
||||
name: 'Owner'
|
||||
}]
|
||||
}, {
|
||||
email: 'joe@example.com',
|
||||
roles: [{
|
||||
name: 'Publisher'
|
||||
}]
|
||||
}]
|
||||
})
|
||||
};
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel,
|
||||
settingsService: {}
|
||||
});
|
||||
|
||||
const emails = await versionNotificationsDataService.getNotificationEmails();
|
||||
|
||||
assert.equal(UserModel.findAll.called, true);
|
||||
assert.deepEqual(emails, ['simon@example.com', 'bob@example.com']);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue