Hooked up api version compatibility service

refs https://github.com/TryGhost/Toolbox/issues/280

- This is continuation of putting pieces together to allow Ghost notifying owner and admin users about version mismatch errors
-  This changeset adds the api-version-compatibility intialization during the boot (needed to be able to pospone the send email initialization so that it's testeable from the e2e tests)
- There's also a data service which handles fetching/saving notifications and fetching emails of users that should be notified
This commit is contained in:
Naz 2022-04-22 10:29:55 +08:00 committed by naz
parent b7189a99e4
commit b56557ea04
3 changed files with 76 additions and 0 deletions

View File

@ -266,6 +266,7 @@ async function initServices({config}) {
const webhooks = require('./server/services/webhooks');
const appService = require('./frontend/services/apps');
const limits = require('./server/services/limits');
const apiVersionCompatibility = require('./server/services/api-version-compatibility');
const scheduling = require('./server/adapters/scheduling');
const urlUtils = require('./shared/url-utils');
@ -286,6 +287,7 @@ async function initServices({config}) {
mega.listen(),
webhooks.listen(),
appService.init(),
apiVersionCompatibility.init(),
scheduling.init({
apiUrl: urlUtils.urlFor('api', {version: defaultApiVersion, versionType: 'admin'}, true)
})

View File

@ -0,0 +1,24 @@
const APIVersionCompatibilityService = require('@tryghost/api-version-compatibility-service');
const {GhostMailer} = require('../mail');
const {
getNotificationEmails,
fetchNotification,
saveNotification
} = require('./version-notifications-data-service');
const ghostMailer = new GhostMailer();
const init = () => {
this.APIVersionCompatibilityServiceInstance = new APIVersionCompatibilityService({
sendEmail: (options) => {
return ghostMailer.send(options);
},
fetchEmailsToNotify: getNotificationEmails,
fetchHandled: fetchNotification,
saveHandled: saveNotification
});
};
module.exports.APIVersionCompatibilityServiceInstance;
module.exports.init = init;

View File

@ -0,0 +1,50 @@
const settingsService = require('../../services/settings');
const models = require('../../models');
const settingsBREADServiceInstance = settingsService.getSettingsBREADServiceInstance();
const internalContext = {
internal: true
};
const fetchNotification = async (acceptVersion) => {
const setting = await settingsBREADServiceInstance.read('version_notifications', internalContext);
const versionNotifications = JSON.parse(setting.version_notifications.value);
return versionNotifications.find(version => version === acceptVersion);
};
const saveNotification = async (acceptVersion) => {
const setting = await settingsBREADServiceInstance.read('version_notifications', internalContext);
const versionNotifications = JSON.parse(setting.version_notifications.value);
if (!versionNotifications.find(version => version === acceptVersion)) {
versionNotifications.push(acceptVersion);
return settingsBREADServiceInstance.edit([{
key: 'version_notifications',
value: JSON.stringify(versionNotifications)
}], {
context: internalContext
});
}
};
const getNotificationEmails = async () => {
const data = await models.User.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 = {
fetchNotification,
saveNotification,
getNotificationEmails
};