@autofleet/sadot 0.0.1-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/jest.config.d.ts +12 -0
- package/dist/src/api/index.d.ts +2 -0
- package/dist/src/api/index.js +11 -0
- package/dist/src/api/v1/definition/index.d.ts +2 -0
- package/dist/src/api/v1/definition/index.js +118 -0
- package/dist/src/api/v1/definition/validations.d.ts +2 -0
- package/dist/src/api/v1/definition/validations.js +36 -0
- package/dist/src/api/v1/errors.d.ts +2 -0
- package/dist/src/api/v1/errors.js +15 -0
- package/dist/src/api/v1/index.d.ts +2 -0
- package/dist/src/api/v1/index.js +10 -0
- package/dist/src/errors/index.d.ts +16 -0
- package/dist/src/errors/index.js +45 -0
- package/dist/src/events/index.d.ts +4 -0
- package/dist/src/events/index.js +47 -0
- package/dist/src/hooks/create.d.ts +9 -0
- package/dist/src/hooks/create.js +70 -0
- package/dist/src/hooks/enrich.d.ts +5 -0
- package/dist/src/hooks/enrich.js +118 -0
- package/dist/src/hooks/find.d.ts +1 -0
- package/dist/src/hooks/find.js +29 -0
- package/dist/src/hooks/index.d.ts +6 -0
- package/dist/src/hooks/index.js +18 -0
- package/dist/src/hooks/update.d.ts +9 -0
- package/dist/src/hooks/update.js +58 -0
- package/dist/src/hooks/workaround.d.ts +10 -0
- package/dist/src/hooks/workaround.js +37 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.js +105 -0
- package/dist/src/models/CustomFieldDefinition.d.ts +23 -0
- package/dist/src/models/CustomFieldDefinition.js +165 -0
- package/dist/src/models/CustomFieldValue.d.ts +15 -0
- package/dist/src/models/CustomFieldValue.js +148 -0
- package/dist/src/models/index.d.ts +8 -0
- package/dist/src/models/index.js +87 -0
- package/dist/src/models/tests/AssociatedTestModel.d.ts +12 -0
- package/dist/src/models/tests/AssociatedTestModel.js +71 -0
- package/dist/src/models/tests/TestModel.d.ts +11 -0
- package/dist/src/models/tests/TestModel.js +63 -0
- package/dist/src/repository/definition.d.ts +17 -0
- package/dist/src/repository/definition.js +80 -0
- package/dist/src/repository/value.d.ts +24 -0
- package/dist/src/repository/value.js +107 -0
- package/dist/src/tests/api/test-api.d.ts +2 -0
- package/dist/src/tests/api/test-api.js +56 -0
- package/dist/src/tests/helpers/database-config.d.ts +15 -0
- package/dist/src/tests/helpers/database-config.js +16 -0
- package/dist/src/tests/helpers/index.d.ts +2 -0
- package/dist/src/tests/helpers/index.js +18 -0
- package/dist/src/tests/mocks/definition.mock.d.ts +37 -0
- package/dist/src/tests/mocks/definition.mock.js +64 -0
- package/dist/src/tests/mocks/events.mock.d.ts +3 -0
- package/dist/src/tests/mocks/events.mock.js +19 -0
- package/dist/src/tests/mocks/testModel.d.ts +12 -0
- package/dist/src/tests/mocks/testModel.js +35 -0
- package/dist/src/types/definition/index.d.ts +23 -0
- package/dist/src/types/definition/index.js +2 -0
- package/dist/src/types/index.d.ts +13 -0
- package/dist/src/types/index.js +2 -0
- package/dist/src/types/value/index.d.ts +15 -0
- package/dist/src/types/value/index.js +2 -0
- package/dist/src/utils/constants/index.d.ts +1 -0
- package/dist/src/utils/constants/index.js +5 -0
- package/dist/src/utils/db/index.d.ts +4 -0
- package/dist/src/utils/db/index.js +24 -0
- package/dist/src/utils/logger/index.d.ts +2 -0
- package/dist/src/utils/logger/index.js +6 -0
- package/dist/src/utils/validations/custom-fields.d.ts +2 -0
- package/dist/src/utils/validations/custom-fields.js +10 -0
- package/dist/src/utils/validations/custom.d.ts +15 -0
- package/dist/src/utils/validations/custom.js +42 -0
- package/dist/src/utils/validations/index.d.ts +2 -0
- package/dist/src/utils/validations/index.js +20 -0
- package/dist/src/utils/validations/type.d.ts +18 -0
- package/dist/src/utils/validations/type.js +50 -0
- package/dist/src/utils/validations/validators.d.ts +12 -0
- package/dist/src/utils/validations/validators.js +33 -0
- package/package.json +44 -0
- package/src/api/index.ts +9 -0
- package/src/api/v1/definition/index.ts +110 -0
- package/src/api/v1/definition/validations.ts +35 -0
- package/src/api/v1/errors.ts +16 -0
- package/src/api/v1/index.ts +9 -0
- package/src/errors/index.ts +42 -0
- package/src/events/index.ts +50 -0
- package/src/hooks/create.ts +51 -0
- package/src/hooks/enrich.ts +125 -0
- package/src/hooks/find.ts +27 -0
- package/src/hooks/index.ts +15 -0
- package/src/hooks/update.ts +39 -0
- package/src/hooks/workaround.ts +47 -0
- package/src/index.ts +101 -0
- package/src/models/CustomFieldDefinition.ts +140 -0
- package/src/models/CustomFieldValue.ts +114 -0
- package/src/models/index.ts +101 -0
- package/src/models/tests/AssociatedTestModel.ts +57 -0
- package/src/models/tests/TestModel.ts +49 -0
- package/src/repository/definition.ts +111 -0
- package/src/repository/value.ts +99 -0
- package/src/tests/api/test-api.ts +38 -0
- package/src/tests/helpers/database-config.ts +14 -0
- package/src/tests/helpers/index.ts +15 -0
- package/src/tests/mocks/definition.mock.ts +69 -0
- package/src/tests/mocks/events.mock.ts +18 -0
- package/src/tests/mocks/testModel.ts +37 -0
- package/src/types/definition/index.ts +22 -0
- package/src/types/index.ts +15 -0
- package/src/types/value/index.ts +14 -0
- package/src/utils/constants/index.ts +2 -0
- package/src/utils/db/index.ts +21 -0
- package/src/utils/logger/index.ts +6 -0
- package/src/utils/validations/custom-fields.ts +9 -0
- package/src/utils/validations/custom.ts +39 -0
- package/src/utils/validations/index.ts +19 -0
- package/src/utils/validations/type.ts +45 -0
- package/src/utils/validations/validators.ts +34 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.initTestModels = exports.initTables = exports.AssociatedTestModel = exports.TestModel = exports.CustomFieldDefinition = exports.CustomFieldValue = void 0;
|
|
7
|
+
/* eslint-disable no-param-reassign */
|
|
8
|
+
const sequelize_1 = require("sequelize");
|
|
9
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
10
|
+
const CustomFieldDefinition_1 = __importDefault(require("./CustomFieldDefinition"));
|
|
11
|
+
exports.CustomFieldDefinition = CustomFieldDefinition_1.default;
|
|
12
|
+
const CustomFieldValue_1 = __importDefault(require("./CustomFieldValue"));
|
|
13
|
+
exports.CustomFieldValue = CustomFieldValue_1.default;
|
|
14
|
+
const TestModel_1 = __importDefault(require("./tests/TestModel"));
|
|
15
|
+
exports.TestModel = TestModel_1.default;
|
|
16
|
+
const AssociatedTestModel_1 = __importDefault(require("./tests/AssociatedTestModel"));
|
|
17
|
+
exports.AssociatedTestModel = AssociatedTestModel_1.default;
|
|
18
|
+
const productionModels = [CustomFieldDefinition_1.default, CustomFieldValue_1.default];
|
|
19
|
+
const testModels = [TestModel_1.default, AssociatedTestModel_1.default];
|
|
20
|
+
const SADOT_MIGRATION_PREFIX = 'sadot-migration';
|
|
21
|
+
const SCHEMA_VERSION = 1;
|
|
22
|
+
const CUSTOM_FIELDS_SCHEMA_VERSION = `${SADOT_MIGRATION_PREFIX}_${SCHEMA_VERSION}`;
|
|
23
|
+
const initTables = async (sequelize, getUser) => {
|
|
24
|
+
logger_1.default.info('custom-fields: initialize custom-fields tables');
|
|
25
|
+
// Detect models and import them to the orm
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
27
|
+
if (!sequelize.addModels) {
|
|
28
|
+
throw new Error('sequelize instance must have addModels function');
|
|
29
|
+
}
|
|
30
|
+
sequelize.addModels(productionModels);
|
|
31
|
+
CustomFieldDefinition_1.default.addScope('userScope', () => {
|
|
32
|
+
const user = getUser();
|
|
33
|
+
if (user?.permissions) {
|
|
34
|
+
return {
|
|
35
|
+
where: {
|
|
36
|
+
entityId: [
|
|
37
|
+
...Object.keys(user.permissions.fleets),
|
|
38
|
+
...Object.keys(user.permissions.businessModels),
|
|
39
|
+
...Object.keys(user.permissions.demandSources),
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {};
|
|
45
|
+
});
|
|
46
|
+
logger_1.default.info('custom-fields: models added');
|
|
47
|
+
const SequelizeMeta = sequelize.define('SequelizeMeta', {
|
|
48
|
+
name: {
|
|
49
|
+
type: sequelize_1.DataTypes.STRING,
|
|
50
|
+
allowNull: false,
|
|
51
|
+
unique: true,
|
|
52
|
+
primaryKey: true,
|
|
53
|
+
autoIncrement: false,
|
|
54
|
+
},
|
|
55
|
+
}, {
|
|
56
|
+
tableName: 'SequelizeMeta',
|
|
57
|
+
timestamps: false,
|
|
58
|
+
schema: 'public',
|
|
59
|
+
});
|
|
60
|
+
const migrations = await SequelizeMeta.findAll({ raw: true });
|
|
61
|
+
const currentSadotSchemaVersion = migrations
|
|
62
|
+
.reverse().find((m) => m.name.includes(SADOT_MIGRATION_PREFIX));
|
|
63
|
+
if (!currentSadotSchemaVersion
|
|
64
|
+
|| currentSadotSchemaVersion.name !== CUSTOM_FIELDS_SCHEMA_VERSION) {
|
|
65
|
+
await CustomFieldDefinition_1.default.sync({ alter: true });
|
|
66
|
+
await CustomFieldValue_1.default.sync({ alter: true });
|
|
67
|
+
await SequelizeMeta.create({ name: CUSTOM_FIELDS_SCHEMA_VERSION });
|
|
68
|
+
logger_1.default.info('custom-fields: models synced');
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
exports.initTables = initTables;
|
|
72
|
+
const initTestModels = async (sequelize) => {
|
|
73
|
+
logger_1.default.info('custom-fields: initialize custom-fields test models');
|
|
74
|
+
// Detect models and import them to the orm
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
76
|
+
if (!sequelize.addModels) {
|
|
77
|
+
throw new Error('sequelize instance must have addModels function');
|
|
78
|
+
}
|
|
79
|
+
sequelize.addModels(testModels);
|
|
80
|
+
await sequelize.dropSchema('custom-fields', { logging: false });
|
|
81
|
+
await sequelize.createSchema('custom-fields', { logging: false });
|
|
82
|
+
logger_1.default.info('custom-fields: test models added');
|
|
83
|
+
await TestModel_1.default.sync({ alter: true });
|
|
84
|
+
await AssociatedTestModel_1.default.sync({ alter: true });
|
|
85
|
+
logger_1.default.info('custom-fields: test models synced');
|
|
86
|
+
};
|
|
87
|
+
exports.initTestModels = initTestModels;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Model } from 'sequelize-typescript';
|
|
2
|
+
import TestModel from './TestModel';
|
|
3
|
+
declare class AssociatedTestModel extends Model {
|
|
4
|
+
id: string;
|
|
5
|
+
testModelId: string;
|
|
6
|
+
fleetId: string;
|
|
7
|
+
businessModelId: string;
|
|
8
|
+
demandSourceId: string;
|
|
9
|
+
anotherAttribute?: boolean;
|
|
10
|
+
testModel: TestModel;
|
|
11
|
+
}
|
|
12
|
+
export default AssociatedTestModel;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
16
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
17
|
+
const TestModel_1 = __importDefault(require("./TestModel"));
|
|
18
|
+
let AssociatedTestModel = class AssociatedTestModel extends sequelize_typescript_1.Model {
|
|
19
|
+
};
|
|
20
|
+
__decorate([
|
|
21
|
+
sequelize_typescript_1.PrimaryKey,
|
|
22
|
+
(0, sequelize_typescript_1.Column)({
|
|
23
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
24
|
+
defaultValue: sequelize_typescript_1.DataType.UUIDV4,
|
|
25
|
+
allowNull: false,
|
|
26
|
+
}),
|
|
27
|
+
__metadata("design:type", String)
|
|
28
|
+
], AssociatedTestModel.prototype, "id", void 0);
|
|
29
|
+
__decorate([
|
|
30
|
+
(0, sequelize_typescript_1.ForeignKey)(() => TestModel_1.default),
|
|
31
|
+
(0, sequelize_typescript_1.Column)({
|
|
32
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
33
|
+
allowNull: false,
|
|
34
|
+
}),
|
|
35
|
+
__metadata("design:type", String)
|
|
36
|
+
], AssociatedTestModel.prototype, "testModelId", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
(0, sequelize_typescript_1.Column)({
|
|
39
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
40
|
+
allowNull: false,
|
|
41
|
+
}),
|
|
42
|
+
__metadata("design:type", String)
|
|
43
|
+
], AssociatedTestModel.prototype, "fleetId", void 0);
|
|
44
|
+
__decorate([
|
|
45
|
+
(0, sequelize_typescript_1.Column)({
|
|
46
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
47
|
+
allowNull: true,
|
|
48
|
+
}),
|
|
49
|
+
__metadata("design:type", String)
|
|
50
|
+
], AssociatedTestModel.prototype, "businessModelId", void 0);
|
|
51
|
+
__decorate([
|
|
52
|
+
(0, sequelize_typescript_1.Column)({
|
|
53
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
54
|
+
allowNull: true,
|
|
55
|
+
}),
|
|
56
|
+
__metadata("design:type", String)
|
|
57
|
+
], AssociatedTestModel.prototype, "demandSourceId", void 0);
|
|
58
|
+
__decorate([
|
|
59
|
+
(0, sequelize_typescript_1.Column)({
|
|
60
|
+
type: sequelize_typescript_1.DataType.BOOLEAN,
|
|
61
|
+
}),
|
|
62
|
+
__metadata("design:type", Boolean)
|
|
63
|
+
], AssociatedTestModel.prototype, "anotherAttribute", void 0);
|
|
64
|
+
__decorate([
|
|
65
|
+
(0, sequelize_typescript_1.BelongsTo)(() => TestModel_1.default),
|
|
66
|
+
__metadata("design:type", TestModel_1.default)
|
|
67
|
+
], AssociatedTestModel.prototype, "testModel", void 0);
|
|
68
|
+
AssociatedTestModel = __decorate([
|
|
69
|
+
(0, sequelize_typescript_1.Table)({ schema: 'custom-fields', createdAt: false, updatedAt: false })
|
|
70
|
+
], AssociatedTestModel);
|
|
71
|
+
exports.default = AssociatedTestModel;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Model } from 'sequelize-typescript';
|
|
2
|
+
import AssociatedTestModel from './AssociatedTestModel';
|
|
3
|
+
declare class TestModel extends Model {
|
|
4
|
+
id: string;
|
|
5
|
+
fleetId: string;
|
|
6
|
+
businessModelId: string;
|
|
7
|
+
demandSourceId: string;
|
|
8
|
+
coolAttribute?: boolean;
|
|
9
|
+
associatedModels: AssociatedTestModel[];
|
|
10
|
+
}
|
|
11
|
+
export default TestModel;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
16
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
17
|
+
const AssociatedTestModel_1 = __importDefault(require("./AssociatedTestModel"));
|
|
18
|
+
let TestModel = class TestModel extends sequelize_typescript_1.Model {
|
|
19
|
+
};
|
|
20
|
+
__decorate([
|
|
21
|
+
sequelize_typescript_1.PrimaryKey,
|
|
22
|
+
(0, sequelize_typescript_1.Column)({
|
|
23
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
24
|
+
defaultValue: sequelize_typescript_1.DataType.UUIDV4,
|
|
25
|
+
allowNull: false,
|
|
26
|
+
}),
|
|
27
|
+
__metadata("design:type", String)
|
|
28
|
+
], TestModel.prototype, "id", void 0);
|
|
29
|
+
__decorate([
|
|
30
|
+
(0, sequelize_typescript_1.Column)({
|
|
31
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
32
|
+
allowNull: false,
|
|
33
|
+
}),
|
|
34
|
+
__metadata("design:type", String)
|
|
35
|
+
], TestModel.prototype, "fleetId", void 0);
|
|
36
|
+
__decorate([
|
|
37
|
+
(0, sequelize_typescript_1.Column)({
|
|
38
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
39
|
+
allowNull: true,
|
|
40
|
+
}),
|
|
41
|
+
__metadata("design:type", String)
|
|
42
|
+
], TestModel.prototype, "businessModelId", void 0);
|
|
43
|
+
__decorate([
|
|
44
|
+
(0, sequelize_typescript_1.Column)({
|
|
45
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
46
|
+
allowNull: true,
|
|
47
|
+
}),
|
|
48
|
+
__metadata("design:type", String)
|
|
49
|
+
], TestModel.prototype, "demandSourceId", void 0);
|
|
50
|
+
__decorate([
|
|
51
|
+
(0, sequelize_typescript_1.Column)({
|
|
52
|
+
type: sequelize_typescript_1.DataType.BOOLEAN,
|
|
53
|
+
}),
|
|
54
|
+
__metadata("design:type", Boolean)
|
|
55
|
+
], TestModel.prototype, "coolAttribute", void 0);
|
|
56
|
+
__decorate([
|
|
57
|
+
(0, sequelize_typescript_1.HasMany)(() => AssociatedTestModel_1.default),
|
|
58
|
+
__metadata("design:type", Array)
|
|
59
|
+
], TestModel.prototype, "associatedModels", void 0);
|
|
60
|
+
TestModel = __decorate([
|
|
61
|
+
(0, sequelize_typescript_1.Table)({ schema: 'custom-fields', createdAt: false, updatedAt: false })
|
|
62
|
+
], TestModel);
|
|
63
|
+
exports.default = TestModel;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CustomFieldDefinition } from '../models';
|
|
2
|
+
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
3
|
+
export declare const create: (data: CreateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
4
|
+
export declare const findAll: (where: any, options?: any) => Promise<CustomFieldDefinition[]>;
|
|
5
|
+
export declare const findByIds: (ids: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
6
|
+
export declare const findById: (id: string, options?: any) => Promise<CustomFieldDefinition | null>;
|
|
7
|
+
export declare const findByEntityId: (entityId: string, options?: any) => Promise<CustomFieldDefinition[]>;
|
|
8
|
+
export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
9
|
+
export declare const findByWhere: (where: any) => Promise<CustomFieldDefinition | null>;
|
|
10
|
+
export declare const findDefinitionsByModels: (modelTypes: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
11
|
+
export declare const update: (id: string, data: UpdateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
12
|
+
export declare const disable: (id: string) => Promise<any>;
|
|
13
|
+
export declare const destroy: (id: string) => Promise<any>;
|
|
14
|
+
/**
|
|
15
|
+
* Return the names of the required fields for a given model
|
|
16
|
+
*/
|
|
17
|
+
export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[]) => Promise<string[]>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequiredFields = exports.destroy = exports.disable = exports.update = exports.findDefinitionsByModels = exports.findByWhere = exports.findByEntityIds = exports.findByEntityId = exports.findById = exports.findByIds = exports.findAll = exports.create = void 0;
|
|
4
|
+
const sequelize_1 = require("sequelize");
|
|
5
|
+
const models_1 = require("../models");
|
|
6
|
+
const create = (data) => models_1.CustomFieldDefinition.create(data);
|
|
7
|
+
exports.create = create;
|
|
8
|
+
const findAll = (where, options = { withDisabled: false }) => {
|
|
9
|
+
const queryModel = options.withDisabled
|
|
10
|
+
? models_1.CustomFieldDefinition.unscoped()
|
|
11
|
+
: models_1.CustomFieldDefinition;
|
|
12
|
+
return queryModel.scope('userScope').findAll({
|
|
13
|
+
where,
|
|
14
|
+
transaction: options.transaction,
|
|
15
|
+
raw: true,
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
exports.findAll = findAll;
|
|
19
|
+
const findByIds = (ids, options = { withDisabled: false }) => (0, exports.findAll)({ id: { [sequelize_1.Op.in]: ids } }, options);
|
|
20
|
+
exports.findByIds = findByIds;
|
|
21
|
+
const findById = (id, options = { withDisabled: false }) => {
|
|
22
|
+
const { withDisabled } = options;
|
|
23
|
+
if (withDisabled) {
|
|
24
|
+
return models_1.CustomFieldDefinition.unscoped().scope('userScope').findByPk(id);
|
|
25
|
+
}
|
|
26
|
+
return models_1.CustomFieldDefinition.scope('userScope').findByPk(id);
|
|
27
|
+
};
|
|
28
|
+
exports.findById = findById;
|
|
29
|
+
const findByEntityId = async (entityId, options = {}) => models_1.CustomFieldDefinition.findAll({
|
|
30
|
+
where: { entityId },
|
|
31
|
+
transaction: options.transaction,
|
|
32
|
+
});
|
|
33
|
+
exports.findByEntityId = findByEntityId;
|
|
34
|
+
const findByEntityIds = async (modelType, entityIds, options = {}) => models_1.CustomFieldDefinition.findAll({
|
|
35
|
+
where: {
|
|
36
|
+
modelType,
|
|
37
|
+
entityId: entityIds,
|
|
38
|
+
},
|
|
39
|
+
transaction: options.transaction,
|
|
40
|
+
raw: true,
|
|
41
|
+
});
|
|
42
|
+
exports.findByEntityIds = findByEntityIds;
|
|
43
|
+
const findByWhere = (where) => models_1.CustomFieldDefinition.scope('userScope').findOne({
|
|
44
|
+
where,
|
|
45
|
+
});
|
|
46
|
+
exports.findByWhere = findByWhere;
|
|
47
|
+
const findDefinitionsByModels = async (modelTypes, options) => {
|
|
48
|
+
const query = { modelType: { [sequelize_1.Op.in]: modelTypes } };
|
|
49
|
+
return models_1.CustomFieldDefinition.findAll({
|
|
50
|
+
where: query,
|
|
51
|
+
transaction: options?.transaction,
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
exports.findDefinitionsByModels = findDefinitionsByModels;
|
|
55
|
+
const update = async (id, data) => {
|
|
56
|
+
const updatedDefinition = (await models_1.CustomFieldDefinition.scope('userScope').update(data, {
|
|
57
|
+
where: { id },
|
|
58
|
+
returning: true,
|
|
59
|
+
individualHooks: true,
|
|
60
|
+
}))[1][0];
|
|
61
|
+
return updatedDefinition;
|
|
62
|
+
};
|
|
63
|
+
exports.update = update;
|
|
64
|
+
const disable = (id) => models_1.CustomFieldDefinition.update({ disabled: true }, { where: { id } });
|
|
65
|
+
exports.disable = disable;
|
|
66
|
+
const destroy = (id) => models_1.CustomFieldDefinition.destroy({ where: { id } });
|
|
67
|
+
exports.destroy = destroy;
|
|
68
|
+
/**
|
|
69
|
+
* Return the names of the required fields for a given model
|
|
70
|
+
*/
|
|
71
|
+
const getRequiredFields = async (modelType, modelId, entityId) => {
|
|
72
|
+
const entityIds = Array.isArray(entityId) ? entityId : [entityId];
|
|
73
|
+
const requiredFields = await models_1.CustomFieldDefinition.findAll({
|
|
74
|
+
where: { required: true, modelType, entityId: { [sequelize_1.Op.in]: entityIds } },
|
|
75
|
+
logging: true,
|
|
76
|
+
});
|
|
77
|
+
const requiredFieldsNames = requiredFields.map((definition) => definition.name);
|
|
78
|
+
return [...new Set(requiredFieldsNames)];
|
|
79
|
+
};
|
|
80
|
+
exports.getRequiredFields = getRequiredFields;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CustomFieldValue } from '../models';
|
|
2
|
+
import { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
|
|
3
|
+
export declare const findByModelIdAndDefinition: (modelId: string, customFieldDefinitionId: string) => Promise<CustomFieldValue[]>;
|
|
4
|
+
export declare const create: (data: CreateCustomFieldValue, withAssociations?: boolean) => Promise<CustomFieldValue>;
|
|
5
|
+
export declare const findAllValues: () => Promise<CustomFieldValue[]>;
|
|
6
|
+
/**
|
|
7
|
+
* Get all values for model instance id (with their definitions)
|
|
8
|
+
* @param modelId
|
|
9
|
+
* @returns CustomFieldValue[]
|
|
10
|
+
*/
|
|
11
|
+
export declare const findValuesByModelId: (modelId: string) => Promise<CustomFieldValue[]>;
|
|
12
|
+
/**
|
|
13
|
+
* Retrieves custom field values for given model IDs
|
|
14
|
+
* @param modelIds - An array of model IDs to query custom field values for.
|
|
15
|
+
* @param options - Optional configuration object.
|
|
16
|
+
*/
|
|
17
|
+
export declare const findValuesByModelIds: (modelIds: string[], options?: any) => Promise<CustomFieldValue[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Try to update custom field values for a model instance.
|
|
20
|
+
* Create new value record if not exists, but fails if value's definition not exist.
|
|
21
|
+
* Return the updated values
|
|
22
|
+
*/
|
|
23
|
+
export declare const updateValues: (modelType: string, modelId: string, identifiers: string[], valuesToUpdate: ValuesToUpdate, options?: any) => Promise<CustomFieldValue[]>;
|
|
24
|
+
export declare const deleteValue: (id: string, options?: any) => Promise<any>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.deleteValue = exports.updateValues = exports.findValuesByModelIds = exports.findValuesByModelId = exports.findAllValues = exports.create = exports.findByModelIdAndDefinition = void 0;
|
|
30
|
+
/* eslint-disable max-len */
|
|
31
|
+
const models_1 = require("../models");
|
|
32
|
+
const DefinitionRepo = __importStar(require("./definition"));
|
|
33
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
34
|
+
const errors_1 = require("../errors");
|
|
35
|
+
const findByModelIdAndDefinition = async (modelId, customFieldDefinitionId) => models_1.CustomFieldValue.findAll({ where: { modelId, customFieldDefinitionId }, include: [models_1.CustomFieldDefinition] });
|
|
36
|
+
exports.findByModelIdAndDefinition = findByModelIdAndDefinition;
|
|
37
|
+
const create = async (data, withAssociations = false) => {
|
|
38
|
+
const created = await models_1.CustomFieldValue.create(data);
|
|
39
|
+
if (withAssociations) {
|
|
40
|
+
const createdWithAssociations = await (0, exports.findByModelIdAndDefinition)(created.modelId, created.customFieldDefinitionId);
|
|
41
|
+
return createdWithAssociations?.[0];
|
|
42
|
+
}
|
|
43
|
+
return created;
|
|
44
|
+
};
|
|
45
|
+
exports.create = create;
|
|
46
|
+
const findAllValues = async () => models_1.CustomFieldValue.findAll({ include: [models_1.CustomFieldDefinition] });
|
|
47
|
+
exports.findAllValues = findAllValues;
|
|
48
|
+
/**
|
|
49
|
+
* Get all values for model instance id (with their definitions)
|
|
50
|
+
* @param modelId
|
|
51
|
+
* @returns CustomFieldValue[]
|
|
52
|
+
*/
|
|
53
|
+
const findValuesByModelId = async (modelId) => models_1.CustomFieldValue.findAll({ where: { modelId }, include: [models_1.CustomFieldDefinition] });
|
|
54
|
+
exports.findValuesByModelId = findValuesByModelId;
|
|
55
|
+
/**
|
|
56
|
+
* Retrieves custom field values for given model IDs
|
|
57
|
+
* @param modelIds - An array of model IDs to query custom field values for.
|
|
58
|
+
* @param options - Optional configuration object.
|
|
59
|
+
*/
|
|
60
|
+
const findValuesByModelIds = async (modelIds, options) => {
|
|
61
|
+
const { transaction } = options;
|
|
62
|
+
return models_1.CustomFieldValue.findAll({
|
|
63
|
+
where: { modelId: modelIds },
|
|
64
|
+
transaction,
|
|
65
|
+
raw: true,
|
|
66
|
+
nest: true,
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
exports.findValuesByModelIds = findValuesByModelIds;
|
|
70
|
+
/**
|
|
71
|
+
* Try to update custom field values for a model instance.
|
|
72
|
+
* Create new value record if not exists, but fails if value's definition not exist.
|
|
73
|
+
* Return the updated values
|
|
74
|
+
*/
|
|
75
|
+
const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}) => {
|
|
76
|
+
logger_1.default.info(`custom-fields: updating values for ${modelType} ${modelId}`, { valuesToUpdate });
|
|
77
|
+
const names = Object.keys(valuesToUpdate);
|
|
78
|
+
const fieldDefinitions = await DefinitionRepo.findAll({ modelType, name: names, entityId: identifiers }, { withDisabled: true, transaction: options.transaction }) || [];
|
|
79
|
+
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
80
|
+
if (fieldDefinitions.length !== names.length) {
|
|
81
|
+
const missingDefinitions = names.filter((name) => !fieldDefinitions.some((def) => def.name === name));
|
|
82
|
+
throw new errors_1.MissingDefinitionError(missingDefinitions);
|
|
83
|
+
}
|
|
84
|
+
const disabledNames = disabledDefinitions?.map((def) => def.name) || [];
|
|
85
|
+
const valuesWithDisabledDefinitions = names.filter((name) => disabledNames.includes(name));
|
|
86
|
+
if (valuesWithDisabledDefinitions?.length > 0) {
|
|
87
|
+
logger_1.default.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
|
|
88
|
+
}
|
|
89
|
+
const values = names.map((name) => ({
|
|
90
|
+
modelId,
|
|
91
|
+
value: valuesToUpdate[name],
|
|
92
|
+
updatedAt: new Date(),
|
|
93
|
+
customFieldDefinitionId: fieldDefinitions.find((def) => def.name === name).id,
|
|
94
|
+
}));
|
|
95
|
+
return Promise.all(values.map(async (value) => {
|
|
96
|
+
const [cfv] = await models_1.CustomFieldValue.upsert(value, {
|
|
97
|
+
transaction: options.transaction,
|
|
98
|
+
});
|
|
99
|
+
return cfv;
|
|
100
|
+
}));
|
|
101
|
+
};
|
|
102
|
+
exports.updateValues = updateValues;
|
|
103
|
+
const deleteValue = (id, options = {}) => models_1.CustomFieldValue.update({ deletedAt: new Date() }, {
|
|
104
|
+
where: { id },
|
|
105
|
+
transaction: options.transaction,
|
|
106
|
+
});
|
|
107
|
+
exports.deleteValue = deleteValue;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const express_1 = __importStar(require("express"));
|
|
27
|
+
const models_1 = require("../../models");
|
|
28
|
+
const app = (0, express_1.default)();
|
|
29
|
+
app.use(express_1.default.json());
|
|
30
|
+
const api = (0, express_1.Router)();
|
|
31
|
+
api.get('/v1/test-models', async (req, res) => {
|
|
32
|
+
const testModels = await models_1.TestModel.findAll();
|
|
33
|
+
return res.json(testModels);
|
|
34
|
+
});
|
|
35
|
+
api.get('/v1/test-models/:testModelId', async (req, res) => {
|
|
36
|
+
const { params: { testModelId } } = req;
|
|
37
|
+
const testModel = await models_1.TestModel.findByPk(testModelId);
|
|
38
|
+
return res.json(testModel);
|
|
39
|
+
});
|
|
40
|
+
api.post('/v1/test-models', async (req, res) => {
|
|
41
|
+
const { body } = req;
|
|
42
|
+
const testModel = await models_1.TestModel.create(body);
|
|
43
|
+
return res.json(testModel);
|
|
44
|
+
});
|
|
45
|
+
api.patch('/v1/test-models/:testModelId', async (req, res) => {
|
|
46
|
+
const { body, params: { testModelId } } = req;
|
|
47
|
+
const testModel = await models_1.TestModel.update(body, {
|
|
48
|
+
where: {
|
|
49
|
+
id: testModelId,
|
|
50
|
+
},
|
|
51
|
+
returning: true,
|
|
52
|
+
});
|
|
53
|
+
return res.json(testModel[1][0]);
|
|
54
|
+
});
|
|
55
|
+
app.use('/api', api);
|
|
56
|
+
exports.default = app;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
test: {
|
|
3
|
+
username: string;
|
|
4
|
+
password: string;
|
|
5
|
+
database: string;
|
|
6
|
+
host: string;
|
|
7
|
+
dialect: string;
|
|
8
|
+
define: {
|
|
9
|
+
underscored: boolean;
|
|
10
|
+
underscoredAll: boolean;
|
|
11
|
+
};
|
|
12
|
+
logging: boolean;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = {
|
|
4
|
+
test: {
|
|
5
|
+
username: process.env.DB_USERNAME || '',
|
|
6
|
+
password: process.env.DB_PASSWORD || null,
|
|
7
|
+
database: process.env.DB_NAME || 'sadot_package_test',
|
|
8
|
+
host: process.env.DB_HOST || '127.0.0.1',
|
|
9
|
+
dialect: process.env.DB_TYPE || 'postgres',
|
|
10
|
+
define: {
|
|
11
|
+
underscored: true,
|
|
12
|
+
underscoredAll: true,
|
|
13
|
+
},
|
|
14
|
+
logging: false,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getModel = exports.cleanup = void 0;
|
|
4
|
+
const models_1 = require("../../models");
|
|
5
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
6
|
+
const cleanup = async () => {
|
|
7
|
+
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
|
|
8
|
+
await models_1.CustomFieldDefinition.unscoped().destroy({ where: {} });
|
|
9
|
+
await models_1.TestModel.destroy({ where: {} });
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
exports.cleanup = cleanup;
|
|
13
|
+
const getModel = (name) => {
|
|
14
|
+
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
15
|
+
const models = require('../../models');
|
|
16
|
+
return models[name];
|
|
17
|
+
};
|
|
18
|
+
exports.getModel = getModel;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
|
|
2
|
+
export declare const coolFieldDefinition: CreateCustomFieldDefinition;
|
|
3
|
+
export declare const coolFieldDefinition2: {
|
|
4
|
+
name: string;
|
|
5
|
+
required?: boolean;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
description?: string;
|
|
8
|
+
createdAt?: Date;
|
|
9
|
+
updatedAt?: Date;
|
|
10
|
+
deletedAt?: Date;
|
|
11
|
+
displayName?: string;
|
|
12
|
+
validation?: any;
|
|
13
|
+
fieldType: string;
|
|
14
|
+
entityId: string;
|
|
15
|
+
entityType: string;
|
|
16
|
+
modelType: string;
|
|
17
|
+
};
|
|
18
|
+
export declare const coolFieldDefinition3: {
|
|
19
|
+
name: string;
|
|
20
|
+
required?: boolean;
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
description?: string;
|
|
23
|
+
createdAt?: Date;
|
|
24
|
+
updatedAt?: Date;
|
|
25
|
+
deletedAt?: Date;
|
|
26
|
+
displayName?: string;
|
|
27
|
+
validation?: any;
|
|
28
|
+
fieldType: string;
|
|
29
|
+
entityId: string;
|
|
30
|
+
entityType: string;
|
|
31
|
+
modelType: string;
|
|
32
|
+
};
|
|
33
|
+
export declare const booleanField: (modelType: string) => CreateCustomFieldDefinition;
|
|
34
|
+
export declare const enumField: (modelType: string, options: any) => CreateCustomFieldDefinition;
|
|
35
|
+
export declare const rangeField: (modelType: string) => CreateCustomFieldDefinition;
|
|
36
|
+
export declare const createDefinition: (defaults: Partial<CustomFieldDefinitionDTO>) => CreateCustomFieldDefinition;
|
|
37
|
+
export declare const createDefinitions: (defaults: Partial<CustomFieldDefinitionDTO>, length?: number) => CreateCustomFieldDefinition[];
|