@autofleet/sadot 0.13.5-beta-45e699d4.3 → 0.13.5-beta
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 +16 -0
- package/dist/hooks/hooks.js +24 -2
- package/dist/models/index.js +7 -0
- package/package.json +4 -3
- package/src/hooks/hooks.ts +34 -1
- package/.env +0 -3
package/dist/hooks/hooks.d.ts
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
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
|
+
}[];
|
|
2
17
|
/**
|
|
3
18
|
* Hook to handle validation and custom fields during creation
|
|
4
19
|
*/
|
|
@@ -15,3 +30,4 @@ export declare const beforeBulkCreate: (options: any) => void;
|
|
|
15
30
|
* Hook to enable individual hooks for bulk update operations
|
|
16
31
|
*/
|
|
17
32
|
export declare const beforeBulkUpdate: (options: any) => void;
|
|
33
|
+
export {};
|
package/dist/hooks/hooks.js
CHANGED
|
@@ -26,11 +26,12 @@ 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 = void 0;
|
|
29
|
+
exports.beforeBulkUpdate = exports.beforeBulkCreate = exports.beforeUpdate = exports.beforeCreate = exports.formatAjvErrors = 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"));
|
|
34
35
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
35
36
|
const ValidatorRepo = __importStar(require("../repository/validator"));
|
|
36
37
|
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
@@ -48,6 +49,7 @@ const ajv = new ajv_1.default({
|
|
|
48
49
|
$data: true, // Enable $data references
|
|
49
50
|
});
|
|
50
51
|
(0, ajv_formats_1.default)(ajv);
|
|
52
|
+
(0, ajv_errors_1.default)(ajv);
|
|
51
53
|
/**
|
|
52
54
|
* Helper function to manually copy object properties
|
|
53
55
|
* This is more efficient for large objects and avoids excessive object creation
|
|
@@ -88,6 +90,21 @@ const getCompleteCustomFields = async (instance, options) => {
|
|
|
88
90
|
}
|
|
89
91
|
return instance.customFields || {};
|
|
90
92
|
};
|
|
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;
|
|
91
108
|
/**
|
|
92
109
|
* Validates the model using custom validators
|
|
93
110
|
*/
|
|
@@ -172,8 +189,13 @@ const validateModel = async (instance, options, scopeAttributes, isCreate = fals
|
|
|
172
189
|
},
|
|
173
190
|
})));
|
|
174
191
|
if (!isValid) {
|
|
192
|
+
const formattedErrors = (0, exports.formatAjvErrors)(validateSchema.errors);
|
|
193
|
+
console.log(JSON.stringify({ AAAAAAA: formattedErrors, BBBBB: validateSchema.errors }));
|
|
175
194
|
const errorDetails = validateSchema.errors?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
|
|
176
|
-
throw new errors_1.BadRequest([
|
|
195
|
+
throw new errors_1.BadRequest([
|
|
196
|
+
new Error(`Validation failed for ${modelType}: ${errorDetails}`),
|
|
197
|
+
...formattedErrors.map((err) => new Error(err.message)),
|
|
198
|
+
]);
|
|
177
199
|
}
|
|
178
200
|
}
|
|
179
201
|
}
|
package/dist/models/index.js
CHANGED
|
@@ -59,6 +59,13 @@ 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
|
+
});
|
|
62
69
|
return {
|
|
63
70
|
where: {
|
|
64
71
|
entityId: [
|
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",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -31,6 +31,7 @@
|
|
|
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",
|
|
34
35
|
"ajv-formats": "^3.0.1",
|
|
35
36
|
"http-status-codes": "^2.3.0",
|
|
36
37
|
"joi": "^17.7.0",
|
|
@@ -42,7 +43,7 @@
|
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@autofleet/errors": "^3.0.1",
|
|
44
45
|
"@autofleet/logger": "^4.2.1",
|
|
45
|
-
"@autofleet/node-common": "4.0.
|
|
46
|
+
"@autofleet/node-common": "^4.0.2",
|
|
46
47
|
"@autofleet/zehut": "^3.2.0",
|
|
47
48
|
"@types/express": "^4.17.17",
|
|
48
49
|
"@types/jest": "^29.5.13",
|
|
@@ -62,7 +63,7 @@
|
|
|
62
63
|
"peerDependencies": {
|
|
63
64
|
"@autofleet/errors": "^3",
|
|
64
65
|
"@autofleet/logger": "^4",
|
|
65
|
-
"@autofleet/node-common": "^4
|
|
66
|
+
"@autofleet/node-common": "^4",
|
|
66
67
|
"@autofleet/sheilta": "^2",
|
|
67
68
|
"@autofleet/zehut": "^3"
|
|
68
69
|
},
|
package/src/hooks/hooks.ts
CHANGED
|
@@ -3,6 +3,7 @@ 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';
|
|
6
7
|
import logger from '../utils/logger';
|
|
7
8
|
import * as ValidatorRepo from '../repository/validator';
|
|
8
9
|
import * as DefinitionRepo from '../repository/definition';
|
|
@@ -25,6 +26,7 @@ const ajv = new Ajv({
|
|
|
25
26
|
});
|
|
26
27
|
|
|
27
28
|
addFormats(ajv);
|
|
29
|
+
ajvErrors(ajv);
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* Helper function to manually copy object properties
|
|
@@ -76,6 +78,33 @@ const getCompleteCustomFields = async (instance, options): Promise<Record<string
|
|
|
76
78
|
return instance.customFields || {};
|
|
77
79
|
};
|
|
78
80
|
|
|
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
|
+
|
|
79
108
|
/**
|
|
80
109
|
* Validates the model using custom validators
|
|
81
110
|
*/
|
|
@@ -186,10 +215,14 @@ const validateModel = async (
|
|
|
186
215
|
})));
|
|
187
216
|
|
|
188
217
|
if (!isValid) {
|
|
218
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
189
219
|
const errorDetails = validateSchema.errors?.map((err) =>
|
|
190
220
|
`${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
|
|
191
221
|
|
|
192
|
-
throw new BadRequest([
|
|
222
|
+
throw new BadRequest([
|
|
223
|
+
new Error(`Validation failed for ${modelType}: ${errorDetails}`),
|
|
224
|
+
...formattedErrors.map((err) => new Error(err.message)),
|
|
225
|
+
]);
|
|
193
226
|
}
|
|
194
227
|
}
|
|
195
228
|
} else {
|
package/.env
DELETED