@contrail/cli 3.6.0-alpha-8-alpha.694fd70 → 3.6.0-alpha.aae3e70
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/README.md +45 -151
- package/dist/api/feature-flags.d.ts +3 -0
- package/dist/api/feature-flags.js +29 -0
- package/dist/api/types.d.ts +0 -35
- package/dist/api/types.js +1 -49
- package/dist/api/users.d.ts +1 -0
- package/dist/api/users.js +10 -1
- package/dist/auth/login.d.ts +4 -0
- package/dist/auth/login.js +167 -93
- package/dist/auth/login.spec.js +305 -0
- package/dist/auth/sso.d.ts +36 -0
- package/dist/auth/sso.js +164 -0
- package/dist/auth/sso.spec.js +119 -0
- package/dist/basecommand.d.ts +1 -3
- package/dist/basecommand.js +2 -2
- package/dist/commands/app/getApiKey.js +2 -2
- package/dist/commands/config/set.js +1 -1
- package/dist/commands/feature-flags/export.d.ts +19 -0
- package/dist/commands/feature-flags/export.js +66 -0
- package/dist/commands/feature-flags/export.spec.js +109 -0
- package/dist/commands/feature-flags/import.d.ts +42 -0
- package/dist/commands/feature-flags/import.js +191 -0
- package/dist/commands/feature-flags/import.spec.js +194 -0
- package/dist/commands/instance/export.d.ts +9 -0
- package/dist/commands/instance/export.js +18 -3
- package/dist/commands/instance/export.spec.js +32 -0
- package/dist/commands/instance/import.d.ts +3 -0
- package/dist/commands/instance/import.js +5 -0
- package/dist/commands/instance/import.spec.js +4 -0
- package/dist/commands/login.d.ts +12 -0
- package/dist/commands/login.js +25 -0
- package/dist/common.d.ts +1 -3
- package/dist/common.js +1 -13
- package/dist/helpers/config/config.d.ts +5 -4
- package/dist/helpers/config/config.js +10 -12
- package/dist/helpers/file-helpers.d.ts +0 -1
- package/dist/helpers/file-helpers.js +1 -18
- package/dist/logger.js +1 -4
- package/node_modules/@contrail/entity-compare/package.json +3 -6
- package/node_modules/@contrail/loader/lib/common/conditional-columns.js +1 -1
- package/node_modules/@contrail/loader/lib/constants.d.ts +8 -0
- package/node_modules/@contrail/loader/lib/constants.js +9 -1
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.d.ts +4 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.js +57 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.spec.js +492 -34
- package/node_modules/@contrail/loader/lib/pipeline/validate.js +2 -52
- package/node_modules/@contrail/loader/lib/pipeline/validate.spec.js +0 -85
- package/node_modules/@contrail/loader/package.json +2 -2
- package/node_modules/@contrail/sdk/.github/workflows/publish-to-npm.yml +4 -1
- package/node_modules/@contrail/sdk/lib/auth/login.js +1 -1
- package/node_modules/@contrail/sdk/lib/config/config.d.ts +0 -3
- package/node_modules/@contrail/sdk/lib/config/config.js +7 -13
- package/node_modules/@contrail/sdk/lib/core/request.js +2 -6
- package/node_modules/@contrail/sdk/lib/entities/entities.client.d.ts +0 -2
- package/node_modules/@contrail/sdk/lib/entities/entities.client.js +1 -3
- package/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.js +0 -1
- package/node_modules/@contrail/sdk/package.json +1 -1
- package/oclif.manifest.json +75 -365
- package/package.json +15 -5
- package/dist/commands/entities/create.d.ts +0 -48
- package/dist/commands/entities/create.js +0 -639
- package/dist/commands/entities/create.spec.js +0 -541
- package/dist/commands/entities/delete.d.ts +0 -19
- package/dist/commands/entities/delete.js +0 -134
- package/dist/commands/entities/get.d.ts +0 -24
- package/dist/commands/entities/get.js +0 -273
- package/dist/commands/entities/grammar.spec.js +0 -80
- package/dist/commands/entities/output.spec.js +0 -72
- package/dist/commands/entities/update.d.ts +0 -31
- package/dist/commands/entities/update.js +0 -157
- package/dist/helpers/entities/assortment-items.d.ts +0 -10
- package/dist/helpers/entities/assortment-items.js +0 -84
- package/dist/helpers/entities/batchFetch.d.ts +0 -8
- package/dist/helpers/entities/batchFetch.js +0 -49
- package/dist/helpers/entities/index.d.ts +0 -3
- package/dist/helpers/entities/index.js +0 -6
- package/dist/helpers/entities/items.d.ts +0 -10
- package/dist/helpers/entities/items.js +0 -102
- package/dist/helpers/entities/project-items.d.ts +0 -3
- package/dist/helpers/entities/project-items.js +0 -65
- package/dist/helpers/propertySpecifications/dateRanges.d.ts +0 -38
- package/dist/helpers/propertySpecifications/dateRanges.js +0 -89
- package/dist/helpers/propertySpecifications/dateRanges.spec.js +0 -95
- package/dist/helpers/propertySpecifications/propertySpecifications.d.ts +0 -40
- package/dist/helpers/propertySpecifications/propertySpecifications.js +0 -109
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.d.ts +0 -1
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.js +0 -373
- package/dist/logger.spec.d.ts +0 -1
- package/dist/logger.spec.js +0 -12
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/pull_request_template.md +0 -31
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/check-code-formatting.yml +0 -34
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/empty.yml +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/generate-sdk-docs.yml +0 -32
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/publish-to-npm.yml +0 -117
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/sdk-lib.yml +0 -27
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.prettierrc.yaml +0 -2
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/README.md +0 -72
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/home-hero.svg +0 -11
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/assets.d.ts +0 -9
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/assets.js +0 -54
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/auth.d.ts +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/auth.js +0 -6
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/index.d.ts +0 -2
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/index.js +0 -18
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/login.d.ts +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/login.js +0 -209
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/config.d.ts +0 -48
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/config.js +0 -42
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/content.d.ts +0 -12
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/content.js +0 -70
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/request.d.ts +0 -15
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/request.js +0 -241
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entities.client.d.ts +0 -101
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entities.client.js +0 -306
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference-expander.d.ts +0 -9
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference-expander.js +0 -86
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference.d.ts +0 -6
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference.js +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.d.ts +0 -5
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.js +0 -59
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/index.d.ts +0 -3
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/index.js +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/file-type-helper.d.ts +0 -3
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/file-type-helper.js +0 -24
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/files.d.ts +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/files.js +0 -151
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/index.d.ts +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/index.js +0 -26
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/notifications-dispatcher.d.ts +0 -16
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/notifications-dispatcher.js +0 -37
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies-cache.d.ts +0 -12
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies-cache.js +0 -55
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies.client.d.ts +0 -9
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies.client.js +0 -74
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/index.d.ts +0 -2
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/index.js +0 -18
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/sub-types-helper.d.ts +0 -18
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/sub-types-helper.js +0 -98
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types-cache.d.ts +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types-cache.js +0 -73
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types.client.d.ts +0 -42
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types.client.js +0 -217
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/package.json +0 -64
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/scripts/replicate-bug.js +0 -69
- package/node_modules/@contrail/sdk/scripts/replicate-bug.js +0 -69
- /package/dist/{commands/entities/create.spec.d.ts → auth/sso.spec.d.ts} +0 -0
- /package/dist/commands/{entities/grammar.spec.d.ts → feature-flags/export.spec.d.ts} +0 -0
- /package/dist/commands/{entities/output.spec.d.ts → feature-flags/import.spec.d.ts} +0 -0
- /package/dist/{helpers/propertySpecifications/dateRanges.spec.d.ts → commands/instance/export.spec.d.ts} +0 -0
|
@@ -1,639 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.doesPropertyLevelMatchRole = exports.selectRandomObject = exports.buildObjectReferenceCache = exports.getReferencedObjectPaths = exports.castValueToPropertyType = exports.pickRandomValueForProperty = exports.summarizeSettledCreates = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
6
|
-
const unique_names_generator_1 = require("unique-names-generator");
|
|
7
|
-
const p_limit_1 = tslib_1.__importDefault(require("p-limit"));
|
|
8
|
-
const types_1 = require("@contrail/types");
|
|
9
|
-
const types_2 = require("../../api/types");
|
|
10
|
-
const core_1 = require("@oclif/core");
|
|
11
|
-
const types_3 = require("../../api/types");
|
|
12
|
-
const login_1 = require("../../auth/login");
|
|
13
|
-
const common_1 = require("../../common");
|
|
14
|
-
const basecommand_1 = require("../../basecommand");
|
|
15
|
-
const sdk_1 = require("@contrail/sdk");
|
|
16
|
-
const entities_1 = require("../../helpers/entities");
|
|
17
|
-
const propertySpecifications_1 = require("../../helpers/propertySpecifications/propertySpecifications");
|
|
18
|
-
const file_helpers_1 = require("../../helpers/file-helpers");
|
|
19
|
-
let IS_DRY_RUN = false;
|
|
20
|
-
let SHOULD_USE_READABLE_IDS = false;
|
|
21
|
-
let SHOULD_SKIP_HYDRATE_RELATIONSHIPS = false;
|
|
22
|
-
let SHOULD_GENERATE_MISSING_VALUES = false;
|
|
23
|
-
let NUM_ENTITIES_TO_CREATE = 1; // Default to creating one entity if not specified
|
|
24
|
-
const getNumEntitiesToCreate = () => NUM_ENTITIES_TO_CREATE; // method to get the latest number in case this changes later
|
|
25
|
-
const baseValidationMap = {
|
|
26
|
-
assortmentId: async (_slug, assortmentId) => (0, entities_1.throwIfInvalidAssortmentId)(assortmentId),
|
|
27
|
-
itemId: async (_slug, itemId) => (0, entities_1.throwIfInvalidItemId)(itemId),
|
|
28
|
-
projectId: async (_slug, projectId) => (0, entities_1.throwIfInvalidProjectId)(projectId),
|
|
29
|
-
};
|
|
30
|
-
const throwIfInvalidPropertyLevelFn = async (_slug, maybeRoles) => [].concat(maybeRoles).forEach((maybeRole) => (0, entities_1.throwIfInvalidItemLevel)(maybeRole));
|
|
31
|
-
const throwIfInvalidUserOrgRoleFn = async (_slug, maybeRole) => {
|
|
32
|
-
if (maybeRole !== 'MEMBER' && maybeRole !== 'ADMIN') {
|
|
33
|
-
throw new Error(`${maybeRole} is not a valid User Org Role`); // TODO: get these roles from a shared library
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
const SLUG_VALIDATION_BY_ROOT_MAP = {
|
|
37
|
-
item: { ...baseValidationMap, roles: throwIfInvalidPropertyLevelFn },
|
|
38
|
-
'project-item': { ...baseValidationMap, roles: throwIfInvalidPropertyLevelFn },
|
|
39
|
-
'assortment-item': baseValidationMap,
|
|
40
|
-
'user-org': baseValidationMap,
|
|
41
|
-
'custom-entity': { ...baseValidationMap, role: throwIfInvalidUserOrgRoleFn },
|
|
42
|
-
};
|
|
43
|
-
const SLUGS_THAT_MUST_BE_SPECIFIED_BY_TYPE_PATH = {
|
|
44
|
-
item: ['roles'],
|
|
45
|
-
'project-item': ['projectId'],
|
|
46
|
-
'assortment-item': ['assortmentId'],
|
|
47
|
-
'user-org': ['role'],
|
|
48
|
-
};
|
|
49
|
-
const FIND_VALID_VALUE_MAP = {
|
|
50
|
-
assortment: [
|
|
51
|
-
{
|
|
52
|
-
slug: 'name',
|
|
53
|
-
findValidValuesFn: async (assortments) => new Set(Array.from({ length: assortments.length }, () => generateRandomReadableString())),
|
|
54
|
-
applyValueToEntityFn: (entity, validValues) => {
|
|
55
|
-
if (entity.name)
|
|
56
|
-
return; // skip if already specified
|
|
57
|
-
entity.name = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
58
|
-
validValues.delete(entity.name);
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
],
|
|
62
|
-
'assortment-item': [
|
|
63
|
-
{
|
|
64
|
-
slug: 'itemId',
|
|
65
|
-
findValidValuesFn: async (assortmentItems, count) => new Set((await (0, entities_1.findItemsNotInAssortment)(assortmentItems[0].assortmentId, count)).map(({ id }) => id)),
|
|
66
|
-
applyValueToEntityFn: (entity, validValues) => {
|
|
67
|
-
if (entity.itemId)
|
|
68
|
-
return; // skip if already specified
|
|
69
|
-
entity.itemId = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
70
|
-
validValues.delete(entity.itemId);
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
'project-item': [
|
|
75
|
-
{
|
|
76
|
-
slug: 'itemId',
|
|
77
|
-
findValidValuesFn: async (projectItems) => new Set((await (0, entities_1.findItemsNotInProject)(projectItems[0].projectId)).map(({ id }) => id)),
|
|
78
|
-
applyValueToEntityFn: (entity, validValues) => {
|
|
79
|
-
if (entity.itemId)
|
|
80
|
-
return; // skip if already specified
|
|
81
|
-
entity.itemId = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
82
|
-
validValues.delete(entity.itemId);
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
],
|
|
86
|
-
item: [
|
|
87
|
-
{
|
|
88
|
-
slug: 'itemFamilyId',
|
|
89
|
-
findValidValuesFn: async (_items) => new Set(await (0, entities_1.getItemFamilies)(getNumEntitiesToCreate()).then((families) => families === undefined ? undefined : families.map(({ id }) => id))),
|
|
90
|
-
applyValueToEntityFn: (entity, validValues) => {
|
|
91
|
-
if (entity.itemFamilyId)
|
|
92
|
-
return; // skip if already specified
|
|
93
|
-
if (entity.roles?.includes('option')) {
|
|
94
|
-
entity.itemFamilyId =
|
|
95
|
-
Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
96
|
-
// can reuse itemFamilyId for options
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
slug: 'name',
|
|
102
|
-
findValidValuesFn: async (items) => new Set(Array.from({ length: items.length }, () => generateRandomReadableString())),
|
|
103
|
-
applyValueToEntityFn: (entity, validValues) => {
|
|
104
|
-
if (entity.name)
|
|
105
|
-
return; // skip if already specified
|
|
106
|
-
entity.name = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
107
|
-
validValues.delete(entity.name);
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
slug: 'optionName',
|
|
112
|
-
findValidValuesFn: async (items) => new Set(Array.from({ length: items.length }, () => generateRandomReadableString())),
|
|
113
|
-
applyValueToEntityFn: (entity, validValues) => {
|
|
114
|
-
if (entity.optionName)
|
|
115
|
-
return; // skip if already specified
|
|
116
|
-
if (!entity.roles?.includes('option'))
|
|
117
|
-
return; // only apply to options
|
|
118
|
-
entity.optionName = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
119
|
-
validValues.delete(entity.optionName);
|
|
120
|
-
},
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
slug: 'optionGroup',
|
|
124
|
-
findValidValuesFn: async (_items) => new Set(),
|
|
125
|
-
applyValueToEntityFn: (entity, _validValues) => {
|
|
126
|
-
if (entity.optionGroup)
|
|
127
|
-
return; // skip if already specified
|
|
128
|
-
if (!entity.roles?.includes('option'))
|
|
129
|
-
return; // only apply to options
|
|
130
|
-
entity.optionGroup = 'color';
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
],
|
|
134
|
-
workspace: [
|
|
135
|
-
{
|
|
136
|
-
slug: 'name',
|
|
137
|
-
findValidValuesFn: async (items) => new Set(Array.from({ length: items.length }, () => generateRandomReadableString())),
|
|
138
|
-
applyValueToEntityFn: (entity, validValues) => {
|
|
139
|
-
if (entity.name)
|
|
140
|
-
return; // skip if already specified
|
|
141
|
-
entity.name = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
142
|
-
validValues.delete(entity.name);
|
|
143
|
-
},
|
|
144
|
-
},
|
|
145
|
-
],
|
|
146
|
-
};
|
|
147
|
-
class CreateEntity extends basecommand_1.BaseCommand {
|
|
148
|
-
static description = 'Create entities';
|
|
149
|
-
static examples = [
|
|
150
|
-
'contrail entities create color -g -n=10 # creates 10 random colors',
|
|
151
|
-
'contrail entities create color -- name="Electric Blue" green=1 red=2 blue=3 # creates one color with specified properties only',
|
|
152
|
-
'contrail entities create custom-entities:franchise',
|
|
153
|
-
'contrail entities create project-item -n=1 -- projectId=abc # creates 10 project items within project ABC',
|
|
154
|
-
'contrail entities create color -g -r # creates a random color with a human readable ID',
|
|
155
|
-
'contrail entities create color -d -- name=test # simulates the creation of a color without actually creating it',
|
|
156
|
-
'contrail entities create custom-entity:franchise -s # creates an entity and the response does not contain hydrating relationships',
|
|
157
|
-
'contrail entities create color -g -- name=Electric Blue # creates a color with the name specified and other properties auto-generated',
|
|
158
|
-
'contrail entities create color -o /tmp/colors.json -- name=Blue green=1 red=2 blue=3 # creates a color and saves the output to /tmp/colors.json',
|
|
159
|
-
];
|
|
160
|
-
static flags = {
|
|
161
|
-
...basecommand_1.BaseCommand.baseFlags,
|
|
162
|
-
count: core_1.Flags.integer({
|
|
163
|
-
char: 'n',
|
|
164
|
-
description: 'Number of random entities to create',
|
|
165
|
-
required: false,
|
|
166
|
-
}),
|
|
167
|
-
dryRun: core_1.Flags.boolean({
|
|
168
|
-
char: 'd',
|
|
169
|
-
description: 'If true, does not create the entity but simulates the creation',
|
|
170
|
-
required: false,
|
|
171
|
-
default: false,
|
|
172
|
-
}),
|
|
173
|
-
inputFile: core_1.Flags.string({
|
|
174
|
-
char: 'i',
|
|
175
|
-
description: 'A file with a JSON array of objects where each object is one entity to create. Cannot be used with --count. Unless --generate-values is specified, only the properties in the file will be used.',
|
|
176
|
-
required: false,
|
|
177
|
-
multiple: false,
|
|
178
|
-
}),
|
|
179
|
-
outputFile: core_1.Flags.string({
|
|
180
|
-
char: 'o',
|
|
181
|
-
description: 'File to save the output (optional)',
|
|
182
|
-
required: false,
|
|
183
|
-
multiple: false,
|
|
184
|
-
}),
|
|
185
|
-
generateValues: core_1.Flags.boolean({
|
|
186
|
-
char: 'g',
|
|
187
|
-
description: 'If true, generates random values for properties that are not specified. By default, only specified properties are used.',
|
|
188
|
-
required: false,
|
|
189
|
-
default: false,
|
|
190
|
-
}),
|
|
191
|
-
readableIds: core_1.Flags.boolean({
|
|
192
|
-
char: 'r',
|
|
193
|
-
description: 'If true, uses readable IDs for the created entities',
|
|
194
|
-
required: false,
|
|
195
|
-
default: false,
|
|
196
|
-
}),
|
|
197
|
-
skipHydrateRelationships: core_1.Flags.boolean({
|
|
198
|
-
char: 'S',
|
|
199
|
-
description: 'If true, does not hydrate relationships for the created entities',
|
|
200
|
-
required: false,
|
|
201
|
-
default: false,
|
|
202
|
-
}),
|
|
203
|
-
suffix: core_1.Flags.string({
|
|
204
|
-
description: 'Append a sub-route to the entity endpoint (e.g. --suffix=synchronous targets /<entities>/synchronous)',
|
|
205
|
-
required: false,
|
|
206
|
-
}),
|
|
207
|
-
apiVersion: core_1.Flags.string({
|
|
208
|
-
description: 'Api-Version header to send; selects the V1 or V2 variant of the endpoint',
|
|
209
|
-
options: ['1', '2'],
|
|
210
|
-
required: false,
|
|
211
|
-
}),
|
|
212
|
-
};
|
|
213
|
-
static args = {
|
|
214
|
-
path: core_1.Args.string({
|
|
215
|
-
description: 'Path of the entity type to create',
|
|
216
|
-
required: true,
|
|
217
|
-
multiple: false,
|
|
218
|
-
}),
|
|
219
|
-
};
|
|
220
|
-
static strict = false;
|
|
221
|
-
async run() {
|
|
222
|
-
(0, common_1.routeConsoleLogToStderr)();
|
|
223
|
-
await (0, login_1.authUser)();
|
|
224
|
-
const { flags, argv, args } = await this.parse(CreateEntity);
|
|
225
|
-
const { count, dryRun: isDryRun, inputFile: inputFilename, outputFile: outputFilename, generateValues: shouldGenerateMissingValues, readableIds: shouldUseReadableIds, skipHydrateRelationships, suffix, apiVersion, } = flags;
|
|
226
|
-
const { path: entityTypePath } = args;
|
|
227
|
-
// Global flags is a little hacky, but it was easier
|
|
228
|
-
// and it doesn't see *that* bad because they are only valid
|
|
229
|
-
// for the lifecycle of the command.
|
|
230
|
-
IS_DRY_RUN = isDryRun;
|
|
231
|
-
SHOULD_USE_READABLE_IDS = shouldUseReadableIds;
|
|
232
|
-
SHOULD_SKIP_HYDRATE_RELATIONSHIPS = skipHydrateRelationships;
|
|
233
|
-
SHOULD_GENERATE_MISSING_VALUES = shouldGenerateMissingValues;
|
|
234
|
-
const { ids: strayTokens, specs } = (0, propertySpecifications_1.partitionIdsAndSpecs)(argv.slice(1));
|
|
235
|
-
if (strayTokens.length > 0) {
|
|
236
|
-
this.error(`create takes no positional ids (got: ${strayTokens.join(' ')}); did you forget -- before property values?`);
|
|
237
|
-
}
|
|
238
|
-
const propertySpecBySlug = (0, propertySpecifications_1.parsePropertySpecificationsIntoMap)(specs);
|
|
239
|
-
// Validate args
|
|
240
|
-
if (!entityTypePath) {
|
|
241
|
-
this.error('You must specify an entity type path.');
|
|
242
|
-
}
|
|
243
|
-
if (inputFilename && count) {
|
|
244
|
-
throw new Error('You cannot use --input and --count together. Please specify one or the other.');
|
|
245
|
-
}
|
|
246
|
-
if (count !== undefined && count <= 0) {
|
|
247
|
-
this.error('Count must be a positive integer.');
|
|
248
|
-
}
|
|
249
|
-
const partialEntities = inputFilename
|
|
250
|
-
? (0, file_helpers_1.parseFileAsJsonArray)(inputFilename)
|
|
251
|
-
: Array.from({ length: count || 1 }).fill({});
|
|
252
|
-
NUM_ENTITIES_TO_CREATE = partialEntities.length;
|
|
253
|
-
const { root: entityName } = (0, types_2.parseTypePath)(entityTypePath);
|
|
254
|
-
// Ensure we have the required properties for the entity type
|
|
255
|
-
if (SLUGS_THAT_MUST_BE_SPECIFIED_BY_TYPE_PATH[entityName]) {
|
|
256
|
-
const requiredSlugs = SLUGS_THAT_MUST_BE_SPECIFIED_BY_TYPE_PATH[entityName];
|
|
257
|
-
for (const slug of requiredSlugs) {
|
|
258
|
-
// Check if the property is specified in command-line args
|
|
259
|
-
const isInCommandLineArgs = propertySpecBySlug.has(slug);
|
|
260
|
-
// Check if all entities from input file have this property
|
|
261
|
-
const allEntitiesHaveProperty = inputFilename
|
|
262
|
-
? partialEntities.every((entity) => entity[slug] !== undefined && entity[slug] !== null)
|
|
263
|
-
: false;
|
|
264
|
-
if (!isInCommandLineArgs && !allEntitiesHaveProperty) {
|
|
265
|
-
this.error(`Property '${slug}' must be specified for entity type '${entityName}'.`);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
try {
|
|
270
|
-
const entitiesToCreate = await buildEntitiesToCreate(entityTypePath, partialEntities, propertySpecBySlug);
|
|
271
|
-
const summary = await createEntities(entityName, entitiesToCreate, {
|
|
272
|
-
suffix,
|
|
273
|
-
apiVersion: apiVersion,
|
|
274
|
-
});
|
|
275
|
-
if (outputFilename) {
|
|
276
|
-
fs_1.default.writeFileSync(outputFilename, JSON.stringify(summary, null, 2));
|
|
277
|
-
console.error(`Output saved to ${outputFilename}`);
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
this.log(JSON.stringify(summary, null, 2));
|
|
281
|
-
}
|
|
282
|
-
if (summary.failed.length > 0) {
|
|
283
|
-
console.error(`${summary.failed.length} of ${entitiesToCreate.length} entities failed.`);
|
|
284
|
-
process.exitCode = 1;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
catch (error) {
|
|
288
|
-
this.error(`Failed to create entities: ${error.message}, ${error.stack}`);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
exports.default = CreateEntity;
|
|
293
|
-
function summarizeSettledCreates(inputs, settled) {
|
|
294
|
-
const created = [];
|
|
295
|
-
const failed = [];
|
|
296
|
-
settled.forEach((result, index) => {
|
|
297
|
-
if (result.status === 'fulfilled') {
|
|
298
|
-
created.push(result.value);
|
|
299
|
-
}
|
|
300
|
-
else {
|
|
301
|
-
failed.push({ input: inputs[index], error: (0, common_1.sdkErrorMessage)(result.reason) });
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
return { created, failed };
|
|
305
|
-
}
|
|
306
|
-
exports.summarizeSettledCreates = summarizeSettledCreates;
|
|
307
|
-
async function createEntities(entityName, entities, routing = {}) {
|
|
308
|
-
if (IS_DRY_RUN) {
|
|
309
|
-
console.error(`Dry run mode: not creating entities, but simulating creation for ${entityName}`);
|
|
310
|
-
return { created: entities, failed: [] };
|
|
311
|
-
}
|
|
312
|
-
const limit = (0, p_limit_1.default)(20);
|
|
313
|
-
const settled = await Promise.allSettled(entities.map(async (e) => limit(() => createEntity(entityName, e, routing))));
|
|
314
|
-
return summarizeSettledCreates(entities, settled);
|
|
315
|
-
}
|
|
316
|
-
async function buildEntitiesToCreate(entityTypePath, entities, propertySpecificationBySlug) {
|
|
317
|
-
let type = await (0, types_3.getTypeByPath)(entityTypePath);
|
|
318
|
-
if (!type) {
|
|
319
|
-
console.warn(`Type not found for path: ${entityTypePath}`);
|
|
320
|
-
type = {
|
|
321
|
-
typePath: '',
|
|
322
|
-
typeProperties: [],
|
|
323
|
-
}; // Fallback to an empty type; This allows us to create entities that are not type-managed
|
|
324
|
-
}
|
|
325
|
-
const [typesCache, objectReferencesCache] = await Promise.all([
|
|
326
|
-
(0, types_2.buildLocalTypesCache)([...(0, types_2.getReferencedTypePaths)(type), entityTypePath]),
|
|
327
|
-
buildLocalObjectReferencesCache(getReferencedObjectPaths(type)),
|
|
328
|
-
]);
|
|
329
|
-
const populatedEntities = entities.map((entity) => ({
|
|
330
|
-
...createEntityWithPopulatedValues(type, typesCache, objectReferencesCache, propertySpecificationBySlug),
|
|
331
|
-
...entity,
|
|
332
|
-
}));
|
|
333
|
-
const limit = (0, p_limit_1.default)(30);
|
|
334
|
-
await Promise.all(populatedEntities.map(async () => limit(() => throwIfEntityIsInvalid)));
|
|
335
|
-
// Skip system value generation unless -g flag is set (generateValues mode)
|
|
336
|
-
if (!SHOULD_GENERATE_MISSING_VALUES) {
|
|
337
|
-
console.error('Using only specified properties. Use --generate-values to auto-generate missing values.');
|
|
338
|
-
return populatedEntities;
|
|
339
|
-
}
|
|
340
|
-
// Add system values if needed
|
|
341
|
-
const { root } = (0, types_2.parseTypePath)(type.typePath);
|
|
342
|
-
const valueFinderApplierForEntity = FIND_VALID_VALUE_MAP[root] || [];
|
|
343
|
-
for (const { slug, findValidValuesFn, applyValueToEntityFn } of valueFinderApplierForEntity) {
|
|
344
|
-
// If the property is already specified in command-line args, skip it
|
|
345
|
-
if (propertySpecificationBySlug?.has(slug)) {
|
|
346
|
-
console.error(`Skipping property ${slug} because it is already specified.`);
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
// If all entities already have this property, skip finding values
|
|
350
|
-
const allEntitiesHaveProperty = populatedEntities.every((entity) => entity[slug] !== undefined && entity[slug] !== null);
|
|
351
|
-
if (allEntitiesHaveProperty) {
|
|
352
|
-
console.error(`Skipping property ${slug} because all entities already have this value.`);
|
|
353
|
-
continue;
|
|
354
|
-
}
|
|
355
|
-
const values = await findValidValuesFn(populatedEntities, entities.length);
|
|
356
|
-
if (values) {
|
|
357
|
-
populatedEntities.forEach((e) => applyValueToEntityFn(e, values));
|
|
358
|
-
}
|
|
359
|
-
else {
|
|
360
|
-
console.warn(`No valid values found for property ${slug}. Skipping.`);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return populatedEntities;
|
|
364
|
-
}
|
|
365
|
-
function createEntityWithPopulatedValues(type, typeReferencesCache, objectReferencesCache, propertySpecificationBySlug) {
|
|
366
|
-
const rolesValue = propertySpecificationBySlug?.get('roles');
|
|
367
|
-
const roles = rolesValue === undefined ? undefined : [].concat(rolesValue);
|
|
368
|
-
const entity = SHOULD_GENERATE_MISSING_VALUES
|
|
369
|
-
? pickRandomPropertyValues(type, objectReferencesCache, typeReferencesCache, roles)
|
|
370
|
-
: {};
|
|
371
|
-
// Handle property specifications
|
|
372
|
-
for (const [propertySlug, specifiedValue] of propertySpecificationBySlug.entries()) {
|
|
373
|
-
const correspondingProperty = type.typeProperties.find((prop) => getPropertySlug(prop) === propertySlug);
|
|
374
|
-
// slug= means unset; on create there is nothing to unset, so skip it.
|
|
375
|
-
// slug='""' is the empty string and is written like any other value.
|
|
376
|
-
if (specifiedValue === undefined) {
|
|
377
|
-
continue;
|
|
378
|
-
}
|
|
379
|
-
entity[propertySlug] = castValueToPropertyType(specifiedValue, correspondingProperty?.propertyType || types_1.PropertyType.String);
|
|
380
|
-
}
|
|
381
|
-
// Give Readbable IDs if specified
|
|
382
|
-
if (SHOULD_USE_READABLE_IDS) {
|
|
383
|
-
entity.specifiedId = generateRandomReadableString();
|
|
384
|
-
}
|
|
385
|
-
// roles is a multi-value system property with no type property to drive
|
|
386
|
-
// coercion; a single value still has to reach the API as an array
|
|
387
|
-
if (typeof entity.roles === 'string') {
|
|
388
|
-
if (entity.roles.includes(',')) {
|
|
389
|
-
console.error(`roles value "${entity.roles}" contains a comma and is treated as one literal value. To pass several roles, repeat the key: roles=${entity.roles
|
|
390
|
-
.split(',')
|
|
391
|
-
.join(' roles=')}`);
|
|
392
|
-
}
|
|
393
|
-
entity.roles = [entity.roles];
|
|
394
|
-
}
|
|
395
|
-
// Support specifiedId
|
|
396
|
-
if (propertySpecificationBySlug?.has('specifiedId')) {
|
|
397
|
-
const specifiedId = propertySpecificationBySlug.get('specifiedId');
|
|
398
|
-
entity.specifiedId = specifiedId;
|
|
399
|
-
}
|
|
400
|
-
// There are some required values for certain entity types
|
|
401
|
-
const requiredValuesForType = requiredValues(type.typePath, typeReferencesCache);
|
|
402
|
-
if (requiredValuesForType) {
|
|
403
|
-
requiredValuesForType.forEach(({ slug, value }) => {
|
|
404
|
-
entity[slug] = value;
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
return entity;
|
|
408
|
-
}
|
|
409
|
-
function pickRandomPropertyValues(type, objectReferencesCache, typeReferencesCache, roles) {
|
|
410
|
-
const entity = {};
|
|
411
|
-
// Get values for properties
|
|
412
|
-
for (const property of type.typeProperties) {
|
|
413
|
-
// Validate property level
|
|
414
|
-
if (roles?.length && roles.every((r) => !doesPropertyLevelMatchRole(property.propertyLevel, r)))
|
|
415
|
-
continue;
|
|
416
|
-
// Generate random value
|
|
417
|
-
if (property.propertyType === types_1.PropertyType.ObjectReference) {
|
|
418
|
-
const cache = objectReferencesCache.get(property.referencedTypePath);
|
|
419
|
-
if (!cache || cache.size === 0) {
|
|
420
|
-
throw new Error(`No object references found for referencedTypePath: ${property.referencedTypePath}`);
|
|
421
|
-
}
|
|
422
|
-
entity[getPropertySlug(property)] = selectRandomObject(cache).id;
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
entity[getPropertySlug(property)] = pickRandomValueForProperty(property, objectReferencesCache, typeReferencesCache);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
return entity;
|
|
429
|
-
}
|
|
430
|
-
async function throwIfEntityIsInvalid(typeRoot, entity) {
|
|
431
|
-
const propertySlugs = Object.keys(entity);
|
|
432
|
-
const validationMap = SLUG_VALIDATION_BY_ROOT_MAP[typeRoot] || {};
|
|
433
|
-
const promises = propertySlugs
|
|
434
|
-
.filter((slug) => validationMap[slug])
|
|
435
|
-
.map((slug) => validationMap[slug](slug, entity[slug]));
|
|
436
|
-
await Promise.all(promises);
|
|
437
|
-
}
|
|
438
|
-
function pickRandomValueForProperty(property, objectReferencesCache, typeReferencesCache) {
|
|
439
|
-
const getRandomObjectReference = (referencedTypePath) => {
|
|
440
|
-
if (!objectReferencesCache.has(referencedTypePath)) {
|
|
441
|
-
throw new Error(`No object references found for referencedTypePath: ${referencedTypePath}`);
|
|
442
|
-
}
|
|
443
|
-
const cache = objectReferencesCache.get(referencedTypePath);
|
|
444
|
-
if (cache.size === 0) {
|
|
445
|
-
return null;
|
|
446
|
-
}
|
|
447
|
-
const randomIndex = Math.floor(Math.random() * cache.size);
|
|
448
|
-
const randomObjectReference = Array.from(cache.values())[randomIndex];
|
|
449
|
-
return randomObjectReference;
|
|
450
|
-
};
|
|
451
|
-
switch (property.propertyType) {
|
|
452
|
-
case types_1.PropertyType.String:
|
|
453
|
-
return generateRandomReadableString();
|
|
454
|
-
case types_1.PropertyType.Text:
|
|
455
|
-
return generateRandomReadableString();
|
|
456
|
-
case types_1.PropertyType.Boolean:
|
|
457
|
-
return generateRandomBooleanValue();
|
|
458
|
-
case types_1.PropertyType.Number:
|
|
459
|
-
return generateRandomNumber();
|
|
460
|
-
case types_1.PropertyType.Currency:
|
|
461
|
-
return generateRandomNumber();
|
|
462
|
-
case types_1.PropertyType.SingleSelect:
|
|
463
|
-
if (!property.options)
|
|
464
|
-
throw new Error(`Option set is required for SingleSelect property ${property.slug}, ${JSON.stringify(property, null, 2)}`);
|
|
465
|
-
return pickRandomOptions(property.options, 1)[0].value;
|
|
466
|
-
case types_1.PropertyType.MultiSelect:
|
|
467
|
-
if (!property.options)
|
|
468
|
-
throw new Error('Option set is required for MultiSelect property');
|
|
469
|
-
const randomSelection = pickRandomOptions(property.options, Math.max(2, property.options.length)); // Avoid picking more than the number of options
|
|
470
|
-
return randomSelection.map((option) => option.value);
|
|
471
|
-
case types_1.PropertyType.Sequence:
|
|
472
|
-
return generateRandomNumber();
|
|
473
|
-
case types_1.PropertyType.Date:
|
|
474
|
-
return generateRandomDateString();
|
|
475
|
-
case types_1.PropertyType.Percent:
|
|
476
|
-
return generateRandomNumber(0, 100);
|
|
477
|
-
case types_1.PropertyType.ObjectReference:
|
|
478
|
-
const { referencedTypePath: typeForObjectReference } = property;
|
|
479
|
-
const randomObjectReference = getRandomObjectReference(typeForObjectReference);
|
|
480
|
-
if (!randomObjectReference) {
|
|
481
|
-
throw new Error(`No object references found for referencedTypePath: ${typeForObjectReference}`);
|
|
482
|
-
}
|
|
483
|
-
return randomObjectReference.id; // Assuming the object reference has an 'id' field
|
|
484
|
-
case types_1.PropertyType.TypeReference:
|
|
485
|
-
const { referencedTypePath } = property;
|
|
486
|
-
const type = typeReferencesCache.get(referencedTypePath);
|
|
487
|
-
if (!type) {
|
|
488
|
-
throw new Error(`Referenced type not found for path: ${referencedTypePath}`);
|
|
489
|
-
}
|
|
490
|
-
return type.id;
|
|
491
|
-
case types_1.PropertyType.Formula:
|
|
492
|
-
return undefined; // Placeholder for formula properties because they are evaluated on save anyway
|
|
493
|
-
default:
|
|
494
|
-
throw new Error(`Cannot handle property type: ${property.propertyType}.\n\n See property: ${JSON.stringify(property, null, 2)}`);
|
|
495
|
-
// TODO: Handle UserList
|
|
496
|
-
// TODO: Handle ObjectReferenceList
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
exports.pickRandomValueForProperty = pickRandomValueForProperty;
|
|
500
|
-
function pickRandomOptions(options, count) {
|
|
501
|
-
const shuffled = options.sort(() => 0.5 - Math.random());
|
|
502
|
-
return shuffled.slice(0, count);
|
|
503
|
-
}
|
|
504
|
-
function generateRandomBooleanValue() {
|
|
505
|
-
return Math.random() < 0.5;
|
|
506
|
-
}
|
|
507
|
-
function generateRandomNumber(min, max) {
|
|
508
|
-
if (min === undefined) {
|
|
509
|
-
min = 0;
|
|
510
|
-
}
|
|
511
|
-
if (max === undefined) {
|
|
512
|
-
max = 100;
|
|
513
|
-
}
|
|
514
|
-
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
515
|
-
}
|
|
516
|
-
function generateRandomReadableString() {
|
|
517
|
-
return (0, unique_names_generator_1.uniqueNamesGenerator)({
|
|
518
|
-
dictionaries: [unique_names_generator_1.adjectives, unique_names_generator_1.animals, unique_names_generator_1.colors, unique_names_generator_1.animals, unique_names_generator_1.colors],
|
|
519
|
-
length: 5,
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
function generateRandomDateString() {
|
|
523
|
-
return new Date().toISOString();
|
|
524
|
-
}
|
|
525
|
-
function castValueToPropertyType(value, propertyType) {
|
|
526
|
-
switch (propertyType) {
|
|
527
|
-
case types_1.PropertyType.Boolean:
|
|
528
|
-
return value.toString().toLowerCase() === 'true';
|
|
529
|
-
case types_1.PropertyType.Number:
|
|
530
|
-
const numValue = parseFloat(value.toString());
|
|
531
|
-
if (isNaN(numValue)) {
|
|
532
|
-
throw new Error(`Invalid number value: ${value}`);
|
|
533
|
-
}
|
|
534
|
-
return numValue;
|
|
535
|
-
case types_1.PropertyType.Date:
|
|
536
|
-
return new Date(value.toString()).toISOString();
|
|
537
|
-
case types_1.PropertyType.MultiSelect:
|
|
538
|
-
if (Array.isArray(value)) {
|
|
539
|
-
return value;
|
|
540
|
-
}
|
|
541
|
-
// the old comma syntax parses as one literal value; warn instead of guessing at a split
|
|
542
|
-
if (typeof value === 'string' && value.includes(',')) {
|
|
543
|
-
console.error(`Value "${value}" for a multi-value property contains a comma and is treated as one literal value. To pass several values, repeat the key: slug=${value
|
|
544
|
-
.split(',')
|
|
545
|
-
.join(' slug=')}`);
|
|
546
|
-
}
|
|
547
|
-
return [value];
|
|
548
|
-
default:
|
|
549
|
-
return value; // For string and other types, return as is
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
exports.castValueToPropertyType = castValueToPropertyType;
|
|
553
|
-
async function createEntity(entityName, entity, routing = {}) {
|
|
554
|
-
const client = new sdk_1.Entities();
|
|
555
|
-
const skipHydrateRelationships = SHOULD_SKIP_HYDRATE_RELATIONSHIPS;
|
|
556
|
-
const { suffix, apiVersion } = routing;
|
|
557
|
-
if (entityName === 'assortment-item' && !suffix)
|
|
558
|
-
return (0, entities_1.createAssortmentItem)(entity, { skipHydrateRelationships }); // special case
|
|
559
|
-
const response = await client.create({
|
|
560
|
-
entityName,
|
|
561
|
-
object: entity,
|
|
562
|
-
skipHydrateRelationships,
|
|
563
|
-
suffix,
|
|
564
|
-
apiVersion,
|
|
565
|
-
});
|
|
566
|
-
return response;
|
|
567
|
-
}
|
|
568
|
-
function getReferencedObjectPaths(type) {
|
|
569
|
-
const properties = type.typeProperties || [];
|
|
570
|
-
return properties
|
|
571
|
-
.filter((prop) => prop.propertyType === types_1.PropertyType.ObjectReference)
|
|
572
|
-
.map(({ referencedTypePath }) => referencedTypePath);
|
|
573
|
-
}
|
|
574
|
-
exports.getReferencedObjectPaths = getReferencedObjectPaths;
|
|
575
|
-
async function buildLocalObjectReferencesCache(paths) {
|
|
576
|
-
const cache = new Map();
|
|
577
|
-
const getExampleCustomEntitiesByTypePath = (path) => {
|
|
578
|
-
const client = new sdk_1.Entities();
|
|
579
|
-
const entities = client.get({
|
|
580
|
-
entityName: 'custom-entity',
|
|
581
|
-
criteria: {
|
|
582
|
-
typePath: path,
|
|
583
|
-
},
|
|
584
|
-
});
|
|
585
|
-
return entities;
|
|
586
|
-
};
|
|
587
|
-
const getExampleEntites = async (path) => {
|
|
588
|
-
if (path.startsWith('custom-entity')) {
|
|
589
|
-
return getExampleCustomEntitiesByTypePath(path);
|
|
590
|
-
}
|
|
591
|
-
const { root } = (0, types_2.parseTypePath)(path);
|
|
592
|
-
const client = new sdk_1.Entities();
|
|
593
|
-
const entities = await client.get({
|
|
594
|
-
entityName: root,
|
|
595
|
-
});
|
|
596
|
-
return entities;
|
|
597
|
-
};
|
|
598
|
-
const entitiesByPath = await Promise.all((paths || []).map(async (path) => {
|
|
599
|
-
const entities = await getExampleEntites(path);
|
|
600
|
-
return { path, entities };
|
|
601
|
-
}));
|
|
602
|
-
entitiesByPath.forEach(({ path, entities }) => {
|
|
603
|
-
const cacheForPath = buildObjectReferenceCache(entities);
|
|
604
|
-
cache.set(path, cacheForPath);
|
|
605
|
-
});
|
|
606
|
-
return cache;
|
|
607
|
-
}
|
|
608
|
-
function buildObjectReferenceCache(entities) {
|
|
609
|
-
const cache = new Map();
|
|
610
|
-
entities.forEach((entity) => {
|
|
611
|
-
const { id } = entity;
|
|
612
|
-
if (id && !cache.has(id)) {
|
|
613
|
-
cache.set(id, entity);
|
|
614
|
-
}
|
|
615
|
-
});
|
|
616
|
-
return cache;
|
|
617
|
-
}
|
|
618
|
-
exports.buildObjectReferenceCache = buildObjectReferenceCache;
|
|
619
|
-
function selectRandomObject(cache) {
|
|
620
|
-
return Array.from(cache.values())[Math.floor(Math.random() * cache.size)];
|
|
621
|
-
}
|
|
622
|
-
exports.selectRandomObject = selectRandomObject;
|
|
623
|
-
function requiredValues(path, typeCache) {
|
|
624
|
-
if (path.startsWith('custom-entity')) {
|
|
625
|
-
return [{ slug: 'typeId', value: typeCache.get(path)?.id || '' }];
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
function getPropertySlug(property) {
|
|
629
|
-
return property.propertyType === types_1.PropertyType.ObjectReference
|
|
630
|
-
? `${property.slug}Id`
|
|
631
|
-
: property.slug;
|
|
632
|
-
}
|
|
633
|
-
function doesPropertyLevelMatchRole(propertyLevel, role) {
|
|
634
|
-
const isMatchingLevel = propertyLevel === role;
|
|
635
|
-
return (isMatchingLevel ||
|
|
636
|
-
propertyLevel === types_1.PropertyLevel.ALL ||
|
|
637
|
-
propertyLevel === types_1.PropertyLevel.OVERRIDABLE);
|
|
638
|
-
}
|
|
639
|
-
exports.doesPropertyLevelMatchRole = doesPropertyLevelMatchRole;
|