@grouparoo/core
Version:
The Grouparoo Core
210 lines (209 loc) • 8.38 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GrouparooModel = exports.ModelTypes = void 0;
const actionhero_1 = require("actionhero");
const sequelize_typescript_1 = require("sequelize-typescript");
const Source_1 = require("./Source");
const apiData_1 = require("../modules/apiData");
const configWriter_1 = require("../modules/configWriter");
const lockableHelper_1 = require("../modules/lockableHelper");
const Destination_1 = require("./Destination");
const Group_1 = require("./Group");
const GrouparooRecord_1 = require("./GrouparooRecord");
const runs_1 = require("../modules/ops/runs");
const stateMachine_1 = require("../modules/stateMachine");
const commonModel_1 = require("../classes/commonModel");
const cls_1 = require("../modules/cls");
const modelsCache_1 = require("../modules/caches/modelsCache");
exports.ModelTypes = ["profile", "account", "event", "custom"];
const STATES = ["ready", "deleted"];
const STATE_TRANSITIONS = [
{ from: "draft", to: "ready", checks: [] },
{ from: "ready", to: "deleted", checks: [] },
{
from: "deleted",
to: "ready",
checks: [],
},
];
let GrouparooModel = class GrouparooModel extends commonModel_1.CommonModel {
idPrefix() {
return "mod";
}
getIcon() {
switch (this.type) {
case "profile":
return "user";
case "account":
return "building";
case "event":
return "location-arrow";
case "custom":
return "database";
default:
throw new Error(`no icon for ${this.type} model`);
}
}
async run(destinationId) {
return runs_1.RunOps.run(this, destinationId);
}
async stopPreviousRuns() {
return runs_1.RunOps.stopPreviousRuns(this);
}
async apiData() {
return {
id: this.id,
name: this.name,
type: this.type,
state: this.state,
locked: this.locked,
icon: this.getIcon(),
createdAt: apiData_1.APIData.formatDate(this.createdAt),
updatedAt: apiData_1.APIData.formatDate(this.updatedAt),
};
}
getConfigId() {
return this.idIsDefault() ? configWriter_1.ConfigWriter.generateId(this.name) : this.id;
}
async getConfigObject() {
if (!this.name)
return;
return {
class: "Model",
id: this.getConfigId(),
name: this.name,
type: this.type,
};
}
// --- Class Methods --- //
static async ensureValidType(instance) {
if (!exports.ModelTypes.includes(instance.type)) {
throw new Error(`${instance.type} is not a valid model type (${exports.ModelTypes.join(", ")})`);
}
}
static async noUpdateIfLocked(instance) {
await lockableHelper_1.LockableHelper.beforeSave(instance);
}
static async updateState(instance) {
await stateMachine_1.StateMachine.transition(instance, STATE_TRANSITIONS);
}
static async noDestroyIfLocked(instance) {
await lockableHelper_1.LockableHelper.beforeDestroy(instance);
}
static async ensureNotInUse(instance) {
const sources = await Source_1.Source.scope(null).findAll({
where: { modelId: instance.id },
});
if (sources.length > 0) {
throw new Error(`cannot delete this model, source ${sources[0].id} relies on it`);
}
const destinations = await Destination_1.Destination.scope(null).findAll({
where: { modelId: instance.id },
});
if (destinations.length > 0) {
throw new Error(`cannot delete this model, destination ${destinations[0].id} relies on it`);
}
const groups = await Group_1.Group.scope(null).findAll({
where: { modelId: instance.id },
});
if (groups.length > 0) {
throw new Error(`cannot delete this model, group ${groups[0].id} relies on it`);
}
const records = await GrouparooRecord_1.GrouparooRecord.unscoped().count({
where: { modelId: instance.id },
});
if (records > 0) {
throw new Error(`cannot delete this model, ${records} records rely on it`);
}
}
static async invalidateCache() {
modelsCache_1.ModelsCache.invalidate();
await cls_1.CLS.afterCommit(async () => await actionhero_1.redis.doCluster("api.rpc.model.invalidateCache"));
}
};
__decorate([
(0, sequelize_typescript_1.Column)({ primaryKey: true }),
__metadata("design:type", String)
], GrouparooModel.prototype, "id", void 0);
__decorate([
sequelize_typescript_1.CreatedAt,
__metadata("design:type", Date)
], GrouparooModel.prototype, "createdAt", void 0);
__decorate([
sequelize_typescript_1.UpdatedAt,
__metadata("design:type", Date)
], GrouparooModel.prototype, "updatedAt", void 0);
__decorate([
(0, sequelize_typescript_1.AllowNull)(false),
sequelize_typescript_1.Column,
__metadata("design:type", String)
], GrouparooModel.prototype, "name", void 0);
__decorate([
(0, sequelize_typescript_1.AllowNull)(false),
(0, sequelize_typescript_1.Column)(sequelize_typescript_1.DataType.ENUM(...exports.ModelTypes)),
__metadata("design:type", String)
], GrouparooModel.prototype, "type", void 0);
__decorate([
sequelize_typescript_1.Column,
__metadata("design:type", String)
], GrouparooModel.prototype, "locked", void 0);
__decorate([
(0, sequelize_typescript_1.AllowNull)(false),
(0, sequelize_typescript_1.Default)("ready"),
(0, sequelize_typescript_1.Column)(sequelize_typescript_1.DataType.ENUM(...STATES)),
__metadata("design:type", Object)
], GrouparooModel.prototype, "state", void 0);
__decorate([
sequelize_typescript_1.BeforeCreate,
sequelize_typescript_1.BeforeSave,
__metadata("design:type", Function),
__metadata("design:paramtypes", [GrouparooModel]),
__metadata("design:returntype", Promise)
], GrouparooModel, "ensureValidType", null);
__decorate([
sequelize_typescript_1.BeforeSave,
__metadata("design:type", Function),
__metadata("design:paramtypes", [GrouparooModel]),
__metadata("design:returntype", Promise)
], GrouparooModel, "noUpdateIfLocked", null);
__decorate([
sequelize_typescript_1.BeforeSave,
__metadata("design:type", Function),
__metadata("design:paramtypes", [GrouparooModel]),
__metadata("design:returntype", Promise)
], GrouparooModel, "updateState", null);
__decorate([
sequelize_typescript_1.BeforeDestroy,
__metadata("design:type", Function),
__metadata("design:paramtypes", [GrouparooModel]),
__metadata("design:returntype", Promise)
], GrouparooModel, "noDestroyIfLocked", null);
__decorate([
sequelize_typescript_1.BeforeDestroy,
__metadata("design:type", Function),
__metadata("design:paramtypes", [GrouparooModel]),
__metadata("design:returntype", Promise)
], GrouparooModel, "ensureNotInUse", null);
__decorate([
sequelize_typescript_1.AfterSave,
sequelize_typescript_1.AfterDestroy,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], GrouparooModel, "invalidateCache", null);
GrouparooModel = __decorate([
(0, sequelize_typescript_1.DefaultScope)(() => ({
where: { state: "ready" },
})),
(0, sequelize_typescript_1.Table)({ tableName: "models", paranoid: false })
], GrouparooModel);
exports.GrouparooModel = GrouparooModel;