Skip to content

Commit

Permalink
QuickFix hardis:org:community:update (#859)
Browse files Browse the repository at this point in the history
* QuickFix hardis:org:communoty:update

* Remove unused param
  • Loading branch information
nvuillam authored Nov 2, 2024
1 parent 6afb20e commit b5e7343
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 61 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

Note: Can be used with `sfdx plugins:install sfdx-hardis@beta` and docker image `hardisgroupcom/sfdx-hardis@beta`

## [5.4.0] 2024-11-02

- New command hardis:org:multi-org-query allowing to execute a SOQL Bulk Query in multiple orgs and aggregate the results in a single CSV / XLS report
- New command hardis:org:community:update to Activate / Deactivate communities from command line

Expand Down
117 changes: 56 additions & 61 deletions src/commands/hardis/org/community/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SfCommand, Flags, requiredOrgFlagWithDeprecations } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { Messages, SfError } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import c from 'chalk';
import { isCI, uxLog } from '../../../../common/utils/index.js';
Expand All @@ -26,13 +26,7 @@ export default class HardisOrgCommunityUpdate extends SfCommand<any> {
}),
status: Flags.string({
description: 'New status for the community, available values are: Live, DownForMaintenance',
char:'s',
required: true,
}),
active: Flags.string({
description: 'Activate or deactivate community, available values are: true, false',
char:'a',
default: true,
char: 's',
required: true,
}),
debug: Flags.boolean({
Expand All @@ -52,65 +46,66 @@ export default class HardisOrgCommunityUpdate extends SfCommand<any> {
const conn = flags['target-org'].getConnection();

if (networkNames.length === 0) {
uxLog(this, c.red(`Error: No network name(s) provided.`));
} else if (networkNames.length > 0) {
const networksConstraintIn = networkNames.map((networkName) => `'${networkName}'`).join(',');
const networksQuery = `SELECT Id, Name, Status FROM Network WHERE Name IN (${networksConstraintIn})`;
const networksQueryRes = await soqlQuery(networksQuery, conn);
if (debugMode) {
uxLog(this, c.grey(`Query result:\n${JSON.stringify(networksQueryRes, null, 2)}`));
}
// Check empty result
if (networksQueryRes.length === 0) {
const outputString = `No matching network records found with given names`;
throw new SfError(`Error: No network name(s) provided.`);
}

const networksConstraintIn = networkNames.map((networkName) => `'${networkName}'`).join(',');
const networksQuery = `SELECT Id, Name, Status FROM Network WHERE Name IN (${networksConstraintIn})`;
const networksQueryRes = await soqlQuery(networksQuery, conn);
if (debugMode) {
uxLog(this, c.grey(`Query result:\n${JSON.stringify(networksQueryRes, null, 2)}`));
}
// Check empty result
if (networksQueryRes.length === 0) {
const outputString = `No matching network records found with given names`;
uxLog(this, c.yellow(outputString));
return { outputString };
}
const idToNameMap = new Map(networksQueryRes.records.map(network => [network.Id, network.Name]));

// Request configuration from user
if (!isCI) {
const confirmUpdate = await prompts({
type: 'confirm',
name: 'value',
initial: true,
message: c.cyanBright(
`Are you sure you want to update these ${c.bold(idToNameMap.size)} networks's status to '${status}' in org ${c.green(
flags['target-org'].getUsername()
)} (y/n)?`
),
});
if (confirmUpdate.value !== true) {
const outputString = 'Script cancelled by user';
uxLog(this, c.yellow(outputString));
return { outputString };
}
const idToNameMap = new Map(networksQueryRes.records.map(network => [network.Id, network.Name]));

// Request configuration from user
if (!isCI) {
const confirmUpdate = await prompts({
type: 'confirm',
name: 'value',
initial: true,
message: c.cyanBright(
`Are you sure you want to update these ${c.bold(idToNameMap.size)} networks's status to '${status}' in org ${c.green(
flags['target-org'].getUsername()
)} (y/n)?`
),
});
if (confirmUpdate.value !== true) {
const outputString = 'Script cancelled by user';
uxLog(this, c.yellow(outputString));
return { outputString };
}
}
}

// Process Network update
const networkUpdates = networksQueryRes.records.map((network) => {
return { Id: network.Id, Status: status };
});
const updateResults = await conn.sobject("Network").update(networkUpdates, { allOrNone: false });
let updateSuccessNb = 0;
let updateErrorsNb = 0;
// Process Network update
const networkUpdates = networksQueryRes.records.map((network) => {
return { Id: network.Id, Status: status };
});
const updateResults = await conn.sobject("Network").update(networkUpdates, { allOrNone: false });
let updateSuccessNb = 0;
let updateErrorsNb = 0;

for (const ret of updateResults) {
if (ret.success) {
updateSuccessNb++;
uxLog(this, c.green(`'${c.bold(idToNameMap.get(ret.id))}' Network was updated.`));
} else {
updateErrorsNb++;
uxLog(this, c.red(`Error ${updateErrorsNb}: Network '${idToNameMap.get(ret.id)}' failed to update: [${ret.errors[0].message}]`));
}
for (const ret of updateResults) {
if (ret.success) {
updateSuccessNb++;
uxLog(this, c.green(`'${c.bold(idToNameMap.get(ret.id))}' Network was updated.`));
} else {
updateErrorsNb++;
uxLog(this, c.red(`Error ${updateErrorsNb}: Network '${idToNameMap.get(ret.id)}' failed to update: [${ret.errors[0].message}]`));
}
// Return an object to be displayed with --json
return {
orgId: flags['target-org'].getOrgId(),
communityUpdatesSuccess: updateSuccessNb,
communityUpdatesErrors: updateErrorsNb,
outputString: `${updateSuccessNb} network(s) were updated`,
};
}
// Return an object to be displayed with --json
return {
orgId: flags['target-org'].getOrgId(),
communityUpdatesSuccess: updateSuccessNb,
communityUpdatesErrors: updateErrorsNb,
outputString: `${updateSuccessNb} network(s) were updated`,
};

}
}

0 comments on commit b5e7343

Please sign in to comment.