@autofleet/sadot 0.5.5-beta.5 → 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,2 +1,10 @@
1
1
  import { WhereOptions } from 'sequelize';
2
- export declare const buildCustomFieldsSearchWhereClause: (modelName: string) => WhereOptions;
2
+ import { ModelStatic } from 'sequelize-typescript';
3
+ /**
4
+ * This function builds a WHERE clause to be added to the main query.
5
+ * The WHERE clause enable free term search by custom fields.
6
+ *
7
+ * @param {string} name - The model type name to be search for.
8
+ * @returns {WhereOptions} - A where clause to be added to the main query.
9
+ */
10
+ export declare const buildCustomFieldsSearchWhereClause: (model: ModelStatic, entityId: string) => WhereOptions;
@@ -4,14 +4,25 @@ exports.buildCustomFieldsSearchWhereClause = void 0;
4
4
  /* eslint-disable import/prefer-default-export */
5
5
  const sequelize_1 = require("sequelize");
6
6
  const sequelize_typescript_1 = require("sequelize-typescript");
7
- const buildCustomFieldsSearchWhereClause = (modelName) => {
8
- const subQuery = `${'EXISTS ('
7
+ /**
8
+ * This function builds a WHERE clause to be added to the main query.
9
+ * The WHERE clause enable free term search by custom fields.
10
+ *
11
+ * @param {string} name - The model type name to be search for.
12
+ * @returns {WhereOptions} - A where clause to be added to the main query.
13
+ */
14
+ const buildCustomFieldsSearchWhereClause = (model, entityId) => {
15
+ const subQuery = 'EXISTS ('
9
16
  + ' SELECT 1'
10
17
  + ' FROM "custom_field_values" AS "cv"'
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}' `
11
22
  + ' WHERE'
12
23
  + ' "cv"."deleted_at" IS NULL'
13
- + ` AND "cv"."model_id" = "${modelName}"."id"`
14
- + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)'}`;
24
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
25
+ + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
15
26
  return {
16
27
  [sequelize_1.Op.or]: [
17
28
  sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.5.5-beta.5",
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,17 +1,30 @@
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
- export const buildCustomFieldsSearchWhereClause = (modelName: string): WhereOptions => {
6
- const subQuery = `${
7
- 'EXISTS ('
5
+ /**
6
+ * This function builds a WHERE clause to be added to the main query.
7
+ * The WHERE clause enable free term search by custom fields.
8
+ *
9
+ * @param {string} name - The model type name to be search for.
10
+ * @returns {WhereOptions} - A where clause to be added to the main query.
11
+ */
12
+
13
+ export const buildCustomFieldsSearchWhereClause = (
14
+ model: ModelStatic,
15
+ entityId : string,
16
+ ): WhereOptions => {
17
+ const subQuery = 'EXISTS ('
8
18
  + ' SELECT 1'
9
19
  + ' FROM "custom_field_values" AS "cv"'
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}' `
10
24
  + ' WHERE'
11
25
  + ' "cv"."deleted_at" IS NULL'
12
- + ` AND "cv"."model_id" = "${modelName}"."id"`
13
- + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)'
14
- }`;
26
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
27
+ + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
15
28
  return {
16
29
  [Op.or]: [
17
30
  Sequelize.where(Sequelize.literal(subQuery), true),