file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import { Connection } from '@salesforce/core' ; import { AnyJson } from '@salesforce/ts-types' ; import { JsonMap } from '@salesforce/ts-types' ; import * as jsf from 'jsforce' ; import { SfdxFalconDebug } from '../sfdx-falcon-debug' ; import { resolveConnection } from './sfdx' ; import { AliasOrConnection } from '../sfdx-falcon-types' ; import { MetadataPackage } from '../sfdx-falcon-types' ; import { ObjectDescribe } from '../sfdx-falcon-types' ; import { PermissionSetAssignment } from '../sfdx-falcon-types' ; import { Profile } from '../sfdx-falcon-types' ; import { QueryResult } from '../sfdx-falcon-types' ; import { RestApiRequestDefinition } from '../sfdx-falcon-types' ; import { User } from '../sfdx-falcon-types' ; const dbgNs = 'UTILITY:jsforce:' ; function param { AliasOrConnection } aliasOrConnection param { string } userId param { string } newPassword returns { Promise < void > } description version public async export async function changePassword ( aliasOrConnection , userId , newPassword ) { if ( typeof userId !== 'string' || typeof newPassword !== 'string' ) { throw new TypeError ( `ERROR_INVALID_PARAMETERS: Missing or invalid parameters` ) ; } const rc = await resolveConnection ( aliasOrConnection ) ; await rc . connection . request ( { method : 'post' , url : `/sobjects/User/${ userId }/password` , body : `{"NewPassword": "${ newPassword }"}` } , { options : { noContentResponse : 'SUCCESS_PASSWORD_CHANGED' } } ) ; } function param { AliasOrConnection } aliasOrConnection param { string } username param { string } password param { string } orgAlias returns { Promise < void > } description version public async export async function createSfdxOrgConfig ( aliasOrConnection , username , password , orgAlias ) { const rc = await resolveConnection ( aliasOrConnection ) ; const newConnection = new jsf . Connection ( { loginUrl : rc . connection . instanceUrl } ) ; const userInfo = await newConnection . login ( username , password ) . catch ( error => { SfdxFalconDebug . obj ( `${ dbgNs }createSfdxOrgConfig:error:` , error , `error: ` ) ; throw error ; } ) ; SfdxFalconDebug . obj ( `${ dbgNs }createSfdxOrgConfig:userInfo` , userInfo , `userInfo: ` ) ; const orgSaveData = { } as any ; orgSaveData . orgId = userInfo . organizationId ; orgSaveData . accessToken = newConnection . accessToken ; orgSaveData . instanceUrl = newConnection . instanceUrl ; orgSaveData . username = username ; orgSaveData . loginUrl = rc . connection . instanceUrl + `/secur/frontdoor.jsp?sid=${ newConnection . accessToken }` ; SfdxFalconDebug . obj ( `${ dbgNs }createSfdxOrgConfig:orgSaveData:` , orgSaveData , `orgSaveData: ` ) ; return ; } function param { AliasOrConnection } aliasOrConnection returns { Promise < ObjectDescribe > } description public async export async function describeSignupRequest ( aliasOrConnection ) { SfdxFalconDebug . obj ( `${ dbgNs }describeSignupRequest:arguments:` , arguments , `arguments: ` ) ; const describeRequest = { aliasOrConnection : aliasOrConnection , request : { method : 'get' , url : `/sobjects/SignupRequest` } } ; const describeResponse = await restApiRequest ( describeRequest ) . catch ( error => { SfdxFalconDebug . obj ( `${ dbgNs }describeSignupRequest:error:` , error , `error: ` ) ; return Promise . resolve ( { } ) ; } ) as JsonMap ; SfdxFalconDebug . obj ( `${ dbgNs }describeSignupRequest:describeResponse:` , describeResponse , `describeResponse: ` ) ; return ( describeResponse . objectDescribe || { } ) as ObjectDescribe ; } function param { string | Connection } aliasOrConnection param { string } userId returns { Promise < Array < string > > } description public async export async function getAssignedPermsets ( aliasOrConnection , userId ) { SfdxFalconDebug . obj ( `${ dbgNs }getAssignedPermsets:arguments:` , arguments , `arguments: ` ) ; const rc = await resolveConnection ( aliasOrConnection ) ; const queryResult = await rc . connection . query ( `SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId='${ userId }'` ) as jsf . QueryResult < jsf . Record < PermissionSetAssignment > > ; SfdxFalconDebug . obj ( `${ dbgNs }getAssignedPermsets:queryResult.records:` , queryResult . records , `queryResult.records: ` ) ; const assignedPermsets = new Array < string > ( ) ; for ( const record of queryResult . records ) { assignedPermsets . push ( record . PermissionSetId ) ; } SfdxFalconDebug . obj ( `${ dbgNs }getAssignedPermsets:assignedPermsets:` , assignedPermsets , `assignedPermsets: ` ) ; return assignedPermsets ; } function param { AliasOrConnection } aliasOrConnection returns { Promise < QueryResult < MetadataPackage > > } description public async export async function getPackages ( aliasOrConnection ) { SfdxFalconDebug . obj ( `${ dbgNs }getPackages:arguments:` , arguments , `arguments: ` ) ; const packageCheckQuery = 'SELECT Id, Name, NamespacePrefix, ' + '( ' + '  SELECT ' + '    Id, Name, BuildNumber, MetadataPackageId, MajorVersion, ' + '    MinorVersion, PatchVersion, ReleaseState ' + '  FROM MetadataPackageVersions' + ') ' + 'FROM MetadataPackage' ; const metadataPackageResults = await toolingApiQuery < MetadataPackage > ( aliasOrConnection , packageCheckQuery ) ; SfdxFalconDebug . obj ( `${ dbgNs }getPackages:metadataPackageResults:` , metadataPackageResults , `metadataPackageResults: ` ) ; return metadataPackageResults ; } function param { AliasOrConnection } aliasOrConnection param { string } profileName returns { Promise < string > } description public async export async function getProfileId ( aliasOrConnection , profileName ) { SfdxFalconDebug . obj ( `${ dbgNs }getProfileId:arguments:` , arguments , `arguments: ` ) ; const rc = await resolveConnection ( aliasOrConnection ) ; const queryResult = await rc . connection . query ( `SELECT Id FROM Profile WHERE Name='${ profileName }'` ) as jsf . QueryResult < jsf . Record < Profile > > ; SfdxFalconDebug . obj ( `${ dbgNs }getProfileId:queryResult.records[0]` , queryResult . records [ 0 ] , `queryResult.records[0]: ` ) ; if ( typeof queryResult . records [ 0 ] === 'undefined' ) { throw new Error ( `ERROR_PROFILE_NOT_FOUND: Profile '${ profileName }' does not exist in org '${ rc . orgIdentifier }'` ) ; } return queryResult . records [ 0 ] . Id ; } function param { AliasOrConnection } aliasOrConnection param { string } username returns { Promise < string > } description public async export async function getUserId ( aliasOrConnection , username ) { SfdxFalconDebug . obj ( `${ dbgNs }getUserId:arguments:` , arguments , `arguments: ` ) ; const rc = await resolveConnection ( aliasOrConnection ) ; const queryResult = await rc . connection . query ( `SELECT Id FROM User WHERE Username='${ username }'` ) as jsf . QueryResult < jsf . Record < User > > ; SfdxFalconDebug . obj ( `${ dbgNs }getUserId:queryResult.records[0]:` , queryResult . records [ 0 ] , `queryResult.records[0]: ` ) ; if ( typeof queryResult . records [ 0 ] === 'undefined' ) { throw new Error ( `ERROR_USER_NOT_FOUND: User '${ username }' does not exist in org '${ rc . orgIdentifier }'` ) ; } return queryResult . records [ 0 ] . Id ; } function param { RestApiRequestDefinition } restRequestDef returns { Promise < any > } description version public async export async function restApiRequest ( restRequestDef ) { const rc = await resolveConnection ( restRequestDef . aliasOrConnection ) ; const restResult = await rc . connection . request ( restRequestDef . request ) ; SfdxFalconDebug . obj ( `${ dbgNs }restApiRequest:restResult:` , restResult , `restResult: ` ) ; return restResult ; } function param { AliasOrConnection } aliasOrConnection param { string } query returns { Promise < any > } description public async export async function toolingApiQuery < T > ( aliasOrConnection , query ) { SfdxFalconDebug . obj ( `${ dbgNs }toolingApiQuery:arguments:` , arguments , `arguments: ` ) ; const rc = await resolveConnection ( aliasOrConnection ) ; return new Promise ( ( resolve , reject ) => { rc . connection . tooling . query < T > ( query , { } , ( err , records ) => { if ( err ) { SfdxFalconDebug . obj ( `${ dbgNs }toolingApiQuery:err:` , err , `Callback returned with the following Error: ` ) ; SfdxFalconDebug . obj ( `${ dbgNs }toolingApiQuery:records:` , records , `Records returned as part of Query Error: ` ) ; reject ( err ) ; } SfdxFalconDebug . obj ( `${ dbgNs }toolingApiQuery:records:` , records , `Records successfully returned: ` ) ; resolve ( records ) ; } ) ; } ) ; }