Skip to content

Commit

Permalink
#139: reduce amount of calls needed to find single key
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Mar 23, 2023
1 parent a4ef963 commit d4037f3
Showing 1 changed file with 87 additions and 61 deletions.
148 changes: 87 additions & 61 deletions lib/metadataTypes/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ class User extends MetadataType {
const fields = this.getFieldNamesToRetrieve(additionalFields, !retrieveDir);
const soapType = this.definition.soapType || this.definition.type;
let resultsBulk;
let foundSingle = false;
for (const active of [true, false]) {
requestParamsUser.filter.leftOperand.rightOperand = active;
try {
Expand All @@ -626,75 +627,23 @@ class User extends MetadataType {
} else {
resultsBulk = resultsBatch;
}
if (singleRetrieve && resultsBatch?.Results.length) {
foundSingle = true;
break;
}
}
} catch (ex) {
this._handleSOAPErrors(ex, 'retrieving');
return {};
}
}
const requestParamsInstalledPackage = {
QueryAllAccounts: true,

filter: {
leftOperand: {
leftOperand: 'ActiveFlag',
operator: 'equals',
rightOperand: true, // inactive installed packages are not visible in UI and hence cannot be reactivated there. Let's not retrieve them
},
operator: 'AND',
rightOperand: {
leftOperand: {
// filter out normal users
leftOperand: 'Email',
operator: 'isNull',
},
operator: 'OR',
rightOperand: {
// installed packages
leftOperand: {
leftOperand: 'Name',
operator: 'like',
rightOperand: ' app user', // ! will not work if the name was too long as "app user" might be cut off
},
operator: 'AND',
rightOperand: {
// this is used to filter out system generated installed packages. in our testing, at least those installed packages created in the last few years have hat set this to false while additional (hidden) installed packages have it set to true.
leftOperand: 'MustChangePassword',
operator: 'equals',
rightOperand: 'false',
},
},
},
},
};
if (requestParams?.filter?.leftOperand?.leftOperand === 'CustomerKey') {
requestParamsInstalledPackage.filter = {
leftOperand: requestParams?.filter?.leftOperand,
operator: 'AND',
rightOperand: requestParamsInstalledPackage.filter,
};
}
try {
const resultsBatch = await this.client.soap.retrieveBulk(
soapType,
fields,
requestParamsInstalledPackage
);
if (Array.isArray(resultsBatch?.Results)) {
Util.logger.debug(
Util.getGrayMsg(` - found ${resultsBatch?.Results.length} installed packages`)
);
if (resultsBulk) {
// once first batch is done, the follow just add to result payload
resultsBulk.Results.push(...resultsBatch.Results);
} else {
resultsBulk = resultsBatch;
}
}
} catch (ex) {
this._handleSOAPErrors(ex, 'retrieving');
if (
!foundSingle &&
!this._retrieveSOAP_installedPackage(requestParams, soapType, fields, resultsBulk)
) {
return {};
}

const metadata = this.parseResponseBody(resultsBulk);
// get BUs that each users have access to
if (resultsBulk?.Results?.length > 0) {
Expand Down Expand Up @@ -779,6 +728,83 @@ class User extends MetadataType {
return { metadata: metadata, type: this.definition.type };
}

/**
* helper for {@link retrieveSOAP}
*
* @private
* @param {TYPE.SoapRequestParams} [requestParams] required for the specific request (filter for example)
* @param {string} soapType e.g. AccountUser
* @param {string[]} fields list of fields to retrieve
* @param {object} resultsBulk actual return value of this method
* @returns {boolean} success flag
*/
static async _retrieveSOAP_installedPackage(requestParams, soapType, fields, resultsBulk) {
const requestParamsInstalledPackage = {
QueryAllAccounts: true,

filter: {
leftOperand: {
leftOperand: 'ActiveFlag',
operator: 'equals',
rightOperand: true, // inactive installed packages are not visible in UI and hence cannot be reactivated there. Let's not retrieve them
},
operator: 'AND',
rightOperand: {
leftOperand: {
// filter out normal users
leftOperand: 'Email',
operator: 'isNull',
},
operator: 'OR',
rightOperand: {
// installed packages
leftOperand: {
leftOperand: 'Name',
operator: 'like',
rightOperand: ' app user', // ! will not work if the name was too long as "app user" might be cut off
},
operator: 'AND',
rightOperand: {
// this is used to filter out system generated installed packages. in our testing, at least those installed packages created in the last few years have hat set this to false while additional (hidden) installed packages have it set to true.
leftOperand: 'MustChangePassword',
operator: 'equals',
rightOperand: 'false',
},
},
},
},
};
if (requestParams?.filter?.leftOperand?.leftOperand === 'CustomerKey') {
requestParamsInstalledPackage.filter = {
leftOperand: requestParams?.filter?.leftOperand,
operator: 'AND',
rightOperand: requestParamsInstalledPackage.filter,
};
}
try {
const resultsBatch = await this.client.soap.retrieveBulk(
soapType,
fields,
requestParamsInstalledPackage
);
if (Array.isArray(resultsBatch?.Results)) {
Util.logger.debug(
Util.getGrayMsg(` - found ${resultsBatch?.Results.length} installed packages`)
);
if (resultsBulk) {
// once first batch is done, the follow just add to result payload
resultsBulk.Results.push(...resultsBatch.Results);
} else {
resultsBulk = resultsBatch;
}
}
} catch (ex) {
this._handleSOAPErrors(ex, 'retrieving');
return false;
}
return true;
}

/**
*
* @private
Expand Down

0 comments on commit d4037f3

Please sign in to comment.