2022-04-22 09:46:02 +03:00
|
|
|
const internalContext = {
|
|
|
|
internal: true
|
|
|
|
};
|
|
|
|
|
|
|
|
class VersionNotificationsDataService {
|
2022-05-09 13:15:46 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Object} options.UserModel - ghost user model
|
2022-05-10 11:26:52 +03:00
|
|
|
* @param {Object} options.ApiKeyModel - ghost api key model
|
2022-05-09 13:15:46 +03:00
|
|
|
* @param {Object} options.settingsService - ghost settings service
|
|
|
|
*/
|
2022-05-10 11:26:52 +03:00
|
|
|
constructor({UserModel, ApiKeyModel, settingsService}) {
|
2022-04-22 09:46:02 +03:00
|
|
|
this.UserModel = UserModel;
|
2022-05-10 11:26:52 +03:00
|
|
|
this.ApiKeyModel = ApiKeyModel;
|
2022-04-22 09:46:02 +03:00
|
|
|
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;
|
|
|
|
}
|
2022-05-10 11:26:52 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} key - api key identification value, it's "secret" in case of Content API key and "id" for Admin API
|
|
|
|
* @param {String} type - one of "content" or "admin" values
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
async getIntegrationName(key, type) {
|
|
|
|
let queryOptions = null;
|
|
|
|
|
|
|
|
if (type === 'content') {
|
|
|
|
queryOptions = {secret: key};
|
|
|
|
} else if (type === 'admin') {
|
|
|
|
queryOptions = {id: key};
|
|
|
|
}
|
|
|
|
|
|
|
|
const apiKey = await this.ApiKeyModel.findOne(queryOptions, {withRelated: ['integration']});
|
|
|
|
|
|
|
|
return apiKey.relations.integration.get('name');
|
|
|
|
}
|
2022-04-22 09:46:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = VersionNotificationsDataService;
|