@grouparoo/core
Version:
The Grouparoo Core
305 lines (304 loc) • 12.5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecordDestroy = exports.RecordView = exports.RecordExport = exports.RecordsImport = exports.RecordImport = exports.RecordCreate = exports.RecordsImportAndUpdate = exports.RecordAutocompleteRecordProperty = exports.RecordsList = void 0;
const actionhero_1 = require("actionhero");
const authenticatedAction_1 = require("../classes/actions/authenticatedAction");
const GrouparooRecord_1 = require("../models/GrouparooRecord");
const RecordProperty_1 = require("../models/RecordProperty");
const GrouparooModel_1 = require("../models/GrouparooModel");
const internalRun_1 = require("../modules/internalRun");
const sequelize_1 = __importStar(require("sequelize"));
const configWriter_1 = require("../modules/configWriter");
const record_1 = require("../modules/ops/record");
const apiData_1 = require("../modules/apiData");
const cls_1 = require("../modules/cls");
const destination_1 = require("../modules/ops/destination");
class RecordsList extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "records:list";
this.description = "list all the records in a group";
this.outputExample = {};
this.permission = { topic: "record", mode: "read" };
this.inputs = {
groupId: { required: false },
searchKey: { required: false },
searchValue: { required: false },
state: { required: false },
modelId: { required: false },
caseSensitive: {
required: false,
formatter: apiData_1.APIData.ensureBoolean,
},
limit: { required: true, default: 100, formatter: apiData_1.APIData.ensureNumber },
offset: { required: true, default: 0, formatter: apiData_1.APIData.ensureNumber },
order: {
required: false,
formatter: apiData_1.APIData.ensureArray,
default: [["createdAt", "asc"]],
},
};
}
async runWithinTransaction({ params }) {
const { records, total } = await record_1.RecordOps.search(params);
return {
total,
records: await Promise.all(records.map((record) => record.apiData())),
};
}
}
exports.RecordsList = RecordsList;
class RecordAutocompleteRecordProperty extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "records:autocompleteRecordProperty";
this.description = "return matching record property values";
this.outputExample = {};
this.permission = { topic: "record", mode: "read" };
this.inputs = {
propertyId: { required: true },
match: { required: true },
limit: { required: false, default: 25, formatter: apiData_1.APIData.ensureNumber },
offset: { required: false, default: 0, formatter: apiData_1.APIData.ensureNumber },
order: {
required: false,
formatter: apiData_1.APIData.ensureArray,
default: [["rawValue", "asc"]],
},
};
}
async runWithinTransaction({ params, }) {
const op = actionhero_1.config.sequelize.dialect === "postgres" ? sequelize_1.Op.iLike : sequelize_1.Op.like;
const rawValueWhereClause = {
[op]: `%${params.match}%`,
};
const recordProperties = await RecordProperty_1.RecordProperty.findAll({
attributes: [
[sequelize_1.default.fn("DISTINCT", sequelize_1.default.col("rawValue")), "rawValue"],
"propertyId",
],
where: {
propertyId: params.propertyId,
rawValue: rawValueWhereClause,
},
group: ["rawValue", "propertyId"],
limit: params.limit,
offset: params.offset,
order: params.order,
});
return {
recordProperties: await Promise.all(recordProperties.map((prop) => prop.getValue())),
};
}
}
exports.RecordAutocompleteRecordProperty = RecordAutocompleteRecordProperty;
class RecordsImportAndUpdate extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "records:importAndUpdate";
this.description = "create a run to import and update every record";
this.outputExample = {};
this.permission = { topic: "record", mode: "write" };
this.inputs = {};
}
async runWithinTransaction({ session, }) {
const run = await (0, internalRun_1.internalRun)("teamMember", session.teamMember.id);
return { run: await run.apiData() };
}
}
exports.RecordsImportAndUpdate = RecordsImportAndUpdate;
class RecordCreate extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "record:create";
this.description = "create a record";
this.outputExample = {};
this.permission = { topic: "record", mode: "write" };
this.inputs = {
modelId: { required: true },
properties: {
required: false,
default: {},
formatter: apiData_1.APIData.ensureObject,
},
};
}
async runWithinTransaction({ params }) {
const record = new GrouparooRecord_1.GrouparooRecord({ modelId: params.modelId });
await record.save();
if (params.properties) {
await record.addOrUpdateProperties(params.properties);
}
await record.markPending();
await record.import();
await record.updateGroupMembership();
await record.update({ state: "ready" });
const properties = await record.getProperties();
let allPropertiesNull = true;
for (const key of Object.keys(params.properties)) {
if (properties[key].values.find((v) => !!v))
allPropertiesNull = false;
}
if (allPropertiesNull) {
throw new Error(`Record cannot be created because the primary source does not contain the values (${JSON.stringify(params.properties)})`);
}
const groups = await record.$get("groups");
const destinations = await destination_1.DestinationOps.relevantFor(record, groups);
await configWriter_1.ConfigWriter.run();
return {
record: await record.apiData(),
groups: await Promise.all(groups.map((group) => group.apiData())),
destinations: await Promise.all(destinations.map((destination) => destination.apiData(false, false))),
};
}
}
exports.RecordCreate = RecordCreate;
class RecordImport extends actionhero_1.Action {
constructor() {
super(...arguments);
this.name = "record:import";
this.description = "fully import a record";
this.outputExample = {};
this.permission = { topic: "record", mode: "write" };
this.middleware = ["authenticated-action"];
this.inputs = {
id: { required: true },
};
}
async run({ params }) {
let record;
let response;
await cls_1.CLS.wrap(async () => {
record = await GrouparooRecord_1.GrouparooRecord.findById(params.id);
});
const responses = await record_1.RecordOps.opportunisticallyImportAndUpdateInline([
record,
]);
if (!responses[0].success)
throw new Error(responses[0].error);
await cls_1.CLS.wrap(async () => {
await record.reload({ include: [RecordProperty_1.RecordProperty] });
const groups = await record.$get("groups");
response = {
success: true,
record: await record.apiData(),
groups: await Promise.all(groups.map((group) => group.apiData())),
};
});
return response;
}
}
exports.RecordImport = RecordImport;
class RecordsImport extends actionhero_1.Action {
constructor() {
super(...arguments);
this.name = "records:import";
this.description = "fully import all records associated with a model";
this.outputExample = {};
this.permission = { topic: "record", mode: "write" };
this.middleware = ["authenticated-action"];
this.inputs = {
modelId: { required: true },
};
}
async run({ params }) {
let model;
let records;
await cls_1.CLS.wrap(async () => {
model = await GrouparooModel_1.GrouparooModel.findById(params.modelId);
records = await GrouparooRecord_1.GrouparooRecord.findAll({
where: { modelId: model.id },
});
});
const responses = await record_1.RecordOps.opportunisticallyImportAndUpdateInline(records);
return { responses };
}
}
exports.RecordsImport = RecordsImport;
class RecordExport extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "record:export";
this.description = "fully export a record";
this.outputExample = {};
this.permission = { topic: "record", mode: "write" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params }) {
const record = await GrouparooRecord_1.GrouparooRecord.findById(params.id);
const exports = await record.export(true);
return {
success: true,
record: await record.apiData(),
exports: await Promise.all(exports.map((e) => e.apiData())),
};
}
}
exports.RecordExport = RecordExport;
class RecordView extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "record:view";
this.description = "view a record and members";
this.outputExample = {};
this.permission = { topic: "record", mode: "read" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params }) {
const record = await GrouparooRecord_1.GrouparooRecord.findById(params.id);
const groups = await record.$get("groups");
const destinations = await destination_1.DestinationOps.relevantFor(record, groups);
return {
record: await record.apiData(),
groups: await Promise.all(groups.map((group) => group.apiData())),
destinations: await Promise.all(destinations.map((destination) => destination.apiData(false, false))),
};
}
}
exports.RecordView = RecordView;
class RecordDestroy extends authenticatedAction_1.AuthenticatedAction {
constructor() {
super(...arguments);
this.name = "record:destroy";
this.description = "destroy a record";
this.outputExample = {};
this.permission = { topic: "record", mode: "write" };
this.inputs = {
id: { required: true },
};
}
async runWithinTransaction({ params, }) {
const record = await GrouparooRecord_1.GrouparooRecord.findById(params.id);
await record.destroy();
await configWriter_1.ConfigWriter.run();
return { success: true };
}
}
exports.RecordDestroy = RecordDestroy;