@grouparoo/core
Version:
The Grouparoo Core
59 lines (58 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GroupDestroy = void 0;
const clsTask_1 = require("../../classes/tasks/clsTask");
const Group_1 = require("../../models/Group");
const Run_1 = require("../../models/Run");
const actionhero_1 = require("actionhero");
class GroupDestroy extends clsTask_1.CLSTask {
constructor() {
super(...arguments);
this.name = "group:destroy";
this.description = "kick off a run to remove all group members, and then delete the group when empty";
this.frequency = 0;
this.queue = "groups";
this.inputs = {
groupId: { required: true },
};
}
async runWithinTransaction({ groupId }) {
const limit = actionhero_1.config.batchSize.imports;
const group = await Group_1.Group.scope(null).findOne({
where: { id: groupId, state: "deleted" },
});
if (!group)
return; // the group may have been force-deleted
try {
await Group_1.Group.ensureNotInUse(group);
}
catch (error) {
if (error.message.match(/group still in use by/)) {
return null;
}
throw error;
}
let run = await Run_1.Run.findOne({
where: { creatorId: group.id, creatorType: "group", state: "running" },
});
if (run)
return null; // wait for runs to settle
const remainingMembers = await group.$count("groupMembers");
if (remainingMembers > 0) {
// kick off a run in the `runRemoveGroupMembers` stage
// this will be processed by run:tick -> group:run
return Run_1.Run.create({
creatorId: group.id,
creatorType: "group",
state: "running",
memberOffset: 0,
memberLimit: limit,
method: "runRemoveGroupMembers",
});
}
// we're done here, the group can be destroyed
await group.destroy();
return null;
}
}
exports.GroupDestroy = GroupDestroy;