UNPKG

@grouparoo/core

Version:
91 lines (90 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteDestinations = exports.loadDestination = 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 loadDestination(configObject, externallyValidate, validate = false) { var _a; let isNew = false; const app = await (0, codeConfig_1.getParentByName)(__1.App, configObject.appId); (0, codeConfig_1.validateConfigObjectKeys)(__1.Destination, configObject, [ "options", "mapping", "destinationGroupMemberships", ]); let destination = await __1.Destination.scope(null).findOne({ where: { id: configObject.id, appId: app.id, [sequelize_1.Op.or]: { locked: (0, codeConfig_1.getCodeConfigLockKey)(), state: "deleted", }, }, }); if (!destination) { isNew = true; destination = await __1.Destination.create({ id: configObject.id, name: configObject.name, type: configObject.type, syncMode: configObject.syncMode, appId: app.id, modelId: configObject.modelId, }); } let group; if (configObject.groupId) { group = await (0, codeConfig_1.getParentByName)(__1.Group, configObject.groupId); } // do not set groupId or collection here, that's handled within the updateTracking method await destination.update({ name: configObject.name, type: configObject.type, syncMode: configObject.syncMode, modelId: configObject.modelId, locked: configWriter_1.ConfigWriter.getLockKey(configObject), }); const options = (0, codeConfig_1.extractNonNullParts)(configObject, "options"); if (options) await destination.setOptions(options, externallyValidate); let mapping = {}; let destinationGroupMemberships = {}; const sanitizedMappings = (0, codeConfig_1.extractNonNullParts)(configObject, "mapping"); for (const key in sanitizedMappings) { const property = await (0, codeConfig_1.getParentByName)(__1.Property, sanitizedMappings[key]); mapping[key] = property.key; } await destination.setMapping(mapping, externallyValidate, false); const sanitizedDestinationGroupMemberships = (0, codeConfig_1.extractNonNullParts)(configObject, "destinationGroupMemberships"); for (const remoteName in sanitizedDestinationGroupMemberships) { const membershipGroup = await (0, codeConfig_1.getParentByName)(__1.Group, sanitizedDestinationGroupMemberships[remoteName]); destinationGroupMemberships[membershipGroup.id] = remoteName; } await destination.setDestinationGroupMemberships(destinationGroupMemberships); await destination.updateTracking((_a = configObject.collection) !== null && _a !== void 0 ? _a : "none", // allow a "null" collection in config to be treated as a "none" collection configObject.groupId); if (destination.state === "deleted") { // when bringing back deleted destinations, we need to be sure to trigger a new export even though options may be the same await destination.exportMembers(); } await destination.update({ state: "ready" }); (0, codeConfig_1.logModel)(destination, validate ? "validated" : isNew ? "created" : "updated"); return { destination: [destination.id] }; } exports.loadDestination = loadDestination; async function deleteDestinations(ids) { const destinations = await __1.Destination.scope(null).findAll({ where: { locked: (0, codeConfig_1.getCodeConfigLockKey)(), id: { [sequelize_1.Op.notIn]: ids } }, }); for (const i in destinations) { const destination = destinations[i]; // destination:destroy will be enqueued by the `destroy` system task await destination.update({ state: "deleted", locked: null }); (0, codeConfig_1.logModel)(destination, "deleted"); } return destinations.map((instance) => instance.id); } exports.deleteDestinations = deleteDestinations;