@autofleet/sadot 0.5.4-beta.19 → 0.5.4-beta.20

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.
@@ -22,11 +22,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
29
  exports.beforeCreate = exports.beforeBulkCreate = void 0;
27
30
  const ValueRepo = __importStar(require("../repository/value"));
28
31
  const DefinitionRepo = __importStar(require("../repository/definition"));
29
32
  const errors_1 = require("../errors");
33
+ const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
30
34
  /**
31
35
  * A hook to create the custom fields when updating a model (more then one instance).
32
36
  */
@@ -44,10 +48,7 @@ const beforeCreate = (scopeAttributes, modelOptions) => async (instance, options
44
48
  const { fields } = options;
45
49
  const modelType = instance.constructor.name;
46
50
  const { scopeAttributeReplacer } = modelOptions;
47
- let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
48
- if (scopeAttributeReplacer) {
49
- identifiers = await scopeAttributeReplacer(identifiers);
50
- }
51
+ const identifiers = await (0, scopeAttributes_1.default)(instance, scopeAttributes, scopeAttributeReplacer);
51
52
  // get all model's required definitions
52
53
  const requiredFieldsNames = await DefinitionRepo.getRequiredFields(modelType, instance.id, identifiers);
53
54
  const customFieldsIdx = fields.indexOf('customFields');
@@ -30,6 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
30
30
  const ValueRepo = __importStar(require("../repository/value"));
31
31
  const DefinitionRepo = __importStar(require("../repository/definition"));
32
32
  const logger_1 = __importDefault(require("../utils/logger"));
33
+ const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
33
34
  /**
34
35
  * Serialize custom fields value into the format of {[name] -> [fieldData]}
35
36
  */
@@ -55,11 +56,7 @@ const enrichResults = (modelType, scopeAttributes, getScope, hookType, modelOpti
55
56
  ? instancesOrInstance
56
57
  : [instancesOrInstance];
57
58
  instances = instances.filter(Boolean);
58
- let identifiers = instances.map((instance) => scopeAttributes
59
- .map((attr) => instance[attr])).flat();
60
- if (scopeAttributeReplacer) {
61
- identifiers = await scopeAttributeReplacer(identifiers);
62
- }
59
+ const identifiers = await (0, scopeAttributes_1.default)(instances, scopeAttributes, scopeAttributeReplacer);
63
60
  logger_1.default.info('enrichResults - sadot - indentifiers: ', identifiers);
64
61
  const uniqueIdentifiers = [...new Set(identifiers)].filter(Boolean);
65
62
  const identifierCustomFieldDefinitionsMapping = uniqueIdentifiers.reduce((map, identifier) => ({
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.beforeUpdate = exports.beforeBulkUpdate = void 0;
30
30
  const logger_1 = __importDefault(require("../utils/logger"));
31
31
  const ValueRepo = __importStar(require("../repository/value"));
32
+ const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
32
33
  /**
33
34
  * A hook to update the custom fields when updating a model (more then one instance).
34
35
  */
@@ -47,10 +48,7 @@ const beforeUpdate = (scopeAttributes, modelOptions) => async (instance, options
47
48
  const { fields } = options;
48
49
  const { scopeAttributeReplacer } = modelOptions;
49
50
  const modelType = instance.constructor.name;
50
- let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
51
- if (scopeAttributeReplacer) {
52
- identifiers = await scopeAttributeReplacer(identifiers);
53
- }
51
+ const identifiers = await (0, scopeAttributes_1.default)(instance, scopeAttributes, scopeAttributeReplacer);
54
52
  const customFieldsIdx = fields.indexOf('customFields');
55
53
  if (customFieldsIdx > -1) {
56
54
  const { customFields } = instance;
@@ -0,0 +1,3 @@
1
+ import { ModelOptions } from '../types';
2
+ declare const getScopeAttributes: (instance: any, scopeAttributes: string[], scopeAttributeReplacer?: ModelOptions['scopeAttributeReplacer']) => Promise<any[]>;
3
+ export default getScopeAttributes;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getIdentifiers = (instance, scopeAttributes) => {
4
+ if (Array.isArray(instance)) {
5
+ return instance.map((ins) => scopeAttributes.map((attr) => ins[attr])).flat();
6
+ }
7
+ return scopeAttributes.map((attribute) => instance[attribute]);
8
+ };
9
+ const getScopeAttributes = async (instance, scopeAttributes, scopeAttributeReplacer) => {
10
+ let identifiers = getIdentifiers(instance, scopeAttributes);
11
+ if (scopeAttributeReplacer) {
12
+ identifiers = await scopeAttributeReplacer(identifiers);
13
+ }
14
+ return identifiers;
15
+ };
16
+ exports.default = getScopeAttributes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.5.4-beta.19",
3
+ "version": "0.5.4-beta.20",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -2,6 +2,7 @@ import * as ValueRepo from '../repository/value';
2
2
  import * as DefinitionRepo from '../repository/definition';
3
3
  import { MissingRequiredCustomFieldError } from '../errors';
4
4
  import { ModelOptions } from '../types';
5
+ import getScopeAttributes from '../utils/scopeAttributes';
5
6
 
6
7
  /**
7
8
  * A hook to create the custom fields when updating a model (more then one instance).
@@ -23,10 +24,7 @@ export const beforeCreate = (scopeAttributes: string[], modelOptions: ModelOptio
23
24
  const modelType = instance.constructor.name;
24
25
  const { scopeAttributeReplacer } = modelOptions;
25
26
 
26
- let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
27
- if (scopeAttributeReplacer) {
28
- identifiers = await scopeAttributeReplacer(identifiers);
29
- }
27
+ const identifiers = await getScopeAttributes(instance, scopeAttributes, scopeAttributeReplacer);
30
28
  // get all model's required definitions
31
29
  const requiredFieldsNames = await DefinitionRepo.getRequiredFields(modelType,
32
30
  instance.id,
@@ -6,6 +6,7 @@ import CustomFieldDefinition from '../models/CustomFieldDefinition';
6
6
  import { SerializedCustomFields } from '../types/definition';
7
7
  import logger from '../utils/logger';
8
8
  import { ModelOptions } from '../types';
9
+ import getScopeAttributes from '../utils/scopeAttributes';
9
10
 
10
11
  type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
11
12
 
@@ -53,13 +54,7 @@ const enrichResults = (
53
54
 
54
55
  instances = instances.filter(Boolean);
55
56
 
56
- let identifiers = instances.map((instance) =>
57
- scopeAttributes
58
- .map((attr) => instance[attr])).flat();
59
-
60
- if (scopeAttributeReplacer) {
61
- identifiers = await scopeAttributeReplacer(identifiers);
62
- }
57
+ const identifiers = await getScopeAttributes(instances, scopeAttributes, scopeAttributeReplacer);
63
58
  logger.info('enrichResults - sadot - indentifiers: ', identifiers);
64
59
 
65
60
  const uniqueIdentifiers = [...new Set(identifiers)].filter(Boolean);
@@ -1,6 +1,7 @@
1
1
  import logger from '../utils/logger';
2
2
  import * as ValueRepo from '../repository/value';
3
3
  import { ModelOptions } from '../types';
4
+ import getScopeAttributes from '../utils/scopeAttributes';
4
5
 
5
6
  /**
6
7
  * A hook to update the custom fields when updating a model (more then one instance).
@@ -23,10 +24,8 @@ export const beforeUpdate = (scopeAttributes: string[], modelOptions: ModelOptio
23
24
  const { fields } = options;
24
25
  const { scopeAttributeReplacer } = modelOptions;
25
26
  const modelType = instance.constructor.name;
26
- let identifiers = scopeAttributes.map((attribute) => instance[attribute]);
27
- if (scopeAttributeReplacer) {
28
- identifiers = await scopeAttributeReplacer(identifiers);
29
- }
27
+ const identifiers = await getScopeAttributes(instance, scopeAttributes, scopeAttributeReplacer);
28
+
30
29
  const customFieldsIdx = fields.indexOf('customFields');
31
30
  if (customFieldsIdx > -1) {
32
31
  const { customFields } = instance;
@@ -0,0 +1,18 @@
1
+ import { ModelOptions } from '../types';
2
+
3
+ const getIdentifiers = (instance: any, scopeAttributes: string[]) => {
4
+ if (Array.isArray(instance)) {
5
+ return instance.map((ins) =>
6
+ scopeAttributes.map((attr) => ins[attr])).flat();
7
+ }
8
+ return scopeAttributes.map((attribute) => instance[attribute]);
9
+ };
10
+ const getScopeAttributes = async (instance: any, scopeAttributes: string[], scopeAttributeReplacer?: ModelOptions['scopeAttributeReplacer']) => {
11
+ let identifiers = getIdentifiers(instance, scopeAttributes);
12
+ if (scopeAttributeReplacer) {
13
+ identifiers = await scopeAttributeReplacer(identifiers);
14
+ }
15
+ return identifiers;
16
+ };
17
+
18
+ export default getScopeAttributes;