@autofleet/sadot 1.1.5 → 1.1.6-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/.nvmrc +1 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.js +12 -2
- package/dist/api/v1/definition/index.d.ts +3 -0
- package/dist/api/v1/definition/index.js +116 -2
- package/dist/api/v1/definition/validations.d.ts +2 -0
- package/dist/api/v1/definition/validations.js +77 -2
- package/dist/api/v1/errors.d.ts +4 -0
- package/dist/api/v1/errors.js +12 -2
- package/dist/api/v1/index.d.ts +3 -0
- package/dist/api/v1/index.js +13 -2
- package/dist/api/v1/validator/index.d.ts +3 -0
- package/dist/api/v1/validator/index.js +143 -2
- package/dist/api/v1/validator/validations.d.ts +6 -23
- package/dist/api/v1/validator/validations.js +38 -2
- package/dist/errors/index.d.ts +24 -0
- package/dist/errors/index.js +66 -3
- package/dist/events/index.d.ts +5 -0
- package/dist/events/index.js +54 -2
- package/dist/hooks/create.d.ts +10 -0
- package/dist/hooks/create.js +95 -0
- package/dist/hooks/enrich.d.ts +25 -0
- package/dist/hooks/enrich.js +198 -2
- package/dist/hooks/find.d.ts +1 -0
- package/dist/hooks/find.js +29 -2
- package/dist/hooks/hooks.d.ts +17 -0
- package/dist/hooks/hooks.js +388 -2
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +17 -1
- package/dist/hooks/update.d.ts +10 -0
- package/dist/hooks/update.js +49 -0
- package/dist/hooks/utils/updateInstanceValues.d.ts +15 -0
- package/dist/hooks/utils/updateInstanceValues.js +50 -2
- package/dist/hooks/workaround.d.ts +10 -0
- package/dist/hooks/workaround.js +37 -0
- package/dist/index.d.ts +12 -22
- package/dist/index.js +67 -2
- package/dist/models/CustomFieldDefinition.d.ts +23 -29
- package/dist/models/CustomFieldDefinition.js +192 -2
- package/dist/models/CustomFieldEntries.d.ts +13 -14
- package/dist/models/CustomFieldEntries.js +123 -2
- package/dist/models/CustomFieldValue.d.ts +14 -20
- package/dist/models/CustomFieldValue.js +151 -2
- package/dist/models/CustomValidator.d.ts +15 -17
- package/dist/models/CustomValidator.js +98 -2
- package/dist/models/index.d.ts +18 -6
- package/dist/models/index.js +131 -2
- package/dist/models/tests/AssociatedTestModel.d.ts +12 -0
- package/dist/models/tests/AssociatedTestModel.js +71 -2
- package/dist/models/tests/TestModel.d.ts +12 -0
- package/dist/models/tests/TestModel.js +69 -2
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +10 -0
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +53 -2
- package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +13 -0
- package/dist/models/tests/contextAwareModels/ContextTestModel.js +47 -2
- package/dist/repository/definition.d.ts +36 -0
- package/dist/repository/definition.js +121 -2
- package/dist/repository/entries.d.ts +13 -0
- package/dist/repository/entries.js +92 -2
- package/dist/repository/utils/formatValues.d.ts +3 -0
- package/dist/repository/utils/formatValues.js +16 -2
- package/dist/repository/validator.d.ts +28 -0
- package/dist/repository/validator.js +69 -2
- package/dist/repository/value.d.ts +28 -0
- package/dist/repository/value.js +124 -2
- package/dist/scopes/filter.d.ts +29 -22
- package/dist/scopes/filter.js +75 -2
- package/dist/scopes/helpers/filter.helpers.d.ts +40 -15
- package/dist/scopes/helpers/filter.helpers.js +183 -25
- package/dist/scopes/index.d.ts +2 -0
- package/dist/scopes/index.js +6 -1
- package/dist/tests/api/test-api.d.ts +2 -0
- package/dist/tests/api/test-api.js +38 -0
- package/dist/tests/functional/searching/index.d.ts +8 -0
- package/dist/tests/functional/searching/index.js +44 -0
- package/dist/tests/helpers/commonHooks.d.ts +6 -0
- package/dist/tests/helpers/commonHooks.js +62 -0
- package/dist/tests/helpers/database-config.d.ts +16 -0
- package/dist/tests/helpers/database-config.js +17 -0
- package/dist/tests/helpers/index.d.ts +7 -0
- package/dist/tests/helpers/index.js +33 -0
- package/dist/tests/mocks/definition.mock.d.ts +48 -0
- package/dist/tests/mocks/definition.mock.js +78 -0
- package/dist/tests/mocks/events.mock.d.ts +4 -0
- package/dist/tests/mocks/events.mock.js +21 -0
- package/dist/tests/mocks/testModel.d.ts +12 -0
- package/dist/tests/mocks/testModel.js +35 -0
- package/dist/types/definition/index.d.ts +25 -0
- package/dist/types/definition/index.js +2 -0
- package/dist/types/entries/index.d.ts +25 -0
- package/dist/types/entries/index.js +2 -0
- package/dist/types/index.d.ts +46 -45
- package/dist/types/index.js +2 -0
- package/dist/types/value/index.d.ts +15 -0
- package/dist/types/value/index.js +2 -0
- package/dist/utils/constants/index.d.ts +17 -20
- package/dist/utils/constants/index.js +22 -2
- package/dist/utils/db/index.d.ts +4 -0
- package/dist/utils/db/index.js +24 -2
- package/dist/utils/helpers/index.d.ts +23 -28
- package/dist/utils/helpers/index.js +40 -2
- package/dist/utils/init.d.ts +7 -0
- package/dist/utils/init.js +112 -2
- package/dist/utils/logger/index.d.ts +3 -0
- package/dist/utils/logger/index.js +42 -2
- package/dist/utils/scopeAttributes.d.ts +2 -0
- package/dist/utils/scopeAttributes.js +11 -2
- package/dist/utils/validations/index.d.ts +8 -0
- package/dist/utils/validations/index.js +41 -2
- package/dist/utils/validations/schema/custom-fields.d.ts +2 -6
- package/dist/utils/validations/schema/custom-fields.js +9 -2
- package/dist/utils/validations/schema/validator-schema.d.ts +9 -0
- package/dist/utils/validations/schema/validator-schema.js +95 -2
- package/dist/utils/validations/type.d.ts +15 -0
- package/dist/utils/validations/type.js +2 -0
- package/dist/utils/validations/validators/index.d.ts +14 -0
- package/dist/utils/validations/validators/index.js +40 -2
- package/dist/utils/validations/validators/select.validator.d.ts +5 -0
- package/dist/utils/validations/validators/select.validator.js +12 -2
- package/dist/utils/validations/validators/status.validator.d.ts +12 -0
- package/dist/utils/validations/validators/status.validator.js +15 -2
- package/package.json +39 -40
- package/src/api/index.ts +10 -0
- package/src/api/v1/definition/index.ts +104 -0
- package/src/api/v1/definition/validations.ts +75 -0
- package/src/api/v1/errors.ts +13 -0
- package/src/api/v1/index.ts +11 -0
- package/src/api/v1/validator/index.ts +141 -0
- package/src/api/v1/validator/validations.ts +38 -0
- package/src/errors/index.ts +70 -0
- package/src/events/index.ts +63 -0
- package/src/hooks/create.ts +81 -0
- package/src/hooks/enrich.ts +255 -0
- package/src/hooks/find.ts +27 -0
- package/src/hooks/hooks.ts +479 -0
- package/src/hooks/index.ts +20 -0
- package/src/hooks/update.ts +55 -0
- package/src/hooks/utils/updateInstanceValues.ts +63 -0
- package/src/hooks/workaround.ts +47 -0
- package/src/index.ts +52 -0
- package/src/models/CustomFieldDefinition.ts +162 -0
- package/src/models/CustomFieldEntries.ts +81 -0
- package/src/models/CustomFieldValue.ts +118 -0
- package/src/models/CustomValidator.ts +78 -0
- package/src/models/index.ts +165 -0
- package/src/models/tests/AssociatedTestModel.ts +57 -0
- package/src/models/tests/TestModel.ts +54 -0
- package/src/models/tests/contextAwareModels/ContextAwareTestModel.ts +43 -0
- package/src/models/tests/contextAwareModels/ContextTestModel.ts +38 -0
- package/src/repository/definition.ts +175 -0
- package/src/repository/entries.ts +88 -0
- package/src/repository/utils/formatValues.ts +14 -0
- package/src/repository/validator.ts +119 -0
- package/src/repository/value.ts +116 -0
- package/src/scopes/filter.ts +100 -0
- package/src/scopes/helpers/filter.helpers.ts +227 -0
- package/src/scopes/index.ts +6 -0
- package/src/tests/api/test-api.ts +40 -0
- package/src/tests/functional/searching/index.ts +39 -0
- package/src/tests/helpers/commonHooks.ts +43 -0
- package/src/tests/helpers/database-config.ts +15 -0
- package/src/tests/helpers/index.ts +35 -0
- package/src/tests/mocks/definition.mock.ts +84 -0
- package/src/tests/mocks/events.mock.ts +21 -0
- package/src/tests/mocks/testModel.ts +37 -0
- package/src/types/definition/index.ts +24 -0
- package/src/types/entries/index.ts +27 -0
- package/src/types/index.ts +52 -0
- package/src/types/value/index.ts +14 -0
- package/src/utils/constants/index.ts +25 -0
- package/src/utils/db/index.ts +21 -0
- package/src/utils/helpers/index.ts +66 -0
- package/src/utils/init.ts +122 -0
- package/src/utils/logger/index.ts +14 -0
- package/src/utils/scopeAttributes.ts +12 -0
- package/src/utils/validations/index.ts +46 -0
- package/src/utils/validations/schema/README.md +93 -0
- package/src/utils/validations/schema/custom-fields.ts +8 -0
- package/src/utils/validations/schema/validator-schema.ts +106 -0
- package/src/utils/validations/type.ts +20 -0
- package/src/utils/validations/validators/index.ts +38 -0
- package/src/utils/validations/validators/select.validator.ts +12 -0
- package/src/utils/validations/validators/status.validator.ts +22 -0
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.js +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.js +0 -1
- package/dist/_virtual/rolldown_runtime.cjs +0 -1
- package/dist/api/index.cjs +0 -2
- package/dist/api/index.cjs.map +0 -1
- package/dist/api/index.js.map +0 -1
- package/dist/api/v1/definition/index.cjs +0 -2
- package/dist/api/v1/definition/index.cjs.map +0 -1
- package/dist/api/v1/definition/index.js.map +0 -1
- package/dist/api/v1/definition/validations.cjs +0 -2
- package/dist/api/v1/definition/validations.cjs.map +0 -1
- package/dist/api/v1/definition/validations.js.map +0 -1
- package/dist/api/v1/errors.cjs +0 -2
- package/dist/api/v1/errors.cjs.map +0 -1
- package/dist/api/v1/errors.js.map +0 -1
- package/dist/api/v1/index.cjs +0 -2
- package/dist/api/v1/index.cjs.map +0 -1
- package/dist/api/v1/index.js.map +0 -1
- package/dist/api/v1/validator/index.cjs +0 -2
- package/dist/api/v1/validator/index.cjs.map +0 -1
- package/dist/api/v1/validator/index.js.map +0 -1
- package/dist/api/v1/validator/validations.cjs +0 -2
- package/dist/api/v1/validator/validations.cjs.map +0 -1
- package/dist/api/v1/validator/validations.d.cts +0 -23
- package/dist/api/v1/validator/validations.js.map +0 -1
- package/dist/errors/index.cjs +0 -3
- package/dist/errors/index.cjs.map +0 -1
- package/dist/errors/index.js.map +0 -1
- package/dist/events/index.cjs +0 -2
- package/dist/events/index.cjs.map +0 -1
- package/dist/events/index.js.map +0 -1
- package/dist/hooks/enrich.cjs +0 -2
- package/dist/hooks/enrich.cjs.map +0 -1
- package/dist/hooks/enrich.js.map +0 -1
- package/dist/hooks/find.cjs +0 -2
- package/dist/hooks/find.cjs.map +0 -1
- package/dist/hooks/find.js.map +0 -1
- package/dist/hooks/hooks.cjs +0 -2
- package/dist/hooks/hooks.cjs.map +0 -1
- package/dist/hooks/hooks.js.map +0 -1
- package/dist/hooks/index.cjs +0 -1
- package/dist/hooks/utils/updateInstanceValues.cjs +0 -2
- package/dist/hooks/utils/updateInstanceValues.cjs.map +0 -1
- package/dist/hooks/utils/updateInstanceValues.js.map +0 -1
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -23
- package/dist/index.js.map +0 -1
- package/dist/models/CustomFieldDefinition.cjs +0 -2
- package/dist/models/CustomFieldDefinition.cjs.map +0 -1
- package/dist/models/CustomFieldDefinition.d.cts +0 -31
- package/dist/models/CustomFieldDefinition.js.map +0 -1
- package/dist/models/CustomFieldEntries.cjs +0 -2
- package/dist/models/CustomFieldEntries.cjs.map +0 -1
- package/dist/models/CustomFieldEntries.d.cts +0 -16
- package/dist/models/CustomFieldEntries.js.map +0 -1
- package/dist/models/CustomFieldValue.cjs +0 -2
- package/dist/models/CustomFieldValue.cjs.map +0 -1
- package/dist/models/CustomFieldValue.d.cts +0 -22
- package/dist/models/CustomFieldValue.js.map +0 -1
- package/dist/models/CustomValidator.cjs +0 -2
- package/dist/models/CustomValidator.cjs.map +0 -1
- package/dist/models/CustomValidator.d.cts +0 -19
- package/dist/models/CustomValidator.js.map +0 -1
- package/dist/models/index.cjs +0 -2
- package/dist/models/index.cjs.map +0 -1
- package/dist/models/index.d.cts +0 -6
- package/dist/models/index.js.map +0 -1
- package/dist/models/tests/AssociatedTestModel.cjs +0 -2
- package/dist/models/tests/AssociatedTestModel.cjs.map +0 -1
- package/dist/models/tests/AssociatedTestModel.js.map +0 -1
- package/dist/models/tests/TestModel.cjs +0 -2
- package/dist/models/tests/TestModel.cjs.map +0 -1
- package/dist/models/tests/TestModel.js.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.cjs +0 -2
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.cjs.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextTestModel.cjs +0 -2
- package/dist/models/tests/contextAwareModels/ContextTestModel.cjs.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextTestModel.js.map +0 -1
- package/dist/repository/definition.cjs +0 -2
- package/dist/repository/definition.cjs.map +0 -1
- package/dist/repository/definition.js.map +0 -1
- package/dist/repository/entries.cjs +0 -2
- package/dist/repository/entries.cjs.map +0 -1
- package/dist/repository/entries.js.map +0 -1
- package/dist/repository/utils/formatValues.cjs +0 -2
- package/dist/repository/utils/formatValues.cjs.map +0 -1
- package/dist/repository/utils/formatValues.js.map +0 -1
- package/dist/repository/validator.cjs +0 -2
- package/dist/repository/validator.cjs.map +0 -1
- package/dist/repository/validator.js.map +0 -1
- package/dist/repository/value.cjs +0 -2
- package/dist/repository/value.cjs.map +0 -1
- package/dist/repository/value.js.map +0 -1
- package/dist/scopes/filter.cjs +0 -2
- package/dist/scopes/filter.cjs.map +0 -1
- package/dist/scopes/filter.d.cts +0 -23
- package/dist/scopes/filter.js.map +0 -1
- package/dist/scopes/helpers/filter.helpers.cjs +0 -46
- package/dist/scopes/helpers/filter.helpers.cjs.map +0 -1
- package/dist/scopes/helpers/filter.helpers.d.cts +0 -17
- package/dist/scopes/helpers/filter.helpers.js.map +0 -1
- package/dist/scopes/index.cjs +0 -1
- package/dist/types/index.d.cts +0 -45
- package/dist/utils/constants/index.cjs +0 -2
- package/dist/utils/constants/index.cjs.map +0 -1
- package/dist/utils/constants/index.d.cts +0 -22
- package/dist/utils/constants/index.js.map +0 -1
- package/dist/utils/db/index.cjs +0 -2
- package/dist/utils/db/index.cjs.map +0 -1
- package/dist/utils/db/index.js.map +0 -1
- package/dist/utils/helpers/index.cjs +0 -2
- package/dist/utils/helpers/index.cjs.map +0 -1
- package/dist/utils/helpers/index.d.cts +0 -31
- package/dist/utils/helpers/index.js.map +0 -1
- package/dist/utils/init.cjs +0 -2
- package/dist/utils/init.cjs.map +0 -1
- package/dist/utils/init.js.map +0 -1
- package/dist/utils/logger/index.cjs +0 -2
- package/dist/utils/logger/index.cjs.map +0 -1
- package/dist/utils/logger/index.js.map +0 -1
- package/dist/utils/scopeAttributes.cjs +0 -2
- package/dist/utils/scopeAttributes.cjs.map +0 -1
- package/dist/utils/scopeAttributes.js.map +0 -1
- package/dist/utils/validations/index.cjs +0 -2
- package/dist/utils/validations/index.cjs.map +0 -1
- package/dist/utils/validations/index.js.map +0 -1
- package/dist/utils/validations/schema/custom-fields.cjs +0 -2
- package/dist/utils/validations/schema/custom-fields.cjs.map +0 -1
- package/dist/utils/validations/schema/custom-fields.d.cts +0 -7
- package/dist/utils/validations/schema/custom-fields.js.map +0 -1
- package/dist/utils/validations/schema/validator-schema.cjs +0 -2
- package/dist/utils/validations/schema/validator-schema.cjs.map +0 -1
- package/dist/utils/validations/schema/validator-schema.js.map +0 -1
- package/dist/utils/validations/validators/index.cjs +0 -2
- package/dist/utils/validations/validators/index.cjs.map +0 -1
- package/dist/utils/validations/validators/index.js.map +0 -1
- package/dist/utils/validations/validators/select.validator.cjs +0 -2
- package/dist/utils/validations/validators/select.validator.cjs.map +0 -1
- package/dist/utils/validations/validators/select.validator.js.map +0 -1
- package/dist/utils/validations/validators/status.validator.cjs +0 -2
- package/dist/utils/validations/validators/status.validator.cjs.map +0 -1
- package/dist/utils/validations/validators/status.validator.js.map +0 -1
package/dist/events/index.js
CHANGED
|
@@ -1,2 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.sendDimEvent = void 0;
|
|
7
|
+
const events_1 = __importDefault(require("@autofleet/events"));
|
|
8
|
+
const zehut_1 = require("@autofleet/zehut");
|
|
9
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
10
|
+
const events = new events_1.default({
|
|
11
|
+
logger: logger_1.default,
|
|
12
|
+
getUserId: (payload) => payload?.user_id ?? (0, zehut_1.getUser)()?.id ?? null,
|
|
13
|
+
});
|
|
14
|
+
const KEYS_TO_CONVERT = ['value', 'defaultValue', 'blockEditingFromUI'];
|
|
15
|
+
const stringifyBooleans = (savedObject, keysToConvert) => {
|
|
16
|
+
const savedObjectKeySet = new Set(Object.keys(savedObject));
|
|
17
|
+
if (keysToConvert.every((key) => !savedObjectKeySet.has(key))) {
|
|
18
|
+
return savedObject;
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
...savedObject,
|
|
22
|
+
...Object.fromEntries(keysToConvert.map((key) => [key, typeof savedObject[key] === 'boolean' ? savedObject[key].toString() : savedObject[key]])),
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const modelTableMapping = {
|
|
26
|
+
CustomFieldDefinition: {
|
|
27
|
+
tableName: 'dim_custom_field_definition',
|
|
28
|
+
eventVersion: '1',
|
|
29
|
+
},
|
|
30
|
+
CustomFieldValue: {
|
|
31
|
+
tableName: 'dim_custom_field_value',
|
|
32
|
+
eventVersion: '1',
|
|
33
|
+
},
|
|
34
|
+
CustomFieldEntries: {
|
|
35
|
+
tableName: 'dim_custom_field_entries',
|
|
36
|
+
eventVersion: '1',
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
const sendDimEvent = (instance) => {
|
|
40
|
+
const mapping = modelTableMapping[instance.constructor.name];
|
|
41
|
+
if (!mapping) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let objectToSend = instance.get();
|
|
45
|
+
try {
|
|
46
|
+
objectToSend = stringifyBooleans(instance.get(), KEYS_TO_CONVERT);
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
logger_1.default.error('Failed to convert booleans in dim event payload', err);
|
|
50
|
+
}
|
|
51
|
+
events.sendObject(mapping.tableName, mapping.eventVersion, objectToSend).catch(() => { });
|
|
52
|
+
};
|
|
53
|
+
exports.sendDimEvent = sendDimEvent;
|
|
54
|
+
exports.default = events;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CustomFieldOptions, ModelOptions } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* A hook to create the custom fields when updating a model (more then one instance).
|
|
4
|
+
*/
|
|
5
|
+
export declare const beforeBulkCreate: (options: any) => void;
|
|
6
|
+
/**
|
|
7
|
+
* A hook to create the custom fields when updating a model instance.
|
|
8
|
+
* TODO - cleanup if update fail
|
|
9
|
+
*/
|
|
10
|
+
export declare const beforeCreate: (scopeAttributes: string[], modelOptions?: ModelOptions, sadotOptions?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => (instance: any, options: any) => Promise<void>;
|
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.beforeCreate = exports.beforeBulkCreate = void 0;
|
|
30
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
31
|
+
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
32
|
+
const errors_1 = require("../errors");
|
|
33
|
+
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
34
|
+
const updateInstanceValues_1 = __importDefault(require("./utils/updateInstanceValues"));
|
|
35
|
+
/**
|
|
36
|
+
* A hook to create the custom fields when updating a model (more then one instance).
|
|
37
|
+
*/
|
|
38
|
+
const beforeBulkCreate = (options) => {
|
|
39
|
+
// This will activate the beforeCreate hook on each updating instance.
|
|
40
|
+
// eslint-disable-next-line no-param-reassign
|
|
41
|
+
options.individualHooks = true;
|
|
42
|
+
};
|
|
43
|
+
exports.beforeBulkCreate = beforeBulkCreate;
|
|
44
|
+
/**
|
|
45
|
+
* A hook to create the custom fields when updating a model instance.
|
|
46
|
+
* TODO - cleanup if update fail
|
|
47
|
+
*/
|
|
48
|
+
const beforeCreate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCustomFieldsEntries: false }) => async (instance, options) => {
|
|
49
|
+
logger_1.default.debug('sadot - before create hook');
|
|
50
|
+
const { fields } = options;
|
|
51
|
+
const { include, useEntityIdFromInclude } = modelOptions;
|
|
52
|
+
const modelType = instance.constructor.name;
|
|
53
|
+
const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
|
|
54
|
+
const where = {
|
|
55
|
+
modelType,
|
|
56
|
+
disabled: false,
|
|
57
|
+
...(!useEntityIdFromInclude && { entityId: identifiers }),
|
|
58
|
+
};
|
|
59
|
+
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: false, transaction: options.transaction, include: include?.(identifiers) });
|
|
60
|
+
const requiredFieldsNames = Array.from(new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)));
|
|
61
|
+
const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
|
|
62
|
+
if (fieldsWithDefaultValue.length) {
|
|
63
|
+
// eslint-disable-next-line no-param-reassign
|
|
64
|
+
instance.customFields || (instance.customFields = {});
|
|
65
|
+
fieldsWithDefaultValue.filter((def) => (instance.customFields?.[def.name] === undefined)).forEach(({ name, defaultValue }) => {
|
|
66
|
+
// eslint-disable-next-line no-param-reassign
|
|
67
|
+
instance.customFields[name] = defaultValue;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const { customFields } = instance;
|
|
71
|
+
const fieldsNames = Object.keys(customFields ?? {});
|
|
72
|
+
const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
|
|
73
|
+
if (missingFields?.length) {
|
|
74
|
+
throw new errors_1.MissingRequiredCustomFieldError(missingFields);
|
|
75
|
+
}
|
|
76
|
+
const customFieldsIdx = fields.indexOf('customFields');
|
|
77
|
+
if (customFieldsIdx === -1 || !customFields || !Object.keys(customFields).length) {
|
|
78
|
+
// After checking for required fields and fields with default values, and we have no custom fields.
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
await (0, updateInstanceValues_1.default)({
|
|
82
|
+
modelId: instance.id,
|
|
83
|
+
modelType,
|
|
84
|
+
identifiers,
|
|
85
|
+
customFields,
|
|
86
|
+
options: {
|
|
87
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
88
|
+
transaction: options.transaction,
|
|
89
|
+
modelOptions,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
// eslint-disable-next-line no-param-reassign
|
|
93
|
+
fields.splice(customFieldsIdx, 1);
|
|
94
|
+
};
|
|
95
|
+
exports.beforeCreate = beforeCreate;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type CustomFieldValue from '../models/CustomFieldValue';
|
|
2
|
+
import type { CustomFieldOptions, ModelOptions, TransactionOptions } from '../types';
|
|
3
|
+
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
4
|
+
type CustomFieldEntries = Record<string, any>;
|
|
5
|
+
interface GetValuesGroupByInstanceResponse {
|
|
6
|
+
[modelId: string]: CustomFieldValue[];
|
|
7
|
+
}
|
|
8
|
+
interface GetCustomFieldEntriesByInstanceIdResponse {
|
|
9
|
+
[modelId: string]: CustomFieldEntries;
|
|
10
|
+
}
|
|
11
|
+
export declare const getCustomFieldEntriesByInstanceId: ({ instancesIds, options, sadotOptions, }: {
|
|
12
|
+
instancesIds: string[];
|
|
13
|
+
options?: TransactionOptions;
|
|
14
|
+
sadotOptions: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>;
|
|
15
|
+
}) => Promise<GetCustomFieldEntriesByInstanceIdResponse>;
|
|
16
|
+
export declare const getValuesGroupByInstance: ({ instancesIds, options, sadotOptions, }: {
|
|
17
|
+
instancesIds: string[];
|
|
18
|
+
options?: TransactionOptions;
|
|
19
|
+
sadotOptions: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>;
|
|
20
|
+
}) => Promise<GetValuesGroupByInstanceResponse>;
|
|
21
|
+
/**
|
|
22
|
+
* A hook to attach the custom fields when fetching a model instances.
|
|
23
|
+
*/
|
|
24
|
+
declare const enrichResults: (modelType: string, scopeAttributes: string[], hookType?: SupportedHookTypes, modelOptions?: ModelOptions, sadotOptions?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => (instancesOrInstance: any | any[], options: TransactionOptions) => Promise<void>;
|
|
25
|
+
export default enrichResults;
|
package/dist/hooks/enrich.js
CHANGED
|
@@ -1,2 +1,198 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.getValuesGroupByInstance = exports.getCustomFieldEntriesByInstanceId = void 0;
|
|
30
|
+
/* eslint-disable no-param-reassign */
|
|
31
|
+
const ValueRepo = __importStar(require("../repository/value"));
|
|
32
|
+
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
33
|
+
const EntriesRepo = __importStar(require("../repository/entries"));
|
|
34
|
+
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
35
|
+
// Include all required fields for proper functioning
|
|
36
|
+
const CUSTOM_FIELD_DEFINITION_ATTRIBUTES_TO_PULL = [
|
|
37
|
+
'id',
|
|
38
|
+
'name',
|
|
39
|
+
'entityId',
|
|
40
|
+
'fieldType',
|
|
41
|
+
'displayName',
|
|
42
|
+
'validation',
|
|
43
|
+
'entityType',
|
|
44
|
+
'modelType',
|
|
45
|
+
'required',
|
|
46
|
+
'disabled',
|
|
47
|
+
'defaultValue',
|
|
48
|
+
];
|
|
49
|
+
const getCustomFieldEntriesByInstanceId = async ({ instancesIds, options, sadotOptions, }) => {
|
|
50
|
+
if (!sadotOptions.useCustomFieldsEntries) {
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
const customFieldEntries = await EntriesRepo.findEntriesByModelIds(instancesIds, options ?? {});
|
|
54
|
+
const customFieldEntriesByInstanceId = Object.fromEntries(customFieldEntries.map((instanceEntries) => {
|
|
55
|
+
const { modelId, customFields } = instanceEntries?.dataValues ?? {};
|
|
56
|
+
if (!modelId) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return [modelId, customFields];
|
|
60
|
+
}).filter(Boolean));
|
|
61
|
+
instancesIds.forEach((instanceId) => {
|
|
62
|
+
customFieldEntriesByInstanceId[instanceId] ?? (customFieldEntriesByInstanceId[instanceId] = {});
|
|
63
|
+
});
|
|
64
|
+
return customFieldEntriesByInstanceId;
|
|
65
|
+
};
|
|
66
|
+
exports.getCustomFieldEntriesByInstanceId = getCustomFieldEntriesByInstanceId;
|
|
67
|
+
const getValuesGroupByInstance = async ({ instancesIds, options, sadotOptions, }) => {
|
|
68
|
+
if (sadotOptions.useCustomFieldsEntries) {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
const customFieldValues = await ValueRepo.findValuesByModelIds(instancesIds, options ?? {});
|
|
72
|
+
// Group fields by modelId
|
|
73
|
+
return customFieldValues.reduce((acc, v) => {
|
|
74
|
+
const { modelId } = v;
|
|
75
|
+
acc[modelId] ?? (acc[modelId] = []);
|
|
76
|
+
acc[modelId].push(v);
|
|
77
|
+
return acc;
|
|
78
|
+
}, {});
|
|
79
|
+
};
|
|
80
|
+
exports.getValuesGroupByInstance = getValuesGroupByInstance;
|
|
81
|
+
/**
|
|
82
|
+
* Serialize custom fields value into the format of {[name] -> [fieldData]}
|
|
83
|
+
*/
|
|
84
|
+
const serializeCustomFields = (customFieldValues, customFieldDefinitionsHash) => {
|
|
85
|
+
const customFields = customFieldValues.reduce((acc, cfv) => ({
|
|
86
|
+
...acc,
|
|
87
|
+
...(customFieldDefinitionsHash[cfv.customFieldDefinitionId]
|
|
88
|
+
&& { [customFieldDefinitionsHash[cfv.customFieldDefinitionId].name]: cfv.value }),
|
|
89
|
+
}), {});
|
|
90
|
+
return customFields;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* A hook to attach the custom fields when fetching a model instances.
|
|
94
|
+
*/
|
|
95
|
+
const enrichResults = (modelType, scopeAttributes, hookType, modelOptions = {}, sadotOptions = { useCustomFieldsEntries: false }) => async (instancesOrInstance, options) => {
|
|
96
|
+
var _a;
|
|
97
|
+
if (options.originalAttributes?.length > 0
|
|
98
|
+
&& !options.originalAttributes?.includes?.('customFields')) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const primaryKey = 'id';
|
|
102
|
+
let instances = Array.isArray(instancesOrInstance)
|
|
103
|
+
? instancesOrInstance
|
|
104
|
+
: [instancesOrInstance];
|
|
105
|
+
instances = instances.filter(Boolean);
|
|
106
|
+
const identifiers = (0, scopeAttributes_1.default)(instances, scopeAttributes);
|
|
107
|
+
const uniqueIdentifiers = [...new Set(identifiers)].filter(Boolean);
|
|
108
|
+
const identifierCustomFieldDefinitionsMapping = uniqueIdentifiers.reduce((map, identifier) => ({
|
|
109
|
+
...map,
|
|
110
|
+
[identifier]: [],
|
|
111
|
+
}), {});
|
|
112
|
+
// Cache for definitions by model type and transaction to avoid redundant DB queries
|
|
113
|
+
let customFieldDefinitionsPromise;
|
|
114
|
+
let cacheKey;
|
|
115
|
+
if (options.transaction) {
|
|
116
|
+
// Initialize definition cache Map if not already present directly on the transaction object
|
|
117
|
+
(_a = options.transaction).definitionCache || (_a.definitionCache = new Map());
|
|
118
|
+
cacheKey = `${modelType}:${uniqueIdentifiers.slice().sort().join(',')}`;
|
|
119
|
+
customFieldDefinitionsPromise = options.transaction.definitionCache.get(cacheKey);
|
|
120
|
+
}
|
|
121
|
+
if (!customFieldDefinitionsPromise) {
|
|
122
|
+
// Fetch from database (either first time in this transaction or no transaction)
|
|
123
|
+
customFieldDefinitionsPromise = DefinitionRepo.findByEntityIds(modelType, uniqueIdentifiers, { transaction: options.transaction, modelOptions, attributes: CUSTOM_FIELD_DEFINITION_ATTRIBUTES_TO_PULL });
|
|
124
|
+
options.transaction?.definitionCache?.set(cacheKey, customFieldDefinitionsPromise);
|
|
125
|
+
}
|
|
126
|
+
const customFieldDefinitions = await customFieldDefinitionsPromise;
|
|
127
|
+
if (customFieldDefinitions.length === 0) {
|
|
128
|
+
// if no custom fields, we can return
|
|
129
|
+
instances.forEach((instance) => {
|
|
130
|
+
instance.customFields = {};
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (modelOptions?.include && modelOptions.useEntityIdFromInclude) {
|
|
135
|
+
// if we pass useEntityIdFromInclude,
|
|
136
|
+
// map the entity from the options to the identifierCustomFieldDefinitionsMapping
|
|
137
|
+
modelOptions.include(identifiers).forEach(({ model }) => {
|
|
138
|
+
customFieldDefinitions.forEach((cfd) => {
|
|
139
|
+
const entityId = cfd[`${model.name}.entityId`];
|
|
140
|
+
identifierCustomFieldDefinitionsMapping[entityId] = [];
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const definitionsMap = customFieldDefinitions.reduce((map, definition) => ({
|
|
145
|
+
...map,
|
|
146
|
+
[definition.id]: definition,
|
|
147
|
+
}), {});
|
|
148
|
+
customFieldDefinitions.forEach((cfd) => {
|
|
149
|
+
identifierCustomFieldDefinitionsMapping[cfd.entityId].push(cfd);
|
|
150
|
+
});
|
|
151
|
+
// Get the values per instates ids:
|
|
152
|
+
const instancesIds = instances.map((i) => i[primaryKey]);
|
|
153
|
+
// Group fields by modelId
|
|
154
|
+
const [valuesGroupByInstance, customFieldEntriesByInstanceId] = await Promise.all([
|
|
155
|
+
(0, exports.getValuesGroupByInstance)({
|
|
156
|
+
instancesIds,
|
|
157
|
+
options,
|
|
158
|
+
sadotOptions,
|
|
159
|
+
}),
|
|
160
|
+
(0, exports.getCustomFieldEntriesByInstanceId)({
|
|
161
|
+
instancesIds,
|
|
162
|
+
options,
|
|
163
|
+
sadotOptions,
|
|
164
|
+
}),
|
|
165
|
+
]);
|
|
166
|
+
// Attach custom fields to the instances
|
|
167
|
+
instances.forEach((instance) => {
|
|
168
|
+
const { id } = instance;
|
|
169
|
+
const instanceValues = valuesGroupByInstance[id];
|
|
170
|
+
const serializedCustomFieldsValues = instanceValues ? serializeCustomFields(instanceValues, definitionsMap) : {};
|
|
171
|
+
const customFields = sadotOptions.useCustomFieldsEntries
|
|
172
|
+
? customFieldEntriesByInstanceId[id]
|
|
173
|
+
: serializedCustomFieldsValues;
|
|
174
|
+
scopeAttributes.forEach((attribute) => {
|
|
175
|
+
const identifier = instance[attribute];
|
|
176
|
+
const entityCustomFieldDefinitions = identifierCustomFieldDefinitionsMapping[identifier];
|
|
177
|
+
if (entityCustomFieldDefinitions?.length > 0) {
|
|
178
|
+
entityCustomFieldDefinitions.forEach((customFieldDefinition) => {
|
|
179
|
+
if (customFields[customFieldDefinition.name] === undefined) {
|
|
180
|
+
customFields[customFieldDefinition.name] = null;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
instance.customFields = customFields;
|
|
186
|
+
options.attributesToRemove?.forEach?.((attribute) => {
|
|
187
|
+
delete instance.dataValues?.[attribute];
|
|
188
|
+
// if raw:
|
|
189
|
+
delete instance?.[attribute];
|
|
190
|
+
});
|
|
191
|
+
// sequelize will think customFields changed also in 'find', so we need to mark it as unchanged
|
|
192
|
+
if (hookType === 'afterFind') {
|
|
193
|
+
// changed() could be undefined, i.e in raw: true
|
|
194
|
+
instance?.changed?.('customFields', false);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
exports.default = enrichResults;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const beforeFind: (scopeAttributes: string[]) => (options: any) => void;
|
package/dist/hooks/find.js
CHANGED
|
@@ -1,2 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.beforeFind = void 0;
|
|
7
|
+
/* eslint-disable no-param-reassign */
|
|
8
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
9
|
+
const doScopeAttributesMissing = (scopeAttributes, queryAttributes) => {
|
|
10
|
+
const attributes = scopeAttributes
|
|
11
|
+
.filter((attribute) => !queryAttributes.includes(attribute));
|
|
12
|
+
if (!queryAttributes.includes?.('id')) {
|
|
13
|
+
attributes.push('id');
|
|
14
|
+
}
|
|
15
|
+
return attributes;
|
|
16
|
+
};
|
|
17
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
18
|
+
const beforeFind = (scopeAttributes) => (options) => {
|
|
19
|
+
const { attributes: queryAttributes } = options;
|
|
20
|
+
if (queryAttributes?.includes?.('customFields')) {
|
|
21
|
+
const missingScopeAttributes = doScopeAttributesMissing(scopeAttributes, queryAttributes);
|
|
22
|
+
logger_1.default.debug('sadot - before find hook');
|
|
23
|
+
if (missingScopeAttributes?.length > 0) {
|
|
24
|
+
queryAttributes.push(...missingScopeAttributes);
|
|
25
|
+
options.attributesToRemove = missingScopeAttributes;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.beforeFind = beforeFind;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CustomFieldOptions, ModelOptions } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Hook to handle validation and custom fields during creation
|
|
4
|
+
*/
|
|
5
|
+
export declare const beforeCreate: (scopeAttributes: string[], modelOptions?: ModelOptions, sadotOptions?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => (instance: any, options: any) => Promise<void>;
|
|
6
|
+
/**
|
|
7
|
+
* Hook to handle validation and custom fields during update
|
|
8
|
+
*/
|
|
9
|
+
export declare const beforeUpdate: (scopeAttributes: string[], modelOptions?: ModelOptions, sadotOptions?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => (instance: any, options: any) => Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Hook to enable individual hooks for bulk create operations
|
|
12
|
+
*/
|
|
13
|
+
export declare const beforeBulkCreate: (options: any) => void;
|
|
14
|
+
/**
|
|
15
|
+
* Hook to enable individual hooks for bulk update operations
|
|
16
|
+
*/
|
|
17
|
+
export declare const beforeBulkUpdate: (options: any) => void;
|