@autofleet/sadot 0.6.10 → 0.7.0-beta.1
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/repository/value.js +3 -6
- package/dist/scopes/filter.d.ts +9 -3
- package/dist/scopes/filter.js +21 -23
- package/dist/tests/helpers/database-config.d.ts +4 -1
- package/dist/tests/helpers/database-config.js +1 -1
- package/package.json +2 -2
- package/src/repository/value.ts +3 -6
- package/src/scopes/filter.ts +21 -25
- package/src/tests/helpers/database-config.ts +1 -1
package/dist/repository/value.js
CHANGED
|
@@ -73,11 +73,8 @@ exports.findValuesByModelIds = findValuesByModelIds;
|
|
|
73
73
|
*/
|
|
74
74
|
const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}) => {
|
|
75
75
|
const names = Object.keys(valuesToUpdate);
|
|
76
|
-
logger_1.default.
|
|
77
|
-
names,
|
|
78
|
-
optionsKeys: options ? Object.keys(options) : null,
|
|
79
|
-
valuesToUpdate,
|
|
80
|
-
identifiers,
|
|
76
|
+
logger_1.default.info(`custom-fields: updating values for ${modelType} ${modelId}`, {
|
|
77
|
+
names, options, valuesToUpdate, identifiers,
|
|
81
78
|
});
|
|
82
79
|
const { modelOptions, transaction } = options;
|
|
83
80
|
const where = {
|
|
@@ -90,7 +87,7 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
|
|
|
90
87
|
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) || [];
|
|
91
88
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
92
89
|
if (fieldDefinitions.length !== names.length) {
|
|
93
|
-
logger_1.default.
|
|
90
|
+
logger_1.default.info(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
|
|
94
91
|
const missingDefinitions = names.filter((name) => !fieldDefinitions.some((def) => def.name === name));
|
|
95
92
|
throw new errors_1.MissingDefinitionError(missingDefinitions);
|
|
96
93
|
}
|
package/dist/scopes/filter.d.ts
CHANGED
|
@@ -24,9 +24,15 @@ export type CustomFieldFilterOptions = {
|
|
|
24
24
|
* @param name - The model type name used to join custom_field_definitions.
|
|
25
25
|
* @returns A function that takes conditions and returns the Sequelize options object.
|
|
26
26
|
*/
|
|
27
|
-
export declare const customFieldsFilterScope: (name: string) => (
|
|
27
|
+
export declare const customFieldsFilterScope: (name: string) => ({ replacementsMap, scopeValue }: {
|
|
28
|
+
replacementsMap: any;
|
|
29
|
+
scopeValue: any;
|
|
30
|
+
}) => CustomFieldFilterOptions;
|
|
28
31
|
export declare const scopeName = "filterByCustomFields";
|
|
29
|
-
export declare const customFieldsSortScope: (name: string) => (
|
|
32
|
+
export declare const customFieldsSortScope: (name: string) => ({ replacementsMap, scopeValue: sort }: {
|
|
33
|
+
replacementsMap: any;
|
|
34
|
+
scopeValue: any;
|
|
35
|
+
}) => {
|
|
30
36
|
attributes?: undefined;
|
|
31
37
|
order?: undefined;
|
|
32
38
|
replacements?: undefined;
|
|
@@ -35,6 +41,6 @@ export declare const customFieldsSortScope: (name: string) => (sort: CustomField
|
|
|
35
41
|
include: (string | import("sequelize/types/utils").Literal)[][];
|
|
36
42
|
};
|
|
37
43
|
order: import("sequelize/types/utils").Literal[];
|
|
38
|
-
replacements:
|
|
44
|
+
replacements: any;
|
|
39
45
|
};
|
|
40
46
|
export {};
|
package/dist/scopes/filter.js
CHANGED
|
@@ -28,18 +28,19 @@ const AND_DELIMETER = ' AND ';
|
|
|
28
28
|
* @param name - The model type name used to join custom_field_definitions.
|
|
29
29
|
* @returns A function that takes conditions and returns the Sequelize options object.
|
|
30
30
|
*/
|
|
31
|
-
const customFieldsFilterScope = (name) => (
|
|
31
|
+
const customFieldsFilterScope = (name) => ({ replacementsMap, scopeValue }) => {
|
|
32
|
+
const conditions = scopeValue;
|
|
33
|
+
const replacements = replacementsMap;
|
|
34
|
+
console.log('filter replacementsMap:', replacements);
|
|
32
35
|
if (!conditions || Object.keys(conditions).length === 0) {
|
|
33
36
|
return {};
|
|
34
37
|
}
|
|
35
|
-
const ConditionNameRandomStr = (0, helpers_1.generateRandomString)();
|
|
36
|
-
const replacements = {};
|
|
37
|
-
replacements[ConditionNameRandomStr] = `${name}`;
|
|
38
38
|
// Build the WHERE clause for custom field filtering
|
|
39
39
|
const conditionsStrings = Object.entries(conditions)
|
|
40
40
|
.map(([key, condition]) => {
|
|
41
|
-
const replacemetKey = (
|
|
42
|
-
|
|
41
|
+
const replacemetKey = Object.keys(replacements).find((randomString) => replacements[randomString] === key);
|
|
42
|
+
if (!replacemetKey)
|
|
43
|
+
return false;
|
|
43
44
|
if (Array.isArray(condition)) {
|
|
44
45
|
if (condition.length === 0) {
|
|
45
46
|
// if empty array, the condition is ignored
|
|
@@ -47,41 +48,39 @@ const customFieldsFilterScope = (name) => (conditions) => {
|
|
|
47
48
|
}
|
|
48
49
|
if (typeof condition[0] === 'string') {
|
|
49
50
|
const values = condition.map((v) => {
|
|
50
|
-
const valRandom = (
|
|
51
|
-
replacements[`${valRandom}`] = `${v}`;
|
|
51
|
+
const valRandom = Object.keys(replacements).find((randomString) => replacements[randomString] === v);
|
|
52
52
|
return ` :${valRandom} `;
|
|
53
53
|
}).join(',');
|
|
54
54
|
return `(custom_fields->> :${replacemetKey} ) IN ( ${values} )`;
|
|
55
55
|
}
|
|
56
56
|
return condition
|
|
57
57
|
.map((c) => {
|
|
58
|
-
const valRep = (
|
|
59
|
-
replacements[valRep] = `${c.value}`;
|
|
58
|
+
const valRep = Object.keys(replacements).find((key) => replacements[key] === c.value);
|
|
60
59
|
return `(custom_fields->> :${replacemetKey} )${castIfNeeded(c.value)} ${c.operator} :${valRep}`;
|
|
61
60
|
}).join(AND_DELIMETER);
|
|
62
61
|
}
|
|
63
|
-
if (typeof condition === 'string'
|
|
64
|
-
const conditionRep = (
|
|
65
|
-
replacements[conditionRep] = `${condition}`;
|
|
62
|
+
if (typeof condition === 'string') {
|
|
63
|
+
const conditionRep = Object.keys(replacements).find((key) => replacements[key] === condition);
|
|
66
64
|
return `(custom_fields->> :${replacemetKey} ) ${castIfNeeded(condition)} = :${conditionRep}`;
|
|
67
65
|
}
|
|
68
66
|
if (condition?.operator) {
|
|
69
|
-
const valueRep = (
|
|
70
|
-
replacements[valueRep] = `${condition.value}`;
|
|
67
|
+
const valueRep = Object.keys(replacements).find((key) => replacements[key] === condition.value);
|
|
71
68
|
return `(custom_fields->> :${replacemetKey} ) ${castIfNeeded(condition.value)} ${condition.operator} :${valueRep}`;
|
|
72
69
|
}
|
|
73
70
|
return false;
|
|
74
71
|
})
|
|
75
72
|
.filter(Boolean);
|
|
76
73
|
if (conditionsStrings.length === 0) {
|
|
77
|
-
return {
|
|
74
|
+
return {
|
|
75
|
+
replacements,
|
|
76
|
+
};
|
|
78
77
|
}
|
|
79
78
|
const customFieldConditions = conditionsStrings.join(AND_DELIMETER);
|
|
80
79
|
const subQuery = `${'SELECT model_id FROM ('
|
|
81
80
|
+ 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
|
|
82
81
|
+ 'FROM custom_field_values AS cv '
|
|
83
82
|
+ 'INNER JOIN custom_field_definitions AS cd ON cv.custom_field_definition_id = cd.id '
|
|
84
|
-
+ `AND cd.model_type =
|
|
83
|
+
+ `AND cd.model_type = '${name}'`
|
|
85
84
|
+ 'GROUP BY cv.model_id'
|
|
86
85
|
+ ') AS CustomFieldAggregation WHERE '} ${customFieldConditions}`;
|
|
87
86
|
return {
|
|
@@ -95,15 +94,14 @@ const customFieldsFilterScope = (name) => (conditions) => {
|
|
|
95
94
|
};
|
|
96
95
|
exports.customFieldsFilterScope = customFieldsFilterScope;
|
|
97
96
|
exports.scopeName = CUSTOM_FIELDS_FILTER_SCOPE;
|
|
98
|
-
const customFieldsSortScope = (name) => (sort) => {
|
|
97
|
+
const customFieldsSortScope = (name) => ({ replacementsMap, scopeValue: sort }) => {
|
|
99
98
|
if (!sort || sort.length === 0) {
|
|
100
99
|
return {};
|
|
101
100
|
}
|
|
102
101
|
const randomStr = (0, helpers_1.generateRandomString)();
|
|
103
|
-
const replacements = {};
|
|
104
102
|
const includes = Object.entries(sort).map(([key]) => {
|
|
105
|
-
const
|
|
106
|
-
|
|
103
|
+
const replacemetKey = Object.keys(replacementsMap).find((randomString) => replacementsMap[randomString] === key);
|
|
104
|
+
console.log('sort replacemetKey:', replacemetKey);
|
|
107
105
|
return ([
|
|
108
106
|
sequelize_typescript_1.Sequelize.literal(`(
|
|
109
107
|
SELECT value
|
|
@@ -112,7 +110,7 @@ const customFieldsSortScope = (name) => (sort) => {
|
|
|
112
110
|
ON cv.custom_field_definition_id = cd.id
|
|
113
111
|
AND cd.model_type = '${name}'
|
|
114
112
|
WHERE cv.model_id = "${name}"."id"
|
|
115
|
-
AND cd.name = :${
|
|
113
|
+
AND cd.name = :${replacemetKey}
|
|
116
114
|
) AS CustomFieldAggregation
|
|
117
115
|
)
|
|
118
116
|
`), randomStr,
|
|
@@ -124,7 +122,7 @@ const customFieldsSortScope = (name) => (sort) => {
|
|
|
124
122
|
include: includes,
|
|
125
123
|
},
|
|
126
124
|
order: orders,
|
|
127
|
-
replacements,
|
|
125
|
+
replacements: replacementsMap,
|
|
128
126
|
};
|
|
129
127
|
};
|
|
130
128
|
exports.customFieldsSortScope = customFieldsSortScope;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sadot",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0-beta.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"linter": "./node_modules/.bin/eslint .",
|
|
10
10
|
"test": "jest --forceExit --runInBand",
|
|
11
11
|
"coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
|
|
12
|
-
"build-to-local-repo": "npm run build && cp -r dist/*
|
|
12
|
+
"build-to-local-repo": "npm run build && cp -r dist/* ../task-ms/node_modules/@autofleet/sadot/dist",
|
|
13
13
|
"dev": "nodemon",
|
|
14
14
|
"watch": "npm-watch build-to-local-repo",
|
|
15
15
|
"publish-dev": "npm run build && npm publish --tag dev"
|
package/src/repository/value.ts
CHANGED
|
@@ -55,11 +55,8 @@ export const updateValues = async (
|
|
|
55
55
|
options: FindOptions & { modelOptions?: ModelOptions } = {},
|
|
56
56
|
): Promise<CustomFieldValue[]> => {
|
|
57
57
|
const names = Object.keys(valuesToUpdate);
|
|
58
|
-
logger.
|
|
59
|
-
names,
|
|
60
|
-
optionsKeys: options ? Object.keys(options) : null,
|
|
61
|
-
valuesToUpdate,
|
|
62
|
-
identifiers,
|
|
58
|
+
logger.info(`custom-fields: updating values for ${modelType} ${modelId}`, {
|
|
59
|
+
names, options, valuesToUpdate, identifiers,
|
|
63
60
|
});
|
|
64
61
|
const { modelOptions, transaction } = options;
|
|
65
62
|
|
|
@@ -75,7 +72,7 @@ export const updateValues = async (
|
|
|
75
72
|
|
|
76
73
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
77
74
|
if (fieldDefinitions.length !== names.length) {
|
|
78
|
-
logger.
|
|
75
|
+
logger.info(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
|
|
79
76
|
const missingDefinitions = names.filter((name) => !fieldDefinitions.some((def) => def.name === name));
|
|
80
77
|
throw new MissingDefinitionError(missingDefinitions);
|
|
81
78
|
}
|
package/src/scopes/filter.ts
CHANGED
|
@@ -47,20 +47,20 @@ const AND_DELIMETER = ' AND ';
|
|
|
47
47
|
*/
|
|
48
48
|
export const customFieldsFilterScope = (
|
|
49
49
|
name: string,
|
|
50
|
-
) => (
|
|
50
|
+
) => ({ replacementsMap, scopeValue }): CustomFieldFilterOptions => {
|
|
51
|
+
const conditions: Record<string, ConditionValue> = scopeValue;
|
|
52
|
+
const replacements: Record<string, string> = replacementsMap;
|
|
53
|
+
console.log('filter replacementsMap:', replacements);
|
|
51
54
|
if (!conditions || Object.keys(conditions).length === 0) {
|
|
52
55
|
return {};
|
|
53
56
|
}
|
|
54
|
-
const ConditionNameRandomStr = generateRandomString();
|
|
55
|
-
const replacements: Record<string, string> = {};
|
|
56
|
-
replacements[ConditionNameRandomStr] = `${name}`;
|
|
57
|
-
|
|
58
57
|
// Build the WHERE clause for custom field filtering
|
|
59
58
|
const conditionsStrings = Object.entries(conditions)
|
|
60
59
|
.map(
|
|
61
60
|
([key, condition]) => {
|
|
62
|
-
const replacemetKey =
|
|
63
|
-
|
|
61
|
+
const replacemetKey = Object.keys(replacements).find((randomString) => replacements[randomString] === key);
|
|
62
|
+
if (!replacemetKey) return false;
|
|
63
|
+
|
|
64
64
|
if (Array.isArray(condition)) {
|
|
65
65
|
if (condition.length === 0) {
|
|
66
66
|
// if empty array, the condition is ignored
|
|
@@ -68,43 +68,40 @@ export const customFieldsFilterScope = (
|
|
|
68
68
|
}
|
|
69
69
|
if (typeof condition[0] === 'string') {
|
|
70
70
|
const values = condition.map((v) => {
|
|
71
|
-
const valRandom =
|
|
72
|
-
replacements[`${valRandom}`] = `${v}`;
|
|
71
|
+
const valRandom = Object.keys(replacements).find((randomString) => replacements[randomString] === v);
|
|
73
72
|
return ` :${valRandom} `;
|
|
74
73
|
}).join(',');
|
|
75
74
|
return `(custom_fields->> :${replacemetKey} ) IN ( ${values} )`;
|
|
76
75
|
}
|
|
77
76
|
return condition
|
|
78
77
|
.map((c) => {
|
|
79
|
-
const valRep =
|
|
80
|
-
replacements[valRep] = `${c.value}`;
|
|
78
|
+
const valRep = Object.keys(replacements).find((key) => replacements[key] === c.value);
|
|
81
79
|
return `(custom_fields->> :${replacemetKey} )${castIfNeeded(c.value)} ${c.operator} :${valRep}`;
|
|
82
80
|
}).join(AND_DELIMETER);
|
|
83
81
|
}
|
|
84
|
-
if (typeof condition === 'string'
|
|
85
|
-
const conditionRep =
|
|
86
|
-
replacements[conditionRep] = `${condition}`;
|
|
82
|
+
if (typeof condition === 'string') {
|
|
83
|
+
const conditionRep = Object.keys(replacements).find((key) => replacements[key] === condition);
|
|
87
84
|
return `(custom_fields->> :${replacemetKey} ) ${castIfNeeded(condition)} = :${conditionRep}`;
|
|
88
85
|
}
|
|
89
86
|
if (condition?.operator) {
|
|
90
|
-
const valueRep =
|
|
91
|
-
replacements[valueRep] = `${condition.value}`;
|
|
87
|
+
const valueRep = Object.keys(replacements).find((key) => replacements[key] === condition.value);
|
|
92
88
|
return `(custom_fields->> :${replacemetKey} ) ${castIfNeeded(condition.value)} ${condition.operator} :${valueRep}`;
|
|
93
89
|
}
|
|
94
|
-
|
|
95
90
|
return false;
|
|
96
91
|
},
|
|
97
92
|
)
|
|
98
93
|
.filter(Boolean);
|
|
99
94
|
if (conditionsStrings.length === 0) {
|
|
100
|
-
return {
|
|
95
|
+
return {
|
|
96
|
+
replacements,
|
|
97
|
+
};
|
|
101
98
|
}
|
|
102
99
|
const customFieldConditions = conditionsStrings.join(AND_DELIMETER);
|
|
103
100
|
const subQuery = `${'SELECT model_id FROM ('
|
|
104
101
|
+ 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
|
|
105
102
|
+ 'FROM custom_field_values AS cv '
|
|
106
103
|
+ 'INNER JOIN custom_field_definitions AS cd ON cv.custom_field_definition_id = cd.id '
|
|
107
|
-
+ `AND cd.model_type =
|
|
104
|
+
+ `AND cd.model_type = '${name}'`
|
|
108
105
|
+ 'GROUP BY cv.model_id'
|
|
109
106
|
+ ') AS CustomFieldAggregation WHERE '} ${customFieldConditions}`;
|
|
110
107
|
return {
|
|
@@ -121,15 +118,14 @@ export const scopeName = CUSTOM_FIELDS_FILTER_SCOPE;
|
|
|
121
118
|
|
|
122
119
|
export const customFieldsSortScope = (
|
|
123
120
|
name: string,
|
|
124
|
-
) => (
|
|
121
|
+
) => ({ replacementsMap, scopeValue: sort }) => {
|
|
125
122
|
if (!sort || sort.length === 0) {
|
|
126
123
|
return {};
|
|
127
124
|
}
|
|
128
125
|
const randomStr = generateRandomString();
|
|
129
|
-
const replacements: Record<string, string> = {};
|
|
130
126
|
const includes = Object.entries(sort).map(([key]) => {
|
|
131
|
-
const
|
|
132
|
-
|
|
127
|
+
const replacemetKey = Object.keys(replacementsMap).find((randomString) => replacementsMap[randomString] === key);
|
|
128
|
+
console.log('sort replacemetKey:', replacemetKey);
|
|
133
129
|
return ([
|
|
134
130
|
Sequelize.literal(`(
|
|
135
131
|
SELECT value
|
|
@@ -138,7 +134,7 @@ export const customFieldsSortScope = (
|
|
|
138
134
|
ON cv.custom_field_definition_id = cd.id
|
|
139
135
|
AND cd.model_type = '${name}'
|
|
140
136
|
WHERE cv.model_id = "${name}"."id"
|
|
141
|
-
AND cd.name = :${
|
|
137
|
+
AND cd.name = :${replacemetKey}
|
|
142
138
|
) AS CustomFieldAggregation
|
|
143
139
|
)
|
|
144
140
|
`), randomStr,
|
|
@@ -151,6 +147,6 @@ export const customFieldsSortScope = (
|
|
|
151
147
|
include: includes,
|
|
152
148
|
},
|
|
153
149
|
order: orders,
|
|
154
|
-
replacements,
|
|
150
|
+
replacements: replacementsMap,
|
|
155
151
|
};
|
|
156
152
|
};
|