Skip to content

Commit

Permalink
Merge pull request #28 from texei/sharingcalc
Browse files Browse the repository at this point in the history
Adding a command to suspend sharing calculations
  • Loading branch information
FabienTaillon authored Mar 18, 2020
2 parents df82040 + 4e95ecd commit c894f07
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
4 changes: 4 additions & 0 deletions messages/sharingcalc-suspend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"commandDescription": "suspend sharing calculation",
"scopeFlagDescription": "scope of suspended calculations"
}
1 change: 0 additions & 1 deletion src/commands/texei/org/contractfieldhistory/fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default class Fix extends SfdxCommand {
public async run(): Promise<any> {
let result = {};

//const conn = this.org.getConnection();
await this.fixContract();

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/texei/profile/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class Clean extends SfdxCommand {
// Writing back to file
await fs.writeFile(filePath, xmlFile, 'utf8', function (err) {
if (err) {
throw new SfdxError(`Unable to write Products file at path ${filePath}: ${err}`);
throw new SfdxError(`Unable to write Profile file at path ${filePath}: ${err}`);
}
});

Expand Down
107 changes: 107 additions & 0 deletions src/commands/texei/sharingcalc/suspend.ts
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` };
}
}

0 comments on commit c894f07

Please sign in to comment.