UNPKG

@grouparoo/core

Version:
45 lines (44 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RecordPropertySweep = void 0; const actionhero_1 = require("actionhero"); const clsTask_1 = require("../../classes/tasks/clsTask"); const GrouparooRecord_1 = require("../../models/GrouparooRecord"); const RecordProperty_1 = require("../../models/RecordProperty"); const Property_1 = require("../../models/Property"); class RecordPropertySweep extends clsTask_1.CLSTask { constructor() { super(...arguments); this.name = "recordProperties:sweep"; this.description = "Double check that all record properties are removed that don't belong to a record or property"; this.frequency = 1000 * 60 * 60; this.queue = "recordProperties"; } async runWithinTransaction() { let count = 0; const limit = actionhero_1.config.batchSize.recordProperties; // delete those record properties who have no record const recordPropertiesMissingRecord = await RecordProperty_1.RecordProperty.findAll({ include: [{ model: GrouparooRecord_1.GrouparooRecord, required: false }], where: { "$record.id$": null }, limit, }); // delete those record properties who have no property const recordPropertiesMissingRule = await RecordProperty_1.RecordProperty.findAll({ include: [{ model: Property_1.Property, required: false }], where: { "$property.id$": null }, limit, }); count += recordPropertiesMissingRecord.length; count += recordPropertiesMissingRule.length; if (count > 0) { await RecordProperty_1.RecordProperty.destroy({ where: { id: [].concat(recordPropertiesMissingRecord.map((p) => p.id), recordPropertiesMissingRule.map((p) => p.id)), }, }); } return count; } } exports.RecordPropertySweep = RecordPropertySweep;