@grouparoo/core
Version:
The Grouparoo Core
120 lines (119 loc) • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteSources = exports.loadSource = void 0;
const codeConfig_1 = require("../../classes/codeConfig");
const __1 = require("../.."); // configLoader imports need to be from root
const sequelize_1 = require("sequelize");
const configWriter_1 = require("../configWriter");
async function loadSource(configObject, configObjects, externallyValidate, validate = false) {
let isNew = false;
const app = await (0, codeConfig_1.getParentByName)(__1.App, configObject.appId);
(0, codeConfig_1.validateConfigObjectKeys)(__1.Source, configObject, ["options", "mapping"]);
let source = await __1.Source.scope(null).findOne({
where: {
id: configObject.id,
appId: app.id,
[sequelize_1.Op.or]: {
locked: (0, codeConfig_1.getCodeConfigLockKey)(),
state: "deleted",
},
},
});
if (!source) {
isNew = true;
source = await __1.Source.create({
id: configObject.id,
name: configObject.name,
type: configObject.type,
appId: app.id,
modelId: configObject.modelId,
});
}
await source.update({
type: configObject.type,
name: configObject.name,
modelId: configObject.modelId,
});
await source.setOptions((0, codeConfig_1.extractNonNullParts)(configObject, "options"), externallyValidate);
// a form of testing the options
if (externallyValidate && (await source.previewAvailable())) {
await source.sourcePreview();
}
let bootstrappedProperty;
let mappedRecordProperty;
let mapping = {};
async function setMapping() {
if (configObject.mapping) {
mappedRecordProperty = await (0, codeConfig_1.getParentByName)(__1.Property, Object.values((0, codeConfig_1.extractNonNullParts)(configObject, "mapping"))[0]);
mapping[Object.keys((0, codeConfig_1.extractNonNullParts)(configObject, "mapping"))[0]] =
mappedRecordProperty.key;
await source.setMapping(mapping, externallyValidate);
}
}
const bootstrappedPropertyConfig = (0, codeConfig_1.getAutoBootstrappedProperty)(configObject, configObjects.filter((o) => o.id !== configObject.id));
try {
await setMapping();
if (bootstrappedPropertyConfig) {
bootstrappedProperty = await __1.Property.findOne({
where: {
id: bootstrappedPropertyConfig.id,
},
});
}
}
catch (error) {
if (error.toString().match(/cannot find Property/) &&
bootstrappedPropertyConfig) {
const property = bootstrappedPropertyConfig;
if (!property || !property.options)
throw error;
const mappedColumn = Object.values(property.options)[0];
bootstrappedProperty = await source.bootstrapUniqueProperty({
// @ts-ignore
key: property.key || property["name"],
type: property.type,
mappedColumn,
id: property.id,
local: validate,
propertyOptions: (0, codeConfig_1.extractNonNullParts)(property, "options"),
});
await setMapping();
}
else {
throw error;
}
}
if (isNew || source.state === "deleted") {
await source.update({
state: "ready",
locked: configWriter_1.ConfigWriter.getLockKey(configObject),
});
if (bootstrappedProperty) {
await bootstrappedProperty.update({
state: "ready",
locked: configWriter_1.ConfigWriter.getLockKey(configObject),
});
}
}
(0, codeConfig_1.logModel)(source, validate ? "validated" : isNew ? "created" : "updated");
if (bootstrappedProperty) {
(0, codeConfig_1.logModel)(bootstrappedProperty, validate ? "validated" : isNew ? "created" : "updated");
}
return {
source: [source.id],
property: bootstrappedProperty ? [bootstrappedProperty.id] : [], // might have done this
};
}
exports.loadSource = loadSource;
async function deleteSources(ids) {
const sources = await __1.Source.scope(null).findAll({
where: { locked: (0, codeConfig_1.getCodeConfigLockKey)(), id: { [sequelize_1.Op.notIn]: ids } },
});
for (const i in sources) {
const source = sources[i];
await source.update({ state: "deleted", locked: null });
(0, codeConfig_1.logModel)(source, "deleted");
}
return sources.map((instance) => instance.id);
}
exports.deleteSources = deleteSources;