@autofleet/sadot 0.5.4-beta.18 → 0.5.4-beta.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/create.d.ts +2 -1
- package/dist/hooks/create.js +4 -5
- package/dist/hooks/enrich.d.ts +2 -1
- package/dist/hooks/enrich.js +4 -8
- package/dist/hooks/update.d.ts +2 -1
- package/dist/hooks/update.js +4 -5
- package/dist/repository/definition.d.ts +2 -3
- package/dist/repository/definition.js +3 -6
- package/dist/repository/value.js +0 -1
- package/dist/types/index.d.ts +1 -4
- package/dist/utils/init.js +6 -6
- package/package.json +1 -1
- package/src/hooks/create.ts +5 -7
- package/src/hooks/enrich.ts +5 -9
- package/src/hooks/update.ts +4 -7
- package/src/repository/definition.ts +1 -8
- package/src/repository/value.ts +0 -1
- package/src/types/index.ts +1 -6
- package/src/utils/init.ts +6 -6
package/dist/hooks/create.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ModelOptions } from '../types';
|
|
1
2
|
/**
|
|
2
3
|
* A hook to create the custom fields when updating a model (more then one instance).
|
|
3
4
|
*/
|
|
@@ -6,4 +7,4 @@ export declare const beforeBulkCreate: (options: any) => void;
|
|
|
6
7
|
* A hook to create the custom fields when updating a model instance.
|
|
7
8
|
* TODO - cleanup if update fail
|
|
8
9
|
*/
|
|
9
|
-
export declare const beforeCreate: (scopeAttributes: string[],
|
|
10
|
+
export declare const beforeCreate: (scopeAttributes: string[], modelOptions: ModelOptions) => (instance: any, options: any) => Promise<void>;
|
package/dist/hooks/create.js
CHANGED
|
@@ -31,7 +31,6 @@ const errors_1 = require("../errors");
|
|
|
31
31
|
* A hook to create the custom fields when updating a model (more then one instance).
|
|
32
32
|
*/
|
|
33
33
|
const beforeBulkCreate = (options) => {
|
|
34
|
-
console.log('hooks - before bulk create');
|
|
35
34
|
// This will activate the beforeCreate hook on each updating instance.
|
|
36
35
|
// eslint-disable-next-line no-param-reassign
|
|
37
36
|
options.individualHooks = true;
|
|
@@ -41,13 +40,13 @@ exports.beforeBulkCreate = beforeBulkCreate;
|
|
|
41
40
|
* A hook to create the custom fields when updating a model instance.
|
|
42
41
|
* TODO - cleanup if update fail
|
|
43
42
|
*/
|
|
44
|
-
const beforeCreate = (scopeAttributes,
|
|
43
|
+
const beforeCreate = (scopeAttributes, modelOptions) => async (instance, options) => {
|
|
45
44
|
const { fields } = options;
|
|
46
45
|
const modelType = instance.constructor.name;
|
|
47
|
-
|
|
46
|
+
const { scopeAttributeReplacer } = modelOptions;
|
|
48
47
|
let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
49
|
-
if (
|
|
50
|
-
identifiers = await
|
|
48
|
+
if (scopeAttributeReplacer) {
|
|
49
|
+
identifiers = await scopeAttributeReplacer(identifiers);
|
|
51
50
|
}
|
|
52
51
|
// get all model's required definitions
|
|
53
52
|
const requiredFieldsNames = await DefinitionRepo.getRequiredFields(modelType, instance.id, identifiers);
|
package/dist/hooks/enrich.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { ModelOptions } from '../types';
|
|
1
2
|
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
2
3
|
/**
|
|
3
4
|
* A hook to attach the custom fields when fetching a model instances.
|
|
4
5
|
*/
|
|
5
|
-
declare const enrichResults: (modelType: string, scopeAttributes: string[], getScope: any, hookType?: SupportedHookTypes,
|
|
6
|
+
declare const enrichResults: (modelType: string, scopeAttributes: string[], getScope: any, hookType?: SupportedHookTypes, modelOptions?: ModelOptions) => (instancesOrInstance: any | any[], options: any) => Promise<void>;
|
|
6
7
|
export default enrichResults;
|
package/dist/hooks/enrich.js
CHANGED
|
@@ -44,8 +44,8 @@ const serializeCustomFields = (customFieldValues, customFieldDefinitionsHash) =>
|
|
|
44
44
|
/**
|
|
45
45
|
* A hook to attach the custom fields when fetching a model instances.
|
|
46
46
|
*/
|
|
47
|
-
const enrichResults = (modelType, scopeAttributes, getScope, hookType,
|
|
48
|
-
|
|
47
|
+
const enrichResults = (modelType, scopeAttributes, getScope, hookType, modelOptions) => async (instancesOrInstance, options) => {
|
|
48
|
+
const { scopeAttributeReplacer } = modelOptions;
|
|
49
49
|
if (options.originalAttributes?.length > 0
|
|
50
50
|
&& !options.originalAttributes?.includes?.('customFields')) {
|
|
51
51
|
return;
|
|
@@ -57,8 +57,8 @@ const enrichResults = (modelType, scopeAttributes, getScope, hookType, identifie
|
|
|
57
57
|
instances = instances.filter(Boolean);
|
|
58
58
|
let identifiers = instances.map((instance) => scopeAttributes
|
|
59
59
|
.map((attr) => instance[attr])).flat();
|
|
60
|
-
if (
|
|
61
|
-
identifiers = await
|
|
60
|
+
if (scopeAttributeReplacer) {
|
|
61
|
+
identifiers = await scopeAttributeReplacer(identifiers);
|
|
62
62
|
}
|
|
63
63
|
logger_1.default.info('enrichResults - sadot - indentifiers: ', identifiers);
|
|
64
64
|
const uniqueIdentifiers = [...new Set(identifiers)].filter(Boolean);
|
|
@@ -66,7 +66,6 @@ const enrichResults = (modelType, scopeAttributes, getScope, hookType, identifie
|
|
|
66
66
|
...map,
|
|
67
67
|
[identifier]: [],
|
|
68
68
|
}), {});
|
|
69
|
-
console.log({ identifierCustomFieldDefinitionsMapping });
|
|
70
69
|
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(modelType, uniqueIdentifiers, { transaction: options.transaction, getScope });
|
|
71
70
|
const definitionsMap = customFieldDefinitions.reduce((map, definition) => ({
|
|
72
71
|
...map,
|
|
@@ -98,9 +97,6 @@ const enrichResults = (modelType, scopeAttributes, getScope, hookType, identifie
|
|
|
98
97
|
}
|
|
99
98
|
scopeAttributes.forEach((attribute) => {
|
|
100
99
|
const identifier = instance[attribute];
|
|
101
|
-
console.log({
|
|
102
|
-
identifier, attribute, instance, identifierCustomFieldDefinitionsMapping,
|
|
103
|
-
});
|
|
104
100
|
const entityCustomFieldDefinitions = identifierCustomFieldDefinitionsMapping[identifier];
|
|
105
101
|
if (entityCustomFieldDefinitions?.length > 0) {
|
|
106
102
|
entityCustomFieldDefinitions.forEach((customFieldDefinition) => {
|
package/dist/hooks/update.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ModelOptions } from '../types';
|
|
1
2
|
/**
|
|
2
3
|
* A hook to update the custom fields when updating a model (more then one instance).
|
|
3
4
|
*/
|
|
@@ -6,4 +7,4 @@ export declare const beforeBulkUpdate: (options: any) => void;
|
|
|
6
7
|
* A hook to update the custom fields when updating a model instance.
|
|
7
8
|
* TODO - cleanup if update fail
|
|
8
9
|
*/
|
|
9
|
-
export declare const beforeUpdate: (scopeAttributes: string[],
|
|
10
|
+
export declare const beforeUpdate: (scopeAttributes: string[], modelOptions: ModelOptions) => (instance: any, options: any) => Promise<void>;
|
package/dist/hooks/update.js
CHANGED
|
@@ -33,7 +33,6 @@ const ValueRepo = __importStar(require("../repository/value"));
|
|
|
33
33
|
* A hook to update the custom fields when updating a model (more then one instance).
|
|
34
34
|
*/
|
|
35
35
|
const beforeBulkUpdate = (options) => {
|
|
36
|
-
console.log('hooks - beforeBulkUpdate');
|
|
37
36
|
// This will activate the beforeUpdate hook on each updating instance.
|
|
38
37
|
// eslint-disable-next-line no-param-reassign
|
|
39
38
|
options.individualHooks = true;
|
|
@@ -43,14 +42,14 @@ exports.beforeBulkUpdate = beforeBulkUpdate;
|
|
|
43
42
|
* A hook to update the custom fields when updating a model instance.
|
|
44
43
|
* TODO - cleanup if update fail
|
|
45
44
|
*/
|
|
46
|
-
const beforeUpdate = (scopeAttributes,
|
|
45
|
+
const beforeUpdate = (scopeAttributes, modelOptions) => async (instance, options) => {
|
|
47
46
|
logger_1.default.debug('sadot - before update hook');
|
|
48
|
-
console.log('hooks - beforeUpdate');
|
|
49
47
|
const { fields } = options;
|
|
48
|
+
const { scopeAttributeReplacer } = modelOptions;
|
|
50
49
|
const modelType = instance.constructor.name;
|
|
51
50
|
let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
52
|
-
if (
|
|
53
|
-
identifiers = await
|
|
51
|
+
if (scopeAttributeReplacer) {
|
|
52
|
+
identifiers = await scopeAttributeReplacer(identifiers);
|
|
54
53
|
}
|
|
55
54
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
56
55
|
if (customFieldsIdx > -1) {
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { WhereOptions } from 'sequelize';
|
|
2
2
|
import { CustomFieldDefinition } from '../models';
|
|
3
3
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
4
|
-
import { ModelOptions } from '../types';
|
|
5
4
|
export declare const create: (data: CreateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
6
5
|
export declare const findAll: (where: WhereOptions, options?: any) => Promise<CustomFieldDefinition[]>;
|
|
7
6
|
export declare const findByIds: (ids: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
8
7
|
export declare const findById: (id: string, options?: any) => Promise<CustomFieldDefinition | null>;
|
|
9
|
-
export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: any
|
|
8
|
+
export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
10
9
|
export declare const findByWhere: (where: any) => Promise<CustomFieldDefinition | null>;
|
|
11
10
|
export declare const findDefinitionsByModels: (modelTypes: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
12
11
|
export declare const update: (id: string, data: UpdateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
@@ -15,4 +14,4 @@ export declare const destroy: (id: string) => Promise<any>;
|
|
|
15
14
|
/**
|
|
16
15
|
* Return the names of the required fields for a given model
|
|
17
16
|
*/
|
|
18
|
-
export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[]
|
|
17
|
+
export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[]) => Promise<string[]>;
|
|
@@ -17,8 +17,7 @@ const findAll = (where, options = { withDisabled: false }) => {
|
|
|
17
17
|
return queryModel.scope('userScope').findAll({
|
|
18
18
|
where,
|
|
19
19
|
raw: true,
|
|
20
|
-
logging:
|
|
21
|
-
include: options.include,
|
|
20
|
+
logging: true,
|
|
22
21
|
});
|
|
23
22
|
};
|
|
24
23
|
exports.findAll = findAll;
|
|
@@ -32,11 +31,10 @@ const findById = (id, options = { withDisabled: false }) => {
|
|
|
32
31
|
return models_1.CustomFieldDefinition.scope('userScope').findByPk(id);
|
|
33
32
|
};
|
|
34
33
|
exports.findById = findById;
|
|
35
|
-
const findByEntityIds = async (modelType, entityIds, options = {}
|
|
34
|
+
const findByEntityIds = async (modelType, entityIds, options = {}) => models_1.CustomFieldDefinition.findAll({
|
|
36
35
|
where: {
|
|
37
36
|
modelType,
|
|
38
37
|
entityId: entityIds,
|
|
39
|
-
...modelOptions?.customEntityMapper?.(entityIds),
|
|
40
38
|
},
|
|
41
39
|
transaction: options.transaction,
|
|
42
40
|
include: options.include,
|
|
@@ -71,14 +69,13 @@ exports.destroy = destroy;
|
|
|
71
69
|
/**
|
|
72
70
|
* Return the names of the required fields for a given model
|
|
73
71
|
*/
|
|
74
|
-
const getRequiredFields = async (modelType, modelId, entityId
|
|
72
|
+
const getRequiredFields = async (modelType, modelId, entityId) => {
|
|
75
73
|
const entityIds = Array.isArray(entityId) ? entityId : [entityId];
|
|
76
74
|
const requiredFields = await models_1.CustomFieldDefinition.findAll({
|
|
77
75
|
where: {
|
|
78
76
|
required: true,
|
|
79
77
|
modelType,
|
|
80
78
|
entityId: { [sequelize_1.Op.in]: entityIds },
|
|
81
|
-
...modelOptions?.customEntityMapper?.(entityIds),
|
|
82
79
|
},
|
|
83
80
|
logging: true,
|
|
84
81
|
});
|
package/dist/repository/value.js
CHANGED
|
@@ -102,7 +102,6 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
|
|
|
102
102
|
return Promise.all(values.map(async (value) => {
|
|
103
103
|
const [cfv] = await models_1.CustomFieldValue.upsert(value, {
|
|
104
104
|
transaction: options.transaction,
|
|
105
|
-
logging: console.log,
|
|
106
105
|
});
|
|
107
106
|
return cfv;
|
|
108
107
|
}));
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { Includeable } from 'sequelize';
|
|
2
1
|
export type ModelFetcher = (name: string) => any;
|
|
3
2
|
export type ModelOptions = {
|
|
4
|
-
|
|
5
|
-
customEntityMapper?: (identifiers: string[]) => any;
|
|
3
|
+
scopeAttributeReplacer?: (attributes?: string[]) => Promise<any>;
|
|
6
4
|
};
|
|
7
5
|
export type Models = {
|
|
8
6
|
name: string;
|
|
@@ -11,7 +9,6 @@ export type Models = {
|
|
|
11
9
|
creationWebhookHandler?: (instance: any) => any;
|
|
12
10
|
updateWebhookHandler?: (instance: any) => any;
|
|
13
11
|
deletionWebhookHandler?: (instance: any) => any;
|
|
14
|
-
identifiersReplacer?: (...args: any[]) => Promise<any>;
|
|
15
12
|
};
|
|
16
13
|
export type CustomFieldOptions = {
|
|
17
14
|
models: Models[];
|
package/dist/utils/init.js
CHANGED
|
@@ -12,7 +12,7 @@ const scopes_1 = require("../scopes");
|
|
|
12
12
|
const logger_1 = __importDefault(require("./logger"));
|
|
13
13
|
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
14
14
|
const addHooks = (models, getModel) => {
|
|
15
|
-
models.forEach(async ({ name, scopeAttributes,
|
|
15
|
+
models.forEach(async ({ name, scopeAttributes, modelOptions, }) => {
|
|
16
16
|
try {
|
|
17
17
|
const model = getModel(name);
|
|
18
18
|
if (!model) {
|
|
@@ -31,11 +31,11 @@ const addHooks = (models, getModel) => {
|
|
|
31
31
|
model.addHook('beforeFind', 'sadot-beforeFind', (0, hooks_1.beforeFind)(scopeAttributes));
|
|
32
32
|
model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', hooks_1.beforeBulkCreate);
|
|
33
33
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', hooks_1.beforeBulkUpdate);
|
|
34
|
-
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes,
|
|
35
|
-
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes,
|
|
36
|
-
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind', null,
|
|
37
|
-
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, null,
|
|
38
|
-
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, null,
|
|
34
|
+
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes, modelOptions));
|
|
35
|
+
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes, modelOptions));
|
|
36
|
+
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind', null, modelOptions));
|
|
37
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, null, modelOptions));
|
|
38
|
+
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, null, modelOptions));
|
|
39
39
|
}
|
|
40
40
|
catch (e) {
|
|
41
41
|
logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
|
package/package.json
CHANGED
package/src/hooks/create.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import * as ValueRepo from '../repository/value';
|
|
2
2
|
import * as DefinitionRepo from '../repository/definition';
|
|
3
3
|
import { MissingRequiredCustomFieldError } from '../errors';
|
|
4
|
+
import { ModelOptions } from '../types';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* A hook to create the custom fields when updating a model (more then one instance).
|
|
7
8
|
*/
|
|
8
9
|
export const beforeBulkCreate = (options): void => {
|
|
9
|
-
console.log('hooks - before bulk create');
|
|
10
|
-
|
|
11
10
|
// This will activate the beforeCreate hook on each updating instance.
|
|
12
11
|
// eslint-disable-next-line no-param-reassign
|
|
13
12
|
options.individualHooks = true;
|
|
@@ -16,18 +15,17 @@ export const beforeBulkCreate = (options): void => {
|
|
|
16
15
|
* A hook to create the custom fields when updating a model instance.
|
|
17
16
|
* TODO - cleanup if update fail
|
|
18
17
|
*/
|
|
19
|
-
export const beforeCreate = (scopeAttributes: string[],
|
|
18
|
+
export const beforeCreate = (scopeAttributes: string[], modelOptions: ModelOptions) => async (
|
|
20
19
|
instance,
|
|
21
20
|
options,
|
|
22
21
|
): Promise<void> => {
|
|
23
22
|
const { fields } = options;
|
|
24
23
|
const modelType = instance.constructor.name;
|
|
25
|
-
|
|
26
|
-
console.log('hooks - before create');
|
|
24
|
+
const { scopeAttributeReplacer } = modelOptions;
|
|
27
25
|
|
|
28
26
|
let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
29
|
-
if (
|
|
30
|
-
identifiers = await
|
|
27
|
+
if (scopeAttributeReplacer) {
|
|
28
|
+
identifiers = await scopeAttributeReplacer(identifiers);
|
|
31
29
|
}
|
|
32
30
|
// get all model's required definitions
|
|
33
31
|
const requiredFieldsNames = await DefinitionRepo.getRequiredFields(modelType,
|
package/src/hooks/enrich.ts
CHANGED
|
@@ -5,6 +5,7 @@ import CustomFieldValue from '../models/CustomFieldValue';
|
|
|
5
5
|
import CustomFieldDefinition from '../models/CustomFieldDefinition';
|
|
6
6
|
import { SerializedCustomFields } from '../types/definition';
|
|
7
7
|
import logger from '../utils/logger';
|
|
8
|
+
import { ModelOptions } from '../types';
|
|
8
9
|
|
|
9
10
|
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
10
11
|
|
|
@@ -32,13 +33,12 @@ const enrichResults = (
|
|
|
32
33
|
scopeAttributes: string[],
|
|
33
34
|
getScope,
|
|
34
35
|
hookType?: SupportedHookTypes,
|
|
35
|
-
|
|
36
|
+
modelOptions?: ModelOptions,
|
|
36
37
|
) => async (
|
|
37
38
|
instancesOrInstance: any | any[],
|
|
38
39
|
options,
|
|
39
40
|
): Promise<void> => {
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
const { scopeAttributeReplacer } = modelOptions;
|
|
42
42
|
if (
|
|
43
43
|
options.originalAttributes?.length > 0
|
|
44
44
|
&& !options.originalAttributes?.includes?.('customFields')
|
|
@@ -57,8 +57,8 @@ const enrichResults = (
|
|
|
57
57
|
scopeAttributes
|
|
58
58
|
.map((attr) => instance[attr])).flat();
|
|
59
59
|
|
|
60
|
-
if (
|
|
61
|
-
identifiers = await
|
|
60
|
+
if (scopeAttributeReplacer) {
|
|
61
|
+
identifiers = await scopeAttributeReplacer(identifiers);
|
|
62
62
|
}
|
|
63
63
|
logger.info('enrichResults - sadot - indentifiers: ', identifiers);
|
|
64
64
|
|
|
@@ -69,7 +69,6 @@ const enrichResults = (
|
|
|
69
69
|
[identifier]: [],
|
|
70
70
|
|
|
71
71
|
}), {});
|
|
72
|
-
console.log({ identifierCustomFieldDefinitionsMapping });
|
|
73
72
|
|
|
74
73
|
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(
|
|
75
74
|
modelType,
|
|
@@ -118,9 +117,6 @@ const enrichResults = (
|
|
|
118
117
|
|
|
119
118
|
scopeAttributes.forEach((attribute) => {
|
|
120
119
|
const identifier = instance[attribute];
|
|
121
|
-
console.log({
|
|
122
|
-
identifier, attribute, instance, identifierCustomFieldDefinitionsMapping,
|
|
123
|
-
});
|
|
124
120
|
|
|
125
121
|
const entityCustomFieldDefinitions = identifierCustomFieldDefinitionsMapping[identifier];
|
|
126
122
|
if (entityCustomFieldDefinitions?.length > 0) {
|
package/src/hooks/update.ts
CHANGED
|
@@ -6,8 +6,6 @@ import { ModelOptions } from '../types';
|
|
|
6
6
|
* A hook to update the custom fields when updating a model (more then one instance).
|
|
7
7
|
*/
|
|
8
8
|
export const beforeBulkUpdate = (options): void => {
|
|
9
|
-
console.log('hooks - beforeBulkUpdate');
|
|
10
|
-
|
|
11
9
|
// This will activate the beforeUpdate hook on each updating instance.
|
|
12
10
|
// eslint-disable-next-line no-param-reassign
|
|
13
11
|
options.individualHooks = true;
|
|
@@ -17,18 +15,17 @@ export const beforeBulkUpdate = (options): void => {
|
|
|
17
15
|
* A hook to update the custom fields when updating a model instance.
|
|
18
16
|
* TODO - cleanup if update fail
|
|
19
17
|
*/
|
|
20
|
-
export const beforeUpdate = (scopeAttributes: string[],
|
|
18
|
+
export const beforeUpdate = (scopeAttributes: string[], modelOptions: ModelOptions) => async (
|
|
21
19
|
instance,
|
|
22
20
|
options,
|
|
23
21
|
): Promise<void> => {
|
|
24
22
|
logger.debug('sadot - before update hook');
|
|
25
|
-
console.log('hooks - beforeUpdate');
|
|
26
|
-
|
|
27
23
|
const { fields } = options;
|
|
24
|
+
const { scopeAttributeReplacer } = modelOptions;
|
|
28
25
|
const modelType = instance.constructor.name;
|
|
29
26
|
let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
30
|
-
if (
|
|
31
|
-
identifiers = await
|
|
27
|
+
if (scopeAttributeReplacer) {
|
|
28
|
+
identifiers = await scopeAttributeReplacer(identifiers);
|
|
32
29
|
}
|
|
33
30
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
34
31
|
if (customFieldsIdx > -1) {
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
import { CustomFieldDefinition } from '../models';
|
|
5
5
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
6
6
|
import logger from '../utils/logger';
|
|
7
|
-
import { ModelOptions } from '../types';
|
|
8
7
|
|
|
9
8
|
export const create = (data: CreateCustomFieldDefinition): Promise<CustomFieldDefinition> =>
|
|
10
9
|
CustomFieldDefinition.create(data);
|
|
@@ -21,9 +20,7 @@ export const findAll = (
|
|
|
21
20
|
return queryModel.scope('userScope').findAll({
|
|
22
21
|
where,
|
|
23
22
|
raw: true,
|
|
24
|
-
logging:
|
|
25
|
-
include: options.include,
|
|
26
|
-
|
|
23
|
+
logging: true,
|
|
27
24
|
});
|
|
28
25
|
};
|
|
29
26
|
|
|
@@ -47,12 +44,10 @@ export const findByEntityIds = async (
|
|
|
47
44
|
modelType: string,
|
|
48
45
|
entityIds: string[],
|
|
49
46
|
options: any = {},
|
|
50
|
-
modelOptions: ModelOptions = {},
|
|
51
47
|
): Promise<CustomFieldDefinition[]> => CustomFieldDefinition.findAll({
|
|
52
48
|
where: {
|
|
53
49
|
modelType,
|
|
54
50
|
entityId: entityIds,
|
|
55
|
-
...modelOptions?.customEntityMapper?.(entityIds),
|
|
56
51
|
},
|
|
57
52
|
transaction: options.transaction,
|
|
58
53
|
include: options.include,
|
|
@@ -103,7 +98,6 @@ export const getRequiredFields = async (
|
|
|
103
98
|
modelType: string,
|
|
104
99
|
modelId: string | string[],
|
|
105
100
|
entityId: string | string[],
|
|
106
|
-
modelOptions: ModelOptions = {},
|
|
107
101
|
): Promise<string[]> => {
|
|
108
102
|
const entityIds = Array.isArray(entityId) ? entityId : [entityId];
|
|
109
103
|
const requiredFields = await CustomFieldDefinition.findAll({
|
|
@@ -111,7 +105,6 @@ export const getRequiredFields = async (
|
|
|
111
105
|
required: true,
|
|
112
106
|
modelType,
|
|
113
107
|
entityId: { [Op.in]: entityIds },
|
|
114
|
-
...modelOptions?.customEntityMapper?.(entityIds),
|
|
115
108
|
},
|
|
116
109
|
logging: true,
|
|
117
110
|
});
|
package/src/repository/value.ts
CHANGED
package/src/types/index.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { Includeable } from 'sequelize';
|
|
2
|
-
import { ModelCtor } from 'sequelize-typescript';
|
|
3
|
-
|
|
4
1
|
export type ModelFetcher = (name: string) => any;
|
|
5
2
|
|
|
6
3
|
export type ModelOptions = {
|
|
7
|
-
|
|
8
|
-
customEntityMapper?: (identifiers: string[]) => any;
|
|
4
|
+
scopeAttributeReplacer?: (attributes?: string[]) => Promise<any>;
|
|
9
5
|
}
|
|
10
6
|
export type Models = {
|
|
11
7
|
name: string;
|
|
@@ -14,7 +10,6 @@ export type Models = {
|
|
|
14
10
|
creationWebhookHandler?: (instance: any) => any;
|
|
15
11
|
updateWebhookHandler?: (instance: any) => any;
|
|
16
12
|
deletionWebhookHandler?: (instance: any) => any;
|
|
17
|
-
identifiersReplacer?: (...args) => Promise<any>;
|
|
18
13
|
}
|
|
19
14
|
|
|
20
15
|
export type CustomFieldOptions = {
|
package/src/utils/init.ts
CHANGED
|
@@ -20,7 +20,7 @@ const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
|
|
|
20
20
|
|
|
21
21
|
export const addHooks = (models: Models[], getModel: ModelFetcher): void => {
|
|
22
22
|
models.forEach(async ({
|
|
23
|
-
name, scopeAttributes,
|
|
23
|
+
name, scopeAttributes, modelOptions,
|
|
24
24
|
}) => {
|
|
25
25
|
try {
|
|
26
26
|
const model = getModel(name);
|
|
@@ -40,11 +40,11 @@ export const addHooks = (models: Models[], getModel: ModelFetcher): void => {
|
|
|
40
40
|
model.addHook('beforeFind', 'sadot-beforeFind', beforeFind(scopeAttributes));
|
|
41
41
|
model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', beforeBulkCreate);
|
|
42
42
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', beforeBulkUpdate);
|
|
43
|
-
model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes,
|
|
44
|
-
model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes,
|
|
45
|
-
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes, 'afterFind', null,
|
|
46
|
-
model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes, null, null,
|
|
47
|
-
model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes, null, null,
|
|
43
|
+
model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes, modelOptions));
|
|
44
|
+
model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes, modelOptions));
|
|
45
|
+
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes, 'afterFind', null, modelOptions));
|
|
46
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes, null, null, modelOptions));
|
|
47
|
+
model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes, null, null, modelOptions));
|
|
48
48
|
} catch (e) {
|
|
49
49
|
logger.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
50
50
|
}
|