-
Notifications
You must be signed in to change notification settings - Fork 36
/
businessUnit.js
107 lines (104 loc) · 4.93 KB
/
businessUnit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
import TYPE from '../../types/mcdev.d.js';
import { Util } from './util.js';
import File from './file.js';
import auth from './auth.js';
/**
* Helper that handles retrieval of BU info
*/
const BusinessUnit = {
/**
* Refreshes BU names and ID's from MC instance
*
* @param {TYPE.Mcdevrc} properties current properties that have to be refreshed
* @param {string} credentialsName identifying name of the installed package / project
* @returns {Promise.<boolean>} success of refresh
*/
refreshBUProperties: async function (properties, credentialsName) {
const currentCredentials = properties.credentials[credentialsName];
Util.logger.info(`Loading BUs`);
try {
const client = auth.getSDK({
mid: currentCredentials.eid,
eid: currentCredentials.eid,
credential: credentialsName,
businessUnit: '_ParentBU_',
});
const buResult = await client.soap.retrieve(
'BusinessUnit',
['Name', 'ID', 'ParentName', 'ParentID', 'IsActive'],
{ QueryAllAccounts: true }
);
if (buResult !== null && !buResult.Results) {
Util.logger.error(`Credentials worked but no BUs found. Check access rights!`);
} else if (buResult !== null) {
Util.logger.info(`Found ${buResult.Results.length} BUs:`);
// create shortcut and reset BU list at the same time. we don't want old entries clutter the new list
const myBuList = (currentCredentials.businessUnits = {});
// sort array by name for better display (wont affect object in settings)
for (const element of buResult.Results.map((element) => {
element.ID = Number.parseInt(element.ID);
element.ParentID = Number.parseInt(element.ParentID);
return element;
}).sort((a, b) => {
if (a.ParentID === 0) {
return -1;
}
if (b.ParentID === 0) {
return 1;
}
if (a.Name.toLowerCase() < b.Name.toLowerCase()) {
return -1;
}
if (a.Name.toLowerCase() > b.Name.toLowerCase()) {
return 1;
}
return 0;
})) {
if (element.ParentID === 0) {
myBuList[Util.parentBuName] = element.ID;
currentCredentials.eid = element.ID;
Util.logger.info(` - ${Util.parentBuName} (${element.Name})`);
} else {
const equalizedName = element.Name.replaceAll(/[^\w\s]/gi, '') // remove special chars
.replaceAll(/ +/g, '_') // convert spaces to underscore
.replaceAll(/__+/g, '_'); // make sure we never have more than one underscore in a row
myBuList[equalizedName] = element.ID;
Util.logger.info(
` - ${element.Name} ${
element.Name === equalizedName ? '' : `(${equalizedName})`
}`
);
}
}
Util.logger.debug(`EID: ${currentCredentials.eid}`);
if (currentCredentials.eid === null) {
Util.logger.warn(
`It seems your 'installed package' was not created on the Parent BU of your instance.`
);
Util.logger.warn(
`While basic functionality will work, it is strongly recommended that you create a new 'installed package' there to enable support for shared Data Extensions and automatic retrieval of the BU list.`
);
Util.logger.warn(
`If you cannot create a package on the Parent BU, please open your ./.mcdevrc.json and update the list of BUs and their MIDs manually.`
);
// allow user to work by setting this to an obviously false value which nonetheless doesn't block execution
currentCredentials.eid = -1;
}
// store BU list for repo
await File.writeJSONToFile(
properties.directories.businessUnits,
File.filterIllegalFilenames(credentialsName + '.businessUnits'),
buResult.Results
);
// update config
await File.saveConfigFile(properties);
}
} catch (ex) {
Util.logger.error(`Credentials failure. ${ex}`);
return null;
}
return true;
},
};
export default BusinessUnit;