@grouparoo/core
Version:
The Grouparoo Core
300 lines (299 loc) • 12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduleDestroy = exports.ScheduleView = exports.ScheduleFilterOptions = exports.ScheduleEdit = exports.ScheduleCreate = exports.SchedulesRun = exports.ScheduleRun = exports.SchedulesList = void 0;
const Schedule_1 = require("../models/Schedule");
const Run_1 = require("../models/Run");
const authenticatedAction_1 = require("../classes/actions/authenticatedAction");
const configWriter_1 = require("../modules/configWriter");
const filterHelper_1 = require("../modules/filterHelper");
const filterOpsDictionary_1 = require("../modules/filterOpsDictionary");
const apiData_1 = require("../modules/apiData");
const sequelize_1 = require("sequelize");
const Source_1 = require("../models/Source");
class SchedulesList extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedules:list";
this.description = "list all the schedules";
this.outputExample = {};
this.permission = { topic: "source", mode: "read" };
this.inputs = {
modelId: { required: false },
limit: { required: true, default: 100, formatter: apiData_1.APIData.ensureNumber },
offset: { required: true, default: 0, formatter: apiData_1.APIData.ensureNumber },
state: { required: false },
order: {
required: false,
formatter: apiData_1.APIData.ensureArray,
default: [
["name", "desc"],
["createdAt", "desc"],
],
},
};
}
async runWithinTransaction({ params, }) {
const where = {};
const sourceIds = [];
if (params.state)
where["state"] = params.state;
if (params.modelId) {
const sources = await Source_1.Source.scope(null).findAll({
where: {
modelId: params.modelId,
},
});
for (const source of sources) {
sourceIds.push(source.id);
}
where["sourceId"] = { [sequelize_1.Op.in]: sourceIds };
}
const schedules = await Schedule_1.Schedule.scope(null).findAll({
where,
limit: params.limit,
offset: params.offset,
order: params.order,
});
const total = await Schedule_1.Schedule.scope(null).count({ where });
return {
total,
schedules: await Promise.all(schedules.map((conn) => conn.apiData())),
};
}
}
exports.SchedulesList = SchedulesList;
class ScheduleRun extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedule:run";
this.description = "run a schedule either importing or exporting data";
this.outputExample = {};
this.permission = { topic: "source", mode: "write" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params }) {
const schedule = await Schedule_1.Schedule.findById(params.id);
const runningRun = await Run_1.Run.findOne({
where: { creatorId: schedule.id, state: "running" },
});
if (runningRun)
await runningRun.stop();
const run = await schedule.enqueueRun();
return { run: await run.apiData() };
}
}
exports.ScheduleRun = ScheduleRun;
class SchedulesRun extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedules:run";
this.description = "run all schedules";
this.outputExample = {};
this.permission = { topic: "source", mode: "write" };
this.inputs = {
scheduleIds: { required: false, formatter: apiData_1.APIData.ensureArray },
modelId: { required: false },
};
}
async runWithinTransaction({ params }) {
const runs = [];
const where = {};
if (params.scheduleIds && params.scheduleIds.length > 0) {
where["id"] = { [sequelize_1.Op.in]: params.scheduleIds };
}
if (params.modelId) {
const sources = await Source_1.Source.findAll({
where: { modelId: params.modelId },
});
where["sourceId"] = sources.map((source) => source.id);
}
const schedules = await Schedule_1.Schedule.findAll({ where });
for (const schedule of schedules) {
const runningRun = await Run_1.Run.scope(null).findOne({
where: {
state: "running",
creatorId: schedule.id,
creatorType: "schedule",
},
});
if (runningRun)
await runningRun.stop();
const newRun = await schedule.enqueueRun();
runs.push(newRun);
}
return { runs: await Promise.all(runs.map((run) => run.apiData())) };
}
}
exports.SchedulesRun = SchedulesRun;
class ScheduleCreate extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedule:create";
this.description = "create a schedule";
this.outputExample = {};
this.permission = { topic: "source", mode: "write" };
this.inputs = {
name: { required: false },
sourceId: { required: true },
recurring: { required: true, formatter: apiData_1.APIData.ensureBoolean },
confirmRecords: {
required: false,
formatter: apiData_1.APIData.ensureBoolean,
},
state: { required: false },
refreshEnabled: { required: false, formatter: apiData_1.APIData.ensureBoolean },
incremental: { required: false, formatter: apiData_1.APIData.ensureBoolean },
options: { required: false, formatter: apiData_1.APIData.ensureObject },
recurringFrequency: {
required: true,
default: 0,
formatter: apiData_1.APIData.ensureNumber,
},
filters: { required: false, formatter: apiData_1.APIData.ensureArray },
};
}
async runWithinTransaction({ params, }) {
const schedule = await Schedule_1.Schedule.create({
name: params.name,
sourceId: params.sourceId,
recurring: params.recurring,
recurringFrequency: params.recurringFrequency,
confirmRecords: params.confirmRecords,
incremental: params.incremental,
});
if (params.options)
await schedule.setOptions(params.options);
if (params.refreshEnabled)
await schedule.update({ refreshEnabled: params.refreshEnabled });
if (params.filters)
await schedule.setFilters(params.filters);
if (params.state)
await schedule.update({ state: params.state });
await configWriter_1.ConfigWriter.run();
return {
schedule: await schedule.apiData(),
pluginOptions: await schedule.pluginOptions(),
};
}
}
exports.ScheduleCreate = ScheduleCreate;
class ScheduleEdit extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedule:edit";
this.description = "edit a schedule";
this.outputExample = {};
this.permission = { topic: "source", mode: "write" };
this.inputs = {
id: { required: true },
name: { required: false },
sourceId: { required: false },
recurring: { required: false, formatter: apiData_1.APIData.ensureBoolean },
refreshEnabled: { required: false, formatter: apiData_1.APIData.ensureBoolean },
incremental: { required: false, formatter: apiData_1.APIData.ensureBoolean },
confirmRecords: {
required: false,
formatter: apiData_1.APIData.ensureBoolean,
},
state: { required: false },
options: { required: false, formatter: apiData_1.APIData.ensureObject },
recurringFrequency: { required: false, formatter: apiData_1.APIData.ensureNumber },
filters: { required: false, formatter: apiData_1.APIData.ensureArray },
};
}
async runWithinTransaction({ params }) {
const schedule = await Schedule_1.Schedule.findById(params.id);
if (typeof params.recurring === "boolean") {
const recurringFrequency = params.recurring && params.recurringFrequency
? params.recurringFrequency
: 0;
// these timing options are validated separately, and should be set first
await schedule.update({
recurring: params.recurring,
recurringFrequency,
});
}
if (params.options)
await schedule.setOptions(params.options);
if (params.filters)
await schedule.setFilters(params.filters);
if (typeof params.refreshEnabled === "boolean") {
await schedule.update({ refreshEnabled: params.refreshEnabled });
}
await schedule.update({
state: params.state,
name: params.name,
confirmRecords: params.confirmRecords,
incremental: params.incremental,
refreshEnabled: params.refreshEnabled,
});
await configWriter_1.ConfigWriter.run();
return {
schedule: await schedule.apiData(),
pluginOptions: await schedule.pluginOptions(),
};
}
}
exports.ScheduleEdit = ScheduleEdit;
class ScheduleFilterOptions extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedule:filterOptions";
this.description = "view a the filter options for a schedule";
this.outputExample = {};
this.permission = { topic: "source", mode: "read" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params, }) {
const schedule = await Schedule_1.Schedule.findById(params.id);
const options = await filterHelper_1.FilterHelper.pluginFilterOptions(schedule);
return {
options: options,
optionDescriptions: await (0, filterOpsDictionary_1.buildPropertyFilterDictionary)(options),
};
}
}
exports.ScheduleFilterOptions = ScheduleFilterOptions;
class ScheduleView extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedule:view";
this.description = "view a schedule";
this.outputExample = {};
this.permission = { topic: "source", mode: "read" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params }) {
const schedule = await Schedule_1.Schedule.findById(params.id);
return {
schedule: await schedule.apiData(),
pluginOptions: await schedule.pluginOptions(),
};
}
}
exports.ScheduleView = ScheduleView;
class ScheduleDestroy extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "schedule:destroy";
this.description = "destroy a schedule";
this.outputExample = {};
this.permission = { topic: "source", mode: "write" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params, }) {
const schedule = await Schedule_1.Schedule.findById(params.id);
await schedule.destroy();
await configWriter_1.ConfigWriter.run();
return { success: true };
}
}
exports.ScheduleDestroy = ScheduleDestroy;