@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,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,8 @@
1
+ interface CustomFieldsSearchTestFlowInput {
2
+ fieldType: string;
3
+ fieldValue: string | number;
4
+ searchTerm: string;
5
+ expectedNumberOfQueryResults: number;
6
+ }
7
+ declare const customFieldsSearchTestFlow: ({ fieldType, fieldValue, searchTerm, expectedNumberOfQueryResults, }: CustomFieldsSearchTestFlowInput) => Promise<void>;
8
+ export default customFieldsSearchTestFlow;
@@ -0,0 +1,44 @@
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 index_1 = require("../../../index");
27
+ const DefinitionRepo = __importStar(require("../../../repository/definition"));
28
+ const definition_mock_1 = require("../../mocks/definition.mock");
29
+ const testModel_1 = require("../../mocks/testModel");
30
+ const models_1 = require("../../../models");
31
+ const customFieldsSearchTestFlow = async ({ fieldType, fieldValue, searchTerm, expectedNumberOfQueryResults, }) => {
32
+ const definition = (0, definition_mock_1.createDefinition)({ fieldType, name: 'coolDefinition' });
33
+ await DefinitionRepo.create({ ...definition });
34
+ const [testModel1] = await (0, testModel_1.createTestModels)(definition.entityId, 2);
35
+ await testModel1.update({ customFields: { [definition.name]: fieldValue } });
36
+ const models = await models_1.TestModel.findAndCountAll({
37
+ ...(0, index_1.generateCustomFieldSearchQueryPayload)(searchTerm, models_1.TestModel, definition.entityId),
38
+ });
39
+ expect(models.count).toBe(expectedNumberOfQueryResults);
40
+ if (expectedNumberOfQueryResults > 0) {
41
+ expect(models.rows[0].dataValues.id).toBe(testModel1.id);
42
+ }
43
+ };
44
+ exports.default = customFieldsSearchTestFlow;
@@ -0,0 +1,16 @@
1
+ declare const _default: {
2
+ test: {
3
+ username: string;
4
+ password: string;
5
+ database: string;
6
+ host: string;
7
+ port: string | number;
8
+ dialect: string;
9
+ define: {
10
+ underscored: boolean;
11
+ underscoredAll: boolean;
12
+ };
13
+ logging: boolean;
14
+ };
15
+ };
16
+ export default _default;
@@ -0,0 +1,17 @@
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
+ port: process.env.DB_PORT || 5432,
10
+ dialect: process.env.DB_TYPE || 'postgres',
11
+ define: {
12
+ underscored: true,
13
+ underscoredAll: true,
14
+ },
15
+ logging: false,
16
+ },
17
+ };
@@ -0,0 +1,2 @@
1
+ export declare const cleanup: () => Promise<void>;
2
+ export declare const getModel: (name: string) => any;
@@ -0,0 +1,20 @@
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
+ await models_1.ContextAwareTestModel.destroy({ where: {} });
11
+ await models_1.ContextTestModel.destroy({ where: {} });
12
+ }
13
+ };
14
+ exports.cleanup = cleanup;
15
+ const getModel = (name) => {
16
+ // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
17
+ const models = require('../../models');
18
+ return models[name];
19
+ };
20
+ exports.getModel = getModel;
@@ -0,0 +1,43 @@
1
+ import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
2
+ export declare const contextAwareFieldDefinition: {
3
+ name: string;
4
+ modelType: string;
5
+ fieldType: string;
6
+ entityType: string;
7
+ };
8
+ export declare const coolFieldDefinition: CreateCustomFieldDefinition;
9
+ export declare const coolFieldDefinition2: {
10
+ name: string;
11
+ required?: boolean;
12
+ disabled?: boolean;
13
+ description?: string;
14
+ createdAt?: Date;
15
+ updatedAt?: Date;
16
+ deletedAt?: Date;
17
+ displayName?: string;
18
+ validation?: any;
19
+ fieldType: string;
20
+ entityId: string;
21
+ entityType: string;
22
+ modelType: string;
23
+ };
24
+ export declare const coolFieldDefinition3: {
25
+ name: string;
26
+ required?: boolean;
27
+ disabled?: boolean;
28
+ description?: string;
29
+ createdAt?: Date;
30
+ updatedAt?: Date;
31
+ deletedAt?: Date;
32
+ displayName?: string;
33
+ validation?: any;
34
+ fieldType: string;
35
+ entityId: string;
36
+ entityType: string;
37
+ modelType: string;
38
+ };
39
+ export declare const booleanField: (modelType: string) => CreateCustomFieldDefinition;
40
+ export declare const enumField: (modelType: string, options: any) => CreateCustomFieldDefinition;
41
+ export declare const rangeField: (modelType: string) => CreateCustomFieldDefinition;
42
+ export declare const createDefinition: (defaults: Partial<CustomFieldDefinitionDTO>) => CreateCustomFieldDefinition;
43
+ export declare const createDefinitions: (defaults: Partial<CustomFieldDefinitionDTO>, length?: number) => CreateCustomFieldDefinition[];
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDefinitions = exports.createDefinition = exports.rangeField = exports.enumField = exports.booleanField = exports.coolFieldDefinition3 = exports.coolFieldDefinition2 = exports.coolFieldDefinition = exports.contextAwareFieldDefinition = void 0;
4
+ const uuid_1 = require("uuid");
5
+ exports.contextAwareFieldDefinition = {
6
+ name: 'cool field',
7
+ modelType: 'ContextAwareTestModel',
8
+ fieldType: 'number',
9
+ entityType: 'fleetId',
10
+ };
11
+ exports.coolFieldDefinition = {
12
+ name: 'cool field',
13
+ modelType: 'TestModel',
14
+ fieldType: 'number',
15
+ entityId: (0, uuid_1.v4)(),
16
+ entityType: 'fleetId',
17
+ };
18
+ exports.coolFieldDefinition2 = {
19
+ ...exports.coolFieldDefinition,
20
+ name: 'cool field2',
21
+ };
22
+ exports.coolFieldDefinition3 = {
23
+ ...exports.coolFieldDefinition,
24
+ name: 'cool field3',
25
+ };
26
+ const booleanField = (modelType) => ({
27
+ name: 'shapeless',
28
+ modelType,
29
+ fieldType: 'boolean',
30
+ entityId: (0, uuid_1.v4)(),
31
+ entityType: 'fleetId',
32
+ });
33
+ exports.booleanField = booleanField;
34
+ const enumField = (modelType, options) => ({
35
+ name: 'choices',
36
+ modelType,
37
+ fieldType: 'select',
38
+ validation: options,
39
+ entityId: (0, uuid_1.v4)(),
40
+ entityType: 'fleetId',
41
+ });
42
+ exports.enumField = enumField;
43
+ const rangeField = (modelType) => ({
44
+ name: 'ranges',
45
+ modelType,
46
+ fieldType: 'number',
47
+ validation: {
48
+ between: [10, 12],
49
+ },
50
+ entityId: (0, uuid_1.v4)(),
51
+ entityType: 'fleetId',
52
+ });
53
+ exports.rangeField = rangeField;
54
+ // eslint-disable-next-line max-len
55
+ const createDefinition = (defaults) => ({
56
+ name: defaults?.name || `def_${(0, uuid_1.v4)()}`,
57
+ modelType: defaults?.modelType || 'TestModel',
58
+ fieldType: defaults?.fieldType || 'boolean',
59
+ entityId: defaults?.entityId || (0, uuid_1.v4)(),
60
+ entityType: defaults?.entityType || 'fleetId',
61
+ });
62
+ exports.createDefinition = createDefinition;
63
+ const createDefinitions = (defaults, length = 1) => (Array(length).fill({}).map((_) => ({
64
+ name: defaults?.name || `def_${(0, uuid_1.v4)()}`,
65
+ modelType: defaults?.modelType || 'TestModel',
66
+ fieldType: defaults?.fieldType || 'boolean',
67
+ entityId: defaults?.entityId || (0, uuid_1.v4)(),
68
+ entityType: defaults?.entityType || 'fleetId',
69
+ })));
70
+ exports.createDefinitions = createDefinitions;
@@ -0,0 +1,3 @@
1
+ export declare const mockEvent: (events: any, eventName: any, numberOfEvents: any) => any[];
2
+ export declare const mockDimCustomFieldDefinitionEvent: (events: any, numberOfEvents: any) => any[];
3
+ export declare const mockDimCustomFieldValueEvent: (events: any, numberOfEvents: any) => any[];
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ /* eslint-disable no-param-reassign */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.mockDimCustomFieldValueEvent = exports.mockDimCustomFieldDefinitionEvent = exports.mockEvent = void 0;
5
+ const mockEvent = (events, eventName, numberOfEvents) => {
6
+ events.sendObject = jest.fn();
7
+ return [
8
+ events.sendObject,
9
+ () => {
10
+ const matchingEvents = events.sendObject.mock.calls.filter((call) => call[0] === eventName);
11
+ expect(matchingEvents.length).toEqual(numberOfEvents);
12
+ },
13
+ ];
14
+ };
15
+ exports.mockEvent = mockEvent;
16
+ const mockDimCustomFieldDefinitionEvent = (events, numberOfEvents) => (0, exports.mockEvent)(events, 'dim_custom_field_definition', numberOfEvents);
17
+ exports.mockDimCustomFieldDefinitionEvent = mockDimCustomFieldDefinitionEvent;
18
+ const mockDimCustomFieldValueEvent = (events, numberOfEvents) => (0, exports.mockEvent)(events, 'dim_custom_field_value', numberOfEvents);
19
+ exports.mockDimCustomFieldValueEvent = mockDimCustomFieldValueEvent;
@@ -0,0 +1,12 @@
1
+ import { TestModel } from '../../models';
2
+ export declare const createTestModel: (payload?: {}) => Promise<TestModel>;
3
+ export declare const createTestModels: (fleetId: any, total: number) => Promise<TestModel[]>;
4
+ export declare const upsertTestModel: (payload: any) => Promise<[TestModel, boolean]>;
5
+ export declare const destroyTestModels: () => Promise<number>;
6
+ export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel | null>;
7
+ export declare const getSomeTestModels: (options?: {
8
+ limit: number;
9
+ }) => Promise<TestModel[]>;
10
+ export declare const updateTestModel: (payload: any, query: any) => Promise<[affectedCount: number, affectedRows: TestModel[]]>;
11
+ export declare const createTestModelWithAssociation: () => Promise<TestModel>;
12
+ export declare const getTestModelWithAssociation: (limit?: number) => Promise<TestModel[]>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTestModelWithAssociation = exports.createTestModelWithAssociation = exports.updateTestModel = exports.getSomeTestModels = exports.getTestModel = exports.destroyTestModels = exports.upsertTestModel = exports.createTestModels = exports.createTestModel = void 0;
4
+ const models_1 = require("../../models");
5
+ const createTestModel = (payload = {}) => models_1.TestModel.create(payload);
6
+ exports.createTestModel = createTestModel;
7
+ const createTestModels = (fleetId, total) => {
8
+ const models = [];
9
+ for (let i = 0; i < total; i += 1) {
10
+ models.push((0, exports.createTestModel)({ fleetId }));
11
+ }
12
+ return Promise.all(models);
13
+ };
14
+ exports.createTestModels = createTestModels;
15
+ const upsertTestModel = (payload) => models_1.TestModel.upsert(payload);
16
+ exports.upsertTestModel = upsertTestModel;
17
+ const destroyTestModels = () => models_1.TestModel.destroy({ truncate: true });
18
+ exports.destroyTestModels = destroyTestModels;
19
+ const getTestModel = (id, options = {}) => models_1.TestModel.findByPk(id, options);
20
+ exports.getTestModel = getTestModel;
21
+ const getSomeTestModels = (options = { limit: 1 }) => models_1.TestModel.findAll(options);
22
+ exports.getSomeTestModels = getSomeTestModels;
23
+ const updateTestModel = (payload, query) => models_1.TestModel.update(payload, query);
24
+ exports.updateTestModel = updateTestModel;
25
+ // Associations
26
+ const createTestModelWithAssociation = async () => {
27
+ const model = await models_1.TestModel.create({});
28
+ await models_1.AssociatedTestModel.create({ testModelId: model.id });
29
+ return model;
30
+ };
31
+ exports.createTestModelWithAssociation = createTestModelWithAssociation;
32
+ const getTestModelWithAssociation = (limit = 1) => models_1.TestModel.findAll({
33
+ limit, include: [models_1.AssociatedTestModel],
34
+ });
35
+ exports.getTestModelWithAssociation = getTestModelWithAssociation;
@@ -0,0 +1,23 @@
1
+ export interface CustomFieldDefinitionDTO {
2
+ id: string;
3
+ name: string;
4
+ displayName?: string;
5
+ validation?: any;
6
+ fieldType: string;
7
+ entityId: string;
8
+ entityType: string;
9
+ modelType: string;
10
+ required?: boolean;
11
+ description?: string;
12
+ disabled?: boolean;
13
+ createdAt?: Date;
14
+ updatedAt?: Date;
15
+ deletedAt?: Date;
16
+ }
17
+ export type CreateCustomFieldDefinition = Omit<CustomFieldDefinitionDTO, 'id'>;
18
+ export type UpdateCustomFieldDefinition = Partial<CreateCustomFieldDefinition>;
19
+ export type SerializedCustomFields = {
20
+ [name: string]: CustomFieldDefinitionDTO & {
21
+ value: any;
22
+ };
23
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,31 @@
1
+ import { IncludeOptions } from 'sequelize';
2
+ import { ModelCtor } from 'sequelize-typescript';
3
+ export type ModelFetcher = (name: string) => any;
4
+ export type ModelOptions = {
5
+ /**
6
+ * Include options for the model
7
+ */
8
+ include?: (entityIds: string[]) => IncludeOptions[];
9
+ /**
10
+ * Custom association for the model
11
+ * @param model - The model to associate with
12
+ */
13
+ customAssociation?: (model: ModelCtor) => void;
14
+ /**
15
+ * Whether to use the entity id from the instance per scope attribute
16
+ */
17
+ useEntityIdFromInclude?: boolean;
18
+ };
19
+ export type Models = {
20
+ name: string;
21
+ scopeAttributes: any[];
22
+ modelOptions?: ModelOptions;
23
+ creationWebhookHandler?: (instance: any) => any;
24
+ updateWebhookHandler?: (instance: any) => any;
25
+ deletionWebhookHandler?: (instance: any) => any;
26
+ };
27
+ export type CustomFieldOptions = {
28
+ models: Models[];
29
+ databaseConfig: any;
30
+ getUser: () => any;
31
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,15 @@
1
+ export interface CustomFieldValueDTO {
2
+ id?: string;
3
+ modelId: string;
4
+ customFieldDefinitionId: string;
5
+ value: any;
6
+ createdAt?: Date;
7
+ updatedAt?: Date;
8
+ deletedAt?: Date;
9
+ }
10
+ export type ValuesToUpdate = {
11
+ [name: string]: any;
12
+ };
13
+ export type CreateCustomFieldValue = Omit<CustomFieldValueDTO, 'id'>;
14
+ export type UpdateCustomFieldValue = ValuesToUpdate;
15
+ export type BulkUpdateCustomFieldValue = Partial<CustomFieldValueDTO>[];
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ declare const CUSTOM_FIELDS_FILTER_SCOPE: string;
2
+ export declare const supportedEntities: string[];
3
+ export {
4
+ /** @deprecated Use the value from `@autofleet/common-types` instead */
5
+ CUSTOM_FIELDS_FILTER_SCOPE, };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CUSTOM_FIELDS_FILTER_SCOPE = exports.supportedEntities = void 0;
4
+ const common_types_1 = require("@autofleet/common-types");
5
+ const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
6
+ exports.CUSTOM_FIELDS_FILTER_SCOPE = CUSTOM_FIELDS_FILTER_SCOPE;
7
+ // eslint-disable-next-line import/prefer-default-export
8
+ exports.supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
@@ -0,0 +1,4 @@
1
+ import { Sequelize } from 'sequelize-typescript';
2
+ declare const _default: (databaseConfig: any) => Sequelize;
3
+ export default _default;
4
+ export declare const createSequelizeMeta: (sequelize: Sequelize) => Promise<object[]>;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSequelizeMeta = void 0;
4
+ const sequelize_typescript_1 = require("sequelize-typescript");
5
+ const sequelize_1 = require("sequelize");
6
+ exports.default = (databaseConfig) => {
7
+ const ENV_DEV = 'test';
8
+ const env = process.env.NODE_ENV || ENV_DEV;
9
+ const config = databaseConfig[env];
10
+ let sequelize;
11
+ if (config.use_env_variable) {
12
+ sequelize = new sequelize_typescript_1.Sequelize(process.env[config.use_env_variable], config);
13
+ }
14
+ else {
15
+ sequelize = new sequelize_typescript_1.Sequelize(config.database, config.username, config.password, config);
16
+ }
17
+ return sequelize;
18
+ };
19
+ const createSequelizeMeta = (sequelize) => sequelize.query(`
20
+ CREATE TABLE IF NOT EXISTS "SequelizeMeta" (
21
+ name character varying(255) PRIMARY KEY
22
+ );
23
+ `, { type: sequelize_1.QueryTypes.SELECT });
24
+ exports.createSequelizeMeta = createSequelizeMeta;
@@ -0,0 +1,25 @@
1
+ import { WhereOptions, BindOrReplacements } from 'sequelize';
2
+ import { ModelStatic } from 'sequelize-typescript';
3
+ import { CustomFieldDefinitionType } from '../validations/type';
4
+ /**
5
+ * Builds a WHERE clause and replacements for free-text search by custom fields.
6
+ *
7
+ * This function constructs a WHERE clause and replacement bindings that allow searching
8
+ * for a given term within custom fields associated with a specific model type and entity ID.
9
+ * The WHERE clause and replacements are designed to be added to the main query.
10
+ *
11
+ * @param {string} searchTerm - The term to search for within custom fields.
12
+ * @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
13
+ * @param {string} entityId - The entity ID to filter the custom fields by.
14
+ * @param {CustomFieldDefinitionType[]} excludedCustomFieldsTypes - An array of custom field types
15
+ * to exclude from the search
16
+ *
17
+ * @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
18
+ * for Sequelize.
19
+ */
20
+ interface CustomFieldsSearchPayload {
21
+ where: WhereOptions;
22
+ replacements: BindOrReplacements;
23
+ }
24
+ export declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string, customFieldsTypesToExclude?: CustomFieldDefinitionType[]) => CustomFieldsSearchPayload;
25
+ export {};
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateCustomFieldSearchQueryPayload = 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 type_1 = require("../validations/type");
8
+ const generateCustomFieldSearchQueryPayload = (searchTerm, model, entityId, customFieldsTypesToExclude = [
9
+ type_1.CustomFieldDefinitionType.DATETIME,
10
+ type_1.CustomFieldDefinitionType.DATE,
11
+ ]) => {
12
+ const excludedTypesString = customFieldsTypesToExclude.map((type) => `'${type}'`).join(',');
13
+ const subQuery = 'EXISTS ('
14
+ + ' SELECT 1'
15
+ + ' FROM "custom_field_values" AS "cv"'
16
+ + ' INNER JOIN custom_field_definitions AS cd '
17
+ + ` ON cd.entity_id = '${entityId}'`
18
+ + ' AND cv.custom_field_definition_id = cd.id'
19
+ + ` AND cd.model_type = '${model.name}'`
20
+ + ` ${excludedTypesString ? `AND cd.field_type NOT IN (${excludedTypesString})` : ''}`
21
+ + ' WHERE'
22
+ + ' "cv"."deleted_at" IS NULL'
23
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
24
+ + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
25
+ return {
26
+ where: {
27
+ [sequelize_1.Op.or]: [
28
+ sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
29
+ ],
30
+ },
31
+ replacements: { searchTerm: `%${searchTerm}%` },
32
+ };
33
+ };
34
+ exports.generateCustomFieldSearchQueryPayload = generateCustomFieldSearchQueryPayload;
@@ -0,0 +1,5 @@
1
+ import type { ModelFetcher, Models } from '../types';
2
+ export declare const addHooks: (models: Models[], getModel: ModelFetcher) => void;
3
+ export declare const removeHooks: (models: Models[], getModel: ModelFetcher) => void;
4
+ export declare const addScopes: (models: Models[], getModel: ModelFetcher) => void;
5
+ export declare const applyCustomAssociation: (models: Models[]) => void;