@grouparoo/core
Version:
The Grouparoo Core
49 lines (48 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SourceDestroy = void 0;
const clsTask_1 = require("../../classes/tasks/clsTask");
const Source_1 = require("../../models/Source");
class SourceDestroy extends clsTask_1.CLSTask {
constructor() {
super(...arguments);
this.name = "source:destroy";
this.description = "wait for dependencies to finish being deleted, then delete the source";
this.frequency = 0;
this.queue = "sources";
this.inputs = {
sourceId: { required: true },
};
}
async runWithinTransaction({ sourceId }) {
const source = await Source_1.Source.scope(null).findOne({
where: { id: sourceId, state: "deleted" },
});
// the source may have been force-deleted
if (!source)
return;
// check if we still have properties
try {
await Source_1.Source.ensureNotInUse(source);
}
catch (error) {
if (error.message.match(/cannot delete a source that has a property/)) {
return; // check back later
}
throw error;
}
// check if the property is directly mapped
try {
await Source_1.Source.ensurePrimaryKeyPropertyNotInUse(source);
}
catch (error) {
if (error.message.match(/cannot delete property/)) {
return; // check back later
}
throw error;
}
// no properties, let's delete it
await source.destroy();
}
}
exports.SourceDestroy = SourceDestroy;