Ghost/core/server/adapters/scheduling/post-scheduling/index.js
Naz e370d33378 Refactored scheduling index files into class/initializer pattern
refs https://github.com/TryGhost/Team/issues/694

- This refactor is not ideal but moves us closer to the desired form of class with injectable (and testable) parameters. Allowed to refactor the test slightly so at least we can check if schedulerd  subscribed events work and if they trigger the adapter with correct data
- Ideally the api/model calls shoudl be abstracted away as well, but that's for another time
- Also got rid of completely pointless "adapters/scheduling" unit test. All it was checking was if the "init" method was called int the passe in object
2021-05-25 22:32:41 +04:00

54 lines
1.5 KiB
JavaScript

const events = require('../../../lib/common/events');
const localUtils = require('../utils');
const PostScheduler = require('./post-scheduler');
const getSchedulerIntegration = require('./scheduler-intergation');
/**
* @description Load all scheduled posts/pages from database.
* @return {Promise}
*/
const loadScheduledResources = async function () {
const api = require('../../../api');
const SCHEDULED_RESOURCES = ['post', 'page'];
// Fetches all scheduled resources(posts/pages) with default API
const results = await Promise.mapSeries(SCHEDULED_RESOURCES, async (resourceType) => {
const result = await api.schedules.getScheduled.query({
options: {
resource: resourceType
}
});
return result[resourceType] || [];
});
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
return Object.assign(obj, {
[entry]: results[index]
});
}, {});
};
const init = async (options) => {
const integration = await getSchedulerIntegration();
const adapter = await localUtils.createAdapter();
let scheduledResources;
if (!adapter.rescheduleOnBoot) {
scheduledResources = [];
} else {
scheduledResources = await loadScheduledResources();
}
return new PostScheduler({
apiUrl: options.apiUrl,
integration,
adapter,
scheduledResources,
events
});
};
module.exports = init;