Skip to content

Commit

Permalink
feat: enhance org selection and displaying all more org details
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeneos committed Dec 19, 2023
1 parent c5ea42f commit cfab40e
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/salesforce/src/queryService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DateTime } from 'luxon';
import { Logger, injectable, LifecyclePolicy } from '@vlocode/core';
import { CancellationToken } from '@vlocode/util';
import { CancellationToken, PropertyAccessor } from '@vlocode/util';

import { SalesforceConnectionProvider } from './connection';
import { PropertyAccessor, SObjectRecord, Field, FieldType } from './types';
import { SObjectRecord, FieldType } from './types';
import { NamespaceService } from './namespaceService';
import { QueryCache } from './queryCache';
import { RecordFactory } from './queryRecordFactory';
Expand Down
3 changes: 1 addition & 2 deletions packages/salesforce/src/recordBatch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { LogManager } from '@vlocode/core';
import { Timer, arrayMapPush, CancellationToken, groupBy } from '@vlocode/util';
import { Timer, arrayMapPush, CancellationToken, groupBy, AwaitReturnType } from '@vlocode/util';
import { RecordResult, BatchInfo } from 'jsforce';

import { AwaitReturnType } from './types';
import { SalesforceSchemaService } from './salesforceSchemaService';
import { SalesforceConnection } from './connection';

Expand Down
3 changes: 1 addition & 2 deletions packages/salesforce/src/salesforceLookupService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Logger, injectable, LifecyclePolicy } from '@vlocode/core';
import { asArray, joinLimit, isSalesforceId, CancellationToken, groupBy, Iterable, mapKeys, unique } from '@vlocode/util';
import { PropertyAccessor } from './types';
import { asArray, joinLimit, isSalesforceId, CancellationToken, groupBy, Iterable, mapKeys, PropertyAccessor } from '@vlocode/util';
import { QueryService, QueryResult } from './queryService';
import { SalesforceSchemaService } from './salesforceSchemaService';
import { NamespaceService } from './namespaceService';
Expand Down
3 changes: 1 addition & 2 deletions packages/salesforce/src/salesforceProfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { sortProperties, stringEquals, XML } from '@vlocode/util';
import { ArrayElement } from './types';
import { ArrayElement, sortProperties, stringEquals, XML } from '@vlocode/util';

export enum SalesforceFieldPermission {
editable = 'editable',
Expand Down
9 changes: 5 additions & 4 deletions packages/salesforce/src/salesforceService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as jsforce from 'jsforce';
import { FileSystem, injectable, Logger } from '@vlocode/core';
import { cache, evalTemplate, mapAsyncParallel, XML, substringAfter, fileName, Timer, FileSystemUri, CancellationToken, asArray, groupBy, isSalesforceId, spreadAsync, filterUndefined } from '@vlocode/util';
import { cache, evalTemplate, mapAsyncParallel, XML, substringAfter, fileName, Timer, FileSystemUri, CancellationToken, asArray, groupBy, isSalesforceId, spreadAsync, filterUndefined, PropertyAccessor } from '@vlocode/util';

import { HttpMethod, HttpRequestInfo, SalesforceConnectionProvider } from './connection';
import { SalesforcePackageBuilder, SalesforcePackageType } from './deploymentPackageBuilder';
Expand All @@ -10,7 +10,7 @@ import { SalesforceDeployService } from './salesforceDeployService';
import { SalesforceLookupService } from './salesforceLookupService';
import { SalesforceProfile } from './salesforceProfile';
import { SalesforceSchemaService } from './salesforceSchemaService';
import { SObjectRecord, PropertyAccessor } from './types';
import { SObjectRecord } from './types';
import { MetadataRegistry, MetadataType } from './metadataRegistry';
import { NamespaceService } from './namespaceService';
import { SoapClient, SoapDebuggingHeader } from './soapClient';
Expand Down Expand Up @@ -173,7 +173,7 @@ export class SalesforceService implements SalesforceConnectionProvider {
}
yield* batch.execute(await this.getJsForceConnection(), undefined, cancelToken);
}

