@autofleet/sadot 0.13.5-beta → 0.13.5-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/hooks/hooks.d.ts
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
import type { CustomFieldOptions, ModelOptions } from '../types';
|
|
2
|
-
type AjvError = {
|
|
3
|
-
instancePath?: string;
|
|
4
|
-
keyword: string;
|
|
5
|
-
message?: string;
|
|
6
|
-
params?: Record<string, any>;
|
|
7
|
-
schemaPath?: string;
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
};
|
|
10
|
-
export declare const formatAjvErrors: (errors?: AjvError[]) => {
|
|
11
|
-
path: string;
|
|
12
|
-
keyword: string;
|
|
13
|
-
message: string;
|
|
14
|
-
errorCode: string;
|
|
15
|
-
details: Record<string, any>;
|
|
16
|
-
}[];
|
|
17
2
|
/**
|
|
18
3
|
* Hook to handle validation and custom fields during creation
|
|
19
4
|
*/
|
|
@@ -30,4 +15,3 @@ export declare const beforeBulkCreate: (options: any) => void;
|
|
|
30
15
|
* Hook to enable individual hooks for bulk update operations
|
|
31
16
|
*/
|
|
32
17
|
export declare const beforeBulkUpdate: (options: any) => void;
|
|
33
|
-
export {};
|
package/dist/hooks/hooks.js
CHANGED
|
@@ -26,12 +26,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.beforeBulkUpdate = exports.beforeBulkCreate = exports.beforeUpdate = exports.beforeCreate =
|
|
29
|
+
exports.beforeBulkUpdate = exports.beforeBulkCreate = exports.beforeUpdate = exports.beforeCreate = void 0;
|
|
30
30
|
const ajv_1 = __importDefault(require("ajv"));
|
|
31
31
|
const joi_1 = __importDefault(require("joi"));
|
|
32
32
|
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
33
33
|
const errors_1 = require("@autofleet/errors");
|
|
34
|
-
const ajv_errors_1 = __importDefault(require("ajv-errors"));
|
|
35
34
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
36
35
|
const ValidatorRepo = __importStar(require("../repository/validator"));
|
|
37
36
|
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
@@ -49,7 +48,6 @@ const ajv = new ajv_1.default({
|
|
|
49
48
|
$data: true, // Enable $data references
|
|
50
49
|
});
|
|
51
50
|
(0, ajv_formats_1.default)(ajv);
|
|
52
|
-
(0, ajv_errors_1.default)(ajv);
|
|
53
51
|
/**
|
|
54
52
|
* Helper function to manually copy object properties
|
|
55
53
|
* This is more efficient for large objects and avoids excessive object creation
|
|
@@ -90,25 +88,10 @@ const getCompleteCustomFields = async (instance, options) => {
|
|
|
90
88
|
}
|
|
91
89
|
return instance.customFields || {};
|
|
92
90
|
};
|
|
93
|
-
const formatAjvErrors = (errors = []) => errors.map((err) => {
|
|
94
|
-
const path = err.instancePath || '/';
|
|
95
|
-
const { keyword } = err;
|
|
96
|
-
const message = err.message || 'Invalid value';
|
|
97
|
-
const normalizedPath = path.replace(/\//g, '_').replace(/^_/, '');
|
|
98
|
-
const errorCode = `${keyword}_${normalizedPath}`;
|
|
99
|
-
return {
|
|
100
|
-
path,
|
|
101
|
-
keyword,
|
|
102
|
-
message,
|
|
103
|
-
errorCode,
|
|
104
|
-
details: err.params || {},
|
|
105
|
-
};
|
|
106
|
-
});
|
|
107
|
-
exports.formatAjvErrors = formatAjvErrors;
|
|
108
91
|
/**
|
|
109
92
|
* Validates the model using custom validators
|
|
110
93
|
*/
|
|
111
|
-
const validateModel = async (instance, options, scopeAttributes, isCreate = false) => {
|
|
94
|
+
const validateModel = async (instance, options, scopeAttributes, modelOptions = {}, isCreate = false) => {
|
|
112
95
|
var _a;
|
|
113
96
|
const modelType = instance.constructor.name;
|
|
114
97
|
logger_1.default.debug('sadot - validating model', { isCreate, modelType });
|
|
@@ -138,6 +121,9 @@ const validateModel = async (instance, options, scopeAttributes, isCreate = fals
|
|
|
138
121
|
validatorsPromise = ValidatorRepo.findAllByModelType(modelType, entityId, {
|
|
139
122
|
transaction: options.transaction,
|
|
140
123
|
attributes: CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL,
|
|
124
|
+
...(modelOptions.include && {
|
|
125
|
+
include: modelOptions.include?.(entityId),
|
|
126
|
+
}),
|
|
141
127
|
raw: true,
|
|
142
128
|
});
|
|
143
129
|
if (options.transaction) {
|
|
@@ -189,13 +175,8 @@ const validateModel = async (instance, options, scopeAttributes, isCreate = fals
|
|
|
189
175
|
},
|
|
190
176
|
})));
|
|
191
177
|
if (!isValid) {
|
|
192
|
-
const formattedErrors = (0, exports.formatAjvErrors)(validateSchema.errors);
|
|
193
|
-
console.log(JSON.stringify({ AAAAAAA: formattedErrors, BBBBB: validateSchema.errors }));
|
|
194
178
|
const errorDetails = validateSchema.errors?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
|
|
195
|
-
throw new errors_1.BadRequest([
|
|
196
|
-
new Error(`Validation failed for ${modelType}: ${errorDetails}`),
|
|
197
|
-
...formattedErrors.map((err) => new Error(err.message)),
|
|
198
|
-
]);
|
|
179
|
+
throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)]);
|
|
199
180
|
}
|
|
200
181
|
}
|
|
201
182
|
}
|
|
@@ -292,7 +273,7 @@ const beforeCreate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCu
|
|
|
292
273
|
throw new errors_2.MissingRequiredCustomFieldError(missingFields);
|
|
293
274
|
}
|
|
294
275
|
// Step 2: Validate the model data (including custom fields)
|
|
295
|
-
await validateModel(instance, options, scopeAttributes, true);
|
|
276
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, true);
|
|
296
277
|
// format date and datetime fields
|
|
297
278
|
formatDates(fieldDefinitions, instance);
|
|
298
279
|
// Step 3: Save custom field values if they exist
|
|
@@ -330,7 +311,7 @@ const beforeUpdate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCu
|
|
|
330
311
|
modelType, modelOptions, identifiers, options,
|
|
331
312
|
});
|
|
332
313
|
// Step 1: Validate the model data (including custom fields)
|
|
333
|
-
await validateModel(instance, options, scopeAttributes, false);
|
|
314
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, false);
|
|
334
315
|
// format date and datetime fields
|
|
335
316
|
formatDates(fieldDefinitions, instance);
|
|
336
317
|
// Step 2: Update custom field values if they exist
|
package/dist/models/index.js
CHANGED
|
@@ -59,13 +59,6 @@ const initTables = async (sequelize, getUser, { schemaPrefix = SADOT_MIGRATION_P
|
|
|
59
59
|
if (!user?.permissions) {
|
|
60
60
|
return {};
|
|
61
61
|
}
|
|
62
|
-
console.log({
|
|
63
|
-
PPPPPPPP: JSON.stringify([
|
|
64
|
-
...Object.keys(user.permissions.fleets),
|
|
65
|
-
...Object.keys(user.permissions.businessModels),
|
|
66
|
-
...Object.keys(user.permissions.demandSources),
|
|
67
|
-
]),
|
|
68
|
-
});
|
|
69
62
|
return {
|
|
70
63
|
where: {
|
|
71
64
|
entityId: [
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { Transactionable } from 'sequelize';
|
|
1
|
+
import type { IncludeOptions, Transactionable } from 'sequelize';
|
|
2
2
|
import { CustomValidator } from '../models';
|
|
3
3
|
export interface FindValidatorOptions extends Transactionable {
|
|
4
4
|
withDisabled?: boolean;
|
|
5
5
|
attributes?: string[];
|
|
6
6
|
raw?: boolean;
|
|
7
|
+
include?: IncludeOptions[];
|
|
7
8
|
}
|
|
8
9
|
export interface ValidatorAttributes {
|
|
9
10
|
entityId: string;
|
|
@@ -40,7 +40,9 @@ const findAllByModelType = async (modelType, entityId, options = { withDisabled:
|
|
|
40
40
|
logger_1.default.debug('custom-validator - find all validators by model type');
|
|
41
41
|
return (0, exports.findAll)({
|
|
42
42
|
modelType,
|
|
43
|
-
|
|
43
|
+
...(!options.include && {
|
|
44
|
+
entityId,
|
|
45
|
+
}),
|
|
44
46
|
}, options);
|
|
45
47
|
};
|
|
46
48
|
exports.findAllByModelType = findAllByModelType;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sadot",
|
|
3
|
-
"version": "0.13.5-beta",
|
|
3
|
+
"version": "0.13.5-beta-1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
"@autofleet/common-types": "^4.4.0",
|
|
32
32
|
"@autofleet/events": "^4.0.0",
|
|
33
33
|
"ajv": "^8.12.0",
|
|
34
|
-
"ajv-errors": "^3.0.0",
|
|
35
34
|
"ajv-formats": "^3.0.1",
|
|
36
35
|
"http-status-codes": "^2.3.0",
|
|
37
36
|
"joi": "^17.7.0",
|
package/src/hooks/hooks.ts
CHANGED
|
@@ -3,7 +3,6 @@ import Ajv from 'ajv';
|
|
|
3
3
|
import Joi from 'joi';
|
|
4
4
|
import addFormats from 'ajv-formats';
|
|
5
5
|
import { BadRequest } from '@autofleet/errors';
|
|
6
|
-
import ajvErrors from 'ajv-errors';
|
|
7
6
|
import logger from '../utils/logger';
|
|
8
7
|
import * as ValidatorRepo from '../repository/validator';
|
|
9
8
|
import * as DefinitionRepo from '../repository/definition';
|
|
@@ -26,7 +25,6 @@ const ajv = new Ajv({
|
|
|
26
25
|
});
|
|
27
26
|
|
|
28
27
|
addFormats(ajv);
|
|
29
|
-
ajvErrors(ajv);
|
|
30
28
|
|
|
31
29
|
/**
|
|
32
30
|
* Helper function to manually copy object properties
|
|
@@ -78,33 +76,6 @@ const getCompleteCustomFields = async (instance, options): Promise<Record<string
|
|
|
78
76
|
return instance.customFields || {};
|
|
79
77
|
};
|
|
80
78
|
|
|
81
|
-
type AjvError = {
|
|
82
|
-
instancePath?: string;
|
|
83
|
-
keyword: string;
|
|
84
|
-
message?: string;
|
|
85
|
-
params?: Record<string, any>;
|
|
86
|
-
schemaPath?: string;
|
|
87
|
-
[key: string]: any;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export const formatAjvErrors = (errors: AjvError[] = []) =>
|
|
91
|
-
errors.map((err) => {
|
|
92
|
-
const path = err.instancePath || '/';
|
|
93
|
-
const { keyword } = err;
|
|
94
|
-
const message = err.message || 'Invalid value';
|
|
95
|
-
|
|
96
|
-
const normalizedPath = path.replace(/\//g, '_').replace(/^_/, '');
|
|
97
|
-
const errorCode = `${keyword}_${normalizedPath}`;
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
path,
|
|
101
|
-
keyword,
|
|
102
|
-
message,
|
|
103
|
-
errorCode,
|
|
104
|
-
details: err.params || {},
|
|
105
|
-
};
|
|
106
|
-
});
|
|
107
|
-
|
|
108
79
|
/**
|
|
109
80
|
* Validates the model using custom validators
|
|
110
81
|
*/
|
|
@@ -112,6 +83,7 @@ const validateModel = async (
|
|
|
112
83
|
instance,
|
|
113
84
|
options,
|
|
114
85
|
scopeAttributes: string[],
|
|
86
|
+
modelOptions: ModelOptions = {},
|
|
115
87
|
isCreate = false,
|
|
116
88
|
): Promise<void> => {
|
|
117
89
|
const modelType = instance.constructor.name;
|
|
@@ -153,6 +125,9 @@ const validateModel = async (
|
|
|
153
125
|
{
|
|
154
126
|
transaction: options.transaction,
|
|
155
127
|
attributes: CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL,
|
|
128
|
+
...(modelOptions.include && {
|
|
129
|
+
include: modelOptions.include?.(entityId),
|
|
130
|
+
}),
|
|
156
131
|
raw: true,
|
|
157
132
|
},
|
|
158
133
|
);
|
|
@@ -215,14 +190,10 @@ const validateModel = async (
|
|
|
215
190
|
})));
|
|
216
191
|
|
|
217
192
|
if (!isValid) {
|
|
218
|
-
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
219
193
|
const errorDetails = validateSchema.errors?.map((err) =>
|
|
220
194
|
`${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
|
|
221
195
|
|
|
222
|
-
throw new BadRequest([
|
|
223
|
-
new Error(`Validation failed for ${modelType}: ${errorDetails}`),
|
|
224
|
-
...formattedErrors.map((err) => new Error(err.message)),
|
|
225
|
-
]);
|
|
196
|
+
throw new BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)]);
|
|
226
197
|
}
|
|
227
198
|
}
|
|
228
199
|
} else {
|
|
@@ -354,7 +325,7 @@ export const beforeCreate = (
|
|
|
354
325
|
}
|
|
355
326
|
|
|
356
327
|
// Step 2: Validate the model data (including custom fields)
|
|
357
|
-
await validateModel(instance, options, scopeAttributes, true);
|
|
328
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, true);
|
|
358
329
|
|
|
359
330
|
// format date and datetime fields
|
|
360
331
|
formatDates(fieldDefinitions, instance);
|
|
@@ -405,7 +376,7 @@ export const beforeUpdate = (
|
|
|
405
376
|
});
|
|
406
377
|
|
|
407
378
|
// Step 1: Validate the model data (including custom fields)
|
|
408
|
-
await validateModel(instance, options, scopeAttributes, false);
|
|
379
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, false);
|
|
409
380
|
|
|
410
381
|
// format date and datetime fields
|
|
411
382
|
formatDates(fieldDefinitions, instance);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Transactionable } from 'sequelize';
|
|
1
|
+
import type { IncludeOptions, Transactionable } from 'sequelize';
|
|
2
2
|
import logger from '../utils/logger';
|
|
3
3
|
import { CustomValidator } from '../models';
|
|
4
4
|
|
|
@@ -6,6 +6,7 @@ export interface FindValidatorOptions extends Transactionable {
|
|
|
6
6
|
withDisabled?: boolean;
|
|
7
7
|
attributes?: string[];
|
|
8
8
|
raw?: boolean;
|
|
9
|
+
include?: IncludeOptions[];
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
// Make sure this interface is compatible with the Sequelize model
|
|
@@ -68,7 +69,9 @@ export const findAllByModelType = async (
|
|
|
68
69
|
return findAll(
|
|
69
70
|
{
|
|
70
71
|
modelType,
|
|
71
|
-
|
|
72
|
+
...(!options.include && {
|
|
73
|
+
entityId,
|
|
74
|
+
}),
|
|
72
75
|
},
|
|
73
76
|
options,
|
|
74
77
|
);
|