Skip to content

Commit

Permalink
Merge pull request #43 from kylethornton/sharing-calc-additions
Browse files Browse the repository at this point in the history
Sharing calc additions
  • Loading branch information
FabienTaillon authored Sep 17, 2020
2 parents 824d121 + 27616a0 commit da289f0
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 16 deletions.
68 changes: 66 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Link the plugin: sfdx plugins:link .
* [`sfdx texei:package:dependencies:install [-k <string>] [-b <string>] [-p <string>] [-s <string>] [-n <string>] [-w <number>] [-r] [-a <string>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeipackagedependenciesinstall--k-string--b-string--p-string--s-string--n-string--w-number--r--a-string--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx texei:profile:clean [-k <string>] [-p <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeiprofileclean--k-string--p-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx texei:sharedactivities:enable [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeisharedactivitiesenable--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx texei:sharingcalc:recalculate [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeisharingcalcrecalculate--s-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx texei:sharingcalc:resume [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeisharingcalcresume--s-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx texei:sharingcalc:suspend [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeisharingcalcsuspend--s-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx texei:skinnyprofile:retrieve [-t <string>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeiskinnyprofileretrieve--t-string--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx texei:source:customlabel:replace -l <string> -v <string> [-p <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-texeisourcecustomlabelreplace--l-string--v-string--p-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
Expand Down Expand Up @@ -315,13 +317,75 @@ EXAMPLE

_See code: [src/commands/texei/sharedactivities/enable.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.6.3/src/commands/texei/sharedactivities/enable.ts)_

## `sfdx texei:sharingcalc:recalculate [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

suspend sharing calculation

```
USAGE
$ sfdx texei:sharingcalc:recalculatte [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
-s, --scope=sharingRule [default: sharingRule] scope of
recalculation
-u, --targetusername=targetusername username or alias for the target
org; overrides default target org
--apiversion=apiversion override the api version used for
api requests made by this command
--json format output as json
--loglevel=(trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL) [default: warn] logging level for
this command invocation
EXAMPLE
$ sfdx texei:sharingcalc:recalculate"
Recalculated Sharing Rules
```

_See code: [src/commands/texei/sharingcalc/recalculate.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.6.3/src/commands/texei/sharingcalc/recalculate.ts)_

## `sfdx texei:sharingcalc:resume [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

resume sharing calculation

```
USAGE
$ sfdx texei:sharingcalc:resume [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
-s, --scope=sharingRule|groupMembership [default: sharingRule] scope of
resumed calculations
-u, --targetusername=targetusername username or alias for the target
org; overrides default target org
--apiversion=apiversion override the api version used for
api requests made by this command
--json format output as json
--loglevel=(trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL) [default: warn] logging level for
this command invocation
EXAMPLE
$ sfdx texei:sharingcalc:resume"
Sharing calculations resumeed
```

_See code: [src/commands/texei/sharingcalc/suspend.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.6.3/src/commands/texei/sharingcalc/suspend.ts)_

## `sfdx texei:sharingcalc:suspend [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

suspend sharing calculation

```
USAGE
$ sfdx texei:sharingcalc:suspend [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
$ sfdx texei:sharingcalc:suspend [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
Expand All @@ -340,7 +404,7 @@ OPTIONS
this command invocation
EXAMPLE
$ sfdx texei:sharingcalc:suspend"
$ sfdx texei:sharingcalc:suspend"
Sharing calculations suspended
```

Expand Down
4 changes: 4 additions & 0 deletions messages/sharingcalc-recalculate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"commandDescription": "recalculate sharing rules",
"scopeFlagDescription": "scope of recalculations"
}
4 changes: 4 additions & 0 deletions messages/sharingcalc-resume.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"commandDescription": "resumed sharing calculation",
"scopeFlagDescription": "scope of resumed calculations"
}
98 changes: 98 additions & 0 deletions src/commands/texei/sharingcalc/recalculate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { flags, core, SfdxCommand } from "@salesforce/command";
import * as puppeteer from "puppeteer";

// Initialize Messages with the current plugin directory
core.Messages.importMessagesDirectory(__dirname);

// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = core.Messages.loadMessages(
"texei-sfdx-plugin",
"sharingcalc-recalculate"
);

const mapSharingLabel = new Map([
['sharingRule', 'Sharing Rule']
]);

export default class Recalculate extends SfdxCommand {
public static description = messages.getMessage("commandDescription");

public static examples = [
`$ sfdx texei:sharingcalc:recalculate" \nRecalculated Sharing Rules\n`
];

protected static flagsConfig = {
scope: flags.string({
char: "s",
description: messages.getMessage("scopeFlagDescription"),
required: false,
options: ["sharingRule"],
default: "sharingRule"
})
};

// Comment this out if your command does not require an org username
protected static requiresUsername = true;

// Comment this out if your command does not support a hub org username
protected static requiresDevhubUsername = false;

// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = false;

public async run(): Promise<any> {
let result = {};

await this.reclaculateSharing();

return result;
}

private async reclaculateSharing() {
const instanceUrl = this.org.getConnection().instanceUrl;

const SHARING_CALC_PATH = "/p/own/DeferSharingSetupPage";

this.ux.startSpinner(`Resuming ${mapSharingLabel.get(this.flags.scope)} Calculations`, null, { stdout: true });
this.debug(`DEBUG Login to Org`);

const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: !(process.env.BROWSER_DEBUG === "true")
});
const page = await browser.newPage();
await page.goto(
`${instanceUrl}/secur/frontdoor.jsp?sid=${
this.org.getConnection().accessToken
}`,
{ waitUntil: ["domcontentloaded", "networkidle0"] }
);
const navigationPromise = page.waitForNavigation();

