@autofleet/sadot 0.6.1-temp-2 → 0.6.1

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 (94) hide show
  1. package/dist/api/index.d.ts +2 -0
  2. package/dist/api/index.js +11 -0
  3. package/dist/api/v1/definition/index.d.ts +2 -0
  4. package/dist/api/v1/definition/index.js +118 -0
  5. package/dist/api/v1/definition/validations.d.ts +2 -0
  6. package/dist/api/v1/definition/validations.js +36 -0
  7. package/dist/api/v1/errors.d.ts +2 -0
  8. package/dist/api/v1/errors.js +15 -0
  9. package/dist/api/v1/index.d.ts +2 -0
  10. package/dist/api/v1/index.js +10 -0
  11. package/dist/errors/index.d.ts +16 -0
  12. package/dist/errors/index.js +45 -0
  13. package/dist/events/index.d.ts +4 -0
  14. package/dist/events/index.js +47 -0
  15. package/dist/hooks/create.d.ts +10 -0
  16. package/dist/hooks/create.js +74 -0
  17. package/dist/hooks/enrich.d.ts +7 -0
  18. package/dist/hooks/enrich.js +126 -0
  19. package/dist/hooks/find.d.ts +1 -0
  20. package/dist/hooks/find.js +29 -0
  21. package/dist/hooks/index.d.ts +6 -0
  22. package/dist/hooks/index.js +18 -0
  23. package/dist/hooks/update.d.ts +10 -0
  24. package/dist/hooks/update.js +59 -0
  25. package/dist/hooks/workaround.d.ts +10 -0
  26. package/dist/hooks/workaround.js +37 -0
  27. package/dist/index.d.ts +14 -0
  28. package/dist/index.js +56 -0
  29. package/dist/models/CustomFieldDefinition.d.ts +23 -0
  30. package/dist/models/CustomFieldDefinition.js +165 -0
  31. package/dist/models/CustomFieldValue.d.ts +15 -0
  32. package/dist/models/CustomFieldValue.js +148 -0
  33. package/dist/models/index.d.ts +10 -0
  34. package/dist/models/index.js +93 -0
  35. package/dist/models/tests/AssociatedTestModel.d.ts +12 -0
  36. package/dist/models/tests/AssociatedTestModel.js +71 -0
  37. package/dist/models/tests/TestModel.d.ts +12 -0
  38. package/dist/models/tests/TestModel.js +69 -0
  39. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +10 -0
  40. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +55 -0
  41. package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +13 -0
  42. package/dist/models/tests/contextAwareModels/ContextTestModel.js +47 -0
  43. package/dist/repository/definition.d.ts +20 -0
  44. package/dist/repository/definition.js +92 -0
  45. package/dist/repository/value.d.ts +28 -0
  46. package/dist/repository/value.js +117 -0
  47. package/dist/scopes/filter.d.ts +23 -0
  48. package/dist/scopes/filter.js +55 -0
  49. package/dist/scopes/index.d.ts +2 -0
  50. package/dist/scopes/index.js +6 -0
  51. package/dist/tests/api/test-api.d.ts +2 -0
  52. package/dist/tests/api/test-api.js +56 -0
  53. package/dist/tests/functional/searching/index.d.ts +8 -0
  54. package/dist/tests/functional/searching/index.js +44 -0
  55. package/dist/tests/helpers/database-config.d.ts +16 -0
  56. package/dist/tests/helpers/database-config.js +17 -0
  57. package/dist/tests/helpers/index.d.ts +2 -0
  58. package/dist/tests/helpers/index.js +20 -0
  59. package/dist/tests/mocks/definition.mock.d.ts +43 -0
  60. package/dist/tests/mocks/definition.mock.js +70 -0
  61. package/dist/tests/mocks/events.mock.d.ts +3 -0
  62. package/dist/tests/mocks/events.mock.js +19 -0
  63. package/dist/tests/mocks/testModel.d.ts +12 -0
  64. package/dist/tests/mocks/testModel.js +35 -0
  65. package/dist/types/definition/index.d.ts +23 -0
  66. package/dist/types/definition/index.js +2 -0
  67. package/dist/types/index.d.ts +31 -0
  68. package/dist/types/index.js +2 -0
  69. package/dist/types/value/index.d.ts +15 -0
  70. package/dist/types/value/index.js +2 -0
  71. package/dist/utils/constants/index.d.ts +5 -0
  72. package/dist/utils/constants/index.js +8 -0
  73. package/dist/utils/db/index.d.ts +4 -0
  74. package/dist/utils/db/index.js +24 -0
  75. package/dist/utils/helpers/index.d.ts +25 -0
  76. package/dist/utils/helpers/index.js +34 -0
  77. package/dist/utils/init.d.ts +5 -0
  78. package/dist/utils/init.js +106 -0
  79. package/dist/utils/logger/index.d.ts +2 -0
  80. package/dist/utils/logger/index.js +6 -0
  81. package/dist/utils/scopeAttributes.d.ts +2 -0
  82. package/dist/utils/scopeAttributes.js +11 -0
  83. package/dist/utils/validations/custom-fields.d.ts +3 -0
  84. package/dist/utils/validations/custom-fields.js +10 -0
  85. package/dist/utils/validations/custom.d.ts +15 -0
  86. package/dist/utils/validations/custom.js +42 -0
  87. package/dist/utils/validations/index.d.ts +2 -0
  88. package/dist/utils/validations/index.js +20 -0
  89. package/dist/utils/validations/type.d.ts +18 -0
  90. package/dist/utils/validations/type.js +50 -0
  91. package/dist/utils/validations/validators.d.ts +12 -0
  92. package/dist/utils/validations/validators.js +33 -0
  93. package/package.json +2 -2
  94. package/src/utils/validations/type.ts +0 -10
@@ -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,12 @@
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;
@@ -0,0 +1,69 @@
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;
@@ -0,0 +1,10 @@
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;
@@ -0,0 +1,55 @@
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)({
32
+ type: sequelize_typescript_1.DataType.UUID,
33
+ }),
34
+ __metadata("design:type", String)
35
+ ], ContextAwareTestModel.prototype, "contextId", void 0);
36
+ __decorate([
37
+ (0, sequelize_typescript_1.Column)({
38
+ type: sequelize_typescript_1.DataType.BOOLEAN,
39
+ }),
40
+ __metadata("design:type", Boolean)
41
+ ], ContextAwareTestModel.prototype, "coolAttribute", void 0);
42
+ __decorate([
43
+ (0, sequelize_typescript_1.Column)({
44
+ type: sequelize_typescript_1.DataType.VIRTUAL,
45
+ }),
46
+ __metadata("design:type", Object)
47
+ ], ContextAwareTestModel.prototype, "customFields", void 0);
48
+ __decorate([
49
+ (0, sequelize_typescript_1.BelongsTo)(() => ContextTestModel_1.default),
50
+ __metadata("design:type", ContextTestModel_1.default)
51
+ ], ContextAwareTestModel.prototype, "context", void 0);
52
+ ContextAwareTestModel = __decorate([
53
+ (0, sequelize_typescript_1.Table)({ createdAt: false, updatedAt: false })
54
+ ], ContextAwareTestModel);
55
+ exports.default = ContextAwareTestModel;
@@ -0,0 +1,13 @@
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;
@@ -0,0 +1,47 @@
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;
@@ -0,0 +1,20 @@
1
+ import { FindOptions, WhereOptions } from 'sequelize';
2
+ import { CustomFieldDefinition } from '../models';
3
+ import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
4
+ import { ModelOptions } from '../types';
5
+ export declare const create: (data: CreateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
6
+ export declare const findAll: (where: WhereOptions, options?: any) => Promise<CustomFieldDefinition[]>;
7
+ export declare const findByIds: (ids: string[], options?: any) => Promise<CustomFieldDefinition[]>;
8
+ export declare const findById: (id: string, options?: any) => Promise<CustomFieldDefinition | null>;
9
+ export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: FindOptions & {
10
+ modelOptions?: ModelOptions;
11
+ }) => Promise<CustomFieldDefinition[]>;
12
+ export declare const findByWhere: (where: any) => Promise<CustomFieldDefinition | null>;
13
+ export declare const findDefinitionsByModels: (modelTypes: string[], options?: any) => Promise<CustomFieldDefinition[]>;
14
+ export declare const update: (id: string, data: UpdateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
15
+ export declare const disable: (id: string) => Promise<any>;
16
+ export declare const destroy: (id: string) => Promise<any>;
17
+ /**
18
+ * Return the names of the required fields for a given model
19
+ */
20
+ export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[], modelOptions?: ModelOptions) => Promise<string[]>;
@@ -0,0 +1,92 @@
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.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
+ include: options.include,
17
+ });
18
+ };
19
+ exports.findAll = findAll;
20
+ const findByIds = (ids, options = { withDisabled: false }) => (0, exports.findAll)({ id: { [sequelize_1.Op.in]: ids } }, options);
21
+ exports.findByIds = findByIds;
22
+ const findById = (id, options = { withDisabled: false }) => {
23
+ const { withDisabled } = options;
24
+ if (withDisabled) {
25
+ return models_1.CustomFieldDefinition.unscoped().scope('userScope').findByPk(id);
26
+ }
27
+ return models_1.CustomFieldDefinition.scope('userScope').findByPk(id);
28
+ };
29
+ exports.findById = findById;
30
+ const findByEntityIds = async (modelType, entityIds, options = {}) => {
31
+ const { include, useEntityIdFromInclude } = options.modelOptions;
32
+ const where = {
33
+ modelType,
34
+ };
35
+ if (!useEntityIdFromInclude) {
36
+ where.entityId = entityIds;
37
+ }
38
+ return models_1.CustomFieldDefinition.findAll({
39
+ where,
40
+ transaction: options.transaction,
41
+ include: include?.(entityIds),
42
+ raw: true,
43
+ });
44
+ };
45
+ exports.findByEntityIds = findByEntityIds;
46
+ const findByWhere = (where) => models_1.CustomFieldDefinition.scope('userScope').findOne({
47
+ where,
48
+ });
49
+ exports.findByWhere = findByWhere;
50
+ const findDefinitionsByModels = async (modelTypes, options) => {
51
+ const query = { modelType: { [sequelize_1.Op.in]: modelTypes } };
52
+ return models_1.CustomFieldDefinition.findAll({
53
+ where: query,
54
+ transaction: options?.transaction,
55
+ });
56
+ };
57
+ exports.findDefinitionsByModels = findDefinitionsByModels;
58
+ const update = async (id, data) => {
59
+ const updatedDefinition = (await models_1.CustomFieldDefinition.scope('userScope').update(data, {
60
+ where: { id },
61
+ returning: true,
62
+ individualHooks: true,
63
+ }))[1][0];
64
+ return updatedDefinition;
65
+ };
66
+ exports.update = update;
67
+ const disable = (id) => models_1.CustomFieldDefinition.update({ disabled: true }, { where: { id } });
68
+ exports.disable = disable;
69
+ const destroy = (id) => models_1.CustomFieldDefinition.destroy({ where: { id } });
70
+ exports.destroy = destroy;
71
+ /**
72
+ * Return the names of the required fields for a given model
73
+ */
74
+ const getRequiredFields = async (modelType, modelId, entityId, modelOptions = {}) => {
75
+ const entityIds = Array.isArray(entityId) ? entityId : [entityId];
76
+ const { include, useEntityIdFromInclude } = modelOptions;
77
+ const where = {
78
+ modelType,
79
+ required: true,
80
+ };
81
+ if (!useEntityIdFromInclude) {
82
+ where.entityId = entityIds;
83
+ }
84
+ const requiredFields = await models_1.CustomFieldDefinition.findAll({
85
+ where,
86
+ include: include?.(entityIds),
87
+ logging: true,
88
+ });
89
+ const requiredFieldsNames = requiredFields.map((definition) => definition.name);
90
+ return [...new Set(requiredFieldsNames)];
91
+ };
92
+ exports.getRequiredFields = getRequiredFields;
@@ -0,0 +1,28 @@
1
+ import type { FindOptions } from 'sequelize';
2
+ import { CustomFieldValue } from '../models';
3
+ import { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
4
+ import { ModelOptions } from '../types';
5
+ export declare const findByModelIdAndDefinition: (modelId: string, customFieldDefinitionId: string) => Promise<CustomFieldValue[]>;
6
+ export declare const create: (data: CreateCustomFieldValue, withAssociations?: boolean) => Promise<CustomFieldValue>;
7
+ export declare const findAllValues: () => Promise<CustomFieldValue[]>;
8
+ /**
9
+ * Get all values for model instance id (with their definitions)
10
+ * @param modelId
11
+ * @returns CustomFieldValue[]
12
+ */
13
+ export declare const findValuesByModelId: (modelId: string) => Promise<CustomFieldValue[]>;
14
+ /**
15
+ * Retrieves custom field values for given model IDs
16
+ * @param modelIds - An array of model IDs to query custom field values for.
17
+ * @param options - Optional configuration object.
18
+ */
19
+ export declare const findValuesByModelIds: (modelIds: string[], options?: any) => Promise<CustomFieldValue[]>;
20
+ /**
21
+ * Try to update custom field values for a model instance.
22
+ * Create new value record if not exists, but fails if value's definition not exist.
23
+ * Return the updated values
24
+ */
25
+ export declare const updateValues: (modelType: string, modelId: string, identifiers: string[], valuesToUpdate: ValuesToUpdate, options?: FindOptions & {
26
+ modelOptions?: ModelOptions;
27
+ }) => Promise<CustomFieldValue[]>;
28
+ export declare const deleteValue: (id: string, options?: any) => Promise<any>;
@@ -0,0 +1,117 @@
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
+ const models_1 = require("../models");
31
+ const DefinitionRepo = __importStar(require("./definition"));
32
+ const logger_1 = __importDefault(require("../utils/logger"));
33
+ const errors_1 = require("../errors");
34
+ const findByModelIdAndDefinition = async (modelId, customFieldDefinitionId) => models_1.CustomFieldValue.findAll({ where: { modelId, customFieldDefinitionId }, include: [models_1.CustomFieldDefinition] });
35
+ exports.findByModelIdAndDefinition = findByModelIdAndDefinition;
36
+ const create = async (data, withAssociations = false) => {
37
+ const created = await models_1.CustomFieldValue.create(data);
38
+ if (withAssociations) {
39
+ const createdWithAssociations = await (0, exports.findByModelIdAndDefinition)(created.modelId, created.customFieldDefinitionId);
40
+ return createdWithAssociations?.[0];
41
+ }
42
+ return created;
43
+ };
44
+ exports.create = create;
45
+ const findAllValues = async () => models_1.CustomFieldValue.findAll({ include: [models_1.CustomFieldDefinition] });
46
+ exports.findAllValues = findAllValues;
47
+ /**
48
+ * Get all values for model instance id (with their definitions)
49
+ * @param modelId
50
+ * @returns CustomFieldValue[]
51
+ */
52
+ const findValuesByModelId = async (modelId) => models_1.CustomFieldValue.findAll({ where: { modelId }, include: [models_1.CustomFieldDefinition] });
53
+ exports.findValuesByModelId = findValuesByModelId;
54
+ /**
55
+ * Retrieves custom field values for given model IDs
56
+ * @param modelIds - An array of model IDs to query custom field values for.
57
+ * @param options - Optional configuration object.
58
+ */
59
+ const findValuesByModelIds = async (modelIds, options) => {
60
+ const { transaction } = options;
61
+ return models_1.CustomFieldValue.findAll({
62
+ where: { modelId: modelIds },
63
+ transaction,
64
+ raw: true,
65
+ nest: true,
66
+ });
67
+ };
68
+ exports.findValuesByModelIds = findValuesByModelIds;
69
+ /**
70
+ * Try to update custom field values for a model instance.
71
+ * Create new value record if not exists, but fails if value's definition not exist.
72
+ * Return the updated values
73
+ */
74
+ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}) => {
75
+ const names = Object.keys(valuesToUpdate);
76
+ logger_1.default.info(`custom-fields: updating values for ${modelType} ${modelId}`, {
77
+ names, options, valuesToUpdate, identifiers,
78
+ });
79
+ const { modelOptions, transaction } = options;
80
+ const where = {
81
+ modelType,
82
+ name: names,
83
+ };
84
+ if (!options.modelOptions?.useEntityIdFromInclude) {
85
+ where.entityId = identifiers;
86
+ }
87
+ const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) || [];
88
+ const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
89
+ if (fieldDefinitions.length !== names.length) {
90
+ logger_1.default.info(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
91
+ const missingDefinitions = names.filter((name) => !fieldDefinitions.some((def) => def.name === name));
92
+ throw new errors_1.MissingDefinitionError(missingDefinitions);
93
+ }
94
+ const disabledNames = disabledDefinitions?.map((def) => def.name) || [];
95
+ const valuesWithDisabledDefinitions = names.filter((name) => disabledNames.includes(name));
96
+ if (valuesWithDisabledDefinitions?.length > 0) {
97
+ logger_1.default.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
98
+ }
99
+ const values = names.map((name) => ({
100
+ modelId,
101
+ value: valuesToUpdate[name],
102
+ updatedAt: new Date(),
103
+ customFieldDefinitionId: fieldDefinitions.find((def) => def.name === name).id,
104
+ }));
105
+ return Promise.all(values.map(async (value) => {
106
+ const [cfv] = await models_1.CustomFieldValue.upsert(value, {
107
+ transaction: options.transaction,
108
+ });
109
+ return cfv;
110
+ }));
111
+ };
112
+ exports.updateValues = updateValues;
113
+ const deleteValue = (id, options = {}) => models_1.CustomFieldValue.update({ deletedAt: new Date() }, {
114
+ where: { id },
115
+ transaction: options.transaction,
116
+ });
117
+ exports.deleteValue = deleteValue;
@@ -0,0 +1,23 @@
1
+ import { WhereOptions } from 'sequelize';
2
+ /**
3
+ * Type representing possible condition values.
4
+ * Currently supporting strings and arrays of strings.
5
+ * More types to be added (TBA).
6
+ */
7
+ export type ConditionValue = string | string[];
8
+ export type CustomFieldSort = {
9
+ field: string;
10
+ direction: 'ASC' | 'DESC';
11
+ };
12
+ export type CustomFieldFilterOptions = {
13
+ where?: WhereOptions;
14
+ };
15
+ /**
16
+ * A Sequelize scope for filtering models by custom fields.
17
+ * This scope builds a WHERE clause to be applied on the main query.
18
+ *
19
+ * @param {string} name - The model type name used to join custom_field_definitions.
20
+ * @returns {Function} - A function that takes conditions and returns the Sequelize options object.
21
+ */
22
+ export declare const customFieldsFilterScope: (name: string) => (conditions: Record<string, ConditionValue>) => CustomFieldFilterOptions;
23
+ export declare const scopeName = "filterByCustomFields";
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scopeName = exports.customFieldsFilterScope = void 0;
4
+ /* eslint-disable import/prefer-default-export */
5
+ const sequelize_1 = require("sequelize");
6
+ const sequelize_typescript_1 = require("sequelize-typescript");
7
+ const common_types_1 = require("@autofleet/common-types");
8
+ const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
9
+ /**
10
+ * A Sequelize scope for filtering models by custom fields.
11
+ * This scope builds a WHERE clause to be applied on the main query.
12
+ *
13
+ * @param {string} name - The model type name used to join custom_field_definitions.
14
+ * @returns {Function} - A function that takes conditions and returns the Sequelize options object.
15
+ */
16
+ const customFieldsFilterScope = (name) => (conditions) => {
17
+ if (!conditions || Object.keys(conditions).length === 0) {
18
+ return {};
19
+ }
20
+ // Build the WHERE clause for custom field filtering
21
+ const conditionsStrings = Object.entries(conditions)
22
+ .map(([key, value]) => {
23
+ if (Array.isArray(value)) {
24
+ if (value.length === 0) {
25
+ // if empty array, the condition is ignored
26
+ return false;
27
+ }
28
+ const values = value.map((v) => `'${v}'`).join(',');
29
+ return `custom_fields->>'${key}' IN (${values})`;
30
+ }
31
+ return `custom_fields->>'${key}' = '${value}'`;
32
+ })
33
+ .filter(Boolean);
34
+ if (conditionsStrings.length === 0) {
35
+ return {};
36
+ }
37
+ const customFieldConditions = conditionsStrings.join(' AND ');
38
+ const subQuery = `${'SELECT model_id FROM ('
39
+ + 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
40
+ + 'FROM custom_field_values AS cv '
41
+ + 'INNER JOIN custom_field_definitions AS cd ON cv.custom_field_definition_id = cd.id '
42
+ + `AND cd.model_type = '${name}' `
43
+ + 'GROUP BY cv.model_id'
44
+ + ') AS CustomFieldAggregation '
45
+ + 'WHERE '}${customFieldConditions}`;
46
+ return {
47
+ where: {
48
+ id: {
49
+ [sequelize_1.Op.in]: sequelize_typescript_1.Sequelize.literal(`(${subQuery})`),
50
+ },
51
+ },
52
+ };
53
+ };
54
+ exports.customFieldsFilterScope = customFieldsFilterScope;
55
+ exports.scopeName = CUSTOM_FIELDS_FILTER_SCOPE;
@@ -0,0 +1,2 @@
1
+ import { customFieldsFilterScope } from './filter';
2
+ export { customFieldsFilterScope, };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.customFieldsFilterScope = void 0;
4
+ /* eslint-disable import/prefer-default-export */
5
+ const filter_1 = require("./filter");
6
+ Object.defineProperty(exports, "customFieldsFilterScope", { enumerable: true, get: function () { return filter_1.customFieldsFilterScope; } });
@@ -0,0 +1,2 @@
1
+ declare const app: import("express-serve-static-core").Express;
2
+ export default app;