/**
* Delete one or more records based on a SOQL filter condition. Returns array of deleted record-ids
* @param type SObject type to delete
Expand Down Expand Up @@ -424,12 +424,13 @@ export class SalesforceService implements SalesforceConnectionProvider {
return this.connectionProvider.getApiVersion();
}


/**
* Get a list of available API version on the connected server
*/
public async getApiVersions(count: number = 10) {
const connection = await this.getJsForceConnection();
const version = parseFloat(connection.version);
const version = await connection.getMaxApiVersion();
const versions: string[] = [];
for (let i = 0; i < count; i++) {
versions.push((version - i).toFixed(1));
Expand Down
1 change: 0 additions & 1 deletion packages/salesforce/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from './metadata';
export * from './sobjectField';
export * from './sobjectInfo';
export * from './sobjectRecord';
export * from './utilityTypes';
10 changes: 0 additions & 10 deletions packages/salesforce/src/types/utilityTypes.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from './sfdx';
export * from './singleton';
export * from './string';
export * from './task';
export * from './types';
export * from './timer';
export * from './transactionalMap';
export * from './validate';
Expand Down
19 changes: 18 additions & 1 deletion packages/util/src/sfdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export interface SalesforceAuthResult {
}

export interface SalesforceOrgInfo extends SalesforceAuthResult {
isSandbox: boolean;
lastAccessed?: Date;
orgName: string;
alias?: string;
aliases: string[];
}

interface FileSystem {
Expand Down Expand Up @@ -220,16 +224,29 @@ export namespace sfdx {
continue;
}

const allAliases = stateAggregator.aliases.getAll(authFields.username);

// Find org name from instance URL
const orgNameFromUrl = authFields.instanceUrl
.replace(/^https?:\/\//i, '')
.replace(/(\.sandbox)?(\.my)?.salesforce\.com$/i, '');
const isSandbox = /\.sandbox\./i.test(authFields.instanceUrl) || /test\.salesforce\.com/i.test(authFields.loginUrl ?? '');
const lastAccessed = authFields.instanceApiVersionLastRetrieved ? new Date(authFields.instanceApiVersionLastRetrieved) : undefined;

yield {
orgId: authFields.accessToken.split('!').shift()!,
orgName: orgNameFromUrl,
isSandbox: isSandbox,
lastAccessed: lastAccessed,
accessToken: authFields.accessToken,
instanceUrl: authFields.instanceUrl,
loginUrl: authFields.loginUrl ?? authFields.instanceUrl,
username: authFields.username,
clientId: authFields.clientId!,
clientSecret: authFields.clientSecret,
refreshToken: authFields.refreshToken!,
alias: stateAggregator.aliases.get(authFields.username) || undefined
alias: allAliases[0] || undefined,
aliases: allAliases
};
} catch(err) {
logger.warn(`Error while reading SFDX auth for "${username}"`, err);
Expand Down
29 changes: 23 additions & 6 deletions packages/vscode-extension/src/commands/selectOrgCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,26 @@ export default class SelectOrgCommand extends CommandBase {
}
}

protected async getAuthorizedOrgs() : Promise<SelectOrgQuickPickItem[]> {
private async getAuthorizedOrgs() : Promise<SelectOrgQuickPickItem[]> {
const orgList = await sfdx.getAllKnownOrgDetails();
return orgList.map(orgInfo => ({ label: orgInfo.alias ? `${orgInfo.alias} - ${orgInfo.username}` : orgInfo.username, description: orgInfo.instanceUrl, orgInfo }));
return orgList.map(orgInfo => ({
label: this.getOrgLabel(orgInfo),
description: this.getOrgDescription(orgInfo),
orgInfo
}));
}

private getOrgDescription(org: SalesforceOrgInfo) {
return `${org.orgName} (${org.orgId})`;
}

private getOrgLabel(org: SalesforceOrgInfo) {
const prefix = !org.refreshToken ? '$(warning) ' : '';
if (org.aliases?.length) {
const aliases = [...org.aliases].sort().join(', ');
return `${prefix}${aliases} - ${org.username}`;
}
return `${prefix}${org.username}`;
}

public async execute() : Promise<void> {
Expand Down Expand Up @@ -95,7 +112,7 @@ export default class SelectOrgCommand extends CommandBase {
}
}

protected async authorizeNewOrg() : Promise<SalesforceOrgInfo | undefined> {
protected async authorizeNewOrg() : Promise<SalesforceAuthResult | undefined> {
const flowType = await vscode.window.showQuickPick(this.authFlows,
{ placeHolder: 'Select the authorization flows you want use' });

Expand Down Expand Up @@ -130,7 +147,7 @@ export default class SelectOrgCommand extends CommandBase {
}
}

protected async authorizeDeviceLogin(options: { instanceUrl: string, alias?: string }) : Promise<SalesforceOrgInfo | undefined> {
protected async authorizeDeviceLogin(options: { instanceUrl: string, alias?: string }) : Promise<SalesforceAuthResult | undefined> {
const authInfo = await this.vlocode.withActivity({
location: vscode.ProgressLocation.Notification,
progressTitle: 'Salesforce Device Login',
Expand Down Expand Up @@ -181,7 +198,7 @@ export default class SelectOrgCommand extends CommandBase {
return this.processAuthinfo(authInfo, options);
}

protected async authorizeWebLogin(options: { instanceUrl: string, alias?: string }) : Promise<SalesforceOrgInfo | undefined> {
protected async authorizeWebLogin(options: { instanceUrl: string, alias?: string }) : Promise<SalesforceAuthResult | undefined> {
this.logger.log(`Opening '${options.instanceUrl}' in a new browser window`);
const authInfo = await this.vlocode.withActivity({
location: vscode.ProgressLocation.Notification,
Expand All @@ -194,7 +211,7 @@ export default class SelectOrgCommand extends CommandBase {
return this.processAuthinfo(authInfo, options);
}

private async processAuthinfo(authInfo: SalesforceAuthResult | undefined, options: { instanceUrl: string, alias?: string }): Promise<SalesforceOrgInfo | undefined>{
private async processAuthinfo(authInfo: SalesforceAuthResult | undefined, options: { instanceUrl: string, alias?: string }): Promise<SalesforceAuthResult | undefined>{
if (!authInfo || !authInfo.accessToken) {
this.logger.error(`Unable to authorize at '${options.instanceUrl}'`);
void vscode.window.showErrorMessage('Failed to authorize new org, see the log for more details');
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/lib/commandBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ export abstract class CommandBase implements Command {
private getName() : string {
return this.constructor?.name || 'Command';
}
}
}

0 comments on commit cfab40e

Please sign in to comment.