@grouparoo/core
Version:
The Grouparoo Core
86 lines (85 loc) • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImportRecordProperty = void 0;
const retryableTask_1 = require("../../classes/tasks/retryableTask");
const GrouparooRecord_1 = require("../../models/GrouparooRecord");
const RecordProperty_1 = require("../../models/RecordProperty");
const Mapping_1 = require("../../models/Mapping");
const Option_1 = require("../../models/Option");
const property_1 = require("../../modules/ops/property");
const import_1 = require("../../modules/ops/import");
const propertiesCache_1 = require("../../modules/caches/propertiesCache");
class ImportRecordProperty extends retryableTask_1.RetryableTask {
constructor() {
super(...arguments);
this.name = "recordProperty:importRecordProperty";
this.description = "Import the GrouparooRecord Property for a Property";
this.frequency = 0;
this.queue = "recordProperties";
this.inputs = {
recordId: { required: true },
propertyId: { required: true },
};
}
async runWithinTransaction({ recordId, propertyId, }) {
const record = await GrouparooRecord_1.GrouparooRecord.findOne({
where: { id: recordId },
include: RecordProperty_1.RecordProperty,
});
// has this record been removed since we enqueued the import task?
if (!record) {
return RecordProperty_1.RecordProperty.destroy({
where: { recordId, propertyId },
});
}
const property = await propertiesCache_1.PropertiesCache.findOneWithCache(propertyId, record.modelId, "ready");
if (!property)
return;
const recordProperties = await record.getProperties();
const source = await property.$get("source", {
scope: null,
include: [Option_1.Option, Mapping_1.Mapping],
});
const dependencies = await property_1.PropertyOps.dependencies(property);
let ok = true;
dependencies.forEach((dep) => {
if (recordProperties[dep.key].state !== "ready")
ok = false;
});
if (!ok) {
// there's a dependency we don't have yet, sleep a little and will be retried later
return RecordProperty_1.RecordProperty.update({ startedAt: import_1.ImportOps.retryStartedAt() }, {
where: {
propertyId: property.id,
recordId: record.id,
state: "pending",
},
});
}
const propertyValues = await source.importRecordProperty(record, property);
if (propertyValues) {
const hash = {};
hash[property.id] = Array.isArray(propertyValues)
? propertyValues
: [propertyValues];
await record.addOrUpdateProperties(hash);
}
else {
// got no data back, clear value
await RecordProperty_1.RecordProperty.update({
state: "ready",
rawValue: null,
stateChangedAt: new Date(),
confirmedAt: new Date(),
startedAt: null,
}, {
where: {
propertyId: property.id,
recordId: record.id,
state: "pending",
},
});
}
}
}
exports.ImportRecordProperty = ImportRecordProperty;