import { AccessToken , BentleyError , BentleyStatus , GuidString } from "@itwin/core-bentley" ; import { FrontendHubAccess , IModelIdArg } from "@itwin/core-frontend" ; import { BriefcaseId , ChangesetIndexAndId , IModelVersion } from "@itwin/core-common" ; import { BriefcaseQuery } from "./imodelhub/Briefcases" ; import { ChangeSet , ChangeSetQuery } from "./imodelhub/ChangeSets" ; import { IModelHubClient } from "./imodelhub/Client" ; import { Version , VersionQuery } from "./imodelhub/Versions" ; import { IModelQuery } from "./imodelhub/iModels" ; internal export interface IModelNameArg { readonly iModelName : string ; readonly iTwinId : GuidString ; readonly accessToken : AccessToken ; } internal export interface BriefcaseIdArg extends IModelIdArg { readonly briefcaseId : BriefcaseId ; } internal export class IModelHubFrontend implements FrontendHubAccess { public readonly hubClient : IModelHubClient = new IModelHubClient ( ) ; private async _getChangesetFromId ( arg ) { const changeSets = await this . hubClient . changeSets . get ( arg . accessToken , arg . iModelId , new ChangeSetQuery ( ) . byId ( arg . changeSetId ) ) ; if ( ! changeSets [ 0 ] || ! changeSets [ 0 ] . index || ! changeSets [ 0 ] . id ) throw new BentleyError ( BentleyStatus . ERROR , `Changeset ${ arg . changeSetId } not found` ) ; return { index : + changeSets [ 0 ] . index , id : changeSets [ 0 ] . id } ; } public async getLatestChangeset ( arg ) { const changeSets = await this . hubClient . changeSets . get ( arg . accessToken , arg . iModelId , new ChangeSetQuery ( ) . top ( 1 ) . latest ( ) ) ; if ( ! changeSets [ 0 ] || ! changeSets [ 0 ] . index || ! changeSets [ 0 ] . id ) return { index : 0 , id : "" } ; return { index : + changeSets [ 0 ] . index , id : changeSets [ 0 ] . id } ; } public async getChangesetFromVersion ( arg ) { const version = arg . version ; if ( version . isFirst ) return { index : 0 , id : "" } ; const asOfChangeSetId = version . getAsOfChangeSet ( ) ; if ( asOfChangeSetId ) return this . _getChangesetFromId ( { ... arg , changeSetId : asOfChangeSetId } ) ; const versionName = version . getName ( ) ; if ( versionName ) return this . getChangesetFromNamedVersion ( { ... arg , versionName } ) ; return this . getLatestChangeset ( arg ) ; } public async getChangesetFromNamedVersion ( arg ) { const versionQuery = arg . versionName ? new VersionQuery ( ) . select ( "ChangeSetId" ) . byName ( arg . versionName ) : new VersionQuery ( ) . top ( 1 ) ; const versions = await this . hubClient . versions . get ( arg . accessToken , arg . iModelId , versionQuery ) ; if ( ! versions [ 0 ] || ! versions [ 0 ] . changeSetIndex || ! versions [ 0 ] . changeSetId ) throw new BentleyError ( BentleyStatus . ERROR , `Named version ${ arg . versionName ?? "" } not found` ) ; return { index : versions [ 0 ] . changeSetIndex , id : versions [ 0 ] . changeSetId } ; } /** Get the iModelId of an iModel by name. Undefined if no iModel with that name exists.  */ public async queryIModelByName ( arg ) { const iModels = await this . hubClient . iModels . get ( arg . accessToken , arg . iTwinId , new IModelQuery ( ) . byName ( arg . iModelName ) ) ; return iModels . length === 0 ? undefined : iModels [ 0 ] . id ! ; } public async getMyBriefcaseIds ( arg ) { const myHubBriefcases = await this . hubClient . briefcases . get ( arg . accessToken , arg . iModelId , new BriefcaseQuery ( ) . ownedByMe ( ) . selectDownloadUrl ( ) ) ; const myBriefcaseIds = [ ] ; for ( const hubBc of myHubBriefcases ) myBriefcaseIds . push ( hubBc . briefcaseId ! ) ; return myBriefcaseIds ; } public async releaseBriefcase ( arg ) { await this . hubClient . briefcases . delete ( arg . accessToken , arg . iModelId , arg . briefcaseId ) ; } }