@grouparoo/core
Version:
The Grouparoo Core
390 lines (389 loc) • 15.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertyDestroy = exports.PropertyTest = exports.PropertyRecordPreview = exports.PropertyPluginOptions = exports.PropertyView = exports.PropertyFilterOptions = exports.PropertyEdit = exports.PropertyCreate = exports.PropertyGroups = exports.PropertiesOptions = exports.PropertiesList = void 0;
const authenticatedAction_1 = require("../classes/actions/authenticatedAction");
const sequelize_1 = require("sequelize");
const GrouparooRecord_1 = require("../models/GrouparooRecord");
const Property_1 = require("../models/Property");
const RecordProperty_1 = require("../models/RecordProperty");
const Group_1 = require("../models/Group");
const GroupRule_1 = require("../models/GroupRule");
const configWriter_1 = require("../modules/configWriter");
const filterHelper_1 = require("../modules/filterHelper");
const filterOpsDictionary_1 = require("../modules/filterOpsDictionary");
const apiData_1 = require("../modules/apiData");
const Source_1 = require("../models/Source");
const destination_1 = require("../modules/ops/destination");
class PropertiesList extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "properties:list";
this.description = "list all the properties";
this.outputExample = {};
this.permission = { topic: "property", mode: "read" };
this.inputs = {
limit: { required: true, default: 100, formatter: apiData_1.APIData.ensureNumber },
offset: { required: true, default: 0, formatter: apiData_1.APIData.ensureNumber },
unique: { required: false, formatter: apiData_1.APIData.ensureBoolean },
state: { required: false },
modelId: { required: false },
sourceId: { required: false },
includeExamples: {
required: true,
default: "false",
formatter: apiData_1.APIData.ensureBoolean,
},
order: {
required: false,
formatter: apiData_1.APIData.ensureArray,
default: [
["key", "asc"],
["createdAt", "desc"],
],
},
};
}
async runWithinTransaction({ params, }) {
const includeExamples = params.includeExamples;
const where = {};
if (params.state && params.state !== "invalid") {
where["state"] = params.state;
}
if (params.sourceId) {
where["sourceId"] = params.sourceId;
}
if (params.unique) {
where["unique"] = true;
}
const include = [
{
model: Source_1.Source,
where: { state: ["draft", "ready"] },
},
];
const { rows, count: total } = await Property_1.Property.scope(null).findAndCountAll({
include,
limit: params.limit,
offset: params.offset,
order: params.order,
where,
});
const properties = rows.filter((p) => { var _a; return params.modelId ? ((_a = p.source) === null || _a === void 0 ? void 0 : _a.modelId) === params.modelId : true; });
const responseProperties = [];
const responseExamples = {};
for (const property of properties) {
const apiData = await property.apiData();
responseProperties.push(apiData);
if (includeExamples) {
const examples = await RecordProperty_1.RecordProperty.findAll({
where: {
propertyId: property.id,
rawValue: { [sequelize_1.Op.not]: null },
...(params.state === "invalid"
? {
invalidReason: {
[sequelize_1.Op.not]: null,
},
}
: {}),
},
order: [["id", "asc"]],
limit: 5,
});
const exampleValues = examples.map((e) => e.rawValue);
responseExamples[property.id] = exampleValues;
}
}
return {
total,
properties: responseProperties,
examples: includeExamples ? responseExamples : undefined,
};
}
}
exports.PropertiesList = PropertiesList;
class PropertiesOptions extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "properties:options";
this.description = "enumerate the options for creating a new property";
this.outputExample = {};
this.permission = { topic: "property", mode: "read" };
}
async runWithinTransaction() {
return { types: Property_1.Property.rawAttributes.type.values };
}
}
exports.PropertiesOptions = PropertiesOptions;
class PropertyGroups extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:groups";
this.description = "enumerate the groups using this property in their rules";
this.outputExample = {};
this.permission = { topic: "property", mode: "read" };
this.inputs = { id: { required: true } };
}
async runWithinTransaction({ params, }) {
const property = await Property_1.Property.findById(params.id);
const groups = await Group_1.Group.findAll({
include: [
{
model: GroupRule_1.GroupRule,
where: { propertyId: property.id },
},
],
});
return {
groups: await Promise.all(groups.map((group) => group.apiData())),
};
}
}
exports.PropertyGroups = PropertyGroups;
class PropertyCreate extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:create";
this.description = "create a property";
this.outputExample = {};
this.permission = { topic: "property", mode: "write" };
this.inputs = {
id: { required: false },
key: { required: false },
type: { required: true },
unique: { required: false, formatter: apiData_1.APIData.ensureBoolean },
isArray: { required: false, formatter: apiData_1.APIData.ensureBoolean },
state: { required: false },
sourceId: { required: false },
options: { required: false, formatter: apiData_1.APIData.ensureObject },
filters: { required: false, formatter: apiData_1.APIData.ensureArray },
};
}
async runWithinTransaction({ params, }) {
const property = await Property_1.Property.create({
id: params.id,
key: params.key,
type: params.type,
unique: params.unique,
isArray: params.isArray,
sourceId: params.sourceId,
});
if (params.options)
await property.setOptions(params.options);
if (params.filters)
await property.setFilters(params.filters);
if (params.state)
await property.update({ state: params.state });
const source = await property.$get("source");
await configWriter_1.ConfigWriter.run();
return {
property: await property.apiData(),
pluginOptions: await property.pluginOptions(),
source: await source.apiData(),
};
}
}
exports.PropertyCreate = PropertyCreate;
class PropertyEdit extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:edit";
this.description = "edit a property";
this.outputExample = {};
this.permission = { topic: "property", mode: "write" };
this.inputs = {
id: { required: true },
key: { required: false },
type: { required: false },
unique: { required: false, formatter: apiData_1.APIData.ensureBoolean },
isArray: { required: false },
state: { required: false },
sourceId: { required: false },
options: { required: false, formatter: apiData_1.APIData.ensureObject },
filters: { required: false, formatter: apiData_1.APIData.ensureArray },
};
}
async runWithinTransaction({ params }) {
const property = await Property_1.Property.findById(params.id);
if (params.options)
await property.setOptions(params.options);
if (params.filters)
await property.setFilters(params.filters);
await property.update(params);
const source = await property.$get("source");
await configWriter_1.ConfigWriter.run();
return {
property: await property.apiData(),
pluginOptions: await property.pluginOptions(),
source: await source.apiData(),
};
}
}
exports.PropertyEdit = PropertyEdit;
class PropertyFilterOptions extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:filterOptions";
this.description = "view a the filter options for a property";
this.outputExample = {};
this.permission = { topic: "property", mode: "read" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params, }) {
const property = await Property_1.Property.findById(params.id);
const options = await filterHelper_1.FilterHelper.pluginFilterOptions(property);
return {
options: options,
optionDescriptions: await (0, filterOpsDictionary_1.buildPropertyFilterDictionary)(options),
};
}
}
exports.PropertyFilterOptions = PropertyFilterOptions;
class PropertyView extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:view";
this.description = "view a property";
this.outputExample = {};
this.permission = { topic: "property", mode: "read" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params }) {
const property = await Property_1.Property.findById(params.id);
const source = await property.$get("source", { scope: null });
return {
property: await property.apiData(),
source: await source.apiData(),
};
}
}
exports.PropertyView = PropertyView;
class PropertyPluginOptions extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:pluginOptions";
this.description = "view the plugin options for a property";
this.outputExample = {};
this.permission = { topic: "property", mode: "read" };
this.inputs = {
id: { required: true },
options: { required: false, formatter: apiData_1.APIData.ensureObject },
};
}
async runWithinTransaction({ params, }) {
const property = await Property_1.Property.findById(params.id);
return { pluginOptions: await property.pluginOptions(params.options) };
}
}
exports.PropertyPluginOptions = PropertyPluginOptions;
class PropertyRecordPreview extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:recordPreview";
this.description = "view a property's new options against a record";
this.outputExample = {};
this.permission = { topic: "property", mode: "read" };
this.inputs = {
id: { required: true },
recordId: { required: false },
options: { required: false, formatter: apiData_1.APIData.ensureObject },
filters: { required: false, formatter: apiData_1.APIData.ensureArray },
};
}
async runWithinTransaction({ params, }) {
var _a;
let record;
const property = await Property_1.Property.findById(params.id);
const source = await property.$get("source", { scope: null });
if (params.recordId) {
record = await GrouparooRecord_1.GrouparooRecord.findById(params.recordId);
}
else {
record = await GrouparooRecord_1.GrouparooRecord.findOne({
where: { modelId: source.modelId },
order: [["id", "asc"]],
});
if (!record) {
return { errorMessage: "No records found" };
}
}
const apiData = await record.apiData();
let newPropertyValues = [];
let errorMessage;
try {
newPropertyValues =
(_a = (await source.importRecordProperty(record, property, params.options, params.filters))) !== null && _a !== void 0 ? _a : [];
}
catch (error) {
errorMessage = error.toString();
}
apiData.properties[property.key] = {
id: property.id,
sourceId: property.sourceId,
state: null,
values: newPropertyValues,
invalidValue: null,
invalidReason: null,
configId: property.getConfigId(),
type: property.type,
unique: property.unique,
isPrimaryKey: property.isPrimaryKey,
isArray: property.isArray,
valueChangedAt: null,
confirmedAt: null,
stateChangedAt: null,
startedAt: null,
createdAt: null,
updatedAt: null,
};
const groups = await record.$get("groups");
const destinations = await destination_1.DestinationOps.relevantFor(record, groups);
return {
errorMessage,
record: apiData,
groups: await Promise.all(groups.map((group) => group.apiData())),
destinations: await Promise.all(destinations.map((destination) => destination.apiData(false, false))),
};
}
}
exports.PropertyRecordPreview = PropertyRecordPreview;
class PropertyTest extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:test";
this.description = "test a property";
this.outputExample = {};
this.permission = { topic: "property", mode: "write" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params }) {
const property = await Property_1.Property.findById(params.id);
return { test: (await property.test()) || true };
}
}
exports.PropertyTest = PropertyTest;
class PropertyDestroy extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "property:destroy";
this.description = "destroy a property";
this.outputExample = {};
this.permission = { topic: "property", mode: "write" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params, }) {
const property = await Property_1.Property.findById(params.id);
await property.destroy();
await configWriter_1.ConfigWriter.run();
return { success: true };
}
}
exports.PropertyDestroy = PropertyDestroy;