import { Component } from "@angular/core"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { LogService } from "@bitwarden/common/abstractions/log.service"; import { OrganizationConnectionType } from "@bitwarden/common/enums/organizationConnectionType"; import { BillingSyncConfigApi } from "@bitwarden/common/models/api/billingSyncConfigApi"; import { BillingSyncConfigRequest } from "@bitwarden/common/models/request/billingSyncConfigRequest"; import { OrganizationConnectionRequest } from "@bitwarden/common/models/request/organizationConnectionRequest"; import { OrganizationConnectionResponse } from "@bitwarden/common/models/response/organizationConnectionResponse"; @Component({ selector: "app-billing-sync-key", templateUrl: "billing-sync-key.component.html", }) export class BillingSyncKeyComponent { entityId: string; existingConnectionId: string; billingSyncKey: string; setParentConnection: (connection: OrganizationConnectionResponse) => void; formPromise: Promise> | Promise; constructor(private apiService: ApiService, private logService: LogService) {} async submit() { try { const request = new OrganizationConnectionRequest( this.entityId, OrganizationConnectionType.CloudBillingSync, true, new BillingSyncConfigRequest(this.billingSyncKey) ); if (this.existingConnectionId == null) { this.formPromise = this.apiService.createOrganizationConnection( request, BillingSyncConfigApi ); } else { this.formPromise = this.apiService.updateOrganizationConnection( request, BillingSyncConfigApi, this.existingConnectionId ); } const response = (await this .formPromise) as OrganizationConnectionResponse; this.existingConnectionId = response?.id; this.billingSyncKey = response?.config?.billingSyncKey; this.setParentConnection(response); } catch (e) { this.logService.error(e); } } async deleteConnection() { this.formPromise = this.apiService.deleteOrganizationConnection(this.existingConnectionId); await this.formPromise; this.setParentConnection(null); } }