UNPKG

@grouparoo/core

Version:
95 lines (94 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RunInternalRun = void 0; const actionhero_1 = require("actionhero"); const Run_1 = require("../../models/Run"); const GrouparooRecord_1 = require("../../models/GrouparooRecord"); const RecordProperty_1 = require("../../models/RecordProperty"); const Property_1 = require("../../models/Property"); const clsTask_1 = require("../../classes/tasks/clsTask"); const sequelize_1 = require("sequelize"); const record_1 = require("../../modules/ops/record"); const Import_1 = require("../../models/Import"); const GroupMember_1 = require("../../models/GroupMember"); class RunInternalRun extends clsTask_1.CLSTask { constructor() { super(...arguments); this.name = "run:internalRun"; this.description = "build imports that will check and sync all records"; this.frequency = 0; this.queue = "runs"; this.inputs = { runId: { required: true }, }; } async runWithinTransaction({ runId }) { const run = await Run_1.Run.scope(null).findOne({ where: { id: runId } }); if (!run) return; if (run.state === "stopped") return; const offset = run.memberOffset || 0; const limit = run.memberLimit || actionhero_1.config.batchSize.imports; const where = {}; if (run.creatorType === "property") { const property = await Property_1.Property.findById(run.creatorId); const source = await property.$get("source"); where["modelId"] = source.modelId; } const records = await GrouparooRecord_1.GrouparooRecord.findAll({ where, order: [["createdAt", "asc"]], include: [RecordProperty_1.RecordProperty, GroupMember_1.GroupMember], limit, offset, }); if (records.length > 0) { if (run.creatorType === "property") { // ensure the property exists and set this property to pending for these records await record_1.RecordOps.buildNullProperties(records); await RecordProperty_1.RecordProperty.update({ state: "pending" }, { where: { recordId: { [sequelize_1.Op.in]: records.map((p) => p.id) }, propertyId: run.creatorId, }, }); } else { // set all properties to pending for these records await RecordProperty_1.RecordProperty.update({ state: "pending" }, { where: { recordId: { [sequelize_1.Op.in]: records.map((p) => p.id) } } }); } // always mark the record as pending await GrouparooRecord_1.GrouparooRecord.update({ state: "pending" }, { where: { id: { [sequelize_1.Op.in]: records.map((p) => p.id) } } }); // create imports to track the lineage of the record property values const now = new Date(); const bulkImports = []; for (const record of records) { const oldRecordProperties = await record.simplifiedProperties(); const oldGroupIds = record.groupMembers.map((gm) => gm.groupId); bulkImports.push({ state: "importing", recordId: record.id, recordAssociatedAt: now, oldRecordProperties, oldGroupIds, creatorType: "run", creatorId: run.id, }); } await Import_1.Import.bulkCreate(bulkImports); } await run.update({ memberLimit: limit, memberOffset: offset + records.length, }); if (records.length === 0) { await run.afterBatch("complete"); } else { await run.afterBatch(); } return records.length; } } exports.RunInternalRun = RunInternalRun;