0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Extracted data access code to external module

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

- Keeps the code in the core to the minimum. Ideally we should not be adding any logic apart form "hooking things together"
This commit is contained in:
Naz 2022-04-22 14:52:00 +08:00 committed by naz
parent cc71bbfd61
commit 7b9a80fbf4
4 changed files with 18 additions and 61 deletions

View file

@ -1,22 +1,23 @@
const APIVersionCompatibilityService = require('@tryghost/api-version-compatibility-service');
const VersionNotificationsDataService = require('@tryghost/version-notifications-data-service');
const {GhostMailer} = require('../mail');
const {
getNotificationEmails,
fetchNotification,
saveNotification
} = require('./version-notifications-data-service');
const ghostMailer = new GhostMailer();
const settingsService = require('../../services/settings');
const models = require('../../models');
const init = () => {
const ghostMailer = new GhostMailer();
const versionNotificationsDataService = new VersionNotificationsDataService({
UserModel: models.User,
settingsService: settingsService.getSettingsBREADServiceInstance()
});
this.APIVersionCompatibilityServiceInstance = new APIVersionCompatibilityService({
sendEmail: (options) => {
return ghostMailer.send(options);
},
fetchEmailsToNotify: getNotificationEmails,
fetchHandled: fetchNotification,
saveHandled: saveNotification
fetchEmailsToNotify: versionNotificationsDataService.getNotificationEmails.bind(versionNotificationsDataService),
fetchHandled: versionNotificationsDataService.fetchNotification.bind(versionNotificationsDataService),
saveHandled: versionNotificationsDataService.saveNotification.bind(versionNotificationsDataService)
});
};

View file

@ -1,50 +0,0 @@
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
};

View file

@ -113,6 +113,7 @@
"@tryghost/validator": "0.1.22",
"@tryghost/verification-trigger": "0.2.0",
"@tryghost/version": "0.1.12",
"@tryghost/version-notifications-data-service": "0.1.0",
"@tryghost/vhost-middleware": "1.0.22",
"@tryghost/zip": "1.1.22",
"amperize": "0.6.1",

View file

@ -2384,6 +2384,11 @@
"@tryghost/domain-events" "^0.1.9"
"@tryghost/member-events" "^0.4.1"
"@tryghost/version-notifications-data-service@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@tryghost/version-notifications-data-service/-/version-notifications-data-service-0.1.0.tgz#43a05ea5cbf2d83b9820ec4e63d02196ac6ad348"
integrity sha512-aNwX1PaZquGRShmTyXMIB9hhS8cMLOVGDOfgSh6BNT+vIFYXC32f9EH5Wm8oQ2w1/YnhkvTO9kZgW5mfEGPByg==
"@tryghost/version@0.1.12", "@tryghost/version@^0.1.12":
version "0.1.12"
resolved "https://registry.yarnpkg.com/@tryghost/version/-/version-0.1.12.tgz#66f1543053e16b4a4f1725d70ccdc02e88e12a64"