@autofleet/sadot 0.5.5-beta.1 → 0.5.5-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { Application } from 'express';
2
2
  import { Sequelize } from 'sequelize-typescript';
3
- import { WhereOptions } from 'sequelize';
4
3
  import type { CustomFieldOptions, ModelFetcher } from './types';
5
4
  export * from './utils/validations/custom-fields';
6
5
  export * from './utils/constants';
6
+ export * from './utils/helpers';
7
7
  /**
8
8
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
9
9
  * @see {@link 'custom-fields/config'} for configurations
@@ -11,4 +11,3 @@ export * from './utils/constants';
11
11
  declare const useCustomFields: (app: Application | null, getModel: ModelFetcher, options: CustomFieldOptions) => Promise<Sequelize>;
12
12
  export default useCustomFields;
13
13
  export declare const disableCustomFields: (models: any, getModel: any) => void;
14
- export declare const buildCustomFieldsSearchWhereClause: (searchTerm: string) => WhereOptions;
package/dist/index.js CHANGED
@@ -17,9 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.buildCustomFieldsSearchWhereClause = exports.disableCustomFields = void 0;
21
- const sequelize_typescript_1 = require("sequelize-typescript");
22
- const sequelize_1 = require("sequelize");
20
+ exports.disableCustomFields = void 0;
23
21
  const models_1 = require("./models");
24
22
  const api_1 = __importDefault(require("./api"));
25
23
  const db_1 = __importDefault(require("./utils/db"));
@@ -27,6 +25,7 @@ const logger_1 = __importDefault(require("./utils/logger"));
27
25
  const init_1 = require("./utils/init");
28
26
  __exportStar(require("./utils/validations/custom-fields"), exports);
29
27
  __exportStar(require("./utils/constants"), exports);
28
+ __exportStar(require("./utils/helpers"), exports);
30
29
  /**
31
30
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
32
31
  * @see {@link 'custom-fields/config'} for configurations
@@ -52,11 +51,3 @@ const disableCustomFields = (models, getModel) => {
52
51
  (0, init_1.removeHooks)(models, getModel);
53
52
  };
54
53
  exports.disableCustomFields = disableCustomFields;
55
- const buildCustomFieldsSearchWhereClause = (searchTerm) => ({
56
- where: {
57
- [sequelize_1.Op.or]: [
58
- sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.cast(sequelize_typescript_1.Sequelize.col('customFieldValue.value'), 'text'), { [sequelize_1.Op.iLike]: `%${searchTerm}%` }),
59
- ],
60
- },
61
- });
62
- exports.buildCustomFieldsSearchWhereClause = buildCustomFieldsSearchWhereClause;
@@ -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,22 @@
1
+ import { WhereOptions, BindOrReplacements } from 'sequelize';
2
+ import { ModelStatic } from 'sequelize-typescript';
3
+ /**
4
+ * Builds a WHERE clause and replacements for free-text search by custom fields.
5
+ *
6
+ * This function constructs a WHERE clause and replacement bindings that allow searching
7
+ * for a given term within custom fields associated with a specific model type and entity ID.
8
+ * The WHERE clause and replacements are designed to be added to the main query.
9
+ *
10
+ * @param {string} searchTerm - The term to search for within custom fields.
11
+ * @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
12
+ * @param {string} entityId - The entity ID to filter the custom fields by.
13
+ *
14
+ * @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
15
+ * for Sequelize.
16
+ */
17
+ interface CustomFieldsSearchPayload {
18
+ where: WhereOptions;
19
+ replacements: BindOrReplacements;
20
+ }
21
+ export declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string) => CustomFieldsSearchPayload;
22
+ export {};
@@ -0,0 +1,28 @@
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 generateCustomFieldSearchQueryPayload = (searchTerm, model, entityId) => {
8
+ const subQuery = 'EXISTS ('
9
+ + ' SELECT 1'
10
+ + ' FROM "custom_field_values" AS "cv"'
11
+ + ' INNER JOIN custom_field_definitions AS cd '
12
+ + ` ON cd.entity_id = '${entityId}'`
13
+ + ' AND cv.custom_field_definition_id = cd.id '
14
+ + ` AND cd.model_type = '${model.name}' `
15
+ + ' WHERE'
16
+ + ' "cv"."deleted_at" IS NULL'
17
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
18
+ + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
19
+ return {
20
+ where: {
21
+ [sequelize_1.Op.or]: [
22
+ sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
23
+ ],
24
+ },
25
+ replacements: { searchTerm: `%${searchTerm}%` },
26
+ };
27
+ };
28
+ exports.generateCustomFieldSearchQueryPayload = generateCustomFieldSearchQueryPayload;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.5.5-beta.1",
3
+ "version": "0.5.5-beta.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { Application } from 'express';
2
2
  import { Sequelize } from 'sequelize-typescript';
