@autofleet/sadot 0.3.3 → 0.4.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 (96) hide show
  1. package/.nvmrc +1 -0
  2. package/dist/jest.config.d.ts +12 -0
  3. package/dist/src/api/v1/errors.d.ts +2 -0
  4. package/dist/{api → src/api}/v1/errors.js +1 -1
  5. package/dist/{hooks → src/hooks}/find.js +1 -1
  6. package/dist/{index.d.ts → src/index.d.ts} +2 -1
  7. package/dist/src/index.js +52 -0
  8. package/dist/{models → src/models}/tests/TestModel.d.ts +1 -0
  9. package/dist/{models → src/models}/tests/TestModel.js +6 -0
  10. package/dist/src/scopes/filter.d.ts +23 -0
  11. package/dist/src/scopes/filter.js +43 -0
  12. package/dist/src/scopes/index.d.ts +2 -0
  13. package/dist/src/scopes/index.js +6 -0
  14. package/dist/{tests → src/tests}/helpers/database-config.d.ts +1 -1
  15. package/dist/{tests → src/tests}/mocks/definition.mock.d.ts +14 -14
  16. package/dist/{tests → src/tests}/mocks/testModel.d.ts +1 -1
  17. package/dist/src/utils/constants/index.d.ts +2 -0
  18. package/dist/{utils → src/utils}/constants/index.js +2 -1
  19. package/dist/src/utils/init.d.ts +4 -0
  20. package/dist/{index.js → src/utils/init.js} +37 -43
  21. package/package.json +2 -1
  22. package/src/api/v1/errors.ts +1 -1
  23. package/src/hooks/find.ts +1 -1
  24. package/src/index.ts +9 -65
  25. package/src/models/tests/TestModel.ts +5 -0
  26. package/src/scopes/filter.ts +63 -0
  27. package/src/scopes/index.ts +6 -0
  28. package/src/utils/constants/index.ts +2 -0
  29. package/src/utils/init.ts +101 -0
  30. package/tsconfig.json +2 -2
  31. package/dist/api/v1/errors.d.ts +0 -2
  32. package/dist/utils/constants/index.d.ts +0 -1
  33. /package/dist/{api → src/api}/index.d.ts +0 -0
  34. /package/dist/{api → src/api}/index.js +0 -0
  35. /package/dist/{api → src/api}/v1/definition/index.d.ts +0 -0
  36. /package/dist/{api → src/api}/v1/definition/index.js +0 -0
  37. /package/dist/{api → src/api}/v1/definition/validations.d.ts +0 -0
  38. /package/dist/{api → src/api}/v1/definition/validations.js +0 -0
  39. /package/dist/{api → src/api}/v1/index.d.ts +0 -0
  40. /package/dist/{api → src/api}/v1/index.js +0 -0
  41. /package/dist/{errors → src/errors}/index.d.ts +0 -0
  42. /package/dist/{errors → src/errors}/index.js +0 -0
  43. /package/dist/{events → src/events}/index.d.ts +0 -0
  44. /package/dist/{events → src/events}/index.js +0 -0
  45. /package/dist/{hooks → src/hooks}/create.d.ts +0 -0
  46. /package/dist/{hooks → src/hooks}/create.js +0 -0
  47. /package/dist/{hooks → src/hooks}/enrich.d.ts +0 -0
  48. /package/dist/{hooks → src/hooks}/enrich.js +0 -0
  49. /package/dist/{hooks → src/hooks}/find.d.ts +0 -0
  50. /package/dist/{hooks → src/hooks}/index.d.ts +0 -0
  51. /package/dist/{hooks → src/hooks}/index.js +0 -0
  52. /package/dist/{hooks → src/hooks}/update.d.ts +0 -0
  53. /package/dist/{hooks → src/hooks}/update.js +0 -0
  54. /package/dist/{hooks → src/hooks}/workaround.d.ts +0 -0
  55. /package/dist/{hooks → src/hooks}/workaround.js +0 -0
  56. /package/dist/{models → src/models}/CustomFieldDefinition.d.ts +0 -0
  57. /package/dist/{models → src/models}/CustomFieldDefinition.js +0 -0
  58. /package/dist/{models → src/models}/CustomFieldValue.d.ts +0 -0
  59. /package/dist/{models → src/models}/CustomFieldValue.js +0 -0
  60. /package/dist/{models → src/models}/index.d.ts +0 -0
  61. /package/dist/{models → src/models}/index.js +0 -0
  62. /package/dist/{models → src/models}/tests/AssociatedTestModel.d.ts +0 -0
  63. /package/dist/{models → src/models}/tests/AssociatedTestModel.js +0 -0
  64. /package/dist/{repository → src/repository}/definition.d.ts +0 -0
  65. /package/dist/{repository → src/repository}/definition.js +0 -0
  66. /package/dist/{repository → src/repository}/value.d.ts +0 -0
  67. /package/dist/{repository → src/repository}/value.js +0 -0
  68. /package/dist/{tests → src/tests}/api/test-api.d.ts +0 -0
  69. /package/dist/{tests → src/tests}/api/test-api.js +0 -0
  70. /package/dist/{tests → src/tests}/helpers/database-config.js +0 -0
  71. /package/dist/{tests → src/tests}/helpers/index.d.ts +0 -0
  72. /package/dist/{tests → src/tests}/helpers/index.js +0 -0
  73. /package/dist/{tests → src/tests}/mocks/definition.mock.js +0 -0
  74. /package/dist/{tests → src/tests}/mocks/events.mock.d.ts +0 -0
  75. /package/dist/{tests → src/tests}/mocks/events.mock.js +0 -0
  76. /package/dist/{tests → src/tests}/mocks/testModel.js +0 -0
  77. /package/dist/{types → src/types}/definition/index.d.ts +0 -0
  78. /package/dist/{types → src/types}/definition/index.js +0 -0
  79. /package/dist/{types → src/types}/index.d.ts +0 -0
  80. /package/dist/{types → src/types}/index.js +0 -0
  81. /package/dist/{types → src/types}/value/index.d.ts +0 -0
  82. /package/dist/{types → src/types}/value/index.js +0 -0
  83. /package/dist/{utils → src/utils}/db/index.d.ts +0 -0
  84. /package/dist/{utils → src/utils}/db/index.js +0 -0
  85. /package/dist/{utils → src/utils}/logger/index.d.ts +0 -0
  86. /package/dist/{utils → src/utils}/logger/index.js +0 -0
  87. /package/dist/{utils → src/utils}/validations/custom-fields.d.ts +0 -0
  88. /package/dist/{utils → src/utils}/validations/custom-fields.js +0 -0
  89. /package/dist/{utils → src/utils}/validations/custom.d.ts +0 -0
  90. /package/dist/{utils → src/utils}/validations/custom.js +0 -0
  91. /package/dist/{utils → src/utils}/validations/index.d.ts +0 -0
  92. /package/dist/{utils → src/utils}/validations/index.js +0 -0
  93. /package/dist/{utils → src/utils}/validations/type.d.ts +0 -0
  94. /package/dist/{utils → src/utils}/validations/type.js +0 -0
  95. /package/dist/{utils → src/utils}/validations/validators.d.ts +0 -0
  96. /package/dist/{utils → src/utils}/validations/validators.js +0 -0
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 18.17.1
@@ -0,0 +1,12 @@
1
+ export const testEnvironment: string;
2
+ export const roots: string[];
3
+ export const transform: {
4
+ '^.+\\.tsx?$': string;
5
+ };
6
+ export const testRegex: string;
7
+ export const moduleFileExtensions: string[];
8
+ export namespace coverageThreshold {
9
+ namespace global {
10
+ const lines: number;
11
+ }
12
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: (err: any, res: any, additionalData?: any) => Promise<any>;
2
+ export default _default;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const errors_1 = require("@autofleet/errors");
4
4
  const joi_1 = require("@hapi/joi");
5
5
  const sequelize_1 = require("sequelize");
6
- exports.default = (err, res, additionalData = {}) => {
6
+ exports.default = (err, res, additionalData = undefined) => {
7
7
  let error = err;
8
8
  if (err instanceof joi_1.ValidationError) {
9
9
  error = new errors_1.BadRequest([err], null);
@@ -17,7 +17,7 @@ const doScopeAttributesMissing = (scopeAttributes, queryAttributes) => {
17
17
  // eslint-disable-next-line import/prefer-default-export
18
18
  const beforeFind = (scopeAttributes) => (options) => {
19
19
  const { attributes: queryAttributes } = options;
20
- if (queryAttributes?.includes('customFields')) {
20
+ if (queryAttributes?.includes?.('customFields')) {
21
21
  const missingScopeAttributes = doScopeAttributesMissing(scopeAttributes, queryAttributes);
22
22
  logger_1.default.debug('sadot - before find hook');
23
23
  if (missingScopeAttributes?.length > 0) {
@@ -1,7 +1,8 @@
1
1
  import type { Application } from 'express';
2
2
  import { Sequelize } from 'sequelize-typescript';
3
- import { CustomFieldOptions, ModelFetcher } from './types';
3
+ import type { CustomFieldOptions, ModelFetcher } from './types';
4
4
  export * from './utils/validations/custom-fields';
5
+ export * from './utils/constants';
5
6
  /**
6
7
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
7
8
  * @see {@link 'custom-fields/config'} for configurations
@@ -0,0 +1,52 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.disableCustomFields = void 0;
21
+ const models_1 = require("./models");
22
+ const api_1 = __importDefault(require("./api"));
23
+ const db_1 = __importDefault(require("./utils/db"));
24
+ const logger_1 = __importDefault(require("./utils/logger"));
25
+ const init_1 = require("./utils/init");
26
+ __exportStar(require("./utils/validations/custom-fields"), exports);
27
+ __exportStar(require("./utils/constants"), exports);
28
+ /**
29
+ * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
30
+ * @see {@link 'custom-fields/config'} for configurations
31
+ */
32
+ const useCustomFields = async (app, getModel, options) => {
33
+ const { models } = options;
34
+ if (app) {
35
+ app.use('/api', api_1.default);
36
+ }
37
+ const sequelize = (0, db_1.default)(options.databaseConfig);
38
+ if (process.env.NODE_ENV === 'test') {
39
+ await (0, models_1.initTestModels)(sequelize);
40
+ }
41
+ // The order is important
42
+ (0, init_1.addHooks)(models, getModel);
43
+ await (0, models_1.initTables)(sequelize, options.getUser);
44
+ (0, init_1.addScopes)(models, getModel);
45
+ logger_1.default.debug('sadot - custom fields finished initializing with models', models);
46
+ return sequelize;
47
+ };
48
+ exports.default = useCustomFields;
49
+ const disableCustomFields = (models, getModel) => {
50
+ (0, init_1.removeHooks)(models, getModel);
51
+ };
52
+ exports.disableCustomFields = disableCustomFields;
@@ -7,5 +7,6 @@ declare class TestModel extends Model {
7
7
  demandSourceId: string;
8
8
  coolAttribute?: boolean;
9
9
  associatedModels: AssociatedTestModel[];
10
+ customFields?: any;
10
11
  }
11
12
  export default TestModel;
@@ -57,6 +57,12 @@ __decorate([
57
57
  (0, sequelize_typescript_1.HasMany)(() => AssociatedTestModel_1.default),
58
58
  __metadata("design:type", Array)
59
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);
60
66
  TestModel = __decorate([
61
67
  (0, sequelize_typescript_1.Table)({ schema: 'custom-fields', createdAt: false, updatedAt: false })
62
68
  ], TestModel);
@@ -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,43 @@
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 constants_1 = require("../utils/constants");
8
+ /**
9
+ * A Sequelize scope for filtering models by custom fields.
10
+ * This scope builds a WHERE clause to be applied on the main query.
11
+ *
12
+ * @param {string} name - The model type name used to join custom_field_definitions.
13
+ * @returns {Function} - A function that takes conditions and returns the Sequelize options object.
14
+ */
15
+ const customFieldsFilterScope = (name) => (conditions) => {
16
+ // Build the WHERE clause for custom field filtering
17
+ const customFieldConditions = Object.entries(conditions)
18
+ .map(([key, value]) => {
19
+ if (Array.isArray(value)) {
20
+ const values = value.map((v) => `'${v}'`).join(',');
21
+ return `custom_fields->>'${key}' IN (${values})`;
22
+ }
23
+ return `custom_fields->>'${key}' = '${value}'`;
24
+ })
25
+ .join(' AND ');
26
+ const subQuery = `${'SELECT model_id FROM ('
27
+ + 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
28
+ + 'FROM custom_field_values AS cv '
29
+ + 'INNER JOIN custom_field_definitions AS cd ON cv.custom_field_definition_id = cd.id '
30
+ + `AND cd.model_type = '${name}' `
31
+ + 'GROUP BY cv.model_id'
32
+ + ') AS CustomFieldAggregation '
33
+ + 'WHERE '}${customFieldConditions}`;
34
+ return {
35
+ where: {
36
+ id: {
37
+ [sequelize_1.Op.in]: sequelize_typescript_1.Sequelize.literal(`(${subQuery})`),
38
+ },
39
+ },
40
+ };
41
+ };
42
+ exports.customFieldsFilterScope = customFieldsFilterScope;
43
+ exports.scopeName = constants_1.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; } });
@@ -1,7 +1,7 @@
1
1
  declare const _default: {
2
2
  test: {
3
3
  username: string;
4
- password: string | null;
4
+ password: string;
5
5
  database: string;
6
6
  host: string;
7
7
  dialect: string;
@@ -2,13 +2,13 @@ import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../typ
2
2
  export declare const coolFieldDefinition: CreateCustomFieldDefinition;
3
3
  export declare const coolFieldDefinition2: {
4
4
  name: string;
5
- required?: boolean | undefined;
6
- disabled?: boolean | undefined;
7
- description?: string | undefined;
8
- createdAt?: Date | undefined;
9
- updatedAt?: Date | undefined;
10
- deletedAt?: Date | undefined;
11
- displayName?: string | undefined;
5
+ required?: boolean;
6
+ disabled?: boolean;
7
+ description?: string;
8
+ createdAt?: Date;
9
+ updatedAt?: Date;
10
+ deletedAt?: Date;
11
+ displayName?: string;
12
12
  validation?: any;
13
13
  fieldType: string;
14
14
  entityId: string;
@@ -17,13 +17,13 @@ export declare const coolFieldDefinition2: {
17
17
  };
18
18
  export declare const coolFieldDefinition3: {
19
19
  name: string;
20
- required?: boolean | undefined;
21
- disabled?: boolean | undefined;
22
- description?: string | undefined;
23
- createdAt?: Date | undefined;
24
- updatedAt?: Date | undefined;
25
- deletedAt?: Date | undefined;
26
- displayName?: string | undefined;
20
+ required?: boolean;
21
+ disabled?: boolean;
22
+ description?: string;
23
+ createdAt?: Date;
24
+ updatedAt?: Date;
25
+ deletedAt?: Date;
26
+ displayName?: string;
27
27
  validation?: any;
28
28
  fieldType: string;
29
29
  entityId: string;
@@ -1,7 +1,7 @@
1
1
  import { TestModel } from '../../models';
2
2
  export declare const createTestModel: (payload?: {}) => Promise<TestModel>;
3
3
  export declare const createTestModels: (fleetId: any, total: number) => Promise<TestModel[]>;
4
- export declare const upsertTestModel: (payload: any) => Promise<[TestModel, boolean | null]>;
4
+ export declare const upsertTestModel: (payload: any) => Promise<[TestModel, boolean]>;
5
5
  export declare const destroyTestModels: () => Promise<number>;
6
6
  export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel>;
7
7
  export declare const getSomeTestModels: (options?: {
@@ -0,0 +1,2 @@
1
+ export declare const supportedEntities: string[];
2
+ export declare const CUSTOM_FIELDS_FILTER_SCOPE = "filterByCustomFields";
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.supportedEntities = void 0;
3
+ exports.CUSTOM_FIELDS_FILTER_SCOPE = exports.supportedEntities = void 0;
4
4
  // eslint-disable-next-line import/prefer-default-export
5
5
  exports.supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
6
+ exports.CUSTOM_FIELDS_FILTER_SCOPE = 'filterByCustomFields';
@@ -0,0 +1,4 @@
1
+ import type { ModelFetcher, ModelOptions } from '../types';
2
+ export declare const addHooks: (models: ModelOptions[], getModel: ModelFetcher) => void;
3
+ export declare const removeHooks: (models: ModelOptions[], getModel: ModelFetcher) => void;
4
+ export declare const addScopes: (models: ModelOptions[], getModel: ModelFetcher) => void;
@@ -1,30 +1,15 @@
1
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
17
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
4
  };
19
5
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.disableCustomFields = void 0;
6
+ exports.addScopes = exports.removeHooks = exports.addHooks = void 0;
21
7
  const sequelize_1 = require("sequelize");
22
- const models_1 = require("./models");
23
- const api_1 = __importDefault(require("./api"));
24
- const hooks_1 = require("./hooks");
25
- const db_1 = __importDefault(require("./utils/db"));
26
- const logger_1 = __importDefault(require("./utils/logger"));
27
- __exportStar(require("./utils/validations/custom-fields"), exports);
8
+ const models_1 = require("../models");
9
+ const hooks_1 = require("../hooks");
10
+ const scopes_1 = require("../scopes");
11
+ const logger_1 = __importDefault(require("./logger"));
12
+ const constants_1 = require("./constants");
28
13
  const addHooks = (models, getModel) => {
29
14
  models.forEach(async ({ name, scopeAttributes }) => {
30
15
  try {
@@ -56,25 +41,7 @@ const addHooks = (models, getModel) => {
56
41
  }
57
42
  });
58
43
  };
59
- /**
60
- * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
61
- * @see {@link 'custom-fields/config'} for configurations
62
- */
63
- const useCustomFields = async (app, getModel, options) => {
64
- const { models } = options;
65
- if (app) {
66
- app.use('/api', api_1.default);
67
- }
68
- const sequelize = (0, db_1.default)(options.databaseConfig);
69
- if (process.env.NODE_ENV === 'test') {
70
- await (0, models_1.initTestModels)(sequelize);
71
- }
72
- addHooks(models, getModel);
73
- await (0, models_1.initTables)(sequelize, options.getUser);
74
- logger_1.default.debug('sadot - custom fields finished initializing with models', models);
75
- return sequelize;
76
- };
77
- exports.default = useCustomFields;
44
+ exports.addHooks = addHooks;
78
45
  const removeHooks = (models, getModel) => {
79
46
  models.forEach(async ({ name }) => {
80
47
  try {
@@ -99,7 +66,34 @@ const removeHooks = (models, getModel) => {
99
66
  }
100
67
  });
101
68
  };
102
- const disableCustomFields = (models, getModel) => {
103
- removeHooks(models, getModel);
69
+ exports.removeHooks = removeHooks;
70
+ /**
71
+ * Necessary associations for the {@link customFieldsFilterScope} scope
72
+ */
73
+ const addAssociations = (model, modelName) => {
74
+ model.hasMany(models_1.CustomFieldValue, { foreignKey: 'modelId', as: 'customFieldValue' });
75
+ // TBC: maybe can be removed
76
+ models_1.CustomFieldValue.belongsTo(model, { foreignKey: 'modelId', as: modelName });
77
+ };
78
+ const addScopes = (models, getModel) => {
79
+ models.forEach(async ({ name, scopeAttributes }) => {
80
+ try {
81
+ const model = getModel(name);
82
+ if (!model) {
83
+ logger_1.default.warn('sadot - tried to addScopes to a model that does not exist yet', {
84
+ name,
85
+ scopeAttributes,
86
+ });
87
+ return;
88
+ }
89
+ // Necessary associations for the filtering scope
90
+ addAssociations(model, name);
91
+ // Add filter scope
92
+ model.addScope(constants_1.CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name));
93
+ }
94
+ catch (e) {
95
+ logger_1.default.error(`Could not add custom fields scopes to model ${name}. `, e);
96
+ }
97
+ });
104
98
  };
105
- exports.disableCustomFields = disableCustomFields;
99
+ exports.addScopes = addScopes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.3.3",
3
+ "version": "0.4.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -26,6 +26,7 @@
26
26
  "devDependencies": {
27
27
  "@types/express": "^4.17.17",
28
28
  "@types/jest": "^27.0.9",
29
+ "@types/uuid": "^9.0.2",
29
30
  "@typescript-eslint/eslint-plugin": "^4.8.1",
30
31
  "@typescript-eslint/parser": "^4.33.0",
31
32
  "eslint": "^7.13.0",
@@ -2,7 +2,7 @@ import { handleError, BadRequest } from '@autofleet/errors';
2
2
  import { ValidationError as InputValidationError } from '@hapi/joi';
3
3
  import { ValidationError as DatabaseValidationError } from 'sequelize';
4
4
 
5
- export default (err, res, additionalData = {}) => {
5
+ export default (err, res, additionalData = undefined) => {
6
6
  let error = err;
7
7
  if (err instanceof InputValidationError) {
8
8
  error = new BadRequest([err], null);
package/src/hooks/find.ts CHANGED
@@ -16,7 +16,7 @@ const doScopeAttributesMissing = (
16
16
  // eslint-disable-next-line import/prefer-default-export
17
17
  export const beforeFind = (scopeAttributes: string[]) => (options): void => {
18
18
  const { attributes: queryAttributes } = options;
19
- if (queryAttributes?.includes('customFields')) {
19
+ if (queryAttributes?.includes?.('customFields')) {
20
20
  const missingScopeAttributes = doScopeAttributesMissing(scopeAttributes, queryAttributes);
21
21
  logger.debug('sadot - before find hook');
22
22
  if (missingScopeAttributes?.length > 0) {
package/src/index.ts CHANGED
@@ -1,52 +1,17 @@
1
1
  import type { Application } from 'express';
2
- import { DataTypes } from 'sequelize';
3
2
  import { Sequelize } from 'sequelize-typescript';
4
- import { initTables, initTestModels } from './models';
5
- import api from './api';
6
3
  import {
7
- beforeFind,
8
- enrichResults,
9
- beforeBulkUpdate,
10
- beforeUpdate,
11
- beforeCreate,
12
- beforeBulkCreate,
13
- } from './hooks';
4
+ initTables, initTestModels,
5
+ } from './models';
6
+ import api from './api';
14
7
  import initDB from './utils/db';
15
8
  import logger from './utils/logger';
16
- import { CustomFieldOptions, ModelFetcher, ModelOptions } from './types';
9
+ import type { CustomFieldOptions, ModelFetcher } from './types';
10
+ import { addHooks, addScopes, removeHooks } from './utils/init';
17
11
 
18
12
  export * from './utils/validations/custom-fields';
19
13
 
20
- const addHooks = (models: ModelOptions[], getModel: ModelFetcher) => {
21
- models.forEach(async ({ name, scopeAttributes }) => {
22
- try {
23
- const model = getModel(name);
24
- if (!model) {
25
- logger.warn('sadot - tried to addHooks to a model that does not exist yet', {
26
- name,
27
- scopeAttributes,
28
- });
29
- return;
30
- }
31
- model.rawAttributes.customFields = {
32
- type: DataTypes.VIRTUAL,
33
- };
34
- model.refreshAttributes();
35
- // TODO: Uncomment after tests are passed
36
- // model.addHook('afterFind', workaround);
37
- model.addHook('beforeFind', 'sadot-beforeFind', beforeFind(scopeAttributes));
38
- model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', beforeBulkCreate);
39
- model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', beforeBulkUpdate);
40
- model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes));
41
- model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes));
42
- model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes));
43
- model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes));
44
- model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes));
45
- } catch (e) {
46
- logger.error(`Could not add custom fields hook to model ${name}. `, e);
47
- }
48
- });
49
- };
14
+ export * from './utils/constants';
50
15
 
51
16
  /**
52
17
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
@@ -65,37 +30,16 @@ const useCustomFields = async (
65
30
  if (process.env.NODE_ENV === 'test') {
66
31
  await initTestModels(sequelize);
67
32
  }
33
+ // The order is important
68
34
  addHooks(models, getModel);
69
35
  await initTables(sequelize, options.getUser);
36
+ addScopes(models, getModel);
70
37
  logger.debug('sadot - custom fields finished initializing with models', models);
71
38
  return sequelize;
72
39
  };
73
40
 
74
41
  export default useCustomFields;
75
42
 
76
- const removeHooks = (models: ModelOptions[], getModel: ModelFetcher) => {
77
- models.forEach(async ({ name }) => {
78
- try {
79
- const model = getModel(name);
80
- if (!model) return;
81
- if (model.rawAttributes.customFields) {
82
- delete model.rawAttributes.customFields;
83
- model.refreshAttributes();
84
- }
85
- // model.removeHook('afterFind', 'sadot-workaround');
86
- model.removeHook('beforeFind', 'sadot-beforeFind');
87
- model.removeHook('beforeBulkCreate', 'sadot-beforeBulkCreate');
88
- model.removeHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate');
89
- model.removeHook('beforeCreate', 'sadot-beforeCreate');
90
- model.removeHook('beforeUpdate', 'sadot-beforeUpdate');
91
- model.removeHook('afterFind', 'sadot-afterFind');
92
- model.removeHook('afterUpdate', 'sadot-afterUpdate');
93
- } catch (e) {
94
- logger.error(`Could not add custom fields hook to model ${name}. `, e);
95
- }
96
- });
97
- };
98
-
99
- export const disableCustomFields = (models, getModel) => {
43
+ export const disableCustomFields = (models, getModel): void => {
100
44
  removeHooks(models, getModel);
101
45
  };
@@ -44,6 +44,11 @@ class TestModel extends Model {
44
44
 
45
45
  @HasMany(() => AssociatedTestModel)
46
46
  associatedModels: AssociatedTestModel[];
47
+
48
+ @Column({
49
+ type: DataType.VIRTUAL,
50
+ })
51
+ customFields?: any;
47
52
  }
48
53
 
49
54
  export default TestModel;
@@ -0,0 +1,63 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import { Op, WhereOptions } from 'sequelize';
3
+ import { Sequelize } from 'sequelize-typescript';
4
+ import { CUSTOM_FIELDS_FILTER_SCOPE } from '../utils/constants';
5
+
6
+ /**
7
+ * Type representing possible condition values.
8
+ * Currently supporting strings and arrays of strings.
9
+ * More types to be added (TBA).
10
+ */
11
+ export type ConditionValue = string | string[];
12
+
13
+ export type CustomFieldSort = {
14
+ field: string;
15
+ direction: 'ASC' | 'DESC';
16
+ }
17
+
18
+ export type CustomFieldFilterOptions = {
19
+ where?: WhereOptions;
20
+ }
21
+
22
+ /**
23
+ * A Sequelize scope for filtering models by custom fields.
24
+ * This scope builds a WHERE clause to be applied on the main query.
25
+ *
26
+ * @param {string} name - The model type name used to join custom_field_definitions.
27
+ * @returns {Function} - A function that takes conditions and returns the Sequelize options object.
28
+ */
29
+ export const customFieldsFilterScope = (
30
+ name: string,
31
+ ) => (conditions: Record<string, ConditionValue>): CustomFieldFilterOptions => {
32
+ // Build the WHERE clause for custom field filtering
33
+ const customFieldConditions = Object.entries(conditions)
34
+ .map(
35
+ ([key, value]) => {
36
+ if (Array.isArray(value)) {
37
+ const values = value.map((v) => `'${v}'`).join(',');
38
+ return `custom_fields->>'${key}' IN (${values})`;
39
+ }
40
+ return `custom_fields->>'${key}' = '${value}'`;
41
+ },
42
+ )
43
+ .join(' AND ');
44
+
45
+ const subQuery = `${'SELECT model_id FROM ('
46
+ + 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
47
+ + 'FROM custom_field_values AS cv '
48
+ + 'INNER JOIN custom_field_definitions AS cd ON cv.custom_field_definition_id = cd.id '
49
+ + `AND cd.model_type = '${name}' `
50
+ + 'GROUP BY cv.model_id'
51
+ + ') AS CustomFieldAggregation '
52
+ + 'WHERE '}${customFieldConditions}`;
53
+
54
+ return {
55
+ where: {
56
+ id: {
57
+ [Op.in]: Sequelize.literal(`(${subQuery})`),
58
+ },
59
+ },
60
+ };
61
+ };
62
+
63
+ export const scopeName = CUSTOM_FIELDS_FILTER_SCOPE;
@@ -0,0 +1,6 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import { customFieldsFilterScope } from './filter';
3
+
4
+ export {
5
+ customFieldsFilterScope,
6
+ };
@@ -1,2 +1,4 @@
1
1
  // eslint-disable-next-line import/prefer-default-export
2
2
  export const supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
3
+
4
+ export const CUSTOM_FIELDS_FILTER_SCOPE = 'filterByCustomFields';
@@ -0,0 +1,101 @@
1
+ import { DataTypes } from 'sequelize';
2
+ import { ModelCtor } from 'sequelize-typescript';
3
+ import {
4
+ CustomFieldValue,
5
+ } from '../models';
6
+ import {
7
+ beforeFind,
8
+ enrichResults,
9
+ beforeBulkUpdate,
10
+ beforeUpdate,
11
+ beforeCreate,
12
+ beforeBulkCreate,
13
+ } from '../hooks';
14
+ import { customFieldsFilterScope } from '../scopes';
15
+ import logger from './logger';
16
+ import type { ModelFetcher, ModelOptions } from '../types';
17
+ import { CUSTOM_FIELDS_FILTER_SCOPE } from './constants';
18
+
19
+ export const addHooks = (models: ModelOptions[], getModel: ModelFetcher): void => {
20
+ models.forEach(async ({ name, scopeAttributes }) => {
21
+ try {
22
+ const model = getModel(name);
23
+ if (!model) {
24
+ logger.warn('sadot - tried to addHooks to a model that does not exist yet', {
25
+ name,
26
+ scopeAttributes,
27
+ });
28
+ return;
29
+ }
30
+ model.rawAttributes.customFields = {
31
+ type: DataTypes.VIRTUAL,
32
+ };
33
+ model.refreshAttributes();
34
+ // TODO: Uncomment after tests are passed
35
+ // model.addHook('afterFind', workaround);
36
+ model.addHook('beforeFind', 'sadot-beforeFind', beforeFind(scopeAttributes));
37
+ model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', beforeBulkCreate);
38
+ model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', beforeBulkUpdate);
39
+ model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes));
40
+ model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes));
41
+ model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes));
42
+ model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes));
43
+ model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes));
44
+ } catch (e) {
45
+ logger.error(`Could not add custom fields hook to model ${name}. `, e);
46
+ }
47
+ });
48
+ };
49
+
50
+ export const removeHooks = (models: ModelOptions[], getModel: ModelFetcher): void => {
51
+ models.forEach(async ({ name }) => {
52
+ try {
53
+ const model = getModel(name);
54
+ if (!model) return;
55
+ if (model.rawAttributes.customFields) {
56
+ delete model.rawAttributes.customFields;
57
+ model.refreshAttributes();
58
+ }
59
+ // model.removeHook('afterFind', 'sadot-workaround');
60
+ model.removeHook('beforeFind', 'sadot-beforeFind');
61
+ model.removeHook('beforeBulkCreate', 'sadot-beforeBulkCreate');
62
+ model.removeHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate');
63
+ model.removeHook('beforeCreate', 'sadot-beforeCreate');
64
+ model.removeHook('beforeUpdate', 'sadot-beforeUpdate');
65
+ model.removeHook('afterFind', 'sadot-afterFind');
66
+ model.removeHook('afterUpdate', 'sadot-afterUpdate');
67
+ } catch (e) {
68
+ logger.error(`Could not add custom fields hook to model ${name}. `, e);
69
+ }
70
+ });
71
+ };
72
+
73
+ /**
74
+ * Necessary associations for the {@link customFieldsFilterScope} scope
75
+ */
76
+ const addAssociations = (model: ModelCtor, modelName: string): void => {
77
+ model.hasMany(CustomFieldValue, { foreignKey: 'modelId', as: 'customFieldValue' });
78
+ // TBC: maybe can be removed
79
+ CustomFieldValue.belongsTo(model, { foreignKey: 'modelId', as: modelName });
80
+ };
81
+
82
+ export const addScopes = (models: ModelOptions[], getModel: ModelFetcher): void => {
83
+ models.forEach(async ({ name, scopeAttributes }) => {
84
+ try {
85
+ const model = getModel(name);
86
+ if (!model) {
87
+ logger.warn('sadot - tried to addScopes to a model that does not exist yet', {
88
+ name,
89
+ scopeAttributes,
90
+ });
91
+ return;
92
+ }
93
+ // Necessary associations for the filtering scope
94
+ addAssociations(model, name);
95
+ // Add filter scope
96
+ model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, customFieldsFilterScope(name));
97
+ } catch (e) {
98
+ logger.error(`Could not add custom fields scopes to model ${name}. `, e);
99
+ }
100
+ });
101
+ };
package/tsconfig.json CHANGED
@@ -4,10 +4,10 @@
4
4
  "module": "commonjs",
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
- "strict": true,
8
7
  "esModuleInterop": true,
9
8
  "experimentalDecorators": true,
10
- "emitDecoratorMetadata": true
9
+ "emitDecoratorMetadata": true,
10
+ "allowJs": true
11
11
  },
12
12
  "exclude": ["node_modules", "**/*.test.ts"]
13
13
  }
@@ -1,2 +0,0 @@
1
- declare const _default: (err: any, res: any, additionalData?: {}) => any;
2
- export default _default;
@@ -1 +0,0 @@
1
- export declare const supportedEntities: string[];
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes