@autofleet/sadot 0.7.3 → 0.7.4-beta-5d31a6b9.0

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/.env ADDED
@@ -0,0 +1,3 @@
1
+ DB_USERNAME=emiltayeb
2
+ DB_PASSWORD=123456
3
+ NODE_ENV=test
@@ -23,4 +23,5 @@ interface CustomFieldsSearchPayload {
23
23
  }
24
24
  export declare const generateRandomString: (length?: number) => string;
25
25
  export declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string, customFieldsTypesToExclude?: CustomFieldDefinitionType[]) => CustomFieldsSearchPayload;
26
+ export declare const excludeCustomFieldFilterScope: (modelType: string, keyToExclude: string, valuesToExclude: string | string[]) => Omit<CustomFieldsSearchPayload, 'replacements'>;
26
27
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateCustomFieldSearchQueryPayload = exports.generateRandomString = void 0;
3
+ exports.excludeCustomFieldFilterScope = exports.generateCustomFieldSearchQueryPayload = exports.generateRandomString = 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");
@@ -38,3 +38,29 @@ const generateCustomFieldSearchQueryPayload = (searchTerm, model, entityId, cust
38
38
  };
39
39
  };
40
40
  exports.generateCustomFieldSearchQueryPayload = generateCustomFieldSearchQueryPayload;
41
+ const excludeCustomFieldFilterScope = (modelType, keyToExclude, valuesToExclude) => {
42
+ const keyToIgnore = keyToExclude.split('.')[1];
43
+ const valsToIgnore = Array.isArray(valuesToExclude) ? valuesToExclude.map((v) => `'"${v}"'`).join(', ') : null;
44
+ const customFieldsQuery = valsToIgnore ? `AND cv.value IN (${valsToIgnore})` : '';
45
+ const existsCondition = `
46
+ EXISTS (
47
+ SELECT 1
48
+ FROM custom_field_values cv
49
+ INNER JOIN custom_field_definitions cd
50
+ ON cv.custom_field_definition_id = cd.id
51
+ WHERE
52
+ cv.model_id = "${modelType}"."id"
53
+ AND cd.model_type = '${modelType}'
54
+ AND cd.name = '${keyToIgnore}'
55
+ ${customFieldsQuery}
56
+ )
57
+ `;
58
+ return {
59
+ where: {
60
+ [sequelize_1.Op.and]: [
61
+ sequelize_typescript_1.Sequelize.literal(existsCondition),
62
+ ],
63
+ },
64
+ };
65
+ };
66
+ exports.excludeCustomFieldFilterScope = excludeCustomFieldFilterScope;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.7.3",
3
+ "version": "0.7.4-beta-5d31a6b9.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -112,6 +112,7 @@ export const customFieldsFilterScope = (
112
112
  if (conditionsStrings.length === 0) {
113
113
  return {};
114
114
  }
115
+
115
116
  const customFieldConditions = conditionsStrings.join(AND_DELIMETER);
116
117
  const subQuery = `${'SELECT model_id FROM ('
117
118
  + 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
@@ -1,5 +1,7 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
- import { type WhereOptions, Op, type BindOrReplacements } from 'sequelize';
2
+ import {
3
+ type WhereOptions, Op, type BindOrReplacements,
4
+ } from 'sequelize';
3
5
  import { type ModelStatic, Sequelize } from 'sequelize-typescript';
4
6
  import { randomInt } from 'node:crypto';
5
7
  import { CustomFieldDefinitionType } from '../constants';
@@ -64,3 +66,34 @@ export const generateCustomFieldSearchQueryPayload = (
64
66
  replacements: { searchTerm: `%${searchTerm}%` },
65
67
  };
66
68
  };
69
+
70
+ export const excludeCustomFieldFilterScope = (
71
+ modelType: string,
72
+ keyToExclude: string,
73
+ valuesToExclude: string | string[],
74
+ ): Omit<CustomFieldsSearchPayload, 'replacements'> => {
75
+ const keyToIgnore = keyToExclude.split('.')[1];
76
+ const valsToIgnore = Array.isArray(valuesToExclude) ? valuesToExclude.map((v) => `'"${v}"'`).join(', ') : null;
77
+ const customFieldsQuery = valsToIgnore ? `AND cv.value IN (${valsToIgnore})` : '';
78
+ const existsCondition = `
79
+ EXISTS (
80
+ SELECT 1
81
+ FROM custom_field_values cv
82
+ INNER JOIN custom_field_definitions cd
83
+ ON cv.custom_field_definition_id = cd.id
84
+ WHERE
85
+ cv.model_id = "${modelType}"."id"
86
+ AND cd.model_type = '${modelType}'
87
+ AND cd.name = '${keyToIgnore}'
88
+ ${customFieldsQuery}
89
+ )
90
+ `;
91
+
92
+ return {
93
+ where: {
94
+ [Op.and]: [
95
+ Sequelize.literal(existsCondition),
96
+ ],
97
+ },
98
+ };
99
+ };