Skip to content

Commit

Permalink
Juntuchen/ops outgoing call (#31567)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR


### Issues associated with this PR


### Describe the problem that is addressed by this PR


### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
juntuchen-msft authored Nov 13, 2024
1 parent b2bb3e6 commit cf740a0
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class CallAutomationClient {

// @public
export interface CallAutomationClientOptions extends CommonClientOptions {
opsSourceIdentity?: MicrosoftTeamsAppIdentifier;
sourceIdentity?: CommunicationUserIdentifier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import type { InternalPipelineOptions } from "@azure/core-rest-pipeline";
import type {
CommunicationIdentifier,
CommunicationUserIdentifier,
MicrosoftTeamsAppIdentifier,
} from "@azure/communication-common";
import { parseClientArguments, isKeyCredential } from "@azure/communication-common";
import { logger } from "./models/logger";
import type {
AnswerCallRequest,
CallAutomationApiClient,
CommunicationUserIdentifierModel,
MicrosoftTeamsAppIdentifierModel,
CreateCallRequest,
RedirectCallRequest,
RejectCallRequest,
Expand All @@ -34,6 +36,7 @@ import {
communicationIdentifierModelConverter,
communicationUserIdentifierConverter,
communicationUserIdentifierModelConverter,
microsoftTeamsAppIdentifierModelConverter,
phoneNumberIdentifierConverter,
PhoneNumberIdentifierModelConverter,
} from "./utli/converters";
Expand All @@ -49,6 +52,11 @@ export interface CallAutomationClientOptions extends CommonClientOptions {
* The identifier of the source of the call for call creating/answering/inviting operation.
*/
sourceIdentity?: CommunicationUserIdentifier;
/**
* The identifier of the One Phone System bot for call creating operation.
* Should be mutually exclusive with sourceIdentity.
*/
opsSourceIdentity?: MicrosoftTeamsAppIdentifier;
}

/**
Expand All @@ -65,6 +73,7 @@ const isCallAutomationClientOptions = (options: any): options is CallAutomationC
export class CallAutomationClient {
private readonly callAutomationApiClient: CallAutomationApiClient;
private readonly sourceIdentity?: CommunicationUserIdentifierModel;
private readonly opsSourceIdentity?: MicrosoftTeamsAppIdentifierModel;
private readonly credential: TokenCredential | KeyCredential;
private readonly internalPipelineOptions: InternalPipelineOptions;
private readonly callAutomationEventProcessor: CallAutomationEventProcessor;
Expand Down Expand Up @@ -127,6 +136,7 @@ export class CallAutomationClient {
);

this.sourceIdentity = communicationUserIdentifierModelConverter(options.sourceIdentity);
this.opsSourceIdentity = microsoftTeamsAppIdentifierModelConverter(options.opsSourceIdentity);
}

/**
Expand Down Expand Up @@ -245,6 +255,7 @@ export class CallAutomationClient {
): Promise<CreateCallResult> {
const request: CreateCallRequest = {
source: this.sourceIdentity,
opsSource: this.opsSourceIdentity,
targets: [communicationIdentifierModelConverter(targetParticipant.targetParticipant)],
callbackUri: callbackUrl,
operationContext: options.operationContext,
Expand Down Expand Up @@ -277,6 +288,7 @@ export class CallAutomationClient {
): Promise<CreateCallResult> {
const request: CreateCallRequest = {
source: this.sourceIdentity,
opsSource: this.opsSourceIdentity,
targets: targetParticipants.map((target) => communicationIdentifierModelConverter(target)),
callbackUri: callbackUrl,
operationContext: options.operationContext,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
KnownCommunicationCloudEnvironmentModel,
PhoneNumberIdentifierModel,
CommunicationUserIdentifierModel,
MicrosoftTeamsAppIdentifierModel,
} from "../generated/src";
import { KnownCommunicationIdentifierModelKind } from "../generated/src";
import type { CallParticipant } from "../models/models";
Expand Down Expand Up @@ -221,3 +222,25 @@ export function communicationUserIdentifierConverter(

return { communicationUserId: identifier.id };
}

/** Convert MicrosoftTeamsAppIdentifier to MicrosoftTeamsAppIdentifierModel (Internal usage class) */
export function microsoftTeamsAppIdentifierModelConverter(
identifier: MicrosoftTeamsAppIdentifier | undefined,
): MicrosoftTeamsAppIdentifierModel | undefined {
if (!identifier || !identifier.teamsAppId) {
return undefined;
}

return { appId: identifier.teamsAppId };
}

/** Convert MicrosoftTeamsAppIdentifierModel to MicrosoftTeamsAppIdentifier (Public usage class) */
export function microsoftTeamsAppIdentifierConverter(
identifier: MicrosoftTeamsAppIdentifierModel | undefined,
): MicrosoftTeamsAppIdentifier | undefined {
if (!identifier || !identifier.appId) {
return undefined;
}

return { teamsAppId: identifier.appId };
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license-header: MICROSOFT_MIT_NO_VERSION
output-folder: ../src/generated
tag: package-2023-10-03-preview
require:
- https://github.com/Azure/azure-rest-api-specs/blob/156ff363e44f764ddd8a0a6adcd371610240ba15/specification/communication/data-plane/CallAutomation/readme.md
- https://github.com/Azure/azure-rest-api-specs/blob/be2a0fa68829fcb15c4e6b47aa6bc4bdd566c1cf/specification/communication/data-plane/CallAutomation/readme.md
package-version: 1.3.0-beta.1
model-date-time-as-string: false
optional-response-headers: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import type {
CommunicationIdentifier,
CommunicationUserIdentifier,
MicrosoftTeamsAppIdentifier,
} from "@azure/communication-common";
import { assert } from "chai";
import type { Context } from "mocha";
Expand All @@ -39,6 +40,17 @@ import type {
CreateCallEventResult,
} from "../src/eventprocessor/eventResponses";
import { randomUUID } from "@azure/core-util";
import { KnownCommunicationCloudEnvironmentModel } from "../src/generated/src";

function createOPSCallAutomationClient(
oPSSourceIdentity: MicrosoftTeamsAppIdentifier,
): CallAutomationClient {
const connectionString = "endpoint=https://redacted.communication.azure.com/;accesskey=redacted";

return new CallAutomationClient(connectionString, {
opsSourceIdentity: oPSSourceIdentity,
});
}

describe("Call Automation Client Unit Tests", () => {
let targets: CommunicationIdentifier[];
Expand Down Expand Up @@ -132,6 +144,57 @@ describe("Call Automation Client Unit Tests", () => {
.catch((error) => console.error(error));
});

it("CreateOPSCall", async () => {
// defined dummy variables
const appId = "28:acs:redacted";
const appCloud = KnownCommunicationCloudEnvironmentModel.Public;
const oPSSouceStub = {
teamsAppId: appId,
cloud: appCloud,
};

// stub an OPS CallAutomationClient
const createOPSClientStub = Sinon.stub().callsFake(() =>
createOPSCallAutomationClient(oPSSouceStub),
);

// Use the stubbed factory function to create the client
const oPSClient: SinonStubbedInstance<CallAutomationClient> & CallAutomationClient =
createOPSClientStub();

// Explicitly stub the createCall method
oPSClient.createCall = Sinon.stub();

// mocks
const createCallResultMock: CreateCallResult = {
callConnectionProperties: {
source: {
rawId: appId,
teamsAppId: appId,
cloud: appCloud,
} as MicrosoftTeamsAppIdentifier,
} as CallConnectionProperties,
callConnection: {} as CallConnection,
waitForEventProcessor: async () => {
return {} as CreateCallEventResult;
},
};

(oPSClient.createCall as Sinon.SinonStub).returns(Promise.resolve(createCallResultMock));

const promiseResult = oPSClient.createCall(target, CALL_CALLBACK_URL);

// asserts
promiseResult
.then((result: CreateCallResult) => {
assert.isNotNull(result);
assert.isTrue(oPSClient.createCall.calledWith(target, CALL_CALLBACK_URL));
assert.equal(result, createCallResultMock);
return;
})
.catch((error) => console.error(error));
});

it("AnswerCall", async () => {
// mocks
const answerCallResultMock: AnswerCallResult = {
Expand Down

0 comments on commit cf740a0

Please sign in to comment.