Skip to content

Commit

Permalink
feat: add Company Information > Currency Locale
Browse files Browse the repository at this point in the history
This plugins allows changing the default currency locale.

Configuration example:

```json
"companyInformation": {
      "defaultCurrencyIsoCode": "English (Ireland) - EUR"
}
```
  • Loading branch information
amtrack authored May 12, 2023
2 parents 91e6cb6 + 94c282a commit 2aac2fc
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
node_modules
/.sfdx
/.sf
coverage/
.yarn/
.yarnrc.yml

.vscode/settings.json

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ Here is a full blown example showing most of the supported settings in action:
"security": {
"loginAccessPolicies": { "administratorsCanLogInAsAnyUser": true },
"sharing": { "enableExternalSharingModel": true }
},
"companyInformation": {
"defaultCurrencyIsoCode": "English (Ireland) - EUR"
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/company-information/currency-invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"settings": {
"companyInformation": {
"defaultCurrencyIsoCode": "Invalid Currency"
}
}
}
7 changes: 7 additions & 0 deletions src/plugins/company-information/currency-ireland.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"settings": {
"companyInformation": {
"defaultCurrencyIsoCode": "English (Ireland) - EUR"
}
}
}
7 changes: 7 additions & 0 deletions src/plugins/company-information/currency-zar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"settings": {
"companyInformation": {
"defaultCurrencyIsoCode": "English (South Africa) - ZAR"
}
}
}
62 changes: 62 additions & 0 deletions src/plugins/company-information/index.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import assert from 'assert';
import * as child from 'child_process';
import * as path from 'path';
import { CompanyInformation } from '.';
import Os from 'os';

describe(CompanyInformation.name, function () {
this.slow('30s');
this.timeout('2m');

const execStr = path.resolve('bin', Os.platform().startsWith('win') ? 'run.cmd' : 'run');

it('should set the currency to "English (South Africa) - ZAR" for next steps', () => {
const bffile = path.join(__dirname, 'currency-zar.json');
const apply = child.spawnSync(execStr, [
'browserforce:apply',
'-f',
bffile
]);
assert.deepStrictEqual(apply.status, 0, apply.output.toString());
// no additional assertions are done here as this is a preparation for the followup tests
});
it('should change currency to "English (Ireland) - EUR"', () => {
const bffile = path.join(__dirname, 'currency-ireland.json');
const apply = child.spawnSync(execStr, [
'browserforce:apply',
'-f',
bffile
]);
assert.deepStrictEqual(apply.status, 0, apply.output.toString());
assert.ok(
/to '"English \(Ireland\) - EUR"'/.test(apply.output.toString()),
apply.output.toString()
);
});
it('should respond to no action necessary for currency change', () => {
const bffile = path.join(__dirname, 'currency-ireland.json');
const apply = child.spawnSync(execStr, [
'browserforce:apply',
'-f',
bffile
]);
assert.deepStrictEqual(apply.status, 0, apply.output.toString());
assert.ok(
/no action necessary/.test(apply.output.toString()),
apply.output.toString()
);
});
it('should error on invalid input for invalid currency', () => {
const bffile = path.join(__dirname, 'currency-invalid.json');
const apply = child.spawnSync(execStr, [
'browserforce:apply',
'-f',
bffile
]);
assert.notDeepStrictEqual(apply.status, 0, apply.output.toString());
assert.ok(
/Invalid currency provided/.test(apply.output.toString()),
apply.output.toString()
);
});
});
73 changes: 73 additions & 0 deletions src/plugins/company-information/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { BrowserforcePlugin } from '../../plugin';

const PATHS = (orgId: string) => ({
BASE: `/${orgId}/e`
});
const SELECTORS = {
BASE: 'div.pbBody',
CURRENCY_DROPDOWN: '#DefaultCurrencyIsoCode',
SAVE_BUTTON: 'input[class="btn"][type="submit"][name="save"]'
};

type Config = {
defaultCurrencyIsoCode?: string;
};

export class CompanyInformation extends BrowserforcePlugin {
public async retrieve(): Promise<Config> {
const path = PATHS(this.org.getOrgId());
const page = await this.browserforce.openPage(path.BASE);
await page.waitForSelector(SELECTORS.CURRENCY_DROPDOWN);

const response = {
defaultCurrencyIsoCode: ''
};
const selectedOptions = await page.$$eval(
`${SELECTORS.CURRENCY_DROPDOWN} > option[selected]`,
options => options.map(option => option.textContent)
);
if (!selectedOptions) {
throw new Error('No available existing value');
}
response.defaultCurrencyIsoCode = selectedOptions[0];
return response;
}

public async apply(config: Config): Promise<void> {
if (config.defaultCurrencyIsoCode?.length > 0) {
const path = PATHS(this.org.getOrgId());
const page = await this.browserforce.openPage(path.BASE);

// wait for selectors
await page.waitForSelector(SELECTORS.CURRENCY_DROPDOWN);
const selectElem = await page.$(SELECTORS.CURRENCY_DROPDOWN);
await page.waitForSelector(SELECTORS.SAVE_BUTTON);

// apply changes
// await page.click(SELECTORS.CURRENCY_DROPDOWN);
const optionList = await page.$$eval(
`${SELECTORS.CURRENCY_DROPDOWN} > option`,
options => options.map(option => ({
value: (option as HTMLOptionElement).value,
textContent: option.textContent
}))
);
const toBeSelectedOption = optionList.find(option => option.textContent == config.defaultCurrencyIsoCode);
if (!toBeSelectedOption) {
throw new Error(`Invalid currency provided. '${config.defaultCurrencyIsoCode}' is not a valid option available for currencies. Please use the exact name as it appears in the list.`);
}
await selectElem.select(toBeSelectedOption.value);

// auto accept the dialog when it appears
page.on("dialog", (dialog) => {
dialog.accept();
});

// save
await Promise.all([
page.waitForNavigation(),
page.click(SELECTORS.SAVE_BUTTON)
]);
}
}
}
13 changes: 13 additions & 0 deletions src/plugins/company-information/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://github.com/amtrack/sfdx-browserforce-plugin/src/plugins/home-page-layouts/schema.json",
"title": "Company Information",
"description": "",
"type": "object",
"properties": {
"defaultCurrencyIsoCode": {
"title": "Default Currency",
"type": "string"
}
}
}
4 changes: 3 additions & 1 deletion src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { RelateContactToMultipleAccounts as relateContactToMultipleAccounts } fr
import { ReportsAndDashboards as reportsAndDashboards } from './reports-and-dashboards';
import { SalesforceToSalesforce as salesforceToSalesforce } from './salesforce-to-salesforce';
import { Security as security } from './security';
import { CompanyInformation as companyInformation } from './company-information';

export {
activitySettings,
Expand All @@ -31,5 +32,6 @@ export {
relateContactToMultipleAccounts,
reportsAndDashboards,
salesforceToSalesforce,
security
security,
companyInformation
};
3 changes: 3 additions & 0 deletions src/plugins/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"properties": {
"settings": {
"properties": {
"companyInformation": {
"$ref": "./company-information/schema.json"
},
"opportunitySplits": {
"$ref": "./opportunity-splits/schema.json"
},
Expand Down

0 comments on commit 2aac2fc

Please sign in to comment.