3
- import { WhereOptions, Op } from 'sequelize';
4
3
  import {
5
4
  initTables, initTestModels,
6
5
  } from './models';
@@ -14,6 +13,7 @@ export * from './utils/validations/custom-fields';
14
13
 
15
14
  export * from './utils/constants';
16
15
 
16
+ export * from './utils/helpers';
17
17
  /**
18
18
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
19
19
  * @see {@link 'custom-fields/config'} for configurations
@@ -44,13 +44,3 @@ export default useCustomFields;
44
44
  export const disableCustomFields = (models, getModel): void => {
45
45
  removeHooks(models, getModel);
46
46
  };
47
-
48
- export const buildCustomFieldsSearchWhereClause = (
49
- searchTerm: string,
50
- ) : WhereOptions => ({
51
- where: {
52
- [Op.or]: [
53
- Sequelize.where(Sequelize.cast(Sequelize.col('customFieldValue.value'), 'text'), { [Op.iLike]: `%${searchTerm}%` }),
54
- ],
55
- },
56
- });
@@ -0,0 +1,39 @@
1
+ import { generateCustomFieldSearchQueryPayload } from '../../../index';
2
+ import * as DefinitionRepo from '../../../repository/definition';
3
+ import { createDefinition } from '../../mocks/definition.mock';
4
+ import { createTestModels } from '../../mocks/testModel';
5
+ import { TestModel } from '../../../models';
6
+
7
+ interface CustomFieldsSearchTestFlowInput {
8
+ fieldType: string;
9
+ fieldValue: string | number;
10
+ searchTerm: string;
11
+ expectedNumberOfQueryResults: number;
12
+ }
13
+
14
+ const customFieldsSearchTestFlow = async ({
15
+ fieldType,
16
+ fieldValue,
17
+ searchTerm,
18
+ expectedNumberOfQueryResults,
19
+ }: CustomFieldsSearchTestFlowInput) : Promise<void> => {
20
+ const definition = createDefinition({ fieldType, name: 'coolDefinition' });
21
+ await DefinitionRepo.create({ ...definition });
22
+ const [testModel1] = await createTestModels(definition.entityId, 2);
23
+ await testModel1.update({ customFields: { [definition.name]: fieldValue } });
24
+ const models = await TestModel.findAndCountAll(
25
+ {
26
+ ...generateCustomFieldSearchQueryPayload(
27
+ searchTerm,
28
+ TestModel,
29
+ definition.entityId,
30
+ ),
31
+ },
32
+ );
33
+ expect(models.count).toBe(expectedNumberOfQueryResults);
34
+ if (expectedNumberOfQueryResults > 0) {
35
+ expect(models.rows[0].dataValues.id).toBe(testModel1.id);
36
+ }
37
+ };
38
+
39
+ export default customFieldsSearchTestFlow;
@@ -0,0 +1,50 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import { WhereOptions, Op, BindOrReplacements } from 'sequelize';
3
+ import { ModelStatic, Sequelize } from 'sequelize-typescript';
4
+
5
+ /**
6
+ * Builds a WHERE clause and replacements for free-text search by custom fields.
7
+ *
8
+ * This function constructs a WHERE clause and replacement bindings that allow searching
9
+ * for a given term within custom fields associated with a specific model type and entity ID.
10
+ * The WHERE clause and replacements are designed to be added to the main query.
11
+ *
12
+ * @param {string} searchTerm - The term to search for within custom fields.
13
+ * @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
14
+ * @param {string} entityId - The entity ID to filter the custom fields by.
15
+ *
16
+ * @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
17
+ * for Sequelize.
18
+ */
19
+
20
+ interface CustomFieldsSearchPayload {
21
+ where: WhereOptions;
22
+ replacements: BindOrReplacements;
23
+ }
24
+
25
+ export const generateCustomFieldSearchQueryPayload = (
26
+ searchTerm: string,
27
+ model: ModelStatic,
28
+ entityId : string,
29
+ ): CustomFieldsSearchPayload => {
30
+ const subQuery = 'EXISTS ('
31
+ + ' SELECT 1'
32
+ + ' FROM "custom_field_values" AS "cv"'
33
+ + ' INNER JOIN custom_field_definitions AS cd '
34
+ + ` ON cd.entity_id = '${entityId}'`
35
+ + ' AND cv.custom_field_definition_id = cd.id '
36
+ + ` AND cd.model_type = '${model.name}' `
37
+ + ' WHERE'
38
+ + ' "cv"."deleted_at" IS NULL'
39
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
40
+ + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
41
+
42
+ return {
43
+ where: {
44
+ [Op.or]: [
45
+ Sequelize.where(Sequelize.literal(subQuery), true),
46
+ ],
47
+ },
48
+ replacements: { searchTerm: `%${searchTerm}%` },
49
+ };
50
+ };