@autofleet/sadot 0.12.0 → 0.13.0-beta.0

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.
Files changed (124) hide show
  1. package/package.json +3 -1
  2. package/src/api/v1/index.ts +2 -0
  3. package/src/api/v1/validator/index.ts +127 -0
  4. package/src/api/v1/validator/validations.ts +39 -0
  5. package/src/events/index.ts +1 -1
  6. package/src/hooks/hooks.ts +304 -0
  7. package/src/hooks/index.ts +10 -5
  8. package/src/index.ts +6 -0
  9. package/src/models/CustomValidator.ts +76 -0
  10. package/src/models/index.ts +7 -2
  11. package/src/repository/validator.ts +91 -0
  12. package/src/tests/helpers/commonHooks.ts +9 -2
  13. package/src/tests/helpers/index.ts +7 -2
  14. package/src/types/index.ts +1 -0
  15. package/src/utils/validations/schema/README.md +93 -0
  16. package/src/utils/validations/schema/validator-schema.ts +103 -0
  17. package/dist/api/index.d.ts +0 -3
  18. package/dist/api/index.js +0 -12
  19. package/dist/api/v1/definition/index.d.ts +0 -3
  20. package/dist/api/v1/definition/index.js +0 -116
  21. package/dist/api/v1/definition/validations.d.ts +0 -2
  22. package/dist/api/v1/definition/validations.js +0 -77
  23. package/dist/api/v1/errors.d.ts +0 -4
  24. package/dist/api/v1/errors.js +0 -12
  25. package/dist/api/v1/index.d.ts +0 -3
  26. package/dist/api/v1/index.js +0 -11
  27. package/dist/errors/index.d.ts +0 -24
  28. package/dist/errors/index.js +0 -66
  29. package/dist/events/index.d.ts +0 -4
  30. package/dist/events/index.js +0 -50
  31. package/dist/hooks/create.d.ts +0 -10
  32. package/dist/hooks/create.js +0 -95
  33. package/dist/hooks/enrich.d.ts +0 -30
  34. package/dist/hooks/enrich.js +0 -159
  35. package/dist/hooks/find.d.ts +0 -1
  36. package/dist/hooks/find.js +0 -29
  37. package/dist/hooks/index.d.ts +0 -6
  38. package/dist/hooks/index.js +0 -18
  39. package/dist/hooks/update.d.ts +0 -10
  40. package/dist/hooks/update.js +0 -49
  41. package/dist/hooks/utils/updateInstanceValues.d.ts +0 -15
  42. package/dist/hooks/utils/updateInstanceValues.js +0 -50
  43. package/dist/hooks/workaround.d.ts +0 -10
  44. package/dist/hooks/workaround.js +0 -37
  45. package/dist/index.d.ts +0 -13
  46. package/dist/index.js +0 -67
  47. package/dist/models/CustomFieldDefinition.d.ts +0 -25
  48. package/dist/models/CustomFieldDefinition.js +0 -192
  49. package/dist/models/CustomFieldEntries.d.ts +0 -15
  50. package/dist/models/CustomFieldEntries.js +0 -123
  51. package/dist/models/CustomFieldValue.d.ts +0 -16
  52. package/dist/models/CustomFieldValue.js +0 -151
  53. package/dist/models/index.d.ts +0 -17
  54. package/dist/models/index.js +0 -113
  55. package/dist/models/tests/AssociatedTestModel.d.ts +0 -12
  56. package/dist/models/tests/AssociatedTestModel.js +0 -71
  57. package/dist/models/tests/TestModel.d.ts +0 -12
  58. package/dist/models/tests/TestModel.js +0 -69
  59. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +0 -10
  60. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +0 -53
  61. package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +0 -13
  62. package/dist/models/tests/contextAwareModels/ContextTestModel.js +0 -47
  63. package/dist/repository/definition.d.ts +0 -36
  64. package/dist/repository/definition.js +0 -121
  65. package/dist/repository/entries.d.ts +0 -13
  66. package/dist/repository/entries.js +0 -92
  67. package/dist/repository/utils/formatValues.d.ts +0 -3
  68. package/dist/repository/utils/formatValues.js +0 -16
  69. package/dist/repository/value.d.ts +0 -28
  70. package/dist/repository/value.js +0 -124
  71. package/dist/scopes/filter.d.ts +0 -30
  72. package/dist/scopes/filter.js +0 -75
  73. package/dist/scopes/helpers/filter.helpers.d.ts +0 -42
  74. package/dist/scopes/helpers/filter.helpers.js +0 -204
  75. package/dist/scopes/index.d.ts +0 -2
  76. package/dist/scopes/index.js +0 -6
  77. package/dist/tests/api/test-api.d.ts +0 -2
  78. package/dist/tests/api/test-api.js +0 -38
  79. package/dist/tests/functional/searching/index.d.ts +0 -8
  80. package/dist/tests/functional/searching/index.js +0 -44
  81. package/dist/tests/helpers/commonHooks.d.ts +0 -5
  82. package/dist/tests/helpers/commonHooks.js +0 -55
  83. package/dist/tests/helpers/database-config.d.ts +0 -16
  84. package/dist/tests/helpers/database-config.js +0 -17
  85. package/dist/tests/helpers/index.d.ts +0 -7
  86. package/dist/tests/helpers/index.js +0 -29
  87. package/dist/tests/mocks/definition.mock.d.ts +0 -48
  88. package/dist/tests/mocks/definition.mock.js +0 -78
  89. package/dist/tests/mocks/events.mock.d.ts +0 -4
  90. package/dist/tests/mocks/events.mock.js +0 -21
  91. package/dist/tests/mocks/testModel.d.ts +0 -12
  92. package/dist/tests/mocks/testModel.js +0 -35
  93. package/dist/types/definition/index.d.ts +0 -25
  94. package/dist/types/definition/index.js +0 -2
  95. package/dist/types/entries/index.d.ts +0 -25
  96. package/dist/types/entries/index.js +0 -2
  97. package/dist/types/index.d.ts +0 -34
  98. package/dist/types/index.js +0 -2
  99. package/dist/types/value/index.d.ts +0 -15
  100. package/dist/types/value/index.js +0 -2
  101. package/dist/utils/constants/index.d.ts +0 -19
  102. package/dist/utils/constants/index.js +0 -22
  103. package/dist/utils/db/index.d.ts +0 -4
  104. package/dist/utils/db/index.js +0 -24
  105. package/dist/utils/helpers/index.d.ts +0 -26
  106. package/dist/utils/helpers/index.js +0 -40
  107. package/dist/utils/init.d.ts +0 -7
  108. package/dist/utils/init.js +0 -109
  109. package/dist/utils/logger/index.d.ts +0 -3
  110. package/dist/utils/logger/index.js +0 -42
  111. package/dist/utils/scopeAttributes.d.ts +0 -2
  112. package/dist/utils/scopeAttributes.js +0 -11
  113. package/dist/utils/validations/index.d.ts +0 -8
  114. package/dist/utils/validations/index.js +0 -41
  115. package/dist/utils/validations/schema/custom-fields.d.ts +0 -3
  116. package/dist/utils/validations/schema/custom-fields.js +0 -9
  117. package/dist/utils/validations/type.d.ts +0 -15
  118. package/dist/utils/validations/type.js +0 -2
  119. package/dist/utils/validations/validators/index.d.ts +0 -14
  120. package/dist/utils/validations/validators/index.js +0 -40
  121. package/dist/utils/validations/validators/select.validator.d.ts +0 -5
  122. package/dist/utils/validations/validators/select.validator.js +0 -12
  123. package/dist/utils/validations/validators/status.validator.d.ts +0 -12
  124. package/dist/utils/validations/validators/status.validator.js +0 -15
@@ -1,113 +0,0 @@
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.ContextTestModel = exports.ContextAwareTestModel = exports.AssociatedTestModel = exports.TestModel = exports.CustomFieldEntries = 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 ContextAwareTestModel_1 = __importDefault(require("./tests/contextAwareModels/ContextAwareTestModel"));
17
- exports.ContextAwareTestModel = ContextAwareTestModel_1.default;
18
- const ContextTestModel_1 = __importDefault(require("./tests/contextAwareModels/ContextTestModel"));
19
- exports.ContextTestModel = ContextTestModel_1.default;
20
- const AssociatedTestModel_1 = __importDefault(require("./tests/AssociatedTestModel"));
21
- exports.AssociatedTestModel = AssociatedTestModel_1.default;
22
- const CustomFieldEntries_1 = __importDefault(require("./CustomFieldEntries"));
23
- exports.CustomFieldEntries = CustomFieldEntries_1.default;
24
- const productionModels = [CustomFieldDefinition_1.default, CustomFieldValue_1.default];
25
- const testModels = [TestModel_1.default, AssociatedTestModel_1.default, ContextAwareTestModel_1.default, ContextTestModel_1.default];
26
- const SADOT_MIGRATION_PREFIX = 'sadot-migration';
27
- const SCHEMA_VERSION = 'fb0fa867-1241-4816-b08d-5ed9060c7ae5';
28
- const initTables = async (sequelize, getUser, { schemaPrefix, schemaVersion, useCustomFieldsEntries, } = {
29
- schemaPrefix: SADOT_MIGRATION_PREFIX,
30
- schemaVersion: SCHEMA_VERSION,
31
- useCustomFieldsEntries: false,
32
- }) => {
33
- const CUSTOM_FIELDS_SCHEMA_VERSION = `${schemaPrefix}_${schemaVersion}${useCustomFieldsEntries ? '_withEntries' : ''}`;
34
- logger_1.default.info('custom-fields: initialize custom-fields tables');
35
- // Detect models and import them to the orm
36
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
37
- if (!sequelize.addModels) {
38
- throw new Error('sequelize instance must have addModels function');
39
- }
40
- if (useCustomFieldsEntries) {
41
- productionModels.push(CustomFieldEntries_1.default);
42
- }
43
- sequelize.addModels(productionModels);
44
- CustomFieldDefinition_1.default.addScope('userScope', () => {
45
- const user = getUser();
46
- if (!user?.permissions) {
47
- return {};
48
- }
49
- return {
50
- where: {
51
- entityId: [
52
- ...Object.keys(user.permissions.fleets),
53
- ...Object.keys(user.permissions.businessModels),
54
- ...Object.keys(user.permissions.demandSources),
55
- ],
56
- },
57
- };
58
- });
59
- logger_1.default.info('custom-fields: models added');
60
- const SequelizeMeta = sequelize.define('SequelizeMeta', {
61
- name: {
62
- type: sequelize_1.DataTypes.STRING,
63
- allowNull: false,
64
- unique: true,
65
- primaryKey: true,
66
- autoIncrement: false,
67
- },
68
- }, {
69
- tableName: 'SequelizeMeta',
70
- timestamps: false,
71
- schema: 'public',
72
- });
73
- const migrations = await SequelizeMeta.findAll({ where: { name: { [sequelize_1.Op.like]: `${schemaPrefix}%` } }, raw: true });
74
- const currentSadotSchemaVersion = migrations.at(-1);
75
- const expectedSchemaVersionIndex = migrations.findIndex((m) => m.name === CUSTOM_FIELDS_SCHEMA_VERSION);
76
- if (!currentSadotSchemaVersion || currentSadotSchemaVersion.name !== CUSTOM_FIELDS_SCHEMA_VERSION) {
77
- await CustomFieldDefinition_1.default.sync({ alter: true });
78
- await CustomFieldValue_1.default.sync({ alter: true });
79
- // T.Y TODO: Remove the if statement once we're ready to add the new entries table for all MS
80
- if (useCustomFieldsEntries) {
81
- await CustomFieldEntries_1.default.sync({ alter: true });
82
- }
83
- if (expectedSchemaVersionIndex === -1) {
84
- await SequelizeMeta.create({ name: CUSTOM_FIELDS_SCHEMA_VERSION });
85
- }
86
- logger_1.default.info('custom-fields: models synced');
87
- if (migrations.length && expectedSchemaVersionIndex !== -1 && expectedSchemaVersionIndex < migrations.length - 1) {
88
- // We have existing migrations, and we are calling `sync`.
89
- // This means we are in a `down` migration, and hence we should delete newer migrations to ensure we can reapply them.
90
- const migrationsToDelete = migrations.slice(expectedSchemaVersionIndex + 1);
91
- await SequelizeMeta.destroy({ where: { name: { [sequelize_1.Op.in]: migrationsToDelete.map((m) => m.name) } } });
92
- }
93
- }
94
- };
95
- exports.initTables = initTables;
96
- const initTestModels = async (sequelize) => {
97
- logger_1.default.info('custom-fields: initialize custom-fields test models');
98
- // Detect models and import them to the orm
99
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
100
- if (!sequelize.addModels) {
101
- throw new Error('sequelize instance must have addModels function');
102
- }
103
- sequelize.addModels(testModels);
104
- await sequelize.dropSchema('custom-fields', { logging: false });
105
- await sequelize.createSchema('custom-fields', { logging: false });
106
- logger_1.default.info('custom-fields: test models added');
107
- await TestModel_1.default.sync({ alter: true });
108
- await AssociatedTestModel_1.default.sync({ alter: true });
109
- await ContextTestModel_1.default.sync({ alter: true });
110
- await ContextAwareTestModel_1.default.sync({ alter: true });
111
- logger_1.default.info('custom-fields: test models synced');
112
- };
113
- exports.initTestModels = initTestModels;
@@ -1,12 +0,0 @@
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;
@@ -1,71 +0,0 @@
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;
@@ -1,12 +0,0 @@
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
- customFields?: any;
11
- }
12
- export default TestModel;
@@ -1,69 +0,0 @@
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
- __decorate([
61
- (0, sequelize_typescript_1.Column)({
62
- type: sequelize_typescript_1.DataType.VIRTUAL,
63
- }),
64
- __metadata("design:type", Object)
65
- ], TestModel.prototype, "customFields", void 0);
66
- TestModel = __decorate([
67
- (0, sequelize_typescript_1.Table)({ schema: 'custom-fields', createdAt: false, updatedAt: false })
68
- ], TestModel);
69
- exports.default = TestModel;
@@ -1,10 +0,0 @@
1
- import { Model } from 'sequelize-typescript';
2
- import ContextTestModel from './ContextTestModel';
3
- declare class ContextAwareTestModel extends Model {
4
- id: string;
5
- contextId: string;
6
- coolAttribute?: boolean;
7
- customFields?: any;
8
- context: ContextTestModel;
9
- }
10
- export default ContextAwareTestModel;
@@ -1,53 +0,0 @@
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 ContextTestModel_1 = __importDefault(require("./ContextTestModel"));
18
- let ContextAwareTestModel = class ContextAwareTestModel 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
- ], ContextAwareTestModel.prototype, "id", void 0);
29
- __decorate([
30
- (0, sequelize_typescript_1.ForeignKey)(() => ContextTestModel_1.default),
31
- (0, sequelize_typescript_1.Column)({ type: sequelize_typescript_1.DataType.UUID }),
32
- __metadata("design:type", String)
33
- ], ContextAwareTestModel.prototype, "contextId", void 0);
34
- __decorate([
35
- (0, sequelize_typescript_1.Column)({
36
- type: sequelize_typescript_1.DataType.BOOLEAN,
37
- }),
38
- __metadata("design:type", Boolean)
39
- ], ContextAwareTestModel.prototype, "coolAttribute", void 0);
40
- __decorate([
41
- (0, sequelize_typescript_1.Column)({
42
- type: sequelize_typescript_1.DataType.VIRTUAL,
43
- }),
44
- __metadata("design:type", Object)
45
- ], ContextAwareTestModel.prototype, "customFields", void 0);
46
- __decorate([
47
- (0, sequelize_typescript_1.BelongsTo)(() => ContextTestModel_1.default),
48
- __metadata("design:type", ContextTestModel_1.default)
49
- ], ContextAwareTestModel.prototype, "context", void 0);
50
- ContextAwareTestModel = __decorate([
51
- (0, sequelize_typescript_1.Table)({ createdAt: false, updatedAt: false })
52
- ], ContextAwareTestModel);
53
- exports.default = ContextAwareTestModel;
@@ -1,13 +0,0 @@
1
- import { Model } from 'sequelize-typescript';
2
- declare enum EEntityTypes {
3
- BUSINESS_MODEL = "businessModel",
4
- CONTEXT = "context",
5
- DEMAND_SOURCE = "demandSource",
6
- FLEET = "fleet"
7
- }
8
- declare class ContextTestModel extends Model {
9
- id: string;
10
- entityId: string;
11
- entityType: EEntityTypes;
12
- }
13
- export default ContextTestModel;
@@ -1,47 +0,0 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- /* eslint-disable @typescript-eslint/no-unused-vars */
13
- const sequelize_typescript_1 = require("sequelize-typescript");
14
- // eslint-disable-next-line no-shadow
15
- var EEntityTypes;
16
- (function (EEntityTypes) {
17
- EEntityTypes["BUSINESS_MODEL"] = "businessModel";
18
- EEntityTypes["CONTEXT"] = "context";
19
- EEntityTypes["DEMAND_SOURCE"] = "demandSource";
20
- EEntityTypes["FLEET"] = "fleet";
21
- })(EEntityTypes || (EEntityTypes = {}));
22
- let ContextTestModel = class ContextTestModel extends sequelize_typescript_1.Model {
23
- };
24
- __decorate([
25
- sequelize_typescript_1.PrimaryKey,
26
- (0, sequelize_typescript_1.Column)({
27
- defaultValue: sequelize_typescript_1.DataType.UUIDV4,
28
- type: sequelize_typescript_1.DataType.UUID,
29
- }),
30
- __metadata("design:type", String)
31
- ], ContextTestModel.prototype, "id", void 0);
32
- __decorate([
33
- (0, sequelize_typescript_1.Column)({
34
- type: sequelize_typescript_1.DataType.UUID,
35
- }),
36
- __metadata("design:type", String)
37
- ], ContextTestModel.prototype, "entityId", void 0);
38
- __decorate([
39
- (0, sequelize_typescript_1.Column)({
40
- type: sequelize_typescript_1.DataType.ENUM(...Object.values(EEntityTypes)),
41
- }),
42
- __metadata("design:type", String)
43
- ], ContextTestModel.prototype, "entityType", void 0);
44
- ContextTestModel = __decorate([
45
- (0, sequelize_typescript_1.Table)({ createdAt: false, updatedAt: false })
46
- ], ContextTestModel);
47
- exports.default = ContextTestModel;
@@ -1,36 +0,0 @@
1
- import { type Includeable, type Transaction, type FindOptions, type WhereOptions } from 'sequelize';
2
- import { CustomFieldDefinition, type CustomFieldEntries } from '../models';
3
- import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
4
- import type { ModelOptions } from '../types';
5
- export declare const create: (data: CreateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
6
- interface SadotFindOptions {
7
- withDisabled?: boolean;
8
- transaction?: Transaction;
9
- include?: Includeable | Includeable[];
10
- }
11
- type SadotGetDefinitionsByEntityIdsOptions = FindOptions & {
12
- modelOptions?: ModelOptions;
13
- } & Pick<SadotFindOptions, 'withDisabled'>;
14
- export declare const findAll: (where: WhereOptions, options?: SadotFindOptions) => Promise<CustomFieldDefinition[]>;
15
- export declare const findByIds: (ids: string[], options?: SadotFindOptions) => Promise<CustomFieldDefinition[]>;
16
- export declare const findById: (id: string, options?: Pick<SadotFindOptions, 'withDisabled'>) => Promise<CustomFieldDefinition | null>;
17
- export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: FindOptions & {
18
- modelOptions?: ModelOptions;
19
- }) => Promise<CustomFieldDefinition[]>;
20
- export declare const findByWhere: (where: any) => Promise<CustomFieldDefinition | null>;
21
- export declare const findDefinitionsByModels: (modelTypes: string[], options?: any) => Promise<CustomFieldDefinition[]>;
22
- export declare const update: (id: string, data: UpdateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
23
- export declare const disable: (id: string) => Promise<[affectedCount: number]>;
24
- export declare const destroy: (id: string) => Promise<number>;
25
- /**
26
- * Return the names of the required fields for a given model
27
- */
28
- export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[], modelOptions?: ModelOptions) => Promise<string[]>;
29
- /**
30
- * @returns A promise resolving with a dictionary of custom field definitions by name.
31
- * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.
32
- */
33
- export declare const getCustomFieldDefinitionsDictionary: (instances: CustomFieldEntries[], options?: SadotGetDefinitionsByEntityIdsOptions) => Promise<{
34
- [definitionName: string]: CustomFieldDefinition;
35
- }>;
36
- export {};
@@ -1,121 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCustomFieldDefinitionsDictionary = exports.getRequiredFields = exports.destroy = exports.disable = exports.update = exports.findDefinitionsByModels = exports.findByWhere = exports.findByEntityIds = exports.findById = exports.findByIds = exports.findAll = exports.create = void 0;
4
- const sequelize_1 = require("sequelize");
5
- const models_1 = require("../models");
6
- const errors_1 = require("../errors");
7
- const create = (data) => models_1.CustomFieldDefinition.create(data);
8
- exports.create = create;
9
- const findAll = (where, options = { withDisabled: false }) => {
10
- const queryModel = options.withDisabled
11
- ? models_1.CustomFieldDefinition.unscoped()
12
- : models_1.CustomFieldDefinition;
13
- return queryModel.scope('userScope').findAll({
14
- where,
15
- transaction: options.transaction,
16
- raw: true,
17
- include: options.include,
18
- });
19
- };
20
- exports.findAll = findAll;
21
- const findByIds = (ids, options = { withDisabled: false }) => (0, exports.findAll)({ id: { [sequelize_1.Op.in]: ids } }, options);
22
- exports.findByIds = findByIds;
23
- const findById = (id, options = { withDisabled: false }) => {
24
- const { withDisabled } = options;
25
- if (withDisabled) {
26
- return models_1.CustomFieldDefinition.unscoped().scope('userScope').findByPk(id);
27
- }
28
- return models_1.CustomFieldDefinition.scope('userScope').findByPk(id);
29
- };
30
- exports.findById = findById;
31
- const findByEntityIds = async (modelType, entityIds, options = {}) => {
32
- const { include, useEntityIdFromInclude } = options.modelOptions;
33
- const where = {
34
- modelType,
35
- ...(!useEntityIdFromInclude && { entityId: { [sequelize_1.Op.in]: entityIds } }),
36
- };
37
- return models_1.CustomFieldDefinition.findAll({
38
- where,
39
- transaction: options.transaction,
40
- include: include?.(entityIds),
41
- raw: true,
42
- });
43
- };
44
- exports.findByEntityIds = findByEntityIds;
45
- const findByWhere = (where) => models_1.CustomFieldDefinition.scope('userScope').findOne({
46
- where,
47
- });
48
- exports.findByWhere = findByWhere;
49
- const findDefinitionsByModels = async (modelTypes, options) => {
50
- const query = { modelType: { [sequelize_1.Op.in]: modelTypes } };
51
- return models_1.CustomFieldDefinition.findAll({
52
- where: query,
53
- transaction: options?.transaction,
54
- });
55
- };
56
- exports.findDefinitionsByModels = findDefinitionsByModels;
57
- const update = async (id, data) => {
58
- const updatedDefinition = (await models_1.CustomFieldDefinition.scope('userScope').update(data, {
59
- where: { id },
60
- returning: true,
61
- individualHooks: true,
62
- }))[1][0];
63
- return updatedDefinition;
64
- };
65
- exports.update = update;
66
- const disable = (id) => models_1.CustomFieldDefinition.update({ disabled: true }, { where: { id } });
67
- exports.disable = disable;
68
- const destroy = (id) => models_1.CustomFieldDefinition.destroy({ where: { id } });
69
- exports.destroy = destroy;
70
- /**
71
- * Return the names of the required fields for a given model
72
- */
73
- const getRequiredFields = async (modelType, modelId, entityId, modelOptions = {}) => {
74
- const entityIds = Array.isArray(entityId) ? entityId : [entityId];
75
- const { include, useEntityIdFromInclude } = modelOptions;
76
- const where = {
77
- modelType,
78
- required: true,
79
- ...(!useEntityIdFromInclude && { entityId: { [sequelize_1.Op.in]: entityIds } }),
80
- };
81
- const requiredFields = await models_1.CustomFieldDefinition.findAll({
82
- where,
83
- include: include?.(entityIds),
84
- logging: true,
85
- });
86
- const requiredFieldsNames = requiredFields.map((definition) => definition.name);
87
- return [...new Set(requiredFieldsNames)];
88
- };
89
- exports.getRequiredFields = getRequiredFields;
90
- /**
91
- * @returns A promise resolving with a dictionary of custom field definitions by name.
92
- * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.
93
- */
94
- const getCustomFieldDefinitionsDictionary = async (instances, options = { withDisabled: false, modelOptions: {} }) => {
95
- const { modelType } = instances[0]?.dataValues ?? {};
96
- const customFields = new Set();
97
- const modelIds = [];
98
- const entityIds = new Set();
99
- instances.forEach((instance) => {
100
- const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;
101
- modelIds.push(modelId);
102
- entityIds.add(entityId);
103
- Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {
104
- customFields.add(fieldName);
105
- });
106
- });
107
- const where = {
108
- modelType,
109
- entityId: { [sequelize_1.Op.in]: Array.from(entityIds) },
110
- name: { [sequelize_1.Op.in]: Array.from(customFields) },
111
- };
112
- const definitions = await (0, exports.findAll)(where, { ...options });
113
- const matchedDefinitions = definitions.filter((def) => customFields.has(def.name));
114
- const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map((definition) => [definition.name, definition]));
115
- if (!definitions?.length || matchedDefinitions.length !== customFields.size) {
116
- const unmatchedCustomFields = Array.from(customFields).filter((customField) => !matchedDefinitionsByName[customField]);
117
- throw new errors_1.MissingDefinitionError(unmatchedCustomFields);
118
- }
119
- return matchedDefinitionsByName;
120
- };
121
- exports.getCustomFieldDefinitionsDictionary = getCustomFieldDefinitionsDictionary;
@@ -1,13 +0,0 @@
1
- import type { FindOptions, Includeable, Transaction } from 'sequelize';
2
- import { CustomFieldEntries } from '../models';
3
- import type { ModelOptions } from '../types';
4
- type CustomFieldEntriesModelOptions = ModelOptions & {
5
- include?: Includeable;
6
- transaction?: Transaction;
7
- };
8
- export declare const findEntriesByModelId: (modelId: string, options?: CustomFieldEntriesModelOptions) => Promise<CustomFieldEntries>;
9
- export declare const findEntriesByModelIds: (modelIds: string[], options?: CustomFieldEntriesModelOptions) => Promise<CustomFieldEntries[]>;
10
- export declare const updateEntries: (modelId: string, modelType: string, customFields: Record<string, any>, identifiers: string[], options?: FindOptions & {
11
- modelOptions?: ModelOptions;
12
- }) => Promise<[CustomFieldEntries, boolean]>;
13
- export {};