@autofleet/sadot 1.1.3 → 1.1.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/.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 +27 -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 +482 -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 +104 -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 +120 -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.101.0/helpers/decorate.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.101.0/helpers/decorate.js +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.101.0/helpers/decorateMetadata.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.101.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
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import type { WhereOptions } from 'sequelize';
|
|
2
|
+
import Ajv from 'ajv';
|
|
3
|
+
import Joi from 'joi';
|
|
4
|
+
import addFormats from 'ajv-formats';
|
|
5
|
+
import { BadRequest } from '@autofleet/errors';
|
|
6
|
+
import ajvErrors from 'ajv-errors';
|
|
7
|
+
import logger from '../utils/logger';
|
|
8
|
+
import * as ValidatorRepo from '../repository/validator';
|
|
9
|
+
import * as DefinitionRepo from '../repository/definition';
|
|
10
|
+
import { InvalidValueError, MissingRequiredCustomFieldError } from '../errors';
|
|
11
|
+
import type { CustomFieldOptions, ModelOptions } from '../types';
|
|
12
|
+
import applyScopeToInstance from '../utils/scopeAttributes';
|
|
13
|
+
import updateInstanceValues from './utils/updateInstanceValues';
|
|
14
|
+
import { CustomFieldDefinitionType } from '../utils/constants';
|
|
15
|
+
import type { CustomFieldDefinition } from '../models';
|
|
16
|
+
|
|
17
|
+
// Include all required fields for proper validation
|
|
18
|
+
const CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL = ['id', 'schema', 'modelType', 'entityId', 'disabled'];
|
|
19
|
+
|
|
20
|
+
// Initialize Ajv with relaxed settings to avoid warnings
|
|
21
|
+
const ajv = new Ajv({
|
|
22
|
+
allErrors: true,
|
|
23
|
+
strict: false, // Disable strict mode to avoid warnings
|
|
24
|
+
strictTypes: false, // Disable strict type checking
|
|
25
|
+
$data: true, // Enable $data references
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
addFormats(ajv);
|
|
29
|
+
ajvErrors(ajv);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Helper function to manually copy object properties
|
|
33
|
+
* This is more efficient for large objects and avoids excessive object creation
|
|
34
|
+
*/
|
|
35
|
+
// eslint-disable-next-line prefer-object-spread
|
|
36
|
+
const manualObjectCopy = (sourceObj: Record<string, any>, additionalProps?: Record<string, any>): Record<string, any> =>
|
|
37
|
+
({ __proto__: null, ...sourceObj, ...additionalProps });
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Fetches complete custom fields for an instance by merging DB values with update values
|
|
41
|
+
* This is needed for partial updates to ensure all related fields are available for validation
|
|
42
|
+
*/
|
|
43
|
+
const getCompleteCustomFields = async (instance, options): Promise<Record<string, any>> => {
|
|
44
|
+
// If we don't have an instance id or no custom fields being updated, return original fields
|
|
45
|
+
if (!instance.id || !instance.customFields || Object.keys(instance.customFields).length === 0) {
|
|
46
|
+
return instance.customFields || {};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const ModelClass = instance.constructor;
|
|
51
|
+
// Only select the customFields column to minimize data transfer
|
|
52
|
+
const currentCustomFields = await ModelClass.findOne({
|
|
53
|
+
where: { id: instance.id },
|
|
54
|
+
attributes: ['customFields'],
|
|
55
|
+
transaction: options.transaction,
|
|
56
|
+
raw: true, // Get plain object instead of model instance for better performance
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (currentCustomFields?.customFields) {
|
|
60
|
+
// Merge existing fields with update fields using our helper function
|
|
61
|
+
const completeFields = manualObjectCopy(
|
|
62
|
+
currentCustomFields.customFields,
|
|
63
|
+
instance.customFields,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
logger.debug('sadot - fetched complete custom fields for validation', {
|
|
67
|
+
fieldsCount: Object.keys(completeFields).length,
|
|
68
|
+
updateFieldsCount: Object.keys(instance.customFields).length,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return completeFields;
|
|
72
|
+
}
|
|
73
|
+
} catch (error) {
|
|
74
|
+
logger.error('sadot - error fetching complete model for validation', { error });
|
|
75
|
+
// Continue with partial data if we can't fetch the complete model
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return instance.customFields || {};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const buildPreChangeState = (instance: any) => {
|
|
82
|
+
const beforeFull: any = { ...instance.dataValues };
|
|
83
|
+
|
|
84
|
+
const changedKeys: string[] = instance.changed?.() || [];
|
|
85
|
+
changedKeys.forEach((key) => {
|
|
86
|
+
const prevVal = instance.previous?.(key);
|
|
87
|
+
if (prevVal !== undefined) {
|
|
88
|
+
beforeFull[key] = prevVal;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const prevCF = instance.previous?.('customFields');
|
|
93
|
+
if (prevCF !== undefined) {
|
|
94
|
+
beforeFull.customFields = prevCF;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return beforeFull;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const formatAjvErrors = (
|
|
101
|
+
errors: {
|
|
102
|
+
instancePath?: string;
|
|
103
|
+
keyword: string;
|
|
104
|
+
message?: string;
|
|
105
|
+
params?: Record<string, any>;
|
|
106
|
+
}[],
|
|
107
|
+
): Record<string, string> => errors.reduce((acc, err) => {
|
|
108
|
+
const basePath = (err.instancePath || '')
|
|
109
|
+
.split('/')
|
|
110
|
+
.filter(Boolean)
|
|
111
|
+
.join('.')
|
|
112
|
+
.replace(/^after\./, '');
|
|
113
|
+
|
|
114
|
+
const missingProp = err.keyword === 'required' ? `.${err.params?.missingProperty}` : '';
|
|
115
|
+
const key = (basePath + missingProp).replace(/^\./, '') || 'root';
|
|
116
|
+
|
|
117
|
+
const message = err.message || 'Invalid value';
|
|
118
|
+
acc[key] = message;
|
|
119
|
+
|
|
120
|
+
return acc;
|
|
121
|
+
}, {} as Record<string, string>);
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Validates the model using custom validators
|
|
125
|
+
*/
|
|
126
|
+
const validateModel = async (
|
|
127
|
+
instance,
|
|
128
|
+
options,
|
|
129
|
+
scopeAttributes: string[],
|
|
130
|
+
modelOptions: ModelOptions = {},
|
|
131
|
+
isCreate = false,
|
|
132
|
+
): Promise<void> => {
|
|
133
|
+
const modelType = instance.constructor.name;
|
|
134
|
+
|
|
135
|
+
logger.debug('sadot - validating model', { isCreate, modelType });
|
|
136
|
+
const identifiers = applyScopeToInstance(instance, scopeAttributes);
|
|
137
|
+
|
|
138
|
+
logger.debug('sadot - identifiers', { identifiers });
|
|
139
|
+
|
|
140
|
+
// Skip if no identifiers
|
|
141
|
+
if (!identifiers || Object.keys(identifiers).length === 0) {
|
|
142
|
+
logger.debug('sadot - skipping validation: no identifiers');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Find the entityId from identifiers (fleetId, businessModelId, etc.)
|
|
147
|
+
const entityId = Object.values(identifiers)[0]; // Get the first value as entityId
|
|
148
|
+
|
|
149
|
+
logger.debug('sadot - entityId', { entityId });
|
|
150
|
+
|
|
151
|
+
if (!entityId) {
|
|
152
|
+
logger.debug('sadot - skipping validation: no entityId');
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let validatorsPromise;
|
|
157
|
+
let cacheKey;
|
|
158
|
+
if (options.transaction) {
|
|
159
|
+
// eslint-disable-next-line no-param-reassign
|
|
160
|
+
options.transaction.validationsCache ||= new Map();
|
|
161
|
+
cacheKey = `${modelType}-${entityId}`;
|
|
162
|
+
validatorsPromise = options.transaction.validationsCache.get(cacheKey);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!validatorsPromise) {
|
|
166
|
+
validatorsPromise = ValidatorRepo.findAllByModelType(
|
|
167
|
+
modelType,
|
|
168
|
+
entityId,
|
|
169
|
+
{
|
|
170
|
+
transaction: options.transaction,
|
|
171
|
+
attributes: CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL,
|
|
172
|
+
...(modelOptions.include && {
|
|
173
|
+
include: modelOptions.include?.(entityId),
|
|
174
|
+
}),
|
|
175
|
+
raw: true,
|
|
176
|
+
},
|
|
177
|
+
);
|
|
178
|
+
if (options.transaction) {
|
|
179
|
+
options?.transaction?.validationsCache.set(cacheKey, validatorsPromise);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const validators = await validatorsPromise;
|
|
183
|
+
|
|
184
|
+
logger.debug('sadot - validators found', { count: validators.length });
|
|
185
|
+
|
|
186
|
+
if (!validators.length) {
|
|
187
|
+
logger.debug('sadot - skipping validation: no validators found');
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// For updates, get the previous values
|
|
192
|
+
let originalValues = null;
|
|
193
|
+
if (!isCreate) {
|
|
194
|
+
originalValues = buildPreChangeState(instance);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Get complete custom fields by merging DB values with update values
|
|
198
|
+
// This is especially important for partial updates to ensure all related fields are available
|
|
199
|
+
const completeCustomFields = !isCreate
|
|
200
|
+
? await getCompleteCustomFields(instance, options)
|
|
201
|
+
: instance.customFields || {};
|
|
202
|
+
|
|
203
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
204
|
+
for (const validator of validators) {
|
|
205
|
+
const { schema } = validator;
|
|
206
|
+
const typedSchema = schema as Record<string, any>;
|
|
207
|
+
|
|
208
|
+
logger.debug('sadot - validating with schema', {
|
|
209
|
+
schema,
|
|
210
|
+
hasAfterProps: !!typedSchema.properties?.after,
|
|
211
|
+
hasBeforeProps: !!typedSchema.properties?.before,
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
if (isCreate) {
|
|
215
|
+
// For create operations, we only need the 'after' state
|
|
216
|
+
if (typedSchema.properties?.after) {
|
|
217
|
+
const validateSchema = ajv.compile({
|
|
218
|
+
...schema,
|
|
219
|
+
// Focus only on the 'after' validation part for create
|
|
220
|
+
properties: {
|
|
221
|
+
after: typedSchema.properties.after,
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const isValid = validateSchema(JSON.parse(JSON.stringify({
|
|
226
|
+
after: {
|
|
227
|
+
...instance.dataValues,
|
|
228
|
+
customFields: completeCustomFields,
|
|
229
|
+
},
|
|
230
|
+
})));
|
|
231
|
+
|
|
232
|
+
if (!isValid) {
|
|
233
|
+
const errorDetails = validateSchema.errors?.map((err) =>
|
|
234
|
+
`${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
|
|
235
|
+
|
|
236
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
237
|
+
throw new BadRequest(
|
|
238
|
+
[new Error(`Validation failed for ${modelType}: ${errorDetails}`)],
|
|
239
|
+
undefined,
|
|
240
|
+
{
|
|
241
|
+
customError: formattedErrors,
|
|
242
|
+
},
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
// For update operations, we need both before and after
|
|
248
|
+
const validateSchema = ajv.compile(typedSchema);
|
|
249
|
+
|
|
250
|
+
// Create after object with our helper function
|
|
251
|
+
const afterObj = manualObjectCopy(instance.dataValues);
|
|
252
|
+
|
|
253
|
+
// Add complete custom fields
|
|
254
|
+
afterObj.customFields = completeCustomFields;
|
|
255
|
+
|
|
256
|
+
// Create validation payload
|
|
257
|
+
const payload = {
|
|
258
|
+
before: originalValues,
|
|
259
|
+
after: afterObj,
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// Validate
|
|
263
|
+
const isValid = validateSchema(JSON.parse(JSON.stringify(payload)));
|
|
264
|
+
|
|
265
|
+
logger.debug('sadot - validation result', {
|
|
266
|
+
isValid,
|
|
267
|
+
test: {
|
|
268
|
+
before: originalValues,
|
|
269
|
+
after: afterObj,
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
if (!isValid) {
|
|
274
|
+
const errorDetails = validateSchema
|
|
275
|
+
.errors
|
|
276
|
+
?.map((err) => `${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
|
|
277
|
+
|
|
278
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
279
|
+
throw new BadRequest(
|
|
280
|
+
[new Error(`Validation failed for ${modelType}: ${errorDetails}`)],
|
|
281
|
+
undefined,
|
|
282
|
+
{
|
|
283
|
+
customError: formattedErrors,
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const getFieldDefinitions = async ({
|
|
292
|
+
modelType,
|
|
293
|
+
modelOptions,
|
|
294
|
+
identifiers,
|
|
295
|
+
options,
|
|
296
|
+
}: {
|
|
297
|
+
modelType: any,
|
|
298
|
+
modelOptions: ModelOptions,
|
|
299
|
+
identifiers: any[],
|
|
300
|
+
options: any
|
|
301
|
+
}) => {
|
|
302
|
+
const { include, useEntityIdFromInclude } = modelOptions;
|
|
303
|
+
const where: WhereOptions = {
|
|
304
|
+
modelType,
|
|
305
|
+
disabled: false,
|
|
306
|
+
...(!useEntityIdFromInclude && { entityId: identifiers }),
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const fieldDefinitions = await DefinitionRepo.findAll(where, {
|
|
310
|
+
withDisabled: false,
|
|
311
|
+
transaction: options.transaction,
|
|
312
|
+
include: include?.(identifiers),
|
|
313
|
+
});
|
|
314
|
+
return fieldDefinitions;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const formatDates = (fieldDefinitions: CustomFieldDefinition[], instance: any) => {
|
|
318
|
+
(fieldDefinitions || []).forEach((fieldDefinition) => {
|
|
319
|
+
const { fieldType, name } = fieldDefinition;
|
|
320
|
+
if ([CustomFieldDefinitionType.DATE, CustomFieldDefinitionType.DATETIME].includes(fieldType)) {
|
|
321
|
+
const value = instance.customFields?.[name];
|
|
322
|
+
if (value) {
|
|
323
|
+
const { value: joiValue, error: validationError } = Joi.date().validate(value);
|
|
324
|
+
if (validationError) {
|
|
325
|
+
throw new InvalidValueError(value, name, validationError);
|
|
326
|
+
}
|
|
327
|
+
// eslint-disable-next-line no-param-reassign
|
|
328
|
+
instance.customFields[name] = joiValue.toISOString();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Hook to handle validation and custom fields during creation
|
|
336
|
+
*/
|
|
337
|
+
export const beforeCreate = (
|
|
338
|
+
scopeAttributes: string[],
|
|
339
|
+
modelOptions: ModelOptions = {},
|
|
340
|
+
sadotOptions: Pick<CustomFieldOptions, 'useCustomFieldsEntries'> = { useCustomFieldsEntries: false },
|
|
341
|
+
) => async (
|
|
342
|
+
instance,
|
|
343
|
+
options,
|
|
344
|
+
): Promise<void> => {
|
|
345
|
+
logger.debug('sadot - before create hook');
|
|
346
|
+
const { fields } = options;
|
|
347
|
+
const modelType = instance.constructor.name;
|
|
348
|
+
|
|
349
|
+
const identifiers = applyScopeToInstance(instance, scopeAttributes);
|
|
350
|
+
|
|
351
|
+
// Step 1: Handle custom fields default values and required fields
|
|
352
|
+
|
|
353
|
+
const fieldDefinitions = await getFieldDefinitions({
|
|
354
|
+
modelType, modelOptions, identifiers, options,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
// Apply default values
|
|
358
|
+
const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
|
|
359
|
+
if (fieldsWithDefaultValue.length) {
|
|
360
|
+
// eslint-disable-next-line no-param-reassign
|
|
361
|
+
instance.customFields ||= {};
|
|
362
|
+
fieldsWithDefaultValue
|
|
363
|
+
.filter((def) => (instance.customFields?.[def.name] === undefined))
|
|
364
|
+
.forEach(({ name, defaultValue }) => {
|
|
365
|
+
// eslint-disable-next-line no-param-reassign
|
|
366
|
+
instance.customFields[name] = defaultValue;
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Check for required fields
|
|
371
|
+
const requiredFieldsNames = Array.from(
|
|
372
|
+
new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)),
|
|
373
|
+
);
|
|
374
|
+
const { customFields } = instance;
|
|
375
|
+
const fieldsNames = Object.keys(customFields ?? {});
|
|
376
|
+
const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
|
|
377
|
+
if (missingFields?.length) {
|
|
378
|
+
throw new MissingRequiredCustomFieldError(missingFields);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Step 2: Validate the model data (including custom fields)
|
|
382
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, true);
|
|
383
|
+
|
|
384
|
+
// format date and datetime fields
|
|
385
|
+
formatDates(fieldDefinitions, instance);
|
|
386
|
+
|
|
387
|
+
// Step 3: Save custom field values if they exist
|
|
388
|
+
const customFieldsIdx = fields.indexOf('customFields');
|
|
389
|
+
if (customFieldsIdx === -1 || !customFields || !Object.keys(customFields).length) {
|
|
390
|
+
// No custom fields to update
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Save custom field values
|
|
395
|
+
await updateInstanceValues({
|
|
396
|
+
modelId: instance.id,
|
|
397
|
+
modelType,
|
|
398
|
+
identifiers,
|
|
399
|
+
customFields,
|
|
400
|
+
options: {
|
|
401
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
402
|
+
transaction: options.transaction,
|
|
403
|
+
modelOptions,
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// Remove customFields from fields array after handling
|
|
408
|
+
// eslint-disable-next-line no-param-reassign
|
|
409
|
+
fields.splice(customFieldsIdx, 1);
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Hook to handle validation and custom fields during update
|
|
414
|
+
*/
|
|
415
|
+
export const beforeUpdate = (
|
|
416
|
+
scopeAttributes: string[],
|
|
417
|
+
modelOptions: ModelOptions = {},
|
|
418
|
+
sadotOptions: Pick<CustomFieldOptions, 'useCustomFieldsEntries'> = { useCustomFieldsEntries: false },
|
|
419
|
+
) => async (
|
|
420
|
+
instance,
|
|
421
|
+
options,
|
|
422
|
+
): Promise<void> => {
|
|
423
|
+
logger.debug('sadot - before update hook');
|
|
424
|
+
const { fields } = options;
|
|
425
|
+
const modelType = instance.constructor.name;
|
|
426
|
+
const identifiers = applyScopeToInstance(instance, scopeAttributes);
|
|
427
|
+
|
|
428
|
+
const fieldDefinitions = await getFieldDefinitions({
|
|
429
|
+
modelType, modelOptions, identifiers, options,
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// Step 1: Validate the model data (including custom fields)
|
|
433
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, false);
|
|
434
|
+
|
|
435
|
+
// format date and datetime fields
|
|
436
|
+
formatDates(fieldDefinitions, instance);
|
|
437
|
+
|
|
438
|
+
// Step 2: Update custom field values if they exist
|
|
439
|
+
const customFieldsIdx = fields.indexOf('customFields');
|
|
440
|
+
if (customFieldsIdx > -1) {
|
|
441
|
+
const { customFields } = instance;
|
|
442
|
+
|
|
443
|
+
if (!Object.keys(customFields).length) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Save custom field values
|
|
448
|
+
await updateInstanceValues({
|
|
449
|
+
modelId: instance.id,
|
|
450
|
+
modelType,
|
|
451
|
+
identifiers,
|
|
452
|
+
customFields,
|
|
453
|
+
options: {
|
|
454
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
455
|
+
transaction: options.transaction,
|
|
456
|
+
modelOptions,
|
|
457
|
+
},
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
// Remove customFields from fields array after handling
|
|
461
|
+
// eslint-disable-next-line no-param-reassign
|
|
462
|
+
fields.splice(customFieldsIdx, 1);
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Hook to enable individual hooks for bulk create operations
|
|
468
|
+
*/
|
|
469
|
+
export const beforeBulkCreate = (options): void => {
|
|
470
|
+
// This will activate the beforeCreate hook on each instance
|
|
471
|
+
// eslint-disable-next-line no-param-reassign
|
|
472
|
+
options.individualHooks = true;
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Hook to enable individual hooks for bulk update operations
|
|
477
|
+
*/
|
|
478
|
+
export const beforeBulkUpdate = (options): void => {
|
|
479
|
+
// This will activate the beforeUpdate hook on each instance
|
|
480
|
+
// eslint-disable-next-line no-param-reassign
|
|
481
|
+
options.individualHooks = true;
|
|
482
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import enrichResults from './enrich';
|
|
2
|
+
import { beforeFind } from './find';
|
|
3
|
+
import workaround from './workaround';
|
|
4
|
+
import {
|
|
5
|
+
beforeCreate,
|
|
6
|
+
beforeUpdate,
|
|
7
|
+
beforeBulkCreate,
|
|
8
|
+
beforeBulkUpdate,
|
|
9
|
+
} from './hooks';
|
|
10
|
+
|
|
11
|
+
// Export the hooks
|
|
12
|
+
export {
|
|
13
|
+
enrichResults,
|
|
14
|
+
beforeFind,
|
|
15
|
+
workaround,
|
|
16
|
+
beforeCreate,
|
|
17
|
+
beforeUpdate,
|
|
18
|
+
beforeBulkCreate,
|
|
19
|
+
beforeBulkUpdate,
|
|
20
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import logger from '../utils/logger';
|
|
2
|
+
import type { CustomFieldOptions, ModelOptions } from '../types';
|
|
3
|
+
import applyScopeToInstance from '../utils/scopeAttributes';
|
|
4
|
+
import updateInstanceValues from './utils/updateInstanceValues';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A hook to update the custom fields when updating a model (more then one instance).
|
|
8
|
+
*/
|
|
9
|
+
export const beforeBulkUpdate = (options): void => {
|
|
10
|
+
// This will activate the beforeUpdate hook on each updating instance.
|
|
11
|
+
// eslint-disable-next-line no-param-reassign
|
|
12
|
+
options.individualHooks = true;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A hook to update the custom fields when updating a model instance.
|
|
17
|
+
* TODO - cleanup if update fail
|
|
18
|
+
*/
|
|
19
|
+
export const beforeUpdate = (
|
|
20
|
+
scopeAttributes: string[],
|
|
21
|
+
modelOptions: ModelOptions = {},
|
|
22
|
+
sadotOptions: Pick<CustomFieldOptions, 'useCustomFieldsEntries'> = { useCustomFieldsEntries: false },
|
|
23
|
+
) => async (
|
|
24
|
+
instance,
|
|
25
|
+
options,
|
|
26
|
+
): Promise<void> => {
|
|
27
|
+
logger.debug('sadot - before update hook');
|
|
28
|
+
const { fields } = options;
|
|
29
|
+
const modelType = instance.constructor.name;
|
|
30
|
+
const identifiers = applyScopeToInstance(instance, scopeAttributes);
|
|
31
|
+
|
|
32
|
+
const customFieldsIdx = fields.indexOf('customFields');
|
|
33
|
+
if (customFieldsIdx > -1) {
|
|
34
|
+
const { customFields } = instance;
|
|
35
|
+
|
|
36
|
+
if (!Object.keys(customFields).length) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
await updateInstanceValues({
|
|
41
|
+
modelId: instance.id,
|
|
42
|
+
modelType,
|
|
43
|
+
identifiers,
|
|
44
|
+
customFields,
|
|
45
|
+
options: {
|
|
46
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
47
|
+
transaction: options.transaction,
|
|
48
|
+
modelOptions,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// eslint-disable-next-line no-param-reassign
|
|
53
|
+
fields.splice(customFieldsIdx, 1);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Transaction } from 'sequelize';
|
|
2
|
+
import * as ValueRepo from '../../repository/value';
|
|
3
|
+
import * as EntriesRepo from '../../repository/entries';
|
|
4
|
+
import type { ModelOptions } from '../../types';
|
|
5
|
+
|
|
6
|
+
interface UpdateInstanceValuesParams {
|
|
7
|
+
modelId: string;
|
|
8
|
+
modelType: string;
|
|
9
|
+
identifiers: string[];
|
|
10
|
+
customFields: Record<string, any>;
|
|
11
|
+
options?: {
|
|
12
|
+
transaction?: Transaction;
|
|
13
|
+
modelOptions?: ModelOptions;
|
|
14
|
+
useCustomFieldsEntries?: boolean;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const updateInstanceValues = async ({
|
|
19
|
+
modelId,
|
|
20
|
+
modelType,
|
|
21
|
+
identifiers,
|
|
22
|
+
customFields,
|
|
23
|
+
options = {
|
|
24
|
+
modelOptions: {},
|
|
25
|
+
useCustomFieldsEntries: false,
|
|
26
|
+
},
|
|
27
|
+
}: UpdateInstanceValuesParams): Promise<void> => {
|
|
28
|
+
await ValueRepo.updateValues(
|
|
29
|
+
modelType,
|
|
30
|
+
modelId,
|
|
31
|
+
identifiers,
|
|
32
|
+
customFields,
|
|
33
|
+
{
|
|
34
|
+
...options,
|
|
35
|
+
modelOptions: options.modelOptions ?? {},
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/*
|
|
40
|
+
T.Y TODO - Once we're ready to switch from custom_field_values to custom_field_entries, we should remove the ValueRepo.updateValues call.
|
|
41
|
+
Currently, We're updating both tables to keep the data in sync, but not all microservices are using the new table yet.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
if (!options?.useCustomFieldsEntries) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { dataValues: { customFields: oldCustomFields } } = await EntriesRepo.findEntriesByModelId(modelId, options) ?? { dataValues: {} };
|
|
49
|
+
const newCustomFields = { ...oldCustomFields, ...customFields };
|
|
50
|
+
|
|
51
|
+
await EntriesRepo.updateEntries(
|
|
52
|
+
modelId,
|
|
53
|
+
modelType,
|
|
54
|
+
newCustomFields,
|
|
55
|
+
identifiers,
|
|
56
|
+
{
|
|
57
|
+
...options,
|
|
58
|
+
modelOptions: options.modelOptions ?? {},
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export default updateInstanceValues;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workaround to a bug in sequelize.🐛
|
|
3
|
+
*
|
|
4
|
+
* **afterFind hook** isn't working on nested (included) models.
|
|
5
|
+
* The solution here is to add a global afterFind hook,
|
|
6
|
+
* which manually calls the afterFind hook of each model, recursively
|
|
7
|
+
* https://github.com/sequelize/sequelize/issues/4627
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const handleChildrenAfterFindHook = async (instances, options, level = 0) => {
|
|
11
|
+
if (!instances) return Promise.resolve();
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(instances)) {
|
|
14
|
+
return Promise.all(instances.map((instance) => {
|
|
15
|
+
const { options: instanceOptions } = instance.constructor;
|
|
16
|
+
return handleChildrenAfterFindHook(
|
|
17
|
+
instance,
|
|
18
|
+
instanceOptions,
|
|
19
|
+
level,
|
|
20
|
+
);
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const instance = instances;
|
|
25
|
+
const { constructor } = instance;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Root model will have already run their "afterFind" hook.
|
|
29
|
+
* Only run children "afterFind" hooks.
|
|
30
|
+
*/
|
|
31
|
+
if (level >= 1) {
|
|
32
|
+
await constructor.runHooks('afterFind', instance, options);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { associations } = constructor;
|
|
36
|
+
const associatedNames = Object.keys(instance).filter((attribute) =>
|
|
37
|
+
Object.keys(associations).includes(attribute));
|
|
38
|
+
|
|
39
|
+
if (associatedNames.length) {
|
|
40
|
+
const childInstances = associatedNames.map((name) => instance[name]);
|
|
41
|
+
return handleChildrenAfterFindHook(childInstances, options, level + 1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export default handleChildrenAfterFindHook;
|