this.debug(`DEBUG Opening Defer Sharing Calculations page`);

await page.goto(`${instanceUrl + SHARING_CALC_PATH}`);
await navigationPromise;

this.debug(`DEBUG Clicking 'Recalculate' button`);

try {
await page.click(
`#ep > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="rule_recalc"].btn`
);
} catch (ex) {
console.log('Unable to recalculate sharing.', ex.message);
}

await navigationPromise;

this.debug(`DEBUG Closing browser`);

await browser.close();

this.ux.stopSpinner("Done.");

return { message: `Recalculated ${mapSharingLabel.get(this.flags.scope)}s` };
}
}
111 changes: 111 additions & 0 deletions src/commands/texei/sharingcalc/resume.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { flags, core, SfdxCommand } from "@salesforce/command";
import * as puppeteer from "puppeteer";

// Initialize Messages with the current plugin directory
core.Messages.importMessagesDirectory(__dirname);

// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = core.Messages.loadMessages(
"texei-sfdx-plugin",
"sharingcalc-resume"
);

const mapSharingLabel = new Map([
['sharingRule', 'Sharing Rule'],
['groupMembership', 'Group Membership']
]);

export default class Resume extends SfdxCommand {
public static description = messages.getMessage("commandDescription");

public static examples = [
`$ sfdx texei:sharingcalc:resume" \nSharing calculations resumed\n`
];

protected static flagsConfig = {
scope: flags.string({
char: "s",
description: messages.getMessage("scopeFlagDescription"),
required: false,
options: ["sharingRule", "groupMembership"],
default: "sharingRule"
})
};

// Comment this out if your command does not require an org username
protected static requiresUsername = true;

// Comment this out if your command does not support a hub org username
protected static requiresDevhubUsername = false;

// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = false;

public async run(): Promise<any> {
let result = {};

await this.resumeSharingCalc();

return result;
}

private async resumeSharingCalc() {
const instanceUrl = this.org.getConnection().instanceUrl;

const SHARING_CALC_PATH = "/p/own/DeferSharingSetupPage";

this.ux.startSpinner(`Resuming ${mapSharingLabel.get(this.flags.scope)} Calculations`, null, { stdout: true });
this.debug(`DEBUG Login to Org`);

const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: !(process.env.BROWSER_DEBUG === "true")
});
const page = await browser.newPage();
await page.goto(
`${instanceUrl}/secur/frontdoor.jsp?sid=${
this.org.getConnection().accessToken
}`,
{ waitUntil: ["domcontentloaded", "networkidle0"] }
);
const navigationPromise = page.waitForNavigation();

this.debug(`DEBUG Opening Defer Sharing Calculations page`);

await page.goto(`${instanceUrl + SHARING_CALC_PATH}`);
await navigationPromise;

this.debug(`DEBUG Clicking 'Resume' button`);

try {
// Resume either Group Membership or Sharing Rules
if (this.flags.scope === "groupMembership") {
await page.click(
`#gmSect > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="group_resume"].btn`
);

// click the yes button to recaulcate group memberships immediately
await page.click(
`div#group_resume_dialog_buttons > input[value=" Yes "]`
);
} else {
await page.click(
`#ep > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="rule_resume"].btn`
);
}
} catch (ex) {
console.log('Unable to resume sharing.', ex.message);
}

await navigationPromise;

this.debug(`DEBUG Closing browser`);

await browser.close();

this.ux.stopSpinner("Done.");

return { message: `Resumed ${mapSharingLabel.get(this.flags.scope)} Calculations` };
}
}
32 changes: 18 additions & 14 deletions src/commands/texei/sharingcalc/suspend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,23 @@ export default class Suspend extends SfdxCommand {

this.debug(`DEBUG Clicking 'Suspend' button`);

// Suspend either Group Membership or Sharing Rules
if (this.flags.scope === "groupMembership") {
page.on("dialog", dialog => {
dialog.accept();
});

await page.click(
"#gmSect > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > .btn"
);
} else {
await page.click(
"#ep > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > .btn"
);
try {
// Suspend either Group Membership or Sharing Rules
if (this.flags.scope === "groupMembership") {
page.on("dialog", dialog => {
dialog.accept();
});

await page.click(
`#gmSect > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="group_suspend"].btn`
);
} else {
await page.click(
'#ep > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="rule_suspend"].btn'
);
}
} catch (ex) {
console.log('Unable to suspend sharing.', ex.message);
}

await navigationPromise;
Expand All @@ -103,4 +107,4 @@ export default class Suspend extends SfdxCommand {

return { message: `Suspended ${mapSharingLabel.get(this.flags.scope)} Calculations` };
}
}
}

0 comments on commit da289f0

Please sign in to comment.