-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from texei/sharingcalc
Adding a command to suspend sharing calculations
- Loading branch information
Showing
4 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"commandDescription": "suspend sharing calculation", | ||
"scopeFlagDescription": "scope of suspended calculations" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
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-suspend" | ||
); | ||
|
||
const mapSharingLabel = new Map([ | ||
['sharingRule', 'Sharing Rule'], | ||
['groupMembership', 'Group Membership'] | ||
]); | ||
|
||
export default class Suspend extends SfdxCommand { | ||
public static description = messages.getMessage("commandDescription"); | ||
|
||
public static examples = [ | ||
`$ sfdx texei:sharingcalc:suspend" \nSharing calculations suspended\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.suspendSharingCalc(); | ||
|
||
return result; | ||
} | ||
|
||
private async suspendSharingCalc() { | ||
const instanceUrl = this.org.getConnection().instanceUrl; | ||
|
||
const SHARING_CALC_PATH = "/p/own/DeferSharingSetupPage"; | ||
|
||
this.ux.startSpinner(`Suspending ${mapSharingLabel.get(this.flags.scope)} Calculations`); | ||
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 | ||
}&startURL=${encodeURIComponent(SHARING_CALC_PATH)}`, | ||
{ waitUntil: ["load", "domcontentloaded", "networkidle0"] } | ||
); | ||
const navigationPromise = page.waitForNavigation(); | ||
await navigationPromise; | ||
|
||
this.debug(`DEBUG Opening Defer Sharing Calculations page`); | ||
|
||
await page.goto(`${instanceUrl + SHARING_CALC_PATH}`); | ||
await navigationPromise; | ||
|
||
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" | ||
); | ||
} | ||
|
||
await navigationPromise; | ||
|
||
this.debug(`DEBUG Closing browser`); | ||
|
||
await browser.close(); | ||
|
||
this.ux.stopSpinner("Done."); | ||
|
||
return { message: `Suspended ${mapSharingLabel.get(this.flags.scope)} Calculations` }; | ||
} | ||
} |