Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1709 retrieve users by #1710

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion @types/lib/metadataTypes/User.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion @types/lib/util/util.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 48 additions & 40 deletions lib/metadataTypes/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,15 +602,17 @@ class User extends MetadataType {
},
};
if (key) {
// move original filter down one level into rightOperand and add key filter into leftOperand
let isId = false;
let identifier = key;
if (key.startsWith('id:')) {
identifier = key.slice(3);
isId = true;
}
// remove original filter as it might block this search
requestParams.filter = {
leftOperand: {
leftOperand: 'CustomerKey',
operator: 'equals',
rightOperand: key,
},
operator: 'AND',
rightOperand: requestParams.filter,
leftOperand: isId ? 'AccountUserID' : 'CustomerKey',
operator: 'equals',
rightOperand: identifier,
};
} else {
// if we are not filtering by key the following requests will take long. Warn the user
Expand Down Expand Up @@ -692,9 +694,11 @@ class User extends MetadataType {
return;
}

const metadata = this.parseResponseBody(resultsBulk);
// get BUs that each users have access to
let metadata = {};
if (resultsBulk?.Results?.length > 0) {
metadata = this.parseResponseBody(resultsBulk);

// get BUs that each users have access to
if (!singleRetrieve) {
Util.logger.info(
Util.getGrayMsg(` - found ${resultsBulk?.Results.length} users`)
Expand Down Expand Up @@ -734,40 +738,44 @@ class User extends MetadataType {
}
}
}
}

if (retrieveDir) {
const savedMetadata = await this.saveResults(metadata, retrieveDir, null);
Util.logger.info(
`Downloaded: ${this.definition.type} (${Object.keys(savedMetadata).length})` +
Util.getKeysString(singleRetrieve)
);
if (!singleRetrieve) {
// print summary to cli
const counter = {
userActive: 0,
userInactive: 0,
installedPackage: 0,
};
for (const id in savedMetadata) {
/** @typedef {UserDocument} */
const item = savedMetadata[id];
if (item.c__type === 'Installed Package') {
counter.installedPackage++;
} else if (item.ActiveFlag) {
counter.userActive++;
} else {
counter.userInactive++;
}
}
if (retrieveDir) {
const savedMetadata = await this.saveResults(metadata, retrieveDir, null);
Util.logger.info(
Util.getGrayMsg(
`Found ${counter.userActive} active users / ${counter.userInactive} inactive users / ${counter.installedPackage} installed packages`
)
`Downloaded: ${this.definition.type} (${Object.keys(savedMetadata).length})` +
Util.getKeysString(singleRetrieve)
);
if (!singleRetrieve) {
// print summary to cli
const counter = {
userActive: 0,
userInactive: 0,
installedPackage: 0,
};
for (const id in savedMetadata) {
/** @typedef {UserDocument} */
const item = savedMetadata[id];
if (item.c__type === 'Installed Package') {
counter.installedPackage++;
} else if (item.ActiveFlag) {
counter.userActive++;
} else {
counter.userInactive++;
}
}
Util.logger.info(
Util.getGrayMsg(
`Found ${counter.userActive} active users / ${counter.userInactive} inactive users / ${counter.installedPackage} installed packages`
)
);
}
await this.runDocumentOnRetrieve(singleRetrieve, savedMetadata);
}
await this.runDocumentOnRetrieve(singleRetrieve, savedMetadata);
} else {
Util.logger.info(
`Downloaded: ${this.definition.type} (0)` + Util.getKeysString(singleRetrieve)
);
}

return { metadata: metadata, type: this.definition.type };
}

Expand Down
20 changes: 18 additions & 2 deletions lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,17 +840,33 @@ export const Util = {
* @param {boolean} [isId] optional flag to indicate if key is an id
* @returns {string} string to be appended to log message
*/
getKeysString(keyArr, isId) {
getKeysString(keyArr, isId = false) {
if (!keyArr) {
return '';
}
let isName = false;
if (!Array.isArray(keyArr)) {
// if only one key, make it an array
keyArr = [keyArr];
}
if (Array.isArray(keyArr) && keyArr.length === 1) {
// clone it because we don't want our changes to be reflected outside of this function
keyArr = structuredClone(keyArr);

// if we only have one key, we can check if it is an id or name
if (keyArr[0].startsWith('id:')) {
keyArr[0] = keyArr[0].slice(3);
isId = true;
} else if (keyArr[0].startsWith('name:')) {
keyArr[0] = keyArr[0].slice(5);
isName = true;
}
}
const keyType = isId ? 'ID' : isName ? 'Name' : 'Key';

if (keyArr.length > 0) {
return Util.getGrayMsg(
` - ${isId ? 'ID' : 'Key'}${keyArr.length > 1 ? 's' : ''}: ${keyArr.join(', ')}`
` - ${keyType}${keyArr.length > 1 ? 's' : ''}: ${keyArr.join(', ')}`
);
}
return '';
Expand Down
Loading