@autofleet/sadot 0.7.5 → 0.7.6-beta-0ecad376.1
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.js +27 -16
- package/dist/repository/definition.d.ts +12 -6
- package/dist/repository/value.d.ts +1 -1
- package/dist/repository/value.js +3 -17
- package/package.json +1 -1
- package/src/hooks/create.ts +37 -24
- package/src/repository/definition.ts +15 -6
- package/src/repository/value.ts +2 -19
package/dist/hooks/create.js
CHANGED
|
@@ -48,27 +48,38 @@ exports.beforeBulkCreate = beforeBulkCreate;
|
|
|
48
48
|
const beforeCreate = (scopeAttributes, modelOptions = {}) => async (instance, options) => {
|
|
49
49
|
logger_1.default.debug('sadot - before create hook');
|
|
50
50
|
const { fields } = options;
|
|
51
|
+
const { include, useEntityIdFromInclude } = modelOptions;
|
|
51
52
|
const modelType = instance.constructor.name;
|
|
52
53
|
const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
const where = {
|
|
55
|
+
modelType,
|
|
56
|
+
...(!useEntityIdFromInclude && { entityId: identifiers }),
|
|
57
|
+
};
|
|
58
|
+
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: false, transaction: options.transaction, include: include?.(identifiers) });
|
|
59
|
+
const requiredFieldsNames = Array.from(new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)));
|
|
55
60
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
61
|
+
if ((customFieldsIdx === -1 || !instance.customFields) && requiredFieldsNames?.length > 0) {
|
|
62
|
+
throw new errors_1.MissingRequiredCustomFieldError(requiredFieldsNames);
|
|
63
|
+
}
|
|
64
|
+
// eslint-disable-next-line no-param-reassign
|
|
65
|
+
instance.customFields || (instance.customFields = {});
|
|
56
66
|
const { customFields } = instance;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
await ValueRepo.updateValues(modelType, instance.id, identifiers, customFields, {
|
|
64
|
-
transaction: options.transaction,
|
|
65
|
-
modelOptions,
|
|
66
|
-
}, true);
|
|
67
|
-
// eslint-disable-next-line no-param-reassign
|
|
68
|
-
fields.splice(customFieldsIdx, 1);
|
|
67
|
+
fieldDefinitions.filter((def) => !(def.name in customFields) && ![null, undefined].includes(def.defaultValue)).forEach(({ name, defaultValue }) => {
|
|
68
|
+
customFields[name] = defaultValue;
|
|
69
|
+
});
|
|
70
|
+
if (customFieldsIdx === -1) {
|
|
71
|
+
return;
|
|
69
72
|
}
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
const fieldsNames = Object.keys(customFields);
|
|
74
|
+
const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
|
|
75
|
+
if (missingFields?.length > 0) {
|
|
76
|
+
throw new errors_1.MissingRequiredCustomFieldError(missingFields);
|
|
72
77
|
}
|
|
78
|
+
await ValueRepo.updateValues(modelType, instance.id, identifiers, customFields, {
|
|
79
|
+
transaction: options.transaction,
|
|
80
|
+
modelOptions,
|
|
81
|
+
});
|
|
82
|
+
// eslint-disable-next-line no-param-reassign
|
|
83
|
+
fields.splice(customFieldsIdx, 1);
|
|
73
84
|
};
|
|
74
85
|
exports.beforeCreate = beforeCreate;
|
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
import { type FindOptions, type WhereOptions } from 'sequelize';
|
|
1
|
+
import { type Includeable, type Transaction, type FindOptions, type WhereOptions } from 'sequelize';
|
|
2
2
|
import { CustomFieldDefinition } from '../models';
|
|
3
3
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
4
4
|
import type { ModelOptions } from '../types';
|
|
5
5
|
export declare const create: (data: CreateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
interface SadotFindOptions {
|
|
7
|
+
withDisabled?: boolean;
|
|
8
|
+
transaction?: Transaction;
|
|
9
|
+
include?: Includeable | Includeable[];
|
|
10
|
+
}
|
|
11
|
+
export declare const findAll: (where: WhereOptions, options?: SadotFindOptions) => Promise<CustomFieldDefinition[]>;
|
|
12
|
+
export declare const findByIds: (ids: string[], options?: SadotFindOptions) => Promise<CustomFieldDefinition[]>;
|
|
13
|
+
export declare const findById: (id: string, options?: Pick<SadotFindOptions, 'withDisabled'>) => Promise<CustomFieldDefinition | null>;
|
|
9
14
|
export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: FindOptions & {
|
|
10
15
|
modelOptions?: ModelOptions;
|
|
11
16
|
}) => Promise<CustomFieldDefinition[]>;
|
|
12
17
|
export declare const findByWhere: (where: any) => Promise<CustomFieldDefinition | null>;
|
|
13
18
|
export declare const findDefinitionsByModels: (modelTypes: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
14
19
|
export declare const update: (id: string, data: UpdateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
15
|
-
export declare const disable: (id: string) => Promise<
|
|
16
|
-
export declare const destroy: (id: string) => Promise<
|
|
20
|
+
export declare const disable: (id: string) => Promise<[affectedCount: number]>;
|
|
21
|
+
export declare const destroy: (id: string) => Promise<number>;
|
|
17
22
|
/**
|
|
18
23
|
* Return the names of the required fields for a given model
|
|
19
24
|
*/
|
|
20
25
|
export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[], modelOptions?: ModelOptions) => Promise<string[]>;
|
|
26
|
+
export {};
|
|
@@ -24,5 +24,5 @@ export declare const findValuesByModelIds: (modelIds: string[], options?: any) =
|
|
|
24
24
|
*/
|
|
25
25
|
export declare const updateValues: (modelType: string, modelId: string, identifiers: string[], valuesToUpdate: ValuesToUpdate, options?: FindOptions & {
|
|
26
26
|
modelOptions?: ModelOptions;
|
|
27
|
-
}
|
|
27
|
+
}) => Promise<CustomFieldValue[]>;
|
|
28
28
|
export declare const deleteValue: (id: string, options?: any) => Promise<any>;
|
package/dist/repository/value.js
CHANGED
|
@@ -84,7 +84,7 @@ const formatFunctions = {
|
|
|
84
84
|
* Create new value record if not exists, but fails if value's definition not exist.
|
|
85
85
|
* Return the updated values
|
|
86
86
|
*/
|
|
87
|
-
const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}
|
|
87
|
+
const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}) => {
|
|
88
88
|
const names = Object.keys(valuesToUpdate);
|
|
89
89
|
logger_1.default.debug(`custom-fields: updating values for ${modelType} ${modelId}`, {
|
|
90
90
|
names,
|
|
@@ -96,11 +96,9 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
|
|
|
96
96
|
const where = {
|
|
97
97
|
modelType,
|
|
98
98
|
name: names,
|
|
99
|
+
...(!options.modelOptions?.useEntityIdFromInclude && { entityId: identifiers }),
|
|
99
100
|
};
|
|
100
|
-
|
|
101
|
-
where.entityId = identifiers;
|
|
102
|
-
}
|
|
103
|
-
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) || [];
|
|
101
|
+
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) ?? [];
|
|
104
102
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
105
103
|
if (fieldDefinitions.length !== names.length) {
|
|
106
104
|
logger_1.default.warn(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
|
|
@@ -112,10 +110,8 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
|
|
|
112
110
|
if (valuesWithDisabledDefinitions?.length > 0) {
|
|
113
111
|
logger_1.default.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
|
|
114
112
|
}
|
|
115
|
-
const visitedFields = new Set();
|
|
116
113
|
const values = names.map((name) => {
|
|
117
114
|
const fieldDefinition = fieldDefinitions.find((def) => def.name === name);
|
|
118
|
-
visitedFields.add(fieldDefinition);
|
|
119
115
|
const formatFunction = formatFunctions[fieldDefinition.fieldType];
|
|
120
116
|
return {
|
|
121
117
|
modelId,
|
|
@@ -124,16 +120,6 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
|
|
|
124
120
|
customFieldDefinitionId: fieldDefinition.id,
|
|
125
121
|
};
|
|
126
122
|
});
|
|
127
|
-
if (defineAllDefaults) {
|
|
128
|
-
fieldDefinitions.filter((def) => !visitedFields.has(def) && ![null, undefined].includes(def.defaultValue)).forEach(({ id, defaultValue }) => {
|
|
129
|
-
values.push({
|
|
130
|
-
modelId,
|
|
131
|
-
value: defaultValue,
|
|
132
|
-
updatedAt: new Date(),
|
|
133
|
-
customFieldDefinitionId: id,
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
123
|
return Promise.all(values.map(async (value) => {
|
|
138
124
|
const [cfv] = await models_1.CustomFieldValue.upsert(value, {
|
|
139
125
|
transaction: options.transaction,
|
package/package.json
CHANGED
package/src/hooks/create.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { WhereOptions } from 'sequelize';
|
|
1
2
|
import logger from '../utils/logger';
|
|
2
3
|
import * as ValueRepo from '../repository/value';
|
|
3
4
|
import * as DefinitionRepo from '../repository/definition';
|
|
@@ -23,37 +24,49 @@ export const beforeCreate = (scopeAttributes: string[], modelOptions: ModelOptio
|
|
|
23
24
|
): Promise<void> => {
|
|
24
25
|
logger.debug('sadot - before create hook');
|
|
25
26
|
const { fields } = options;
|
|
27
|
+
const { include, useEntityIdFromInclude } = modelOptions;
|
|
26
28
|
const modelType = instance.constructor.name;
|
|
27
29
|
|
|
28
30
|
const identifiers = applyScopeToInstance(instance, scopeAttributes);
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
const where: WhereOptions = {
|
|
33
|
+
modelType,
|
|
34
|
+
...(!useEntityIdFromInclude && { entityId: identifiers }),
|
|
35
|
+
};
|
|
36
|
+
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: false, transaction: options.transaction, include: include?.(identifiers) });
|
|
37
|
+
const requiredFieldsNames = Array.from(new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)));
|
|
32
38
|
|
|
33
39
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
34
|
-
const { customFields } = instance;
|
|
35
|
-
if (customFieldsIdx > -1 && customFields) {
|
|
36
|
-
const fieldsNames = Object.keys(customFields);
|
|
37
|
-
const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
|
|
38
|
-
if (missingFields?.length > 0) {
|
|
39
|
-
throw new MissingRequiredCustomFieldError(missingFields);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
await ValueRepo.updateValues(
|
|
43
|
-
modelType,
|
|
44
|
-
instance.id,
|
|
45
|
-
identifiers,
|
|
46
|
-
customFields,
|
|
47
|
-
{
|
|
48
|
-
transaction: options.transaction,
|
|
49
|
-
modelOptions,
|
|
50
|
-
},
|
|
51
|
-
true,
|
|
52
|
-
);
|
|
53
40
|
|
|
54
|
-
|
|
55
|
-
fields.splice(customFieldsIdx, 1);
|
|
56
|
-
} else if (requiredFieldsNames?.length > 0) {
|
|
41
|
+
if ((customFieldsIdx === -1 || !instance.customFields) && requiredFieldsNames?.length > 0) {
|
|
57
42
|
throw new MissingRequiredCustomFieldError(requiredFieldsNames);
|
|
58
43
|
}
|
|
44
|
+
// eslint-disable-next-line no-param-reassign
|
|
45
|
+
instance.customFields ||= {};
|
|
46
|
+
const { customFields } = instance;
|
|
47
|
+
fieldDefinitions.filter((def) => !(def.name in customFields) && ![null, undefined].includes(def.defaultValue)).forEach(({ name, defaultValue }) => {
|
|
48
|
+
customFields[name] = defaultValue;
|
|
49
|
+
});
|
|
50
|
+
if (customFieldsIdx === -1) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const fieldsNames = Object.keys(customFields);
|
|
54
|
+
const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
|
|
55
|
+
if (missingFields?.length > 0) {
|
|
56
|
+
throw new MissingRequiredCustomFieldError(missingFields);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await ValueRepo.updateValues(
|
|
60
|
+
modelType,
|
|
61
|
+
instance.id,
|
|
62
|
+
identifiers,
|
|
63
|
+
customFields,
|
|
64
|
+
{
|
|
65
|
+
transaction: options.transaction,
|
|
66
|
+
modelOptions,
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// eslint-disable-next-line no-param-reassign
|
|
71
|
+
fields.splice(customFieldsIdx, 1);
|
|
59
72
|
};
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Op,
|
|
3
|
+
type Includeable, type Transaction, type FindOptions, type WhereOptions,
|
|
4
|
+
} from 'sequelize';
|
|
2
5
|
import { CustomFieldDefinition } from '../models';
|
|
3
6
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
4
7
|
import type { ModelOptions } from '../types';
|
|
@@ -6,9 +9,15 @@ import type { ModelOptions } from '../types';
|
|
|
6
9
|
export const create = (data: CreateCustomFieldDefinition): Promise<CustomFieldDefinition> =>
|
|
7
10
|
CustomFieldDefinition.create(data);
|
|
8
11
|
|
|
12
|
+
interface SadotFindOptions {
|
|
13
|
+
withDisabled?: boolean;
|
|
14
|
+
transaction?: Transaction;
|
|
15
|
+
include?: Includeable | Includeable[];
|
|
16
|
+
}
|
|
17
|
+
|
|
9
18
|
export const findAll = (
|
|
10
19
|
where: WhereOptions,
|
|
11
|
-
options:
|
|
20
|
+
options: SadotFindOptions = { withDisabled: false },
|
|
12
21
|
): Promise<CustomFieldDefinition[]> => {
|
|
13
22
|
const queryModel = options.withDisabled
|
|
14
23
|
? CustomFieldDefinition.unscoped()
|
|
@@ -24,12 +33,12 @@ export const findAll = (
|
|
|
24
33
|
|
|
25
34
|
export const findByIds = (
|
|
26
35
|
ids: string[],
|
|
27
|
-
options:
|
|
36
|
+
options: SadotFindOptions = { withDisabled: false },
|
|
28
37
|
): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);
|
|
29
38
|
|
|
30
39
|
export const findById = (
|
|
31
40
|
id: string,
|
|
32
|
-
options:
|
|
41
|
+
options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },
|
|
33
42
|
): Promise<CustomFieldDefinition | null> => {
|
|
34
43
|
const { withDisabled } = options;
|
|
35
44
|
if (withDisabled) {
|
|
@@ -87,13 +96,13 @@ export const update = async (
|
|
|
87
96
|
return updatedDefinition;
|
|
88
97
|
};
|
|
89
98
|
|
|
90
|
-
export const disable = (id: string): Promise<
|
|
99
|
+
export const disable = (id: string): Promise<[affectedCount: number]> =>
|
|
91
100
|
CustomFieldDefinition.update(
|
|
92
101
|
{ disabled: true },
|
|
93
102
|
{ where: { id } },
|
|
94
103
|
);
|
|
95
104
|
|
|
96
|
-
export const destroy = (id: string): Promise<
|
|
105
|
+
export const destroy = (id: string): Promise<number> =>
|
|
97
106
|
CustomFieldDefinition.destroy({ where: { id } });
|
|
98
107
|
|
|
99
108
|
/**
|
package/src/repository/value.ts
CHANGED
|
@@ -67,7 +67,6 @@ export const updateValues = async (
|
|
|
67
67
|
identifiers: string[],
|
|
68
68
|
valuesToUpdate: ValuesToUpdate,
|
|
69
69
|
options: FindOptions & { modelOptions?: ModelOptions } = {},
|
|
70
|
-
defineAllDefaults = false,
|
|
71
70
|
): Promise<CustomFieldValue[]> => {
|
|
72
71
|
const names = Object.keys(valuesToUpdate);
|
|
73
72
|
logger.debug(`custom-fields: updating values for ${modelType} ${modelId}`, {
|
|
@@ -81,12 +80,10 @@ export const updateValues = async (
|
|
|
81
80
|
const where: WhereOptions = {
|
|
82
81
|
modelType,
|
|
83
82
|
name: names,
|
|
83
|
+
...(!options.modelOptions?.useEntityIdFromInclude && { entityId: identifiers }),
|
|
84
84
|
};
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
where.entityId = identifiers;
|
|
88
|
-
}
|
|
89
|
-
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) || [];
|
|
86
|
+
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) ?? [];
|
|
90
87
|
|
|
91
88
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
92
89
|
if (fieldDefinitions.length !== names.length) {
|
|
@@ -101,11 +98,8 @@ export const updateValues = async (
|
|
|
101
98
|
logger.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
|
|
102
99
|
}
|
|
103
100
|
|
|
104
|
-
const visitedFields = new Set<CustomFieldDefinition>();
|
|
105
|
-
|
|
106
101
|
const values: CreateCustomFieldValue[] = names.map((name) => {
|
|
107
102
|
const fieldDefinition = fieldDefinitions.find((def) => def.name === name);
|
|
108
|
-
visitedFields.add(fieldDefinition);
|
|
109
103
|
const formatFunction = formatFunctions[fieldDefinition.fieldType];
|
|
110
104
|
return {
|
|
111
105
|
modelId,
|
|
@@ -115,17 +109,6 @@ export const updateValues = async (
|
|
|
115
109
|
};
|
|
116
110
|
});
|
|
117
111
|
|
|
118
|
-
if (defineAllDefaults) {
|
|
119
|
-
fieldDefinitions.filter((def) => !visitedFields.has(def) && ![null, undefined].includes(def.defaultValue)).forEach(({ id, defaultValue }) => {
|
|
120
|
-
values.push({
|
|
121
|
-
modelId,
|
|
122
|
-
value: defaultValue,
|
|
123
|
-
updatedAt: new Date(),
|
|
124
|
-
customFieldDefinitionId: id,
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
112
|
return Promise.all(values.map(async (value) => {
|
|
130
113
|
const [cfv] = await CustomFieldValue.upsert(value, {
|
|
131
114
|
transaction: options.transaction,
|