@contrail/cli 3.4.2 → 3.4.4-alpha.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/README.md +38 -1
- package/bin/dev +0 -0
- package/bin/run +0 -0
- package/dist/__mocks__/@contrail/sdk.d.ts +123 -123
- package/dist/__mocks__/@contrail/sdk.js +716 -716
- package/dist/api/app-extension-deployments.d.ts +2 -2
- package/dist/api/app-extension-deployments.js +11 -11
- package/dist/api/app-extension-packages.d.ts +2 -2
- package/dist/api/app-extension-packages.js +11 -11
- package/dist/api/apps.d.ts +10 -10
- package/dist/api/apps.js +63 -63
- package/dist/api/event-workflow-action-definition.d.ts +3 -3
- package/dist/api/event-workflow-action-definition.js +18 -18
- package/dist/api/event-workflow-template-definition.d.ts +7 -7
- package/dist/api/event-workflow-template-definition.js +26 -26
- package/dist/api/event-workflow-templates.d.ts +8 -8
- package/dist/api/event-workflow-templates.js +32 -32
- package/dist/api/schedules.d.ts +7 -7
- package/dist/api/schedules.js +32 -32
- package/dist/api/types.d.ts +11 -11
- package/dist/api/types.js +79 -79
- package/dist/api/users.d.ts +2 -2
- package/dist/api/users.js +18 -18
- package/dist/api/webhooks.d.ts +4 -4
- package/dist/api/webhooks.js +16 -16
- package/dist/auth/login.d.ts +3 -3
- package/dist/auth/login.js +221 -221
- package/dist/auth/login.spec.d.ts +1 -1
- package/dist/auth/login.spec.js +10 -10
- package/dist/baseFlags.d.ts +10 -10
- package/dist/baseFlags.js +25 -25
- package/dist/basecommand.d.ts +15 -15
- package/dist/basecommand.js +83 -83
- package/dist/commands/app/create.d.ts +10 -10
- package/dist/commands/app/create.js +96 -96
- package/dist/commands/app/delete.d.ts +11 -11
- package/dist/commands/app/delete.js +40 -40
- package/dist/commands/app/extension/deploy.d.ts +18 -18
- package/dist/commands/app/extension/deploy.js +127 -127
- package/dist/commands/app/extension/deployment/list.d.ts +14 -14
- package/dist/commands/app/extension/deployment/list.js +82 -82
- package/dist/commands/app/getApiKey.d.ts +11 -11
- package/dist/commands/app/getApiKey.js +49 -49
- package/dist/commands/app/init.d.ts +10 -10
- package/dist/commands/app/init.js +67 -67
- package/dist/commands/app/install.d.ts +13 -13
- package/dist/commands/app/install.js +135 -135
- package/dist/commands/app/publish.d.ts +14 -14
- package/dist/commands/app/publish.js +69 -69
- package/dist/commands/app/uninstall.d.ts +12 -12
- package/dist/commands/app/uninstall.js +68 -68
- package/dist/commands/app/upgrade.d.ts +14 -14
- package/dist/commands/app/upgrade.js +108 -108
- package/dist/commands/app/version/major.d.ts +11 -11
- package/dist/commands/app/version/major.js +39 -39
- package/dist/commands/app/version/minor.d.ts +11 -11
- package/dist/commands/app/version/minor.js +39 -39
- package/dist/commands/app/version/patch.d.ts +11 -11
- package/dist/commands/app/version/patch.js +39 -39
- package/dist/commands/config/create.d.ts +12 -12
- package/dist/commands/config/create.js +27 -27
- package/dist/commands/config/describe.d.ts +14 -14
- package/dist/commands/config/describe.js +46 -46
- package/dist/commands/config/list.d.ts +11 -11
- package/dist/commands/config/list.js +38 -38
- package/dist/commands/config/set.d.ts +16 -16
- package/dist/commands/config/set.js +46 -46
- package/dist/commands/config/use.d.ts +14 -14
- package/dist/commands/config/use.js +47 -47
- package/dist/commands/file/upload.d.ts +15 -15
- package/dist/commands/file/upload.js +69 -69
- package/dist/commands/groups/export.d.ts +14 -14
- package/dist/commands/groups/export.js +103 -103
- package/dist/commands/groups/import.d.ts +14 -14
- package/dist/commands/groups/import.js +190 -190
- package/dist/commands/instance/export.d.ts +12 -12
- package/dist/commands/instance/export.js +36 -36
- package/dist/commands/instance/import.d.ts +11 -11
- package/dist/commands/instance/import.js +34 -34
- package/dist/commands/load/colors.d.ts +14 -14
- package/dist/commands/load/colors.js +53 -53
- package/dist/commands/load/custom-entity.d.ts +14 -14
- package/dist/commands/load/custom-entity.js +55 -55
- package/dist/commands/load/line-sheet.d.ts +14 -14
- package/dist/commands/load/line-sheet.js +55 -55
- package/dist/commands/load/upload-and-load.d.ts +14 -14
- package/dist/commands/load/upload-and-load.js +46 -46
- package/dist/commands/load-local/index.d.ts +17 -0
- package/dist/commands/load-local/index.js +156 -0
- package/dist/commands/projects/export.d.ts +13 -13
- package/dist/commands/projects/export.js +35 -35
- package/dist/commands/projects/list.d.ts +10 -10
- package/dist/commands/projects/list.js +36 -36
- package/dist/commands/projects/load.d.ts +25 -25
- package/dist/commands/projects/load.js +198 -198
- package/dist/commands/types/exportOptionSetHierarchies.d.ts +11 -11
- package/dist/commands/types/exportOptionSetHierarchies.js +133 -133
- package/dist/commands/types/getAll.d.ts +21 -21
- package/dist/commands/types/getAll.js +180 -180
- package/dist/commands/types/getOptionSets.d.ts +16 -16
- package/dist/commands/types/getOptionSets.js +170 -170
- package/dist/commands/types/loadAll.d.ts +12 -12
- package/dist/commands/types/loadAll.js +55 -55
- package/dist/commands/types/mergeOptionSets.d.ts +13 -13
- package/dist/commands/types/mergeOptionSets.js +116 -116
- package/dist/commands/types/mergeTypeProperties.d.ts +15 -15
- package/dist/commands/types/mergeTypeProperties.js +95 -95
- package/dist/commands/types/parseOptionSetsHierarchyLocalFile.d.ts +14 -14
- package/dist/commands/types/parseOptionSetsHierarchyLocalFile.js +133 -133
- package/dist/commands/types/parseOptionSetsHierarchyValuesToKeysLocalFile.d.ts +14 -14
- package/dist/commands/types/parseOptionSetsHierarchyValuesToKeysLocalFile.js +102 -102
- package/dist/commands/types/parseOptionSetsLocalFiles.d.ts +39 -39
- package/dist/commands/types/parseOptionSetsLocalFiles.js +229 -229
- package/dist/commands/types/parseOptionSetsToFolderStructure.d.ts +13 -13
- package/dist/commands/types/parseOptionSetsToFolderStructure.js +68 -68
- package/dist/commands/types/parseTypesFromFiles.d.ts +14 -14
- package/dist/commands/types/parseTypesFromFiles.js +40 -40
- package/dist/commands/types/parseTypesFromLocalFiles.d.ts +14 -14
- package/dist/commands/types/parseTypesFromLocalFiles.js +37 -37
- package/dist/commands/types/parseTypesFromTemplateAndDataFromLocalFiles.d.ts +15 -15
- package/dist/commands/types/parseTypesFromTemplateAndDataFromLocalFiles.js +39 -39
- package/dist/commands/types/parseTypesFromTemplateFromLocalFiles.d.ts +14 -14
- package/dist/commands/types/parseTypesFromTemplateFromLocalFiles.js +38 -38
- package/dist/commands/types/uploadOptionSetHierarchies.d.ts +12 -12
- package/dist/commands/types/uploadOptionSetHierarchies.js +257 -257
- package/dist/commands/types/uploadOptionSets.d.ts +13 -13
- package/dist/commands/types/uploadOptionSets.js +155 -155
- package/dist/commands/types/uploadOptionSetsHierarchyLocalFile.d.ts +15 -15
- package/dist/commands/types/uploadOptionSetsHierarchyLocalFile.js +135 -135
- package/dist/commands/types/uploadRuleSets.d.ts +12 -12
- package/dist/commands/types/uploadRuleSets.js +197 -197
- package/dist/commands/types/uploadTypePolicies.d.ts +12 -12
- package/dist/commands/types/uploadTypePolicies.js +233 -233
- package/dist/commands/types/uploadTypePropertyPolicies.d.ts +13 -13
- package/dist/commands/types/uploadTypePropertyPolicies.js +286 -286
- package/dist/commands/types/uploadTypes.d.ts +13 -13
- package/dist/commands/types/uploadTypes.js +35 -35
- package/dist/commands/types/uploadUsageInfo.d.ts +15 -12
- package/dist/commands/types/uploadUsageInfo.js +179 -172
- package/dist/commands/types/uploadUsageInfo.spec.d.ts +1 -0
- package/dist/commands/types/uploadUsageInfo.spec.js +249 -0
- package/dist/commands/users/export.d.ts +14 -14
- package/dist/commands/users/export.js +76 -76
- package/dist/commands/users/import.d.ts +14 -14
- package/dist/commands/users/import.js +68 -68
- package/dist/commands/webhooks/create.d.ts +11 -11
- package/dist/commands/webhooks/create.js +25 -25
- package/dist/commands/webhooks/delete.d.ts +14 -14
- package/dist/commands/webhooks/delete.js +53 -53
- package/dist/commands/webhooks/getWebhookTrigger.d.ts +13 -13
- package/dist/commands/webhooks/getWebhookTrigger.js +40 -40
- package/dist/commands/webhooks/list.d.ts +12 -12
- package/dist/commands/webhooks/list.js +31 -31
- package/dist/commands/workflow-template-definitions/install.d.ts +10 -10
- package/dist/commands/workflow-template-definitions/install.js +19 -19
- package/dist/commands/workflow-template-definitions/list.d.ts +10 -10
- package/dist/commands/workflow-template-definitions/list.js +19 -19
- package/dist/commands/workflow-templates/delete.d.ts +10 -10
- package/dist/commands/workflow-templates/delete.js +19 -19
- package/dist/commands/workflow-templates/export.d.ts +14 -14
- package/dist/commands/workflow-templates/export.js +78 -78
- package/dist/commands/workflow-templates/list.d.ts +10 -10
- package/dist/commands/workflow-templates/list.js +19 -19
- package/dist/commands/workflow-templates/load.d.ts +13 -13
- package/dist/commands/workflow-templates/load.js +26 -26
- package/dist/common.d.ts +4 -4
- package/dist/common.js +18 -18
- package/dist/helpers/app/actions-deployer.d.ts +11 -11
- package/dist/helpers/app/actions-deployer.js +123 -123
- package/dist/helpers/app/app-config-validators.d.ts +22 -22
- package/dist/helpers/app/app-config-validators.js +100 -100
- package/dist/helpers/app/app-config-validators.spec.d.ts +1 -1
- package/dist/helpers/app/app-config-validators.spec.js +310 -310
- package/dist/helpers/app/app-deployer.d.ts +12 -12
- package/dist/helpers/app/app-deployer.js +278 -278
- package/dist/helpers/app/app-template-parsers.d.ts +14 -14
- package/dist/helpers/app/app-template-parsers.js +106 -106
- package/dist/helpers/app/extension-package-deployer.d.ts +7 -7
- package/dist/helpers/app/extension-package-deployer.js +175 -175
- package/dist/helpers/app/extensions-deployer.d.ts +9 -9
- package/dist/helpers/app/extensions-deployer.js +95 -95
- package/dist/helpers/app/load-app-config.d.ts +4 -4
- package/dist/helpers/app/load-app-config.js +22 -22
- package/dist/helpers/app/metadata-extractor.d.ts +1 -1
- package/dist/helpers/app/metadata-extractor.js +100 -100
- package/dist/helpers/app/setup-template.d.ts +8 -8
- package/dist/helpers/app/setup-template.js +48 -48
- package/dist/helpers/app/update-app-config.d.ts +2 -2
- package/dist/helpers/app/update-app-config.js +20 -20
- package/dist/helpers/app/upgrade-version.d.ts +16 -16
- package/dist/helpers/app/upgrade-version.js +37 -37
- package/dist/helpers/app/upgrade-version.spec.d.ts +1 -1
- package/dist/helpers/app/upgrade-version.spec.js +33 -33
- package/dist/helpers/colors/helpers.d.ts +12 -12
- package/dist/helpers/colors/helpers.js +84 -84
- package/dist/helpers/config/config.d.ts +16 -16
- package/dist/helpers/config/config.js +223 -223
- package/dist/helpers/file-helpers.d.ts +2 -2
- package/dist/helpers/file-helpers.js +36 -36
- package/dist/helpers/load/helpers.d.ts +20 -20
- package/dist/helpers/load/helpers.js +141 -141
- package/dist/helpers/load/helpers.spec.d.ts +1 -1
- package/dist/helpers/load/helpers.spec.js +62 -62
- package/dist/helpers/load/validators.d.ts +14 -14
- package/dist/helpers/load/validators.js +182 -182
- package/dist/helpers/load/validators.spec.d.ts +1 -1
- package/dist/helpers/load/validators.spec.js +450 -450
- package/dist/helpers/pipeline/formatters.d.ts +6 -0
- package/dist/helpers/pipeline/formatters.js +219 -0
- package/dist/helpers/projects/helpers.d.ts +3 -3
- package/dist/helpers/projects/helpers.js +167 -167
- package/dist/helpers/types/csv-parser.d.ts +1 -1
- package/dist/helpers/types/csv-parser.js +59 -59
- package/dist/helpers/types/option-set-helper.d.ts +9 -9
- package/dist/helpers/types/option-set-helper.js +48 -48
- package/dist/helpers/types/option-set-helper.spec.d.ts +1 -1
- package/dist/helpers/types/option-set-helper.spec.js +64 -64
- package/dist/helpers/types/parseOptionSetsLocalFiles.spec.d.ts +1 -1
- package/dist/helpers/types/parseOptionSetsLocalFiles.spec.js +285 -285
- package/dist/helpers/types/type-extractor.d.ts +1 -1
- package/dist/helpers/types/type-extractor.js +36 -36
- package/dist/helpers/types/type-parser.d.ts +4 -4
- package/dist/helpers/types/type-parser.js +119 -119
- package/dist/helpers/types/type-uploader.d.ts +20 -20
- package/dist/helpers/types/type-uploader.js +336 -336
- package/dist/helpers/types/type-uploader.spec.d.ts +1 -1
- package/dist/helpers/types/type-uploader.spec.js +266 -266
- package/dist/helpers/types/uploadOptionSets.spec.d.ts +1 -1
- package/dist/helpers/types/uploadOptionSets.spec.js +19 -19
- package/dist/helpers/workflow-template-definitions/workflow-template-definition-deployer.d.ts +12 -12
- package/dist/helpers/workflow-template-definitions/workflow-template-definition-deployer.js +158 -158
- package/dist/helpers/workflow-templates/helper.d.ts +20 -20
- package/dist/helpers/workflow-templates/helper.js +285 -285
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -5
- package/dist/interfaces.d.ts +470 -445
- package/dist/interfaces.js +163 -163
- package/dist/logger.d.ts +3 -3
- package/dist/logger.js +33 -33
- package/oclif.manifest.json +85 -1
- package/package.json +11 -6
|
@@ -1,336 +1,336 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractValueDifferences = exports.uploadTypes = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const sdk_1 = require("@contrail/sdk");
|
|
6
|
-
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
7
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
8
|
-
const path = tslib_1.__importStar(require("path"));
|
|
9
|
-
const _ = tslib_1.__importStar(require("lodash"));
|
|
10
|
-
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
|
|
11
|
-
const login_1 = require("../../auth/login");
|
|
12
|
-
const logger_1 = tslib_1.__importDefault(require("../../logger"));
|
|
13
|
-
async function uploadTypes(dir, options = { isMerge: false }) {
|
|
14
|
-
try {
|
|
15
|
-
const userConfig = await (0, login_1.authUser)();
|
|
16
|
-
logger_1.default.info(`START Getting all types for org ${userConfig.orgSlug}`);
|
|
17
|
-
logger_1.default.info('TYPES Uploading ');
|
|
18
|
-
await uploadReducedTypes(dir, options);
|
|
19
|
-
logger_1.default.info(`${chalk_1.default.yellow.bold('TYPES')} Uploaded`);
|
|
20
|
-
}
|
|
21
|
-
catch (e) {
|
|
22
|
-
logger_1.default.info(`${chalk_1.default.red.bold('FAIL')} Types Upload Failed . ${e}`);
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
logger_1.default.info(`${chalk_1.default.green.bold('COMPLETE')} Types Upload Complete.`);
|
|
26
|
-
}
|
|
27
|
-
exports.uploadTypes = uploadTypes;
|
|
28
|
-
function loadReducedTypes(fromDir) {
|
|
29
|
-
try {
|
|
30
|
-
const dir = path.resolve(path.join(fromDir, 'types'));
|
|
31
|
-
const files = fs_1.default.readdirSync(dir);
|
|
32
|
-
const types = [];
|
|
33
|
-
for (const file of files) {
|
|
34
|
-
types.push(...js_yaml_1.default.load(fs_1.default.readFileSync(path.join(dir, file), 'utf8')));
|
|
35
|
-
}
|
|
36
|
-
return types;
|
|
37
|
-
}
|
|
38
|
-
catch (e) {
|
|
39
|
-
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
async function uploadReducedTypes(dir, options) {
|
|
43
|
-
const types = loadReducedTypes(dir);
|
|
44
|
-
const currentTypesWithoutProperties = await new sdk_1.Entities().get({
|
|
45
|
-
entityName: 'type',
|
|
46
|
-
});
|
|
47
|
-
const currentTypes = [];
|
|
48
|
-
for (const currentTypeWithoutProperties of currentTypesWithoutProperties) {
|
|
49
|
-
const typeWithProperties = await new sdk_1.Entities().get({
|
|
50
|
-
entityName: 'type',
|
|
51
|
-
id: currentTypeWithoutProperties.id,
|
|
52
|
-
relations: ['typeProperties'],
|
|
53
|
-
});
|
|
54
|
-
currentTypes.push(typeWithProperties);
|
|
55
|
-
}
|
|
56
|
-
const currentTypesByTypePath = {};
|
|
57
|
-
for (const currentType of currentTypes) {
|
|
58
|
-
currentTypesByTypePath[currentType.typePath] = currentType;
|
|
59
|
-
}
|
|
60
|
-
const newTypesByTypePath = {};
|
|
61
|
-
for (const type of types) {
|
|
62
|
-
newTypesByTypePath[type.typePath] = type;
|
|
63
|
-
}
|
|
64
|
-
const optionSets = await new sdk_1.Entities().get({
|
|
65
|
-
entityName: 'type-property-option-set',
|
|
66
|
-
});
|
|
67
|
-
if (logger_1.default.isDebugEnabled()) {
|
|
68
|
-
fs_1.default.writeFileSync('reducedTypes.json', JSON.stringify(types, null, 2));
|
|
69
|
-
fs_1.default.writeFileSync('currentTypes.json', JSON.stringify(currentTypes, null, 2));
|
|
70
|
-
}
|
|
71
|
-
for (const type of types) {
|
|
72
|
-
try {
|
|
73
|
-
const found = currentTypesByTypePath[type.typePath];
|
|
74
|
-
if (!found) {
|
|
75
|
-
if (!type.typePath.includes(':')) {
|
|
76
|
-
logger_1.default.info(`Cannot create a base type. Skipping: ${type.slug}`);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
logger_1.default.info(`Creating Type: ${type.typePath}`);
|
|
80
|
-
const createdType = await new sdk_1.Entities().create({
|
|
81
|
-
entityName: 'type',
|
|
82
|
-
object: {
|
|
83
|
-
slug: type.slug,
|
|
84
|
-
label: type.label,
|
|
85
|
-
typePath: type.typePath,
|
|
86
|
-
isArchived: type.isArchived,
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
const newTypeProperties = (await new sdk_1.Types().getType({ id: createdType.id }))
|
|
90
|
-
.typeProperties;
|
|
91
|
-
await uploadReducedTypeProperties(createdType, newTypeProperties, type.typeProperties, optionSets, options);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
const proposedType = Object.fromEntries(Object.entries({
|
|
95
|
-
slug: type.slug,
|
|
96
|
-
label: type.label,
|
|
97
|
-
typePath: type.typePath,
|
|
98
|
-
isArchived: type.isArchived,
|
|
99
|
-
}).filter(([_, value]) => value !== undefined));
|
|
100
|
-
const differences = extractValueDifferences(found, proposedType, [
|
|
101
|
-
'id',
|
|
102
|
-
'createdOn',
|
|
103
|
-
'updatedOn',
|
|
104
|
-
'createdById',
|
|
105
|
-
'updatedById',
|
|
106
|
-
'orgId',
|
|
107
|
-
'typeRootId',
|
|
108
|
-
'parentId',
|
|
109
|
-
'typePolicyIds',
|
|
110
|
-
'typePolicies',
|
|
111
|
-
'typeProperties',
|
|
112
|
-
'typePropertyPolicies',
|
|
113
|
-
'typeRuleSets',
|
|
114
|
-
'typeOptionSetHierarchyLinks',
|
|
115
|
-
'relations',
|
|
116
|
-
'typeInterfaces',
|
|
117
|
-
]);
|
|
118
|
-
let updatedType = found;
|
|
119
|
-
const hasChangesToSave = options.isMerge
|
|
120
|
-
? Boolean(differences.updates.length || differences.adds.length)
|
|
121
|
-
: Boolean(differences.updates.length || differences.adds.length || differences.deletions.length);
|
|
122
|
-
if (hasChangesToSave) {
|
|
123
|
-
logger_1.default.debug(`found differences: ${JSON.stringify(differences, null, 2)}`);
|
|
124
|
-
logger_1.default.info(`Updating Type: ${type.slug}`);
|
|
125
|
-
updatedType = await new sdk_1.Entities().update({
|
|
126
|
-
entityName: 'type',
|
|
127
|
-
id: found.id,
|
|
128
|
-
object: proposedType,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
logger_1.default.debug(`Skipping empty update of type ${type.typePath}`);
|
|
133
|
-
}
|
|
134
|
-
await uploadReducedTypeProperties(updatedType, found.typeProperties, type.typeProperties, optionSets, options);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
catch (err) {
|
|
138
|
-
logger_1.default.info(`Error Uploading Types: ${err}`);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
async function uploadReducedTypeProperties(type, currentPropertyTypes, newPropertyTypes, optionSets, options) {
|
|
143
|
-
const currentPropertyTypeBySlug = {};
|
|
144
|
-
for (const currentPropertyType of currentPropertyTypes) {
|
|
145
|
-
currentPropertyTypeBySlug[currentPropertyType.slug] = currentPropertyType;
|
|
146
|
-
}
|
|
147
|
-
const newPropertyTypesBySlug = {};
|
|
148
|
-
for (const newPropertyType of newPropertyTypes) {
|
|
149
|
-
newPropertyTypesBySlug[newPropertyType.slug] = newPropertyType;
|
|
150
|
-
}
|
|
151
|
-
const optionSetIdBySlug = {};
|
|
152
|
-
for (const optionSet of optionSets) {
|
|
153
|
-
optionSetIdBySlug[optionSet.slug] = optionSet.id;
|
|
154
|
-
}
|
|
155
|
-
const sets = await new sdk_1.Entities().get({ entityName: 'user-list' });
|
|
156
|
-
for (const newPropertyType of newPropertyTypes) {
|
|
157
|
-
let typePropertyUserListId = null;
|
|
158
|
-
try {
|
|
159
|
-
if (newPropertyType.typePropertyUserListName) {
|
|
160
|
-
const userList = sets.find((s) => s.name === newPropertyType.typePropertyUserListName);
|
|
161
|
-
if (userList) {
|
|
162
|
-
typePropertyUserListId = userList.id;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
const found = currentPropertyTypeBySlug[newPropertyType.slug];
|
|
166
|
-
if (!found) {
|
|
167
|
-
logger_1.default.info(`Creating Type-Property: ${newPropertyType.slug}`);
|
|
168
|
-
await sdk_1.Request.request(`/types/${type.id}/type-properties`, {
|
|
169
|
-
method: 'POST',
|
|
170
|
-
body: JSON.stringify({
|
|
171
|
-
isArchived: newPropertyType.isArchived,
|
|
172
|
-
propertyType: newPropertyType.propertyType,
|
|
173
|
-
slug: newPropertyType.slug,
|
|
174
|
-
label: newPropertyType.label,
|
|
175
|
-
editable: newPropertyType.editable,
|
|
176
|
-
searchable: newPropertyType.searchable,
|
|
177
|
-
core: newPropertyType.core,
|
|
178
|
-
typePropertyOptionSetId: optionSetIdBySlug[newPropertyType.typePropertyOptionSetSlug],
|
|
179
|
-
formula: newPropertyType.formula,
|
|
180
|
-
referencedTypePath: newPropertyType.referencedTypePath,
|
|
181
|
-
carryOverBehavior: newPropertyType.carryOverBehavior,
|
|
182
|
-
carryOverDefault: newPropertyType.carryOverDefault,
|
|
183
|
-
referencedTypeRootSlug: newPropertyType.referencedTypeRootSlug,
|
|
184
|
-
defaultValue: newPropertyType.defaultValue,
|
|
185
|
-
typePropertyUserListId,
|
|
186
|
-
referencedTypeRole: newPropertyType.referencedTypeRole,
|
|
187
|
-
isUniqueProperty: newPropertyType.isUniqueProperty,
|
|
188
|
-
isUniquePropertyInFamily: newPropertyType.isUniquePropertyInFamily,
|
|
189
|
-
propertyLevel: newPropertyType.propertyLevel,
|
|
190
|
-
formulaFunction: newPropertyType.formulaFunction,
|
|
191
|
-
validationFunction: newPropertyType.validationFunction,
|
|
192
|
-
numberFormat: newPropertyType.numberFormat,
|
|
193
|
-
sequenceSeedValue: newPropertyType.sequenceSeedValue,
|
|
194
|
-
copyBehavior: newPropertyType?.copyBehavior,
|
|
195
|
-
}),
|
|
196
|
-
headers: {
|
|
197
|
-
Accept: 'application/json',
|
|
198
|
-
'Content-Type': 'application/json',
|
|
199
|
-
},
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
else if (found
|
|
203
|
-
// !newPropertyType.inherited &&
|
|
204
|
-
// !typePropertiesEqual(found, newPropertyType, optionSetIdBySlug)
|
|
205
|
-
) {
|
|
206
|
-
// Skip platform-managed entities
|
|
207
|
-
if (found.core || newPropertyType.core) {
|
|
208
|
-
logger_1.default.debug(`Skipping platform-managed property: ${newPropertyType.slug}`);
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
const defaultValueForUndefinedEntries = options.isMerge ? undefined : null;
|
|
212
|
-
const defaultIfUndefined = (value) => value === undefined ? defaultValueForUndefinedEntries : value;
|
|
213
|
-
const proposedTypeProperty = Object.fromEntries(Object.entries({
|
|
214
|
-
isArchived: defaultIfUndefined(newPropertyType.isArchived),
|
|
215
|
-
propertyType: defaultIfUndefined(newPropertyType.propertyType),
|
|
216
|
-
slug: defaultIfUndefined(newPropertyType.slug),
|
|
217
|
-
label: defaultIfUndefined(newPropertyType.label),
|
|
218
|
-
editable: defaultIfUndefined(newPropertyType.editable),
|
|
219
|
-
searchable: defaultIfUndefined(newPropertyType.searchable),
|
|
220
|
-
core: defaultIfUndefined(newPropertyType.core),
|
|
221
|
-
typePropertyOptionSetId: defaultIfUndefined(optionSetIdBySlug[newPropertyType.typePropertyOptionSetSlug]),
|
|
222
|
-
formula: defaultIfUndefined(newPropertyType.formula),
|
|
223
|
-
referencedTypePath: defaultIfUndefined(newPropertyType.referencedTypePath),
|
|
224
|
-
carryOverBehavior: defaultIfUndefined(newPropertyType.carryOverBehavior),
|
|
225
|
-
carryOverDefault: defaultIfUndefined(newPropertyType.carryOverDefault),
|
|
226
|
-
referencedTypeRootSlug: defaultIfUndefined(newPropertyType.referencedTypeRootSlug),
|
|
227
|
-
defaultValue: defaultIfUndefined(newPropertyType.defaultValue),
|
|
228
|
-
propertyLevel: defaultIfUndefined(newPropertyType.propertyLevel),
|
|
229
|
-
typePropertyUserListId,
|
|
230
|
-
referencedTypeRole: defaultIfUndefined(newPropertyType.referencedTypeRole),
|
|
231
|
-
isUniqueProperty: defaultIfUndefined(newPropertyType.isUniqueProperty),
|
|
232
|
-
isUniquePropertyInFamily: defaultIfUndefined(newPropertyType.isUniquePropertyInFamily),
|
|
233
|
-
formulaFunction: defaultIfUndefined(newPropertyType.formulaFunction),
|
|
234
|
-
validationFunction: defaultIfUndefined(newPropertyType.validationFunction),
|
|
235
|
-
numberFormat: defaultIfUndefined(newPropertyType.numberFormat),
|
|
236
|
-
sequenceSeedValue: defaultIfUndefined(newPropertyType.sequenceSeedValue),
|
|
237
|
-
copyBehavior: defaultIfUndefined(newPropertyType.copyBehavior),
|
|
238
|
-
}).filter(([_, value]) => value !== undefined));
|
|
239
|
-
// logger.debug(
|
|
240
|
-
// `found type property option set: ${newPropertyType.typePropertyOptionSetSlug}, ${proposedTP.typePropertyOptionSetId}, `,
|
|
241
|
-
// );
|
|
242
|
-
const differences = extractValueDifferences(found, proposedTypeProperty, [
|
|
243
|
-
'id',
|
|
244
|
-
'createdOn',
|
|
245
|
-
'updatedOn',
|
|
246
|
-
'createdById',
|
|
247
|
-
'updatedById',
|
|
248
|
-
'orgId',
|
|
249
|
-
'typePropertyUserListId',
|
|
250
|
-
'referencedTypeRole',
|
|
251
|
-
'referencedTypeFilterProperties',
|
|
252
|
-
'referencedTypeId',
|
|
253
|
-
'description',
|
|
254
|
-
'apiDetails',
|
|
255
|
-
'notes',
|
|
256
|
-
'isFederationProperty',
|
|
257
|
-
'required',
|
|
258
|
-
'optionsSet',
|
|
259
|
-
'options',
|
|
260
|
-
'inherited',
|
|
261
|
-
'used',
|
|
262
|
-
'typePropertyPolicyIds',
|
|
263
|
-
]);
|
|
264
|
-
const hasChangesToSave = options.isMerge
|
|
265
|
-
? Boolean(differences.updates.length || differences.adds.length)
|
|
266
|
-
: Boolean(differences.updates.length || differences.adds.length || differences.deletions.length);
|
|
267
|
-
if (hasChangesToSave) {
|
|
268
|
-
logger_1.default.debug(`found differences: ${JSON.stringify(differences, null, 2)}`);
|
|
269
|
-
logger_1.default.debug(`Updating Type-Property: ${newPropertyType.slug}, ${found.id}, ${JSON.stringify(proposedTypeProperty, null, 2)}`);
|
|
270
|
-
await new sdk_1.Entities().update({
|
|
271
|
-
entityName: 'type-property',
|
|
272
|
-
id: found.id,
|
|
273
|
-
object: proposedTypeProperty,
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
logger_1.default.debug(`Skipping empty update of type property ${newPropertyType.slug}`);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
catch (err) {
|
|
282
|
-
logger_1.default.info(`Error Creating Properties: ${err}`);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
if (options.isMerge) {
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
for (const currentPropertyType of currentPropertyTypes) {
|
|
289
|
-
try {
|
|
290
|
-
const found = newPropertyTypesBySlug[currentPropertyType.slug];
|
|
291
|
-
if (!found && !currentPropertyType.inherited) {
|
|
292
|
-
logger_1.default.info(`Deleting Type-Property: ${currentPropertyType.slug}`);
|
|
293
|
-
await sdk_1.Request.request(`/types/${type.id}/type-properties/${currentPropertyType.id}`, {
|
|
294
|
-
method: 'DELETE',
|
|
295
|
-
headers: {
|
|
296
|
-
Accept: 'application/json',
|
|
297
|
-
'Content-Type': 'application/json',
|
|
298
|
-
},
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
catch (err) {
|
|
303
|
-
throw new Error(`Error Deleting Properties: ${err}`);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* compares the keys between different objects and returns the differences
|
|
309
|
-
* captures in adds, updates, and deletions. Object 1 is considered the
|
|
310
|
-
* original, and object 2 is considered the "edit".
|
|
311
|
-
*
|
|
312
|
-
* An add is a property that exists in object2 but not in 1. An edit
|
|
313
|
-
* is a property that exists in both object 1 and 2, but with different values.
|
|
314
|
-
* A deletion is a property in object 1 but not in object 2.
|
|
315
|
-
* @param object1 any object
|
|
316
|
-
* @param object2 any other object
|
|
317
|
-
*/
|
|
318
|
-
function extractValueDifferences(object1, object2, keysToIgnore) {
|
|
319
|
-
const updates = [];
|
|
320
|
-
const object1Keys = Object.keys(object1);
|
|
321
|
-
const object2keys = Object.keys(object2);
|
|
322
|
-
const adds = object2keys.filter((key) => !object1Keys.includes(key) && !keysToIgnore.includes(key));
|
|
323
|
-
const deletions = object1Keys.filter((key) => !object2keys.includes(key) && !keysToIgnore.includes(key));
|
|
324
|
-
const shared = object1Keys.filter((key) => object2keys.includes(key) && !keysToIgnore.includes(key));
|
|
325
|
-
for (const key of shared) {
|
|
326
|
-
if (!_.isEqual(object1[key], object2[key])) {
|
|
327
|
-
updates.push(key);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
return {
|
|
331
|
-
adds,
|
|
332
|
-
updates,
|
|
333
|
-
deletions,
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
exports.extractValueDifferences = extractValueDifferences;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractValueDifferences = exports.uploadTypes = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const sdk_1 = require("@contrail/sdk");
|
|
6
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
7
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
8
|
+
const path = tslib_1.__importStar(require("path"));
|
|
9
|
+
const _ = tslib_1.__importStar(require("lodash"));
|
|
10
|
+
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
|
|
11
|
+
const login_1 = require("../../auth/login");
|
|
12
|
+
const logger_1 = tslib_1.__importDefault(require("../../logger"));
|
|
13
|
+
async function uploadTypes(dir, options = { isMerge: false }) {
|
|
14
|
+
try {
|
|
15
|
+
const userConfig = await (0, login_1.authUser)();
|
|
16
|
+
logger_1.default.info(`START Getting all types for org ${userConfig.orgSlug}`);
|
|
17
|
+
logger_1.default.info('TYPES Uploading ');
|
|
18
|
+
await uploadReducedTypes(dir, options);
|
|
19
|
+
logger_1.default.info(`${chalk_1.default.yellow.bold('TYPES')} Uploaded`);
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
logger_1.default.info(`${chalk_1.default.red.bold('FAIL')} Types Upload Failed . ${e}`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
logger_1.default.info(`${chalk_1.default.green.bold('COMPLETE')} Types Upload Complete.`);
|
|
26
|
+
}
|
|
27
|
+
exports.uploadTypes = uploadTypes;
|
|
28
|
+
function loadReducedTypes(fromDir) {
|
|
29
|
+
try {
|
|
30
|
+
const dir = path.resolve(path.join(fromDir, 'types'));
|
|
31
|
+
const files = fs_1.default.readdirSync(dir);
|
|
32
|
+
const types = [];
|
|
33
|
+
for (const file of files) {
|
|
34
|
+
types.push(...js_yaml_1.default.load(fs_1.default.readFileSync(path.join(dir, file), 'utf8')));
|
|
35
|
+
}
|
|
36
|
+
return types;
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function uploadReducedTypes(dir, options) {
|
|
43
|
+
const types = loadReducedTypes(dir);
|
|
44
|
+
const currentTypesWithoutProperties = await new sdk_1.Entities().get({
|
|
45
|
+
entityName: 'type',
|
|
46
|
+
});
|
|
47
|
+
const currentTypes = [];
|
|
48
|
+
for (const currentTypeWithoutProperties of currentTypesWithoutProperties) {
|
|
49
|
+
const typeWithProperties = await new sdk_1.Entities().get({
|
|
50
|
+
entityName: 'type',
|
|
51
|
+
id: currentTypeWithoutProperties.id,
|
|
52
|
+
relations: ['typeProperties'],
|
|
53
|
+
});
|
|
54
|
+
currentTypes.push(typeWithProperties);
|
|
55
|
+
}
|
|
56
|
+
const currentTypesByTypePath = {};
|
|
57
|
+
for (const currentType of currentTypes) {
|
|
58
|
+
currentTypesByTypePath[currentType.typePath] = currentType;
|
|
59
|
+
}
|
|
60
|
+
const newTypesByTypePath = {};
|
|
61
|
+
for (const type of types) {
|
|
62
|
+
newTypesByTypePath[type.typePath] = type;
|
|
63
|
+
}
|
|
64
|
+
const optionSets = await new sdk_1.Entities().get({
|
|
65
|
+
entityName: 'type-property-option-set',
|
|
66
|
+
});
|
|
67
|
+
if (logger_1.default.isDebugEnabled()) {
|
|
68
|
+
fs_1.default.writeFileSync('reducedTypes.json', JSON.stringify(types, null, 2));
|
|
69
|
+
fs_1.default.writeFileSync('currentTypes.json', JSON.stringify(currentTypes, null, 2));
|
|
70
|
+
}
|
|
71
|
+
for (const type of types) {
|
|
72
|
+
try {
|
|
73
|
+
const found = currentTypesByTypePath[type.typePath];
|
|
74
|
+
if (!found) {
|
|
75
|
+
if (!type.typePath.includes(':')) {
|
|
76
|
+
logger_1.default.info(`Cannot create a base type. Skipping: ${type.slug}`);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
logger_1.default.info(`Creating Type: ${type.typePath}`);
|
|
80
|
+
const createdType = await new sdk_1.Entities().create({
|
|
81
|
+
entityName: 'type',
|
|
82
|
+
object: {
|
|
83
|
+
slug: type.slug,
|
|
84
|
+
label: type.label,
|
|
85
|
+
typePath: type.typePath,
|
|
86
|
+
isArchived: type.isArchived,
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
const newTypeProperties = (await new sdk_1.Types().getType({ id: createdType.id }))
|
|
90
|
+
.typeProperties;
|
|
91
|
+
await uploadReducedTypeProperties(createdType, newTypeProperties, type.typeProperties, optionSets, options);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const proposedType = Object.fromEntries(Object.entries({
|
|
95
|
+
slug: type.slug,
|
|
96
|
+
label: type.label,
|
|
97
|
+
typePath: type.typePath,
|
|
98
|
+
isArchived: type.isArchived,
|
|
99
|
+
}).filter(([_, value]) => value !== undefined));
|
|
100
|
+
const differences = extractValueDifferences(found, proposedType, [
|
|
101
|
+
'id',
|
|
102
|
+
'createdOn',
|
|
103
|
+
'updatedOn',
|
|
104
|
+
'createdById',
|
|
105
|
+
'updatedById',
|
|
106
|
+
'orgId',
|
|
107
|
+
'typeRootId',
|
|
108
|
+
'parentId',
|
|
109
|
+
'typePolicyIds',
|
|
110
|
+
'typePolicies',
|
|
111
|
+
'typeProperties',
|
|
112
|
+
'typePropertyPolicies',
|
|
113
|
+
'typeRuleSets',
|
|
114
|
+
'typeOptionSetHierarchyLinks',
|
|
115
|
+
'relations',
|
|
116
|
+
'typeInterfaces',
|
|
117
|
+
]);
|
|
118
|
+
let updatedType = found;
|
|
119
|
+
const hasChangesToSave = options.isMerge
|
|
120
|
+
? Boolean(differences.updates.length || differences.adds.length)
|
|
121
|
+
: Boolean(differences.updates.length || differences.adds.length || differences.deletions.length);
|
|
122
|
+
if (hasChangesToSave) {
|
|
123
|
+
logger_1.default.debug(`found differences: ${JSON.stringify(differences, null, 2)}`);
|
|
124
|
+
logger_1.default.info(`Updating Type: ${type.slug}`);
|
|
125
|
+
updatedType = await new sdk_1.Entities().update({
|
|
126
|
+
entityName: 'type',
|
|
127
|
+
id: found.id,
|
|
128
|
+
object: proposedType,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
logger_1.default.debug(`Skipping empty update of type ${type.typePath}`);
|
|
133
|
+
}
|
|
134
|
+
await uploadReducedTypeProperties(updatedType, found.typeProperties, type.typeProperties, optionSets, options);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
logger_1.default.info(`Error Uploading Types: ${err}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async function uploadReducedTypeProperties(type, currentPropertyTypes, newPropertyTypes, optionSets, options) {
|
|
143
|
+
const currentPropertyTypeBySlug = {};
|
|
144
|
+
for (const currentPropertyType of currentPropertyTypes) {
|
|
145
|
+
currentPropertyTypeBySlug[currentPropertyType.slug] = currentPropertyType;
|
|
146
|
+
}
|
|
147
|
+
const newPropertyTypesBySlug = {};
|
|
148
|
+
for (const newPropertyType of newPropertyTypes) {
|
|
149
|
+
newPropertyTypesBySlug[newPropertyType.slug] = newPropertyType;
|
|
150
|
+
}
|
|
151
|
+
const optionSetIdBySlug = {};
|
|
152
|
+
for (const optionSet of optionSets) {
|
|
153
|
+
optionSetIdBySlug[optionSet.slug] = optionSet.id;
|
|
154
|
+
}
|
|
155
|
+
const sets = await new sdk_1.Entities().get({ entityName: 'user-list' });
|
|
156
|
+
for (const newPropertyType of newPropertyTypes) {
|
|
157
|
+
let typePropertyUserListId = null;
|
|
158
|
+
try {
|
|
159
|
+
if (newPropertyType.typePropertyUserListName) {
|
|
160
|
+
const userList = sets.find((s) => s.name === newPropertyType.typePropertyUserListName);
|
|
161
|
+
if (userList) {
|
|
162
|
+
typePropertyUserListId = userList.id;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const found = currentPropertyTypeBySlug[newPropertyType.slug];
|
|
166
|
+
if (!found) {
|
|
167
|
+
logger_1.default.info(`Creating Type-Property: ${newPropertyType.slug}`);
|
|
168
|
+
await sdk_1.Request.request(`/types/${type.id}/type-properties`, {
|
|
169
|
+
method: 'POST',
|
|
170
|
+
body: JSON.stringify({
|
|
171
|
+
isArchived: newPropertyType.isArchived,
|
|
172
|
+
propertyType: newPropertyType.propertyType,
|
|
173
|
+
slug: newPropertyType.slug,
|
|
174
|
+
label: newPropertyType.label,
|
|
175
|
+
editable: newPropertyType.editable,
|
|
176
|
+
searchable: newPropertyType.searchable,
|
|
177
|
+
core: newPropertyType.core,
|
|
178
|
+
typePropertyOptionSetId: optionSetIdBySlug[newPropertyType.typePropertyOptionSetSlug],
|
|
179
|
+
formula: newPropertyType.formula,
|
|
180
|
+
referencedTypePath: newPropertyType.referencedTypePath,
|
|
181
|
+
carryOverBehavior: newPropertyType.carryOverBehavior,
|
|
182
|
+
carryOverDefault: newPropertyType.carryOverDefault,
|
|
183
|
+
referencedTypeRootSlug: newPropertyType.referencedTypeRootSlug,
|
|
184
|
+
defaultValue: newPropertyType.defaultValue,
|
|
185
|
+
typePropertyUserListId,
|
|
186
|
+
referencedTypeRole: newPropertyType.referencedTypeRole,
|
|
187
|
+
isUniqueProperty: newPropertyType.isUniqueProperty,
|
|
188
|
+
isUniquePropertyInFamily: newPropertyType.isUniquePropertyInFamily,
|
|
189
|
+
propertyLevel: newPropertyType.propertyLevel,
|
|
190
|
+
formulaFunction: newPropertyType.formulaFunction,
|
|
191
|
+
validationFunction: newPropertyType.validationFunction,
|
|
192
|
+
numberFormat: newPropertyType.numberFormat,
|
|
193
|
+
sequenceSeedValue: newPropertyType.sequenceSeedValue,
|
|
194
|
+
copyBehavior: newPropertyType?.copyBehavior,
|
|
195
|
+
}),
|
|
196
|
+
headers: {
|
|
197
|
+
Accept: 'application/json',
|
|
198
|
+
'Content-Type': 'application/json',
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
else if (found
|
|
203
|
+
// !newPropertyType.inherited &&
|
|
204
|
+
// !typePropertiesEqual(found, newPropertyType, optionSetIdBySlug)
|
|
205
|
+
) {
|
|
206
|
+
// Skip platform-managed entities
|
|
207
|
+
if (found.core || newPropertyType.core) {
|
|
208
|
+
logger_1.default.debug(`Skipping platform-managed property: ${newPropertyType.slug}`);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const defaultValueForUndefinedEntries = options.isMerge ? undefined : null;
|
|
212
|
+
const defaultIfUndefined = (value) => value === undefined ? defaultValueForUndefinedEntries : value;
|
|
213
|
+
const proposedTypeProperty = Object.fromEntries(Object.entries({
|
|
214
|
+
isArchived: defaultIfUndefined(newPropertyType.isArchived),
|
|
215
|
+
propertyType: defaultIfUndefined(newPropertyType.propertyType),
|
|
216
|
+
slug: defaultIfUndefined(newPropertyType.slug),
|
|
217
|
+
label: defaultIfUndefined(newPropertyType.label),
|
|
218
|
+
editable: defaultIfUndefined(newPropertyType.editable),
|
|
219
|
+
searchable: defaultIfUndefined(newPropertyType.searchable),
|
|
220
|
+
core: defaultIfUndefined(newPropertyType.core),
|
|
221
|
+
typePropertyOptionSetId: defaultIfUndefined(optionSetIdBySlug[newPropertyType.typePropertyOptionSetSlug]),
|
|
222
|
+
formula: defaultIfUndefined(newPropertyType.formula),
|
|
223
|
+
referencedTypePath: defaultIfUndefined(newPropertyType.referencedTypePath),
|
|
224
|
+
carryOverBehavior: defaultIfUndefined(newPropertyType.carryOverBehavior),
|
|
225
|
+
carryOverDefault: defaultIfUndefined(newPropertyType.carryOverDefault),
|
|
226
|
+
referencedTypeRootSlug: defaultIfUndefined(newPropertyType.referencedTypeRootSlug),
|
|
227
|
+
defaultValue: defaultIfUndefined(newPropertyType.defaultValue),
|
|
228
|
+
propertyLevel: defaultIfUndefined(newPropertyType.propertyLevel),
|
|
229
|
+
typePropertyUserListId,
|
|
230
|
+
referencedTypeRole: defaultIfUndefined(newPropertyType.referencedTypeRole),
|
|
231
|
+
isUniqueProperty: defaultIfUndefined(newPropertyType.isUniqueProperty),
|
|
232
|
+
isUniquePropertyInFamily: defaultIfUndefined(newPropertyType.isUniquePropertyInFamily),
|
|
233
|
+
formulaFunction: defaultIfUndefined(newPropertyType.formulaFunction),
|
|
234
|
+
validationFunction: defaultIfUndefined(newPropertyType.validationFunction),
|
|
235
|
+
numberFormat: defaultIfUndefined(newPropertyType.numberFormat),
|
|
236
|
+
sequenceSeedValue: defaultIfUndefined(newPropertyType.sequenceSeedValue),
|
|
237
|
+
copyBehavior: defaultIfUndefined(newPropertyType.copyBehavior),
|
|
238
|
+
}).filter(([_, value]) => value !== undefined));
|
|
239
|
+
// logger.debug(
|
|
240
|
+
// `found type property option set: ${newPropertyType.typePropertyOptionSetSlug}, ${proposedTP.typePropertyOptionSetId}, `,
|
|
241
|
+
// );
|
|
242
|
+
const differences = extractValueDifferences(found, proposedTypeProperty, [
|
|
243
|
+
'id',
|
|
244
|
+
'createdOn',
|
|
245
|
+
'updatedOn',
|
|
246
|
+
'createdById',
|
|
247
|
+
'updatedById',
|
|
248
|
+
'orgId',
|
|
249
|
+
'typePropertyUserListId',
|
|
250
|
+
'referencedTypeRole',
|
|
251
|
+
'referencedTypeFilterProperties',
|
|
252
|
+
'referencedTypeId',
|
|
253
|
+
'description',
|
|
254
|
+
'apiDetails',
|
|
255
|
+
'notes',
|
|
256
|
+
'isFederationProperty',
|
|
257
|
+
'required',
|
|
258
|
+
'optionsSet',
|
|
259
|
+
'options',
|
|
260
|
+
'inherited',
|
|
261
|
+
'used',
|
|
262
|
+
'typePropertyPolicyIds',
|
|
263
|
+
]);
|
|
264
|
+
const hasChangesToSave = options.isMerge
|
|
265
|
+
? Boolean(differences.updates.length || differences.adds.length)
|
|
266
|
+
: Boolean(differences.updates.length || differences.adds.length || differences.deletions.length);
|
|
267
|
+
if (hasChangesToSave) {
|
|
268
|
+
logger_1.default.debug(`found differences: ${JSON.stringify(differences, null, 2)}`);
|
|
269
|
+
logger_1.default.debug(`Updating Type-Property: ${newPropertyType.slug}, ${found.id}, ${JSON.stringify(proposedTypeProperty, null, 2)}`);
|
|
270
|
+
await new sdk_1.Entities().update({
|
|
271
|
+
entityName: 'type-property',
|
|
272
|
+
id: found.id,
|
|
273
|
+
object: proposedTypeProperty,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
logger_1.default.debug(`Skipping empty update of type property ${newPropertyType.slug}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
logger_1.default.info(`Error Creating Properties: ${err}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (options.isMerge) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
for (const currentPropertyType of currentPropertyTypes) {
|
|
289
|
+
try {
|
|
290
|
+
const found = newPropertyTypesBySlug[currentPropertyType.slug];
|
|
291
|
+
if (!found && !currentPropertyType.inherited) {
|
|
292
|
+
logger_1.default.info(`Deleting Type-Property: ${currentPropertyType.slug}`);
|
|
293
|
+
await sdk_1.Request.request(`/types/${type.id}/type-properties/${currentPropertyType.id}`, {
|
|
294
|
+
method: 'DELETE',
|
|
295
|
+
headers: {
|
|
296
|
+
Accept: 'application/json',
|
|
297
|
+
'Content-Type': 'application/json',
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
catch (err) {
|
|
303
|
+
throw new Error(`Error Deleting Properties: ${err}`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* compares the keys between different objects and returns the differences
|
|
309
|
+
* captures in adds, updates, and deletions. Object 1 is considered the
|
|
310
|
+
* original, and object 2 is considered the "edit".
|
|
311
|
+
*
|
|
312
|
+
* An add is a property that exists in object2 but not in 1. An edit
|
|
313
|
+
* is a property that exists in both object 1 and 2, but with different values.
|
|
314
|
+
* A deletion is a property in object 1 but not in object 2.
|
|
315
|
+
* @param object1 any object
|
|
316
|
+
* @param object2 any other object
|
|
317
|
+
*/
|
|
318
|
+
function extractValueDifferences(object1, object2, keysToIgnore) {
|
|
319
|
+
const updates = [];
|
|
320
|
+
const object1Keys = Object.keys(object1);
|
|
321
|
+
const object2keys = Object.keys(object2);
|
|
322
|
+
const adds = object2keys.filter((key) => !object1Keys.includes(key) && !keysToIgnore.includes(key));
|
|
323
|
+
const deletions = object1Keys.filter((key) => !object2keys.includes(key) && !keysToIgnore.includes(key));
|
|
324
|
+
const shared = object1Keys.filter((key) => object2keys.includes(key) && !keysToIgnore.includes(key));
|
|
325
|
+
for (const key of shared) {
|
|
326
|
+
if (!_.isEqual(object1[key], object2[key])) {
|
|
327
|
+
updates.push(key);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
adds,
|
|
332
|
+
updates,
|
|
333
|
+
deletions,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
exports.extractValueDifferences = extractValueDifferences;
|