@autofleet/sadot 1.0.0-beta.1 → 1.0.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/README.md +5 -0
- package/dist/api/index.d.ts +2 -1
- package/dist/api/index.js +3 -2
- package/dist/api/v1/definition/index.d.ts +2 -1
- package/dist/api/v1/definition/index.js +12 -14
- package/dist/api/v1/definition/validations.js +14 -12
- package/dist/api/v1/errors.d.ts +3 -1
- package/dist/api/v1/errors.js +1 -4
- package/dist/api/v1/index.d.ts +2 -1
- package/dist/api/v1/index.js +5 -2
- package/dist/api/v1/validator/index.d.ts +3 -0
- package/dist/api/v1/validator/index.js +143 -0
- package/dist/api/v1/validator/validations.d.ts +6 -0
- package/dist/api/v1/validator/validations.js +40 -0
- package/dist/errors/index.d.ts +9 -1
- package/dist/errors/index.js +25 -4
- package/dist/events/index.d.ts +2 -1
- package/dist/events/index.js +17 -11
- package/dist/hooks/create.d.ts +2 -2
- package/dist/hooks/create.js +40 -19
- package/dist/hooks/enrich.d.ts +20 -2
- package/dist/hooks/enrich.js +88 -16
- package/dist/hooks/hooks.d.ts +17 -0
- package/dist/hooks/hooks.js +379 -0
- package/dist/hooks/index.d.ts +2 -3
- package/dist/hooks/index.js +6 -7
- package/dist/hooks/update.d.ts +2 -2
- package/dist/hooks/update.js +16 -26
- package/dist/hooks/utils/updateInstanceValues.d.ts +15 -0
- package/dist/hooks/utils/updateInstanceValues.js +50 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +19 -6
- package/dist/models/CustomFieldDefinition.d.ts +1 -0
- package/dist/models/CustomFieldDefinition.js +10 -2
- package/dist/models/CustomFieldEntries.d.ts +15 -0
- package/dist/models/CustomFieldEntries.js +123 -0
- package/dist/models/CustomFieldValue.d.ts +3 -2
- package/dist/models/CustomFieldValue.js +22 -14
- package/dist/models/CustomValidator.d.ts +17 -0
- package/dist/models/CustomValidator.js +98 -0
- package/dist/models/index.d.ts +10 -2
- package/dist/models/index.js +60 -22
- package/dist/repository/definition.d.ts +23 -7
- package/dist/repository/definition.js +36 -7
- package/dist/repository/entries.d.ts +13 -0
- package/dist/repository/entries.js +92 -0
- package/dist/repository/utils/formatValues.d.ts +3 -0
- package/dist/repository/utils/formatValues.js +16 -0
- package/dist/repository/validator.d.ts +21 -0
- package/dist/repository/validator.js +62 -0
- package/dist/repository/value.d.ts +1 -1
- package/dist/repository/value.js +7 -32
- package/dist/scopes/filter.d.ts +5 -22
- package/dist/scopes/filter.js +18 -65
- package/dist/scopes/helpers/filter.helpers.d.ts +42 -0
- package/dist/scopes/helpers/filter.helpers.js +204 -0
- package/dist/tests/api/test-api.js +6 -24
- package/dist/tests/helpers/commonHooks.d.ts +6 -0
- package/dist/tests/helpers/commonHooks.js +62 -0
- package/dist/tests/helpers/database-config.js +1 -1
- package/dist/tests/helpers/index.d.ts +6 -1
- package/dist/tests/helpers/index.js +15 -2
- package/dist/tests/mocks/definition.mock.d.ts +5 -2
- package/dist/tests/mocks/definition.mock.js +10 -1
- package/dist/tests/mocks/events.mock.d.ts +1 -0
- package/dist/tests/mocks/events.mock.js +4 -2
- package/dist/types/definition/index.d.ts +1 -0
- package/dist/types/entries/index.d.ts +25 -0
- package/dist/types/entries/index.js +2 -0
- package/dist/types/index.d.ts +19 -3
- package/dist/utils/constants/index.d.ts +1 -1
- package/dist/utils/helpers/index.d.ts +4 -3
- package/dist/utils/helpers/index.js +22 -30
- package/dist/utils/init.d.ts +5 -3
- package/dist/utils/init.js +13 -11
- package/dist/utils/logger/index.d.ts +1 -0
- package/dist/utils/logger/index.js +34 -0
- package/dist/utils/validations/index.d.ts +7 -1
- package/dist/utils/validations/index.js +28 -7
- package/dist/utils/validations/schema/validator-schema.d.ts +9 -0
- package/dist/utils/validations/schema/validator-schema.js +95 -0
- package/dist/utils/validations/type.d.ts +2 -1
- package/dist/utils/validations/validators/index.js +9 -9
- package/dist/utils/validations/validators/select.validator.js +5 -2
- package/dist/utils/validations/validators/status.validator.js +8 -2
- package/package.json +28 -12
- package/src/api/index.ts +3 -2
- package/src/api/v1/definition/index.ts +20 -23
- package/src/api/v1/definition/validations.ts +16 -16
- package/src/api/v1/errors.ts +4 -7
- package/src/api/v1/index.ts +5 -3
- package/src/api/v1/validator/index.ts +141 -0
- package/src/api/v1/validator/validations.ts +39 -0
- package/src/errors/index.ts +31 -3
- package/src/events/index.ts +25 -13
- package/src/hooks/create.ts +50 -28
- package/src/hooks/enrich.ts +137 -28
- package/src/hooks/hooks.ts +467 -0
- package/src/hooks/index.ts +10 -5
- package/src/hooks/update.ts +20 -7
- package/src/hooks/utils/updateInstanceValues.ts +63 -0
- package/src/index.ts +10 -8
- package/src/models/CustomFieldDefinition.ts +9 -2
- package/src/models/CustomFieldEntries.ts +81 -0
- package/src/models/CustomFieldValue.ts +25 -17
- package/src/models/CustomValidator.ts +78 -0
- package/src/models/index.ts +83 -25
- package/src/repository/definition.ts +62 -14
- package/src/repository/entries.ts +88 -0
- package/src/repository/utils/formatValues.ts +14 -0
- package/src/repository/validator.ts +104 -0
- package/src/repository/value.ts +5 -35
- package/src/scopes/filter.ts +33 -106
- package/src/scopes/helpers/filter.helpers.ts +227 -0
- package/src/tests/api/test-api.ts +4 -2
- package/src/tests/helpers/commonHooks.ts +43 -0
- package/src/tests/helpers/database-config.ts +1 -1
- package/src/tests/helpers/index.ts +18 -2
- package/src/tests/mocks/definition.mock.ts +18 -9
- package/src/tests/mocks/events.mock.ts +4 -1
- package/src/types/definition/index.ts +1 -0
- package/src/types/entries/index.ts +27 -0
- package/src/types/index.ts +20 -3
- package/src/utils/helpers/index.ts +28 -35
- package/src/utils/init.ts +17 -12
- package/src/utils/logger/index.ts +9 -0
- package/src/utils/validations/index.ts +30 -6
- package/src/utils/validations/schema/README.md +93 -0
- package/src/utils/validations/schema/validator-schema.ts +106 -0
- package/src/utils/validations/type.ts +2 -1
- package/src/utils/validations/validators/index.ts +9 -9
- package/src/utils/validations/validators/select.validator.ts +3 -2
- package/src/utils/validations/validators/status.validator.ts +6 -2
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +1 -1
package/dist/hooks/update.js
CHANGED
|
@@ -1,35 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
6
|
exports.beforeUpdate = exports.beforeBulkUpdate = void 0;
|
|
30
7
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
31
|
-
const ValueRepo = __importStar(require("../repository/value"));
|
|
32
8
|
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
9
|
+
const updateInstanceValues_1 = __importDefault(require("./utils/updateInstanceValues"));
|
|
33
10
|
/**
|
|
34
11
|
* A hook to update the custom fields when updating a model (more then one instance).
|
|
35
12
|
*/
|
|
@@ -43,7 +20,7 @@ exports.beforeBulkUpdate = beforeBulkUpdate;
|
|
|
43
20
|
* A hook to update the custom fields when updating a model instance.
|
|
44
21
|
* TODO - cleanup if update fail
|
|
45
22
|
*/
|
|
46
|
-
const beforeUpdate = (scopeAttributes, modelOptions = {}) => async (instance, options) => {
|
|
23
|
+
const beforeUpdate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCustomFieldsEntries: false }) => async (instance, options) => {
|
|
47
24
|
logger_1.default.debug('sadot - before update hook');
|
|
48
25
|
const { fields } = options;
|
|
49
26
|
const modelType = instance.constructor.name;
|
|
@@ -51,7 +28,20 @@ const beforeUpdate = (scopeAttributes, modelOptions = {}) => async (instance, op
|
|
|
51
28
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
52
29
|
if (customFieldsIdx > -1) {
|
|
53
30
|
const { customFields } = instance;
|
|
54
|
-
|
|
31
|
+
if (!Object.keys(customFields).length) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
await (0, updateInstanceValues_1.default)({
|
|
35
|
+
modelId: instance.id,
|
|
36
|
+
modelType,
|
|
37
|
+
identifiers,
|
|
38
|
+
customFields,
|
|
39
|
+
options: {
|
|
40
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
41
|
+
transaction: options.transaction,
|
|
42
|
+
modelOptions,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
55
45
|
// eslint-disable-next-line no-param-reassign
|
|
56
46
|
fields.splice(customFieldsIdx, 1);
|
|
57
47
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Transaction } from 'sequelize';
|
|
2
|
+
import type { ModelOptions } from '../../types';
|
|
3
|
+
interface UpdateInstanceValuesParams {
|
|
4
|
+
modelId: string;
|
|
5
|
+
modelType: string;
|
|
6
|
+
identifiers: string[];
|
|
7
|
+
customFields: Record<string, any>;
|
|
8
|
+
options?: {
|
|
9
|
+
transaction?: Transaction;
|
|
10
|
+
modelOptions?: ModelOptions;
|
|
11
|
+
useCustomFieldsEntries?: boolean;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
declare const updateInstanceValues: ({ modelId, modelType, identifiers, customFields, options, }: UpdateInstanceValuesParams) => Promise<void>;
|
|
15
|
+
export default updateInstanceValues;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const ValueRepo = __importStar(require("../../repository/value"));
|
|
27
|
+
const EntriesRepo = __importStar(require("../../repository/entries"));
|
|
28
|
+
const updateInstanceValues = async ({ modelId, modelType, identifiers, customFields, options = {
|
|
29
|
+
modelOptions: {},
|
|
30
|
+
useCustomFieldsEntries: false,
|
|
31
|
+
}, }) => {
|
|
32
|
+
await ValueRepo.updateValues(modelType, modelId, identifiers, customFields, {
|
|
33
|
+
...options,
|
|
34
|
+
modelOptions: options.modelOptions ?? {},
|
|
35
|
+
});
|
|
36
|
+
/*
|
|
37
|
+
T.Y TODO - Once we're ready to switch from custom_field_values to custom_field_entries, we should remove the ValueRepo.updateValues call.
|
|
38
|
+
Currently, We're updating both tables to keep the data in sync, but not all microservices are using the new table yet.
|
|
39
|
+
*/
|
|
40
|
+
if (!options?.useCustomFieldsEntries) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const { dataValues: { customFields: oldCustomFields } } = await EntriesRepo.findEntriesByModelId(modelId, options) ?? { dataValues: {} };
|
|
44
|
+
const newCustomFields = { ...oldCustomFields, ...customFields };
|
|
45
|
+
await EntriesRepo.updateEntries(modelId, modelType, newCustomFields, identifiers, {
|
|
46
|
+
...options,
|
|
47
|
+
modelOptions: options.modelOptions ?? {},
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
exports.default = updateInstanceValues;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Application } from 'express';
|
|
2
2
|
import type { Sequelize } from 'sequelize-typescript';
|
|
3
|
-
import type { CustomFieldOptions, ModelFetcher } from './types';
|
|
3
|
+
import type { CustomFieldOptions, ModelFetcher, Models } from './types';
|
|
4
4
|
export * from './utils/validations/schema/custom-fields';
|
|
5
5
|
export * from './utils/constants';
|
|
6
6
|
export * from './utils/helpers';
|
|
@@ -10,4 +10,4 @@ export * from './utils/helpers';
|
|
|
10
10
|
*/
|
|
11
11
|
declare const useCustomFields: (app: Application | null, getModel: ModelFetcher, options: CustomFieldOptions) => Promise<Sequelize>;
|
|
12
12
|
export default useCustomFields;
|
|
13
|
-
export declare const disableCustomFields: (models:
|
|
13
|
+
export declare const disableCustomFields: (models: Models[], getModel: ModelFetcher) => void;
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
@@ -21,7 +33,7 @@ exports.disableCustomFields = void 0;
|
|
|
21
33
|
const models_1 = require("./models");
|
|
22
34
|
const api_1 = __importDefault(require("./api"));
|
|
23
35
|
const db_1 = __importDefault(require("./utils/db"));
|
|
24
|
-
const logger_1 =
|
|
36
|
+
const logger_1 = __importStar(require("./utils/logger"));
|
|
25
37
|
const init_1 = require("./utils/init");
|
|
26
38
|
__exportStar(require("./utils/validations/schema/custom-fields"), exports);
|
|
27
39
|
__exportStar(require("./utils/constants"), exports);
|
|
@@ -31,18 +43,19 @@ __exportStar(require("./utils/helpers"), exports);
|
|
|
31
43
|
* @see {@link 'custom-fields/config'} for configurations
|
|
32
44
|
*/
|
|
33
45
|
const useCustomFields = async (app, getModel, options) => {
|
|
34
|
-
|
|
46
|
+
(0, logger_1.tryAddingTraceIdMiddleware)().catch(() => null);
|
|
47
|
+
const { models, useCustomFieldsEntries } = options;
|
|
35
48
|
if (app) {
|
|
36
49
|
app.use('/api', api_1.default);
|
|
37
50
|
}
|
|
38
|
-
const sequelize = (0, db_1.default)(options.databaseConfig);
|
|
51
|
+
const sequelize = options.sequelize ?? (0, db_1.default)(options.databaseConfig);
|
|
39
52
|
if (process.env.NODE_ENV === 'test') {
|
|
40
53
|
await (0, models_1.initTestModels)(sequelize);
|
|
41
54
|
}
|
|
42
55
|
// The order is important
|
|
43
|
-
(0, init_1.addHooks)(models, getModel);
|
|
44
|
-
await (0, models_1.initTables)(sequelize, options.getUser);
|
|
45
|
-
(0, init_1.addScopes)(models, getModel);
|
|
56
|
+
(0, init_1.addHooks)(models, getModel, { useCustomFieldsEntries });
|
|
57
|
+
await (0, models_1.initTables)(sequelize, options.getUser, { useCustomFieldsEntries });
|
|
58
|
+
(0, init_1.addScopes)(models, getModel, { useCustomFieldsEntries });
|
|
46
59
|
(0, init_1.applyCustomAssociation)(models);
|
|
47
60
|
logger_1.default.debug('sadot - custom fields finished initializing with models', models);
|
|
48
61
|
return sequelize;
|
|
@@ -17,6 +17,7 @@ declare class CustomFieldDefinition extends Model {
|
|
|
17
17
|
createdAt?: Date;
|
|
18
18
|
updatedAt?: Date;
|
|
19
19
|
deletedAt?: Date;
|
|
20
|
+
blockEditingFromUI: boolean;
|
|
20
21
|
values: CustomFieldValue[];
|
|
21
22
|
static displayNameDefaultValue(instance: CustomFieldDefinition): void;
|
|
22
23
|
static afterSaveHandler(instance: CustomFieldDefinition, options: any): void;
|
|
@@ -28,8 +28,8 @@ let CustomFieldDefinition = class CustomFieldDefinition extends sequelize_typesc
|
|
|
28
28
|
}
|
|
29
29
|
if (![null, undefined].includes(instance.defaultValue)) {
|
|
30
30
|
const isValid = (0, validations_1.validateValue)(instance.defaultValue, instance.fieldType, instance.validation);
|
|
31
|
-
if (
|
|
32
|
-
throw new errors_1.InvalidValueError(instance.defaultValue, instance.
|
|
31
|
+
if (isValid.error) {
|
|
32
|
+
throw new errors_1.InvalidValueError(instance.defaultValue, instance.name, isValid.error);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -142,6 +142,14 @@ __decorate([
|
|
|
142
142
|
sequelize_typescript_1.Column,
|
|
143
143
|
__metadata("design:type", Date)
|
|
144
144
|
], CustomFieldDefinition.prototype, "deletedAt", void 0);
|
|
145
|
+
__decorate([
|
|
146
|
+
(0, sequelize_typescript_1.Column)({
|
|
147
|
+
type: sequelize_typescript_1.DataType.BOOLEAN,
|
|
148
|
+
defaultValue: false,
|
|
149
|
+
allowNull: false,
|
|
150
|
+
}),
|
|
151
|
+
__metadata("design:type", Boolean)
|
|
152
|
+
], CustomFieldDefinition.prototype, "blockEditingFromUI", void 0);
|
|
145
153
|
__decorate([
|
|
146
154
|
(0, sequelize_typescript_1.HasMany)(() => _1.CustomFieldValue),
|
|
147
155
|
__metadata("design:type", Array)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Model } from 'sequelize-typescript';
|
|
2
|
+
declare class CustomFieldEntries extends Model {
|
|
3
|
+
/** The ID of the model of which this row hold the custom field entries of, e.g. vehicleId / stopPointId / etc. */
|
|
4
|
+
modelId: string;
|
|
5
|
+
/** The ID of the entity of which this row hold the custom field entries of, e.g. fleetId / etc. */
|
|
6
|
+
entityId: string;
|
|
7
|
+
/** A dictionary of customFields and values with the following structure: `{ customFieldName: 'CustomFieldValue' }` */
|
|
8
|
+
customFields: Record<string, any>;
|
|
9
|
+
/** The type of model which this custom field entry represents. e.g. Vehicle / StopPoint / etc. */
|
|
10
|
+
modelType: string;
|
|
11
|
+
createdAt?: Date;
|
|
12
|
+
updatedAt?: Date;
|
|
13
|
+
static afterSaveHandler(instance: CustomFieldEntries, options: any): void;
|
|
14
|
+
}
|
|
15
|
+
export default CustomFieldEntries;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
36
|
+
const events_1 = require("../events");
|
|
37
|
+
const CustomFieldDefinitionRepo = __importStar(require("../repository/definition"));
|
|
38
|
+
const validations_1 = require("../utils/validations");
|
|
39
|
+
let CustomFieldEntries = class CustomFieldEntries extends sequelize_typescript_1.Model {
|
|
40
|
+
static afterSaveHandler(instance, options) {
|
|
41
|
+
if (options.transaction) {
|
|
42
|
+
options.transaction.afterCommit(() => (0, events_1.sendDimEvent)(instance[0]));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
(0, events_1.sendDimEvent)(instance[0]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
__decorate([
|
|
50
|
+
sequelize_typescript_1.PrimaryKey,
|
|
51
|
+
(0, sequelize_typescript_1.Column)({
|
|
52
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
53
|
+
allowNull: false,
|
|
54
|
+
})
|
|
55
|
+
/** The ID of the model of which this row hold the custom field entries of, e.g. vehicleId / stopPointId / etc. */
|
|
56
|
+
,
|
|
57
|
+
__metadata("design:type", String)
|
|
58
|
+
], CustomFieldEntries.prototype, "modelId", void 0);
|
|
59
|
+
__decorate([
|
|
60
|
+
(0, sequelize_typescript_1.Column)({
|
|
61
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
62
|
+
allowNull: false,
|
|
63
|
+
})
|
|
64
|
+
/** The ID of the entity of which this row hold the custom field entries of, e.g. fleetId / etc. */
|
|
65
|
+
,
|
|
66
|
+
__metadata("design:type", String)
|
|
67
|
+
], CustomFieldEntries.prototype, "entityId", void 0);
|
|
68
|
+
__decorate([
|
|
69
|
+
(0, sequelize_typescript_1.Column)({
|
|
70
|
+
type: sequelize_typescript_1.DataType.JSONB,
|
|
71
|
+
allowNull: false,
|
|
72
|
+
defaultValue: {},
|
|
73
|
+
})
|
|
74
|
+
/** A dictionary of customFields and values with the following structure: `{ customFieldName: 'CustomFieldValue' }` */
|
|
75
|
+
,
|
|
76
|
+
__metadata("design:type", Object)
|
|
77
|
+
], CustomFieldEntries.prototype, "customFields", void 0);
|
|
78
|
+
__decorate([
|
|
79
|
+
(0, sequelize_typescript_1.Column)({
|
|
80
|
+
type: sequelize_typescript_1.DataType.STRING,
|
|
81
|
+
allowNull: false,
|
|
82
|
+
})
|
|
83
|
+
/** The type of model which this custom field entry represents. e.g. Vehicle / StopPoint / etc. */
|
|
84
|
+
,
|
|
85
|
+
__metadata("design:type", String)
|
|
86
|
+
], CustomFieldEntries.prototype, "modelType", void 0);
|
|
87
|
+
__decorate([
|
|
88
|
+
sequelize_typescript_1.Column,
|
|
89
|
+
__metadata("design:type", Date)
|
|
90
|
+
], CustomFieldEntries.prototype, "createdAt", void 0);
|
|
91
|
+
__decorate([
|
|
92
|
+
sequelize_typescript_1.Column,
|
|
93
|
+
__metadata("design:type", Date)
|
|
94
|
+
], CustomFieldEntries.prototype, "updatedAt", void 0);
|
|
95
|
+
__decorate([
|
|
96
|
+
sequelize_typescript_1.AfterUpsert,
|
|
97
|
+
__metadata("design:type", Function),
|
|
98
|
+
__metadata("design:paramtypes", [CustomFieldEntries, Object]),
|
|
99
|
+
__metadata("design:returntype", void 0)
|
|
100
|
+
], CustomFieldEntries, "afterSaveHandler", null);
|
|
101
|
+
CustomFieldEntries = __decorate([
|
|
102
|
+
(0, sequelize_typescript_1.Table)({
|
|
103
|
+
timestamps: true,
|
|
104
|
+
indexes: [
|
|
105
|
+
{
|
|
106
|
+
name: 'idx_cfe_custom_fields',
|
|
107
|
+
using: 'gin',
|
|
108
|
+
operator: 'jsonb_path_ops',
|
|
109
|
+
fields: ['custom_fields'],
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
validate: {
|
|
113
|
+
async validationByType() {
|
|
114
|
+
if (!Object.keys(this.customFields ?? {}).length) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const definitionsByName = await CustomFieldDefinitionRepo.getCustomFieldDefinitionsDictionary([this]);
|
|
118
|
+
(0, validations_1.validateInstanceCustomFieldEntries)(this, definitionsByName);
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
})
|
|
122
|
+
], CustomFieldEntries);
|
|
123
|
+
exports.default = CustomFieldEntries;
|
|
@@ -8,8 +8,9 @@ declare class CustomFieldValue extends Model {
|
|
|
8
8
|
updatedAt?: Date;
|
|
9
9
|
deletedAt?: Date;
|
|
10
10
|
customFieldDefinition: CustomFieldDefinition;
|
|
11
|
-
static
|
|
12
|
-
static
|
|
11
|
+
private static validateValueAgainstDefinition;
|
|
12
|
+
static validateCustomFieldValues(instances: CustomFieldValue[]): Promise<void>;
|
|
13
|
+
static validateCustomFieldValue(instance: CustomFieldValue): Promise<void>;
|
|
13
14
|
static afterSaveHandler(instance: CustomFieldValue, options: any): void;
|
|
14
15
|
}
|
|
15
16
|
export default CustomFieldValue;
|
|
@@ -39,7 +39,21 @@ const validations_1 = require("../utils/validations");
|
|
|
39
39
|
const CustomFieldDefinitionRepo = __importStar(require("../repository/definition"));
|
|
40
40
|
const errors_1 = require("../errors");
|
|
41
41
|
let CustomFieldValue = class CustomFieldValue extends sequelize_typescript_1.Model {
|
|
42
|
-
static
|
|
42
|
+
static validateValueAgainstDefinition(instance, definition) {
|
|
43
|
+
const { validation, fieldType, name } = definition;
|
|
44
|
+
const isValidFieldType = (0, validations_1.validateFieldType)(fieldType);
|
|
45
|
+
if (!isValidFieldType) {
|
|
46
|
+
throw new errors_1.InvalidFieldTypeError(fieldType);
|
|
47
|
+
}
|
|
48
|
+
// Always allow null values
|
|
49
|
+
if (instance.value === null)
|
|
50
|
+
return;
|
|
51
|
+
const validateValueResponse = (0, validations_1.validateValue)(instance.value, fieldType, validation);
|
|
52
|
+
if (validateValueResponse.error) {
|
|
53
|
+
throw new errors_1.InvalidValueError(instance.value, name, validateValueResponse.error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static async validateCustomFieldValues(instances) {
|
|
43
57
|
const ids = instances.map((instance) => instance.customFieldDefinitionId);
|
|
44
58
|
const uniqueIds = [...new Set(ids)];
|
|
45
59
|
const definitions = await CustomFieldDefinitionRepo.findByIds(uniqueIds, { withDisabled: true });
|
|
@@ -47,23 +61,17 @@ let CustomFieldValue = class CustomFieldValue extends sequelize_typescript_1.Mod
|
|
|
47
61
|
throw new Error('Definitions not found');
|
|
48
62
|
}
|
|
49
63
|
instances.forEach((instance) => {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (!isValid) {
|
|
54
|
-
throw new errors_1.InvalidValueError(instance.value, fieldType);
|
|
64
|
+
const definition = definitions.find((d) => d.id === instance.customFieldDefinitionId);
|
|
65
|
+
if (definition) {
|
|
66
|
+
this.validateValueAgainstDefinition(instance, definition);
|
|
55
67
|
}
|
|
56
68
|
});
|
|
57
69
|
}
|
|
58
|
-
static async
|
|
70
|
+
static async validateCustomFieldValue(instance) {
|
|
59
71
|
const { customFieldDefinitionId } = instance;
|
|
60
72
|
// eslint-disable-next-line max-len
|
|
61
73
|
const cfd = await CustomFieldDefinitionRepo.findById(customFieldDefinitionId, { withDisabled: true });
|
|
62
|
-
|
|
63
|
-
const isValid = (0, validations_1.validateValue)(instance.value, fieldType, validation);
|
|
64
|
-
if (!isValid) {
|
|
65
|
-
throw new errors_1.InvalidValueError(instance.value, fieldType);
|
|
66
|
-
}
|
|
74
|
+
this.validateValueAgainstDefinition(instance, cfd);
|
|
67
75
|
}
|
|
68
76
|
static afterSaveHandler(instance, options) {
|
|
69
77
|
if (options.transaction) {
|
|
@@ -120,7 +128,7 @@ __decorate([
|
|
|
120
128
|
__metadata("design:type", Function),
|
|
121
129
|
__metadata("design:paramtypes", [Array]),
|
|
122
130
|
__metadata("design:returntype", Promise)
|
|
123
|
-
], CustomFieldValue, "
|
|
131
|
+
], CustomFieldValue, "validateCustomFieldValues", null);
|
|
124
132
|
__decorate([
|
|
125
133
|
sequelize_typescript_1.BeforeUpdate,
|
|
126
134
|
sequelize_typescript_1.BeforeCreate,
|
|
@@ -128,7 +136,7 @@ __decorate([
|
|
|
128
136
|
__metadata("design:type", Function),
|
|
129
137
|
__metadata("design:paramtypes", [CustomFieldValue]),
|
|
130
138
|
__metadata("design:returntype", Promise)
|
|
131
|
-
], CustomFieldValue, "
|
|
139
|
+
], CustomFieldValue, "validateCustomFieldValue", null);
|
|
132
140
|
__decorate([
|
|
133
141
|
sequelize_typescript_1.AfterUpsert,
|
|
134
142
|
__metadata("design:type", Function),
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Model } from 'sequelize-typescript';
|
|
2
|
+
declare class CustomValidator extends Model {
|
|
3
|
+
id: string;
|
|
4
|
+
/** The ID of the entity for which this validator is defined, e.g. fleetId / etc. */
|
|
5
|
+
entityId: string;
|
|
6
|
+
/** The type of entity (fleet, businessModel, etc). */
|
|
7
|
+
entityType: string;
|
|
8
|
+
/** The type of model this validator applies to. e.g. Vehicle / StopPoint / etc. */
|
|
9
|
+
modelType: string;
|
|
10
|
+
/** JSON Schema to validate models against */
|
|
11
|
+
schema: Record<string, unknown>;
|
|
12
|
+
disabled: boolean;
|
|
13
|
+
createdAt?: Date;
|
|
14
|
+
updatedAt?: Date;
|
|
15
|
+
static afterSaveHandler(instance: CustomValidator, options: any): void;
|
|
16
|
+
}
|
|
17
|
+
export default CustomValidator;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
13
|
+
const node_crypto_1 = require("node:crypto");
|
|
14
|
+
const events_1 = require("../events");
|
|
15
|
+
let CustomValidator = class CustomValidator extends sequelize_typescript_1.Model {
|
|
16
|
+
static afterSaveHandler(instance, options) {
|
|
17
|
+
if (options.transaction) {
|
|
18
|
+
options.transaction.afterCommit(() => (0, events_1.sendDimEvent)(instance));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
(0, events_1.sendDimEvent)(instance);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
__decorate([
|
|
26
|
+
sequelize_typescript_1.PrimaryKey,
|
|
27
|
+
(0, sequelize_typescript_1.Default)(() => (0, node_crypto_1.randomUUID)()),
|
|
28
|
+
(0, sequelize_typescript_1.Column)({
|
|
29
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
30
|
+
allowNull: false,
|
|
31
|
+
}),
|
|
32
|
+
__metadata("design:type", String)
|
|
33
|
+
], CustomValidator.prototype, "id", void 0);
|
|
34
|
+
__decorate([
|
|
35
|
+
(0, sequelize_typescript_1.Column)({
|
|
36
|
+
type: sequelize_typescript_1.DataType.UUID,
|
|
37
|
+
allowNull: false,
|
|
38
|
+
})
|
|
39
|
+
/** The ID of the entity for which this validator is defined, e.g. fleetId / etc. */
|
|
40
|
+
,
|
|
41
|
+
__metadata("design:type", String)
|
|
42
|
+
], CustomValidator.prototype, "entityId", void 0);
|
|
43
|
+
__decorate([
|
|
44
|
+
(0, sequelize_typescript_1.Column)({
|
|
45
|
+
type: sequelize_typescript_1.DataType.STRING,
|
|
46
|
+
allowNull: false,
|
|
47
|
+
})
|
|
48
|
+
/** The type of entity (fleet, businessModel, etc). */
|
|
49
|
+
,
|
|
50
|
+
__metadata("design:type", String)
|
|
51
|
+
], CustomValidator.prototype, "entityType", void 0);
|
|
52
|
+
__decorate([
|
|
53
|
+
(0, sequelize_typescript_1.Column)({
|
|
54
|
+
type: sequelize_typescript_1.DataType.STRING,
|
|
55
|
+
allowNull: false,
|
|
56
|
+
})
|
|
57
|
+
/** The type of model this validator applies to. e.g. Vehicle / StopPoint / etc. */
|
|
58
|
+
,
|
|
59
|
+
__metadata("design:type", String)
|
|
60
|
+
], CustomValidator.prototype, "modelType", void 0);
|
|
61
|
+
__decorate([
|
|
62
|
+
(0, sequelize_typescript_1.Column)({
|
|
63
|
+
type: sequelize_typescript_1.DataType.JSONB,
|
|
64
|
+
allowNull: false,
|
|
65
|
+
})
|
|
66
|
+
/** JSON Schema to validate models against */
|
|
67
|
+
,
|
|
68
|
+
__metadata("design:type", Object)
|
|
69
|
+
], CustomValidator.prototype, "schema", void 0);
|
|
70
|
+
__decorate([
|
|
71
|
+
(0, sequelize_typescript_1.Column)({
|
|
72
|
+
type: sequelize_typescript_1.DataType.BOOLEAN,
|
|
73
|
+
allowNull: false,
|
|
74
|
+
defaultValue: false,
|
|
75
|
+
}),
|
|
76
|
+
__metadata("design:type", Boolean)
|
|
77
|
+
], CustomValidator.prototype, "disabled", void 0);
|
|
78
|
+
__decorate([
|
|
79
|
+
sequelize_typescript_1.Column,
|
|
80
|
+
__metadata("design:type", Date)
|
|
81
|
+
], CustomValidator.prototype, "createdAt", void 0);
|
|
82
|
+
__decorate([
|
|
83
|
+
sequelize_typescript_1.Column,
|
|
84
|
+
__metadata("design:type", Date)
|
|
85
|
+
], CustomValidator.prototype, "updatedAt", void 0);
|
|
86
|
+
__decorate([
|
|
87
|
+
sequelize_typescript_1.AfterUpsert,
|
|
88
|
+
__metadata("design:type", Function),
|
|
89
|
+
__metadata("design:paramtypes", [CustomValidator, Object]),
|
|
90
|
+
__metadata("design:returntype", void 0)
|
|
91
|
+
], CustomValidator, "afterSaveHandler", null);
|
|
92
|
+
CustomValidator = __decorate([
|
|
93
|
+
(0, sequelize_typescript_1.DefaultScope)(() => ({ where: { disabled: false } })),
|
|
94
|
+
(0, sequelize_typescript_1.Table)({
|
|
95
|
+
timestamps: true,
|
|
96
|
+
})
|
|
97
|
+
], CustomValidator);
|
|
98
|
+
exports.default = CustomValidator;
|
package/dist/models/index.d.ts
CHANGED
|
@@ -5,6 +5,14 @@ import TestModel from './tests/TestModel';
|
|
|
5
5
|
import ContextAwareTestModel from './tests/contextAwareModels/ContextAwareTestModel';
|
|
6
6
|
import ContextTestModel from './tests/contextAwareModels/ContextTestModel';
|
|
7
7
|
import AssociatedTestModel from './tests/AssociatedTestModel';
|
|
8
|
-
|
|
8
|
+
import type { CustomFieldOptions } from '../types';
|
|
9
|
+
import CustomFieldEntries from './CustomFieldEntries';
|
|
10
|
+
import CustomValidator from './CustomValidator';
|
|
11
|
+
interface InitTablesOptions {
|
|
12
|
+
schemaPrefix?: string;
|
|
13
|
+
schemaVersion?: string;
|
|
14
|
+
useCustomFieldsEntries?: boolean;
|
|
15
|
+
}
|
|
16
|
+
declare const initTables: (sequelize: Sequelize, getUser: CustomFieldOptions['getUser'], { schemaPrefix, schemaVersion, useCustomFieldsEntries, }?: InitTablesOptions) => Promise<void>;
|
|
9
17
|
declare const initTestModels: (sequelize: Sequelize) => Promise<void>;
|
|
10
|
-
export { CustomFieldValue, CustomFieldDefinition, TestModel, AssociatedTestModel, ContextAwareTestModel, ContextTestModel, initTables, initTestModels, };
|
|
18
|
+
export { CustomFieldValue, CustomFieldDefinition, CustomFieldEntries, CustomValidator, TestModel, AssociatedTestModel, ContextAwareTestModel, ContextTestModel, initTables, initTestModels, };
|