@autofleet/sadot 0.5.5-beta.6 → 0.5.5-beta.7

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.
@@ -35,7 +35,7 @@ const customFieldsSearchTestFlow = async ({ fieldType, fieldValue, searchTerm, e
35
35
  const [testModel1] = await (0, testModel_1.createTestModels)(definition.entityId, 2);
36
36
  await testModel1.update({ customFields: { [definition.name]: fieldValue } });
37
37
  const models = await models_1.TestModel.findAndCountAll({
38
- where: { [sequelize_1.Op.or]: [(0, index_1.buildCustomFieldsSearchWhereClause)('TestModel')] },
38
+ where: { [sequelize_1.Op.or]: [(0, index_1.buildCustomFieldsSearchWhereClause)(models_1.TestModel, definition.entityId)] },
39
39
  replacements: { searchTerm: `%${searchTerm}%` },
40
40
  });
41
41
  expect(models.count).toBe(expectedNumberOfQueryResults);
@@ -1,4 +1,5 @@
1
1
  import { WhereOptions } from 'sequelize';
2
+ import { ModelStatic } from 'sequelize-typescript';
2
3
  /**
3
4
  * This function builds a WHERE clause to be added to the main query.
4
5
  * The WHERE clause enable free term search by custom fields.
@@ -6,4 +7,4 @@ import { WhereOptions } from 'sequelize';
6
7
  * @param {string} name - The model type name to be search for.
7
8
  * @returns {WhereOptions} - A where clause to be added to the main query.
8
9
  */
9
- export declare const buildCustomFieldsSearchWhereClause: (modelName: string) => WhereOptions;
10
+ export declare const buildCustomFieldsSearchWhereClause: (model: ModelStatic, entityId: string) => WhereOptions;
@@ -11,15 +11,17 @@ const sequelize_typescript_1 = require("sequelize-typescript");
11
11
  * @param {string} name - The model type name to be search for.
12
12
  * @returns {WhereOptions} - A where clause to be added to the main query.
13
13
  */
14
- const buildCustomFieldsSearchWhereClause = (modelName) => {
14
+ const buildCustomFieldsSearchWhereClause = (model, entityId) => {
15
15
  const subQuery = 'EXISTS ('
16
16
  + ' SELECT 1'
17
17
  + ' FROM "custom_field_values" AS "cv"'
18
- + ' INNER JOIN custom_field_definitions AS cd ON cv.custom_field_definition_id = cd.id '
19
- + ` AND cd.model_type = '${modelName}' `
18
+ + ' INNER JOIN custom_field_definitions AS cd '
19
+ + ` ON cd.entity_id = '${entityId}'`
20
+ + ' AND cv.custom_field_definition_id = cd.id '
21
+ + ` AND cd.model_type = '${model.name}' `
20
22
  + ' WHERE'
21
23
  + ' "cv"."deleted_at" IS NULL'
22
- + ` AND "cv"."model_id" = "${modelName}"."id"`
24
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
23
25
  + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
24
26
  return {
25
27
  [sequelize_1.Op.or]: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.5.5-beta.6",
3
+ "version": "0.5.5-beta.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -24,7 +24,7 @@ const customFieldsSearchTestFlow = async ({
24
24
  await testModel1.update({ customFields: { [definition.name]: fieldValue } });
25
25
  const models = await TestModel.findAndCountAll(
26
26
  {
27
- where: { [Op.or]: [buildCustomFieldsSearchWhereClause('TestModel')] },
27
+ where: { [Op.or]: [buildCustomFieldsSearchWhereClause(TestModel, definition.entityId)] },
28
28
  replacements: { searchTerm: `%${searchTerm}%` },
29
29
  },
30
30
  );
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
2
  import { WhereOptions, Op } from 'sequelize';
3
- import { Sequelize } from 'sequelize-typescript';
3
+ import { ModelStatic, Sequelize } from 'sequelize-typescript';
4
4
 
5
5
  /**
6
6
  * This function builds a WHERE clause to be added to the main query.
@@ -10,15 +10,20 @@ import { Sequelize } from 'sequelize-typescript';
10
10
  * @returns {WhereOptions} - A where clause to be added to the main query.
11
11
  */
12
12
 
13
- export const buildCustomFieldsSearchWhereClause = (modelName: string): WhereOptions => {
13
+ export const buildCustomFieldsSearchWhereClause = (
14
+ model: ModelStatic,
15
+ entityId : string,
16
+ ): WhereOptions => {
14
17
  const subQuery = 'EXISTS ('
15
18
  + ' SELECT 1'
16
19
  + ' FROM "custom_field_values" AS "cv"'
17
- + ' INNER JOIN custom_field_definitions AS cd ON cv.custom_field_definition_id = cd.id '
18
- + ` AND cd.model_type = '${modelName}' `
20
+ + ' INNER JOIN custom_field_definitions AS cd '
21
+ + ` ON cd.entity_id = '${entityId}'`
22
+ + ' AND cv.custom_field_definition_id = cd.id '
23
+ + ` AND cd.model_type = '${model.name}' `
19
24
  + ' WHERE'
20
25
  + ' "cv"."deleted_at" IS NULL'
21
- + ` AND "cv"."model_id" = "${modelName}"."id"`
26
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
22
27
  + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
23
28
  return {
24
29
  [Op.or]: [