UNPKG

@grouparoo/core

Version:
47 lines (46 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RecordDestroy = void 0; const clsTask_1 = require("../../classes/tasks/clsTask"); const Export_1 = require("../../models/Export"); const GrouparooRecord_1 = require("../../models/GrouparooRecord"); class RecordDestroy extends clsTask_1.CLSTask { constructor() { super(...arguments); this.name = "record:destroy"; this.description = "Export and destroy records that no longer have any directly mapped properties"; this.frequency = 0; this.queue = "records"; this.inputs = { recordId: { required: true }, }; } async runWithinTransaction({ recordId }) { const record = await GrouparooRecord_1.GrouparooRecord.findOne({ where: { id: recordId, state: ["ready", "deleted"] }, }); if (!record) return; const pendingExports = await Export_1.Export.count({ where: { recordId: recordId, state: ["pending", "processing"], }, }); if (pendingExports > 0) return; if (record.state === "ready") { // clear groups and export // when the export is done, this task will be enqueued again to destroy it const oldGroups = await record.$get("groups"); await GrouparooRecord_1.GrouparooRecord.destroyGroupMembers(record); await record.update({ state: "deleted" }); await record.export(false, oldGroups, true, false, true); } else { // use "destroy" to clean up related models await record.destroy(); } } } exports.RecordDestroy = RecordDestroy;