@autofleet/sadot 0.4.2 → 0.4.3

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.
@@ -13,16 +13,27 @@ const constants_1 = require("../utils/constants");
13
13
  * @returns {Function} - A function that takes conditions and returns the Sequelize options object.
14
14
  */
15
15
  const customFieldsFilterScope = (name) => (conditions) => {
16
+ if (!conditions || Object.keys(conditions).length === 0) {
17
+ return {};
18
+ }
16
19
  // Build the WHERE clause for custom field filtering
17
- const customFieldConditions = Object.entries(conditions)
20
+ const conditionsStrings = Object.entries(conditions)
18
21
  .map(([key, value]) => {
19
22
  if (Array.isArray(value)) {
23
+ if (value.length === 0) {
24
+ // if empty array, the condition is ignored
25
+ return false;
26
+ }
20
27
  const values = value.map((v) => `'${v}'`).join(',');
21
28
  return `custom_fields->>'${key}' IN (${values})`;
22
29
  }
23
30
  return `custom_fields->>'${key}' = '${value}'`;
24
31
  })
25
- .join(' AND ');
32
+ .filter(Boolean);
33
+ if (conditionsStrings.length === 0) {
34
+ return {};
35
+ }
36
+ const customFieldConditions = conditionsStrings.join(' AND ');
26
37
  const subQuery = `${'SELECT model_id FROM ('
27
38
  + 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
28
39
  + 'FROM custom_field_values AS cv '
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -29,18 +29,30 @@ export type CustomFieldFilterOptions = {
29
29
  export const customFieldsFilterScope = (
30
30
  name: string,
31
31
  ) => (conditions: Record<string, ConditionValue>): CustomFieldFilterOptions => {
32
+ if (!conditions || Object.keys(conditions).length === 0) {
33
+ return {};
34
+ }
32
35
  // Build the WHERE clause for custom field filtering
33
- const customFieldConditions = Object.entries(conditions)
36
+ const conditionsStrings = Object.entries(conditions)
34
37
  .map(
35
38
  ([key, value]) => {
36
39
  if (Array.isArray(value)) {
40
+ if (value.length === 0) {
41
+ // if empty array, the condition is ignored
42
+ return false;
43
+ }
37
44
  const values = value.map((v) => `'${v}'`).join(',');
38
45
  return `custom_fields->>'${key}' IN (${values})`;
39
46
  }
40
47
  return `custom_fields->>'${key}' = '${value}'`;
41
48
  },
42
49
  )
43
- .join(' AND ');
50
+ .filter(Boolean);
51
+
52
+ if (conditionsStrings.length === 0) {
53
+ return {};
54
+ }
55
+ const customFieldConditions = conditionsStrings.join(' AND ');
44
56
 
45
57
  const subQuery = `${'SELECT model_id FROM ('
46
58
  + 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '