@grouparoo/core
Version:
The Grouparoo Core
134 lines (133 loc) • 5.94 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertyOps = void 0;
const Group_1 = require("../../models/Group");
const Option_1 = require("../../models/Option");
const Mapping_1 = require("../../models/Mapping");
const RecordProperty_1 = require("../../models/RecordProperty");
const GrouparooRecord_1 = require("../../models/GrouparooRecord");
const GroupRule_1 = require("../../models/GroupRule");
const internalRun_1 = require("../internalRun");
const runMode_1 = require("../runMode");
const mustache_1 = __importDefault(require("mustache"));
const propertiesCache_1 = require("../caches/propertiesCache");
const sourcesCache_1 = require("../../modules/caches/sourcesCache");
var PropertyOps;
(function (PropertyOps) {
/**
* Enqueue Runs to update all Groups that rely on this Property
*/
async function enqueueRuns(property) {
if ((0, runMode_1.getGrouparooRunMode)() === "cli:validate")
return;
if ((0, runMode_1.getGrouparooRunMode)() === "cli:config") {
await RecordProperty_1.RecordProperty.update({ state: "pending", startedAt: null }, { where: { propertyId: property.id } });
const source = await property.$get("source", { scope: null });
await GrouparooRecord_1.GrouparooRecord.update({ state: "pending" }, { where: { modelId: source.modelId } });
}
else {
await (0, internalRun_1.internalRun)("property", property.id); // update *all* records
const groups = await Group_1.Group.findAll({
include: [
{
model: GroupRule_1.GroupRule,
where: { propertyId: property.id },
},
],
});
for (const i in groups) {
const group = groups[i];
await group.update({ state: "initializing" });
await group.run();
}
}
}
PropertyOps.enqueueRuns = enqueueRuns;
/**
* Get the options for a Property from its plugin
*/
async function pluginOptions(property, propertyOptions) {
const source = await property.$get("source", {
scope: null,
include: [Option_1.Option, Mapping_1.Mapping],
});
const { pluginConnection } = await source.getPlugin();
if (!pluginConnection) {
throw new Error(`cannot find a pluginConnection for type ${source.type}`);
}
if (!pluginConnection.methods.propertyOptions) {
throw new Error(`cannot find propertyOptions for type ${source.type}`);
}
const response = [];
const app = await source.$get("app", { include: [Option_1.Option], scope: null });
const appOptions = await app.getOptions(true);
const connection = await app.getConnection();
const sourceOptions = await source.getOptions(true);
const sourceMapping = await source.getMapping();
propertyOptions = propertyOptions !== null && propertyOptions !== void 0 ? propertyOptions : (await property.getOptions());
const propertyOptionOptions = await pluginConnection.methods.propertyOptions({
property,
propertyId: property.id,
propertyOptions,
});
for (const i in propertyOptionOptions) {
const opt = propertyOptionOptions[i];
const options = await opt.options({
connection,
app,
appId: app.id,
appOptions,
source,
sourceId: source.id,
sourceOptions,
sourceMapping,
property,
propertyId: property.id,
});
response.push({
key: opt.key,
displayName: opt.displayName,
description: opt.description,
required: opt.required,
type: opt.type,
options,
});
}
return response;
}
PropertyOps.pluginOptions = pluginOptions;
/**
* Returns any GrouparooRecord Properties that this Rule depends on.
* For example, if email depends on userId, this method would return [userIdRule]
*/
async function dependencies(property) {
const dependencies = [];
const source = await sourcesCache_1.SourcesCache.findOneWithCache(property.sourceId);
const sourceMapping = await source.getMapping();
const ruleOptions = await property.getOptions();
const properties = await propertiesCache_1.PropertiesCache.findAllWithCache(source.modelId);
// does our source depend on another property to be mapped?
const remoteMappingKeys = Object.values(sourceMapping);
properties
.filter((p) => remoteMappingKeys.includes(p.key))
.filter((p) => p.id !== property.id)
.forEach((p) => dependencies.push(p));
// does this rule have any mustache variables depended on?
for (const key in ruleOptions) {
const mustacheString = ruleOptions[key];
const mustacheVariables = mustache_1.default.parse(String(mustacheString))
.filter((chunk) => chunk[0] === "&")
.map((chunk) => chunk[1]);
properties
.filter((p) => mustacheVariables.includes(p.key))
.filter((p) => p.id !== property.id)
.forEach((p) => dependencies.push(p));
}
// de-duplicate
return dependencies.filter((v, i, a) => a.findIndex((t) => t.id === v.id) === i);
}
PropertyOps.dependencies = dependencies;
})(PropertyOps = exports.PropertyOps || (exports.PropertyOps = {}));