From a3279d7805a8938b6317d43bcb1c0dd2c5f77d85 Mon Sep 17 00:00:00 2001 From: Orlando Watson Date: Fri, 5 May 2023 11:41:27 -0700 Subject: [PATCH] [Communication][Rooms] throttle participant operations in samples to avoid conflicts (#25762) ### 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) --- .../communication-rooms/README.md | 5 +- .../samples-dev/participantOperations.ts | 62 ++++++++++--- .../samples-dev/roomOperations.ts | 38 ++++++-- .../samples/v1-beta/javascript/package.json | 2 +- .../javascript/participantOperations.js | 73 ++++++++++++---- .../v1-beta/javascript/roomOperations.js | 60 ++++++------- .../samples/v1-beta/typescript/package.json | 2 +- .../typescript/src/participantOperations.ts | 87 ++++++++++++++----- .../v1-beta/typescript/src/roomOperations.ts | 64 +++++++------- .../samples/v1-beta/typescript/tsconfig.json | 2 +- 10 files changed, 272 insertions(+), 123 deletions(-) diff --git a/sdk/communication/communication-rooms/README.md b/sdk/communication/communication-rooms/README.md index 9556d06143ed..098d85411026 100644 --- a/sdk/communication/communication-rooms/README.md +++ b/sdk/communication/communication-rooms/README.md @@ -93,7 +93,8 @@ const roomsClient: RoomsClient = new RoomsClient(CONNECTION_STRING); const validFrom = new Date(Date.now()); let validForDays = 10; -let validUntil = validFrom.setDate(validFrom.getTime() + validForDays); +let validUntil = new Date(validFrom.getTime()); +validUntil.setDate(validFrom.getDate() + validForDays); // options payload to create a room const createRoomOptions: CreateRoomOptions = { @@ -119,7 +120,7 @@ To update the `validFrom` and `validUntil` settings of a room use the `updateRoo ```js validForDays = 60; -validUntil = validFrom.setDate(validFrom.getDate() + validForDays); +validUntil.setDate(validFrom.getDate() + validForDays); const updateRoomOptions: UpdateRoomOptions = { validFrom, validUntil, diff --git a/sdk/communication/communication-rooms/samples-dev/participantOperations.ts b/sdk/communication/communication-rooms/samples-dev/participantOperations.ts index 45f0a68a928d..fb77dab85681 100644 --- a/sdk/communication/communication-rooms/samples-dev/participantOperations.ts +++ b/sdk/communication/communication-rooms/samples-dev/participantOperations.ts @@ -19,6 +19,9 @@ import { PagedAsyncIterableIterator } from "@azure/core-paging"; dotenv.config(); export async function main() { + console.log("Room Participant Operations JavaScript Sample"); + console.log("_________________________________\n"); + const connectionString = process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] || "endpoint=https://.communication.azure.com/;"; @@ -27,6 +30,8 @@ export async function main() { const user1 = await identityClient.createUserAndToken(["voip"]); const user2 = await identityClient.createUserAndToken(["voip"]); + console.log("Creating room..."); + // create RoomsClient const roomsClient: RoomsClient = new RoomsClient(connectionString); @@ -48,7 +53,9 @@ export async function main() { // create a room with the request payload const createRoom = await roomsClient.createRoom(createRoomOptions); const roomId = createRoom.id; - console.log(`Created Room with ID ${roomId}`); + console.log(`Successfully created room with id: ${roomId}.`); + + console.log(`Adding new participant to room with id: ${roomId}...`); // request payload to add participants const addParticipantsList: RoomParticipantPatch[] = [ @@ -60,9 +67,16 @@ export async function main() { // add user2 to the room with the request payload await roomsClient.addOrUpdateParticipants(roomId, addParticipantsList); - const addedParticipants = await roomsClient.listParticipants(roomId); - console.log(`Added Participants`); - printParticipants(addedParticipants); + + console.log(`Successfully added participant to room with id: ${roomId}.`); + console.log("Printing participants in room..."); + + let participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log("Updating role of participant..."); // request payload to update user1 with a new role const updateParticipantsList: RoomParticipantPatch[] = [ @@ -74,8 +88,15 @@ export async function main() { // update user1 with the request payload await roomsClient.addOrUpdateParticipants(roomId, updateParticipantsList); - console.log(`Updated Participants`); - printParticipants(await roomsClient.listParticipants(roomId)); + console.log(`Successfully updated participant in room with id: ${roomId}.`); + console.log("Printing updated participants in room..."); + + participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log("Removing participant from room..."); // request payload to delete both users from the room // this demonstrates both objects that can be used in deleting users from rooms: RoomParticipant or CommunicationIdentifier @@ -83,25 +104,44 @@ export async function main() { // remove both users from the room with the request payload await roomsClient.removeParticipants(roomId, removeParticipantsList); - console.log(`Removed Participants`); - printParticipants(await roomsClient.listParticipants(roomId)); + console.log(`Successfully removed participant from room with id: ${roomId}.`); + + participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log(`Deleting room with id: ${roomId}...`); // deletes the room for cleanup await roomsClient.deleteRoom(roomId); + + console.log(`Successfully deleted room with id: ${roomId}.`); } /** * Outputs the participants within a Participantsn to console. - * @param pc - The Participants being printed to console. + * @param participants - The Participants being printed to console. */ async function printParticipants( participants: PagedAsyncIterableIterator> ): Promise { + var count = 0; for await (const participant of participants) { - const role = participant.role; - console.log(role); + if (participant) { + count++; + const { role, id } = participant; + console.log(`---Participant ${count}---`); + console.log(`Kind: ${id?.kind}`); + console.log(`Role: ${role}`); + } } } + +async function pause(time: number): Promise { + await new Promise((resolve) => setTimeout(resolve, time)); +} + main().catch((error) => { console.error("Encountered an error while sending request: ", error); process.exit(1); diff --git a/sdk/communication/communication-rooms/samples-dev/roomOperations.ts b/sdk/communication/communication-rooms/samples-dev/roomOperations.ts index ad5232c189dc..31ccd8d339e8 100644 --- a/sdk/communication/communication-rooms/samples-dev/roomOperations.ts +++ b/sdk/communication/communication-rooms/samples-dev/roomOperations.ts @@ -18,6 +18,9 @@ import * as dotenv from "dotenv"; dotenv.config(); export async function main() { + console.log("Room Operations JavaScript Sample"); + console.log("_________________________________\n\n"); + const connectionString = process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] || "endpoint=https://.communication.azure.com/;"; @@ -25,11 +28,14 @@ export async function main() { const identityClient = new CommunicationIdentityClient(connectionString); const user1 = await identityClient.createUserAndToken(["voip"]); + console.log("Creating room..."); + // create RoomsClient const roomsClient: RoomsClient = new RoomsClient(connectionString); var validFrom = new Date(Date.now()); - var validUntil = new Date(validFrom.getTime() + 5 * 60 * 1000); + var validForDays = 10; + var validUntil = addDays(validFrom, validForDays); // options payload to create a room const createRoomOptions: CreateRoomOptions = { @@ -46,30 +52,35 @@ export async function main() { // create a room with the request payload const createRoom = await roomsClient.createRoom(createRoomOptions); const roomId = createRoom.id; - console.log(`Created Room`); + console.log("Successfully created room with following properties:"); printRoom(createRoom); + console.log(`Fetching room with id: ${roomId}...`); + // retrieves the room with corresponding ID const getRoom = await roomsClient.getRoom(roomId); - console.log(`Retrieved Room with ID ${roomId}`); + console.log(`Successfully fetched room with id: ${roomId}. Room details:`); printRoom(getRoom); - validFrom.setTime(validUntil.getTime()); - validUntil.setTime(validFrom.getTime() + 5 * 60 * 1000); + console.log(`Updating room with id: ${roomId}...`); // request payload to update a room const updateRoomOptions: UpdateRoomOptions = { validFrom, - validUntil, + validUntil: addDays(validUntil, 10), }; // updates the specified room with the request payload const updateRoom = await roomsClient.updateRoom(roomId, updateRoomOptions); - console.log(`Updated Room`); + console.log(`Successfully updated room with id: ${roomId}. Room details:`); printRoom(updateRoom); + console.log(`Deleting room with id: ${roomId}...`); + // deletes the specified room await roomsClient.deleteRoom(roomId); + + console.log(`Successfully deleted room with id: ${roomId}.`); } /** @@ -79,7 +90,18 @@ export async function main() { function printRoom(room: CommunicationRoom): void { console.log(`Room ID: ${room.id}`); console.log(`Valid From: ${room.validFrom}`); - console.log(`Valid Until: ${room.validUntil}`); + console.log(`Valid Until: ${room.validUntil}\n\n`); +} + +/** + * Adds the specified ampunt of days + * @param current - The original date + * @param days - The number of days to add + */ +function addDays(current: Date, days: number): Date { + const copied = new Date(current.getTime()); + copied.setDate(copied.getDate() + days); + return copied; } main().catch((error) => { diff --git a/sdk/communication/communication-rooms/samples/v1-beta/javascript/package.json b/sdk/communication/communication-rooms/samples/v1-beta/javascript/package.json index 6ef9a97c234b..30294e5cd1ae 100644 --- a/sdk/communication/communication-rooms/samples/v1-beta/javascript/package.json +++ b/sdk/communication/communication-rooms/samples/v1-beta/javascript/package.json @@ -32,6 +32,6 @@ "@azure/communication-rooms": "next", "dotenv": "latest", "@azure/communication-identity": "^1.1.0-beta.2", - "@azure/communication-common": "^2.1.0" + "@azure/core-paging": "^1.5.0" } } diff --git a/sdk/communication/communication-rooms/samples/v1-beta/javascript/participantOperations.js b/sdk/communication/communication-rooms/samples/v1-beta/javascript/participantOperations.js index 91aec0f576b4..717cb918f240 100644 --- a/sdk/communication/communication-rooms/samples/v1-beta/javascript/participantOperations.js +++ b/sdk/communication/communication-rooms/samples/v1-beta/javascript/participantOperations.js @@ -7,12 +7,14 @@ const { RoomsClient } = require("@azure/communication-rooms"); const { CommunicationIdentityClient } = require("@azure/communication-identity"); -const { getIdentifierRawId } = require("@azure/communication-common"); // Load the .env file if it exists require("dotenv").config(); async function main() { + console.log("Room Participant Operations JavaScript Sample"); + console.log("_________________________________\n"); + const connectionString = process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] || "endpoint=https://.communication.azure.com/;"; @@ -21,6 +23,8 @@ async function main() { const user1 = await identityClient.createUserAndToken(["voip"]); const user2 = await identityClient.createUserAndToken(["voip"]); + console.log("Creating room..."); + // create RoomsClient const roomsClient = new RoomsClient(connectionString); @@ -42,7 +46,9 @@ async function main() { // create a room with the request payload const createRoom = await roomsClient.createRoom(createRoomOptions); const roomId = createRoom.id; - console.log(`Created Room with ID ${roomId}`); + console.log(`Successfully created room with id: ${roomId}.`); + + console.log(`Adding new participant to room with id: ${roomId}...`); // request payload to add participants const addParticipantsList = [ @@ -53,10 +59,17 @@ async function main() { ]; // add user2 to the room with the request payload - await roomsClient.addParticipants(roomId, addParticipantsList); - const addParticipants = await roomsClient.getParticipants(roomId); - console.log(`Added Participants`); - printParticipants(addParticipants); + await roomsClient.addOrUpdateParticipants(roomId, addParticipantsList); + + console.log(`Successfully added participant to room with id: ${roomId}.`); + console.log("Printing participants in room..."); + + let participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log("Updating role of participant..."); // request payload to update user1 with a new role const updateParticipantsList = [ @@ -67,9 +80,16 @@ async function main() { ]; // update user1 with the request payload - await roomsClient.updateParticipants(roomId, updateParticipantsList); - console.log(`Updated Participants`); - printParticipants(await roomsClient.getParticipants(roomId)); + await roomsClient.addOrUpdateParticipants(roomId, updateParticipantsList); + console.log(`Successfully updated participant in room with id: ${roomId}.`); + console.log("Printing updated participants in room..."); + + participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log("Removing participant from room..."); // request payload to delete both users from the room // this demonstrates both objects that can be used in deleting users from rooms: RoomParticipant or CommunicationIdentifier @@ -77,25 +97,42 @@ async function main() { // remove both users from the room with the request payload await roomsClient.removeParticipants(roomId, removeParticipantsList); - console.log(`Removed Participants`); - printParticipants(await roomsClient.getParticipants(roomId)); + console.log(`Successfully removed participant from room with id: ${roomId}.`); + + participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log(`Deleting room with id: ${roomId}...`); // deletes the room for cleanup await roomsClient.deleteRoom(roomId); + + console.log(`Successfully deleted room with id: ${roomId}.`); } /** * Outputs the participants within a Participantsn to console. - * @param pc - The Participants being printed to console. + * @param participants - The Participants being printed to console. */ -function printParticipants(participants) { - console.log(`Number of Participants: ${participants.length}`); - for (const participant of participants) { - const id = getIdentifierRawId(participant.id); - const role = participant.role; - console.log(`${id} - ${role}`); +async function printParticipants(participants) { + var count = 0; + for await (const participant of participants) { + if (participant) { + count++; + const { role, id } = participant; + console.log(`---Participant ${count}---`); + console.log(`Kind: ${id?.kind}`); + console.log(`Role: ${role}`); + } } } + +async function pause(time) { + await new Promise((resolve) => setTimeout(resolve, time)); +} + main().catch((error) => { console.error("Encountered an error while sending request: ", error); process.exit(1); diff --git a/sdk/communication/communication-rooms/samples/v1-beta/javascript/roomOperations.js b/sdk/communication/communication-rooms/samples/v1-beta/javascript/roomOperations.js index 94107fa9eaaa..ad61edb56f56 100644 --- a/sdk/communication/communication-rooms/samples/v1-beta/javascript/roomOperations.js +++ b/sdk/communication/communication-rooms/samples/v1-beta/javascript/roomOperations.js @@ -7,25 +7,29 @@ const { RoomsClient } = require("@azure/communication-rooms"); const { CommunicationIdentityClient } = require("@azure/communication-identity"); -const { getIdentifierRawId } = require("@azure/communication-common"); // Load the .env file if it exists require("dotenv").config(); async function main() { + console.log("Room Operations JavaScript Sample"); + console.log("_________________________________\n\n"); + const connectionString = process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] || "endpoint=https://.communication.azure.com/;"; const identityClient = new CommunicationIdentityClient(connectionString); const user1 = await identityClient.createUserAndToken(["voip"]); - const user2 = await identityClient.createUserAndToken(["voip"]); + + console.log("Creating room..."); // create RoomsClient const roomsClient = new RoomsClient(connectionString); var validFrom = new Date(Date.now()); - var validUntil = new Date(validFrom.getTime() + 5 * 60 * 1000); + var validForDays = 10; + var validUntil = addDays(validFrom, validForDays); // options payload to create a room const createRoomOptions = { @@ -42,41 +46,35 @@ async function main() { // create a room with the request payload const createRoom = await roomsClient.createRoom(createRoomOptions); const roomId = createRoom.id; - console.log(`Created Room`); + console.log("Successfully created room with following properties:"); printRoom(createRoom); + console.log(`Fetching room with id: ${roomId}...`); + // retrieves the room with corresponding ID const getRoom = await roomsClient.getRoom(roomId); - console.log(`Retrieved Room with ID ${roomId}`); + console.log(`Successfully fetched room with id: ${roomId}. Room details:`); printRoom(getRoom); - validFrom.setTime(validUntil.getTime()); - validUntil.setTime(validFrom.getTime() + 5 * 60 * 1000); + console.log(`Updating room with id: ${roomId}...`); // request payload to update a room const updateRoomOptions = { - validFrom: validFrom, - validUntil: validUntil, - roomJoinPolicy: "CommunicationServiceUsers", - participants: [ - { - id: user1.user, - role: "Consumer", - }, - { - id: user2.user, - role: "Presenter", - }, - ], + validFrom, + validUntil: addDays(validUntil, 10), }; // updates the specified room with the request payload const updateRoom = await roomsClient.updateRoom(roomId, updateRoomOptions); - console.log(`Updated Room`); + console.log(`Successfully updated room with id: ${roomId}. Room details:`); printRoom(updateRoom); + console.log(`Deleting room with id: ${roomId}...`); + // deletes the specified room await roomsClient.deleteRoom(roomId); + + console.log(`Successfully deleted room with id: ${roomId}.`); } /** @@ -86,14 +84,18 @@ async function main() { function printRoom(room) { console.log(`Room ID: ${room.id}`); console.log(`Valid From: ${room.validFrom}`); - console.log(`Valid Until: ${room.validUntil}`); - console.log(`Room Join Policy: ${room.joinPolicy}`); - console.log(`Participants:`); - for (const participant of room.participants) { - const id = getIdentifierRawId(participant.id); - const role = participant.role; - console.log(`${id} - ${role}`); - } + console.log(`Valid Until: ${room.validUntil}\n\n`); +} + +/** + * Adds the specified ampunt of days + * @param current - The original date + * @param days - The number of days to add + */ +function addDays(current, days) { + const copied = new Date(current.getTime()); + copied.setDate(copied.getDate() + days); + return copied; } main().catch((error) => { diff --git a/sdk/communication/communication-rooms/samples/v1-beta/typescript/package.json b/sdk/communication/communication-rooms/samples/v1-beta/typescript/package.json index d5312ee04fe8..207b3397756f 100644 --- a/sdk/communication/communication-rooms/samples/v1-beta/typescript/package.json +++ b/sdk/communication/communication-rooms/samples/v1-beta/typescript/package.json @@ -36,7 +36,7 @@ "@azure/communication-rooms": "next", "dotenv": "latest", "@azure/communication-identity": "^1.1.0-beta.2", - "@azure/communication-common": "^2.1.0" + "@azure/core-paging": "^1.5.0" }, "devDependencies": { "@types/node": "^14.0.0", diff --git a/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/participantOperations.ts b/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/participantOperations.ts index de79dad02227..fb77dab85681 100644 --- a/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/participantOperations.ts +++ b/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/participantOperations.ts @@ -5,15 +5,23 @@ * @summary Perform participant operations using the RoomsClient. */ -import { RoomsClient, RoomParticipant, CreateRoomOptions } from "@azure/communication-rooms"; +import { + RoomsClient, + RoomParticipantPatch, + CreateRoomOptions, + RoomParticipant, +} from "@azure/communication-rooms"; import { CommunicationIdentityClient } from "@azure/communication-identity"; -import { getIdentifierRawId } from "@azure/communication-common"; // Load the .env file if it exists import * as dotenv from "dotenv"; +import { PagedAsyncIterableIterator } from "@azure/core-paging"; dotenv.config(); export async function main() { + console.log("Room Participant Operations JavaScript Sample"); + console.log("_________________________________\n"); + const connectionString = process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] || "endpoint=https://.communication.azure.com/;"; @@ -22,6 +30,8 @@ export async function main() { const user1 = await identityClient.createUserAndToken(["voip"]); const user2 = await identityClient.createUserAndToken(["voip"]); + console.log("Creating room..."); + // create RoomsClient const roomsClient: RoomsClient = new RoomsClient(connectionString); @@ -43,10 +53,12 @@ export async function main() { // create a room with the request payload const createRoom = await roomsClient.createRoom(createRoomOptions); const roomId = createRoom.id; - console.log(`Created Room with ID ${roomId}`); + console.log(`Successfully created room with id: ${roomId}.`); + + console.log(`Adding new participant to room with id: ${roomId}...`); // request payload to add participants - const addParticipantsList: RoomParticipant[] = [ + const addParticipantsList: RoomParticipantPatch[] = [ { id: user2.user, role: "Consumer", @@ -54,13 +66,20 @@ export async function main() { ]; // add user2 to the room with the request payload - await roomsClient.addParticipants(roomId, addParticipantsList); - const addParticipants = await roomsClient.getParticipants(roomId); - console.log(`Added Participants`); - printParticipants(addParticipants); + await roomsClient.addOrUpdateParticipants(roomId, addParticipantsList); + + console.log(`Successfully added participant to room with id: ${roomId}.`); + console.log("Printing participants in room..."); + + let participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log("Updating role of participant..."); // request payload to update user1 with a new role - const updateParticipantsList: RoomParticipant[] = [ + const updateParticipantsList: RoomParticipantPatch[] = [ { id: user1.user, role: "Presenter", @@ -68,9 +87,16 @@ export async function main() { ]; // update user1 with the request payload - await roomsClient.updateParticipants(roomId, updateParticipantsList); - console.log(`Updated Participants`); - printParticipants(await roomsClient.getParticipants(roomId)); + await roomsClient.addOrUpdateParticipants(roomId, updateParticipantsList); + console.log(`Successfully updated participant in room with id: ${roomId}.`); + console.log("Printing updated participants in room..."); + + participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log("Removing participant from room..."); // request payload to delete both users from the room // this demonstrates both objects that can be used in deleting users from rooms: RoomParticipant or CommunicationIdentifier @@ -78,25 +104,44 @@ export async function main() { // remove both users from the room with the request payload await roomsClient.removeParticipants(roomId, removeParticipantsList); - console.log(`Removed Participants`); - printParticipants(await roomsClient.getParticipants(roomId)); + console.log(`Successfully removed participant from room with id: ${roomId}.`); + + participantsInRoom = await roomsClient.listParticipants(roomId); + await printParticipants(participantsInRoom); + + await pause(500); + + console.log(`Deleting room with id: ${roomId}...`); // deletes the room for cleanup await roomsClient.deleteRoom(roomId); + + console.log(`Successfully deleted room with id: ${roomId}.`); } /** * Outputs the participants within a Participantsn to console. - * @param pc - The Participants being printed to console. + * @param participants - The Participants being printed to console. */ -function printParticipants(participants: RoomParticipant[]): void { - console.log(`Number of Participants: ${participants.length}`); - for (const participant of participants) { - const id = getIdentifierRawId(participant.id); - const role = participant.role; - console.log(`${id} - ${role}`); +async function printParticipants( + participants: PagedAsyncIterableIterator> +): Promise { + var count = 0; + for await (const participant of participants) { + if (participant) { + count++; + const { role, id } = participant; + console.log(`---Participant ${count}---`); + console.log(`Kind: ${id?.kind}`); + console.log(`Role: ${role}`); + } } } + +async function pause(time: number): Promise { + await new Promise((resolve) => setTimeout(resolve, time)); +} + main().catch((error) => { console.error("Encountered an error while sending request: ", error); process.exit(1); diff --git a/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/roomOperations.ts b/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/roomOperations.ts index 1cc1bf423a84..31ccd8d339e8 100644 --- a/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/roomOperations.ts +++ b/sdk/communication/communication-rooms/samples/v1-beta/typescript/src/roomOperations.ts @@ -7,31 +7,35 @@ import { RoomsClient, - Room, + CommunicationRoom, CreateRoomOptions, UpdateRoomOptions, } from "@azure/communication-rooms"; import { CommunicationIdentityClient } from "@azure/communication-identity"; -import { getIdentifierRawId } from "@azure/communication-common"; // Load the .env file if it exists import * as dotenv from "dotenv"; dotenv.config(); export async function main() { + console.log("Room Operations JavaScript Sample"); + console.log("_________________________________\n\n"); + const connectionString = process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] || "endpoint=https://.communication.azure.com/;"; const identityClient = new CommunicationIdentityClient(connectionString); const user1 = await identityClient.createUserAndToken(["voip"]); - const user2 = await identityClient.createUserAndToken(["voip"]); + + console.log("Creating room..."); // create RoomsClient const roomsClient: RoomsClient = new RoomsClient(connectionString); var validFrom = new Date(Date.now()); - var validUntil = new Date(validFrom.getTime() + 5 * 60 * 1000); + var validForDays = 10; + var validUntil = addDays(validFrom, validForDays); // options payload to create a room const createRoomOptions: CreateRoomOptions = { @@ -48,58 +52,56 @@ export async function main() { // create a room with the request payload const createRoom = await roomsClient.createRoom(createRoomOptions); const roomId = createRoom.id; - console.log(`Created Room`); + console.log("Successfully created room with following properties:"); printRoom(createRoom); + console.log(`Fetching room with id: ${roomId}...`); + // retrieves the room with corresponding ID const getRoom = await roomsClient.getRoom(roomId); - console.log(`Retrieved Room with ID ${roomId}`); + console.log(`Successfully fetched room with id: ${roomId}. Room details:`); printRoom(getRoom); - validFrom.setTime(validUntil.getTime()); - validUntil.setTime(validFrom.getTime() + 5 * 60 * 1000); + console.log(`Updating room with id: ${roomId}...`); // request payload to update a room const updateRoomOptions: UpdateRoomOptions = { - validFrom: validFrom, - validUntil: validUntil, - roomJoinPolicy: "CommunicationServiceUsers", - participants: [ - { - id: user1.user, - role: "Consumer", - }, - { - id: user2.user, - role: "Presenter", - }, - ], + validFrom, + validUntil: addDays(validUntil, 10), }; // updates the specified room with the request payload const updateRoom = await roomsClient.updateRoom(roomId, updateRoomOptions); - console.log(`Updated Room`); + console.log(`Successfully updated room with id: ${roomId}. Room details:`); printRoom(updateRoom); + console.log(`Deleting room with id: ${roomId}...`); + // deletes the specified room await roomsClient.deleteRoom(roomId); + + console.log(`Successfully deleted room with id: ${roomId}.`); } /** * Outputs the details of a Room to console. * @param room - The Room being printed to console. */ -function printRoom(room: Room): void { +function printRoom(room: CommunicationRoom): void { console.log(`Room ID: ${room.id}`); console.log(`Valid From: ${room.validFrom}`); - console.log(`Valid Until: ${room.validUntil}`); - console.log(`Room Join Policy: ${room.joinPolicy}`); - console.log(`Participants:`); - for (const participant of room.participants!) { - const id = getIdentifierRawId(participant.id); - const role = participant.role; - console.log(`${id} - ${role}`); - } + console.log(`Valid Until: ${room.validUntil}\n\n`); +} + +/** + * Adds the specified ampunt of days + * @param current - The original date + * @param days - The number of days to add + */ +function addDays(current: Date, days: number): Date { + const copied = new Date(current.getTime()); + copied.setDate(copied.getDate() + days); + return copied; } main().catch((error) => { diff --git a/sdk/communication/communication-rooms/samples/v1-beta/typescript/tsconfig.json b/sdk/communication/communication-rooms/samples/v1-beta/typescript/tsconfig.json index 416c2dd82e00..e26ce2a6d8f7 100644 --- a/sdk/communication/communication-rooms/samples/v1-beta/typescript/tsconfig.json +++ b/sdk/communication/communication-rooms/samples/v1-beta/typescript/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "ES2018", + "target": "ES2020", "module": "commonjs", "moduleResolution": "node", "resolveJsonModule": true,