From 3366272697803383ec4a2687011dc6c886180fbf Mon Sep 17 00:00:00 2001 From: Pavlo Hermanov <117445225+phermanov-msft@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:46:31 +0200 Subject: [PATCH] [communication]-[sms] Fix opt out remove action (#32298) ### Packages impacted by this PR communication-sms ### Issues associated with this PR ### Describe the problem that is addressed by this PR Fix the error in optOut.remove method ### 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-sms/CHANGELOG.md | 9 +-- .../communication-sms/assets.json | 2 +- .../samples/v1/typescript/README.md | 6 ++ .../samples/v1/typescript/src/optOutAdd.ts | 57 +++++++++++++++++++ .../samples/v1/typescript/src/optOutCheck.ts | 57 +++++++++++++++++++ .../samples/v1/typescript/src/optOutRemove.ts | 57 +++++++++++++++++++ .../communication-sms/src/optOutsClient.ts | 5 +- 7 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 sdk/communication/communication-sms/samples/v1/typescript/src/optOutAdd.ts create mode 100644 sdk/communication/communication-sms/samples/v1/typescript/src/optOutCheck.ts create mode 100644 sdk/communication/communication-sms/samples/v1/typescript/src/optOutRemove.ts diff --git a/sdk/communication/communication-sms/CHANGELOG.md b/sdk/communication/communication-sms/CHANGELOG.md index f34fe3cc195f..43d2f563c639 100644 --- a/sdk/communication/communication-sms/CHANGELOG.md +++ b/sdk/communication/communication-sms/CHANGELOG.md @@ -1,14 +1,11 @@ # Release History -## 1.2.0-beta.3 (Unreleased) - -### Features Added - -### Breaking Changes +## 1.2.0-beta.3 (2024-12-19) ### Bugs Fixed -### Other Changes +- Fixed Opt Out Remove action + ## 1.2.0-beta.2 (2024-12-10) diff --git a/sdk/communication/communication-sms/assets.json b/sdk/communication/communication-sms/assets.json index 58fc59782111..994158f9d237 100644 --- a/sdk/communication/communication-sms/assets.json +++ b/sdk/communication/communication-sms/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "js", "TagPrefix": "js/communication/communication-sms", - "Tag": "js/communication/communication-sms_128752023a" + "Tag": "js/communication/communication-sms_49df303bfc" } diff --git a/sdk/communication/communication-sms/samples/v1/typescript/README.md b/sdk/communication/communication-sms/samples/v1/typescript/README.md index 344a4ee8b510..e4a2d007829e 100644 --- a/sdk/communication/communication-sms/samples/v1/typescript/README.md +++ b/sdk/communication/communication-sms/samples/v1/typescript/README.md @@ -17,6 +17,9 @@ These sample programs show how to use the TypeScript client libraries for Azure | [sendSms.ts][sendsms] | Send an SMS message to 1 or more recipients | | [sendSmsWithOptions.ts][sendsmswithoptions] | Configure SMS options when sending a message | | [usingAadAuth.ts][usingaadauth] | Use AAD token credentials when sending a SMS message. | +| [optOutCheck.ts][optoutcheck] | Check if recipients opted out of receiving messages | +| [optOutAdd.ts][optoutadd] | Opt out recipients from receiving messages | +| [optOutRemove.ts][optoutremove] | Remove recipients from Opt Out list | ## Prerequisites @@ -78,3 +81,6 @@ Take a look at our [API Documentation][apiref] for more information about the AP [createinstance_azurecommunicationservicesaccount]: https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource [package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/communication/communication-sms/README.md [typescript]: https://www.typescriptlang.org/docs/home.html +[optoutcheck]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-sms/samples/v1/typescript/src/optOutCheck.ts +[optoutadd]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-sms/samples/v1/typescript/src/optOutAdd.ts +[optoutremove]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-sms/samples/v1/typescript/src/optOutRemove.ts diff --git a/sdk/communication/communication-sms/samples/v1/typescript/src/optOutAdd.ts b/sdk/communication/communication-sms/samples/v1/typescript/src/optOutAdd.ts new file mode 100644 index 000000000000..d19d810944f3 --- /dev/null +++ b/sdk/communication/communication-sms/samples/v1/typescript/src/optOutAdd.ts @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Opt out 1 or more recipients from receiving SMS messages + */ + +import { SmsClient } from "@azure/communication-sms"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + console.log("== Opt Out Add =="); + + // You will need to set this environment variable or edit the following values + const connectionString = + process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING || + "endpoint=https://.communication.azure.com/;"; + + // create new client + const client = new SmsClient(connectionString); + + // construct send parameters + const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || ""; + let phoneNumbers: string[]; + if (process.env.TO_PHONE_NUMBERS !== undefined) { + phoneNumbers = process.env.TO_PHONE_NUMBERS.split(","); + } else if (process.env.AZURE_PHONE_NUMBER !== undefined) { + phoneNumbers = [process.env.AZURE_PHONE_NUMBER]; + } else { + phoneNumbers = ["", ""]; + } + + // send add opt out request + const optOutAddResults = await client.optOuts.add( + from, + phoneNumbers); + + // individual requests can encounter errors during sending + // use the "httpStatusCode" property to verify + for (const optOutAddResult of optOutAddResults) { + if (optOutAddResult.httpStatusCode == 200) { + console.log("Success: ", optOutAddResult); + } else { + console.error("Something went wrong when trying to send opt out add request: ", optOutAddResult); + } + } + + console.log("== Done: Opt Out Add =="); +} + +main().catch((error) => { + console.error("Encountered an error while sending opt out add request: ", error); + process.exit(1); +}); diff --git a/sdk/communication/communication-sms/samples/v1/typescript/src/optOutCheck.ts b/sdk/communication/communication-sms/samples/v1/typescript/src/optOutCheck.ts new file mode 100644 index 000000000000..cb7d80329ea2 --- /dev/null +++ b/sdk/communication/communication-sms/samples/v1/typescript/src/optOutCheck.ts @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Check if 1 or more recipients are opted out of receiving SMS messages + */ + +import { SmsClient } from "@azure/communication-sms"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + console.log("== Opt Out Check =="); + + // You will need to set this environment variable or edit the following values + const connectionString = + process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING || + "endpoint=https://.communication.azure.com/;"; + + // create new client + const client = new SmsClient(connectionString); + + // construct send parameters + const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || ""; + let phoneNumbers: string[]; + if (process.env.TO_PHONE_NUMBERS !== undefined) { + phoneNumbers = process.env.TO_PHONE_NUMBERS.split(","); + } else if (process.env.AZURE_PHONE_NUMBER !== undefined) { + phoneNumbers = [process.env.AZURE_PHONE_NUMBER]; + } else { + phoneNumbers = ["", ""]; + } + + // send check opt out request + const optOutCheckResults = await client.optOuts.check( + from, + phoneNumbers); + + // individual requests can encounter errors during sending + // use the "httpStatusCode" property to verify + for (const optOutCheckResult of optOutCheckResults) { + if (optOutCheckResult.httpStatusCode == 200) { + console.log("Success: ", optOutCheckResult); + } else { + console.error("Something went wrong when trying to send opt out check request: ", optOutCheckResult); + } + } + + console.log("== Done: Opt Out Check =="); +} + +main().catch((error) => { + console.error("Encountered an error while sending Opt Out Check request: ", error); + process.exit(1); +}); diff --git a/sdk/communication/communication-sms/samples/v1/typescript/src/optOutRemove.ts b/sdk/communication/communication-sms/samples/v1/typescript/src/optOutRemove.ts new file mode 100644 index 000000000000..86dce22d3c99 --- /dev/null +++ b/sdk/communication/communication-sms/samples/v1/typescript/src/optOutRemove.ts @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Remove 1 or more recipients from Opt Out list + */ + +import { SmsClient } from "@azure/communication-sms"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + console.log("== Opt Out Remove =="); + + // You will need to set this environment variable or edit the following values + const connectionString = + process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING || + "endpoint=https://.communication.azure.com/;"; + + // create new client + const client = new SmsClient(connectionString); + + // construct send parameters + const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || ""; + let phoneNumbers: string[]; + if (process.env.TO_PHONE_NUMBERS !== undefined) { + phoneNumbers = process.env.TO_PHONE_NUMBERS.split(","); + } else if (process.env.AZURE_PHONE_NUMBER !== undefined) { + phoneNumbers = [process.env.AZURE_PHONE_NUMBER]; + } else { + phoneNumbers = ["", ""]; + } + + // send add opt out request + const optOutRemoveResults = await client.optOuts.remove( + from, + phoneNumbers); + + // individual requests can encounter errors during sending + // use the "httpStatusCode" property to verify + for (const optOutRemoveResult of optOutRemoveResults) { + if (optOutRemoveResult.httpStatusCode == 200) { + console.log("Success: ", optOutRemoveResult); + } else { + console.error("Something went wrong when trying to send opt out remove request: ", optOutRemoveResult); + } + } + + console.log("== Done: Opt Out Remove =="); +} + +main().catch((error) => { + console.error("Encountered an error while sending opt out remove request: ", error); + process.exit(1); +}); diff --git a/sdk/communication/communication-sms/src/optOutsClient.ts b/sdk/communication/communication-sms/src/optOutsClient.ts index 53ba488551aa..10829a2f59f6 100644 --- a/sdk/communication/communication-sms/src/optOutsClient.ts +++ b/sdk/communication/communication-sms/src/optOutsClient.ts @@ -106,7 +106,10 @@ export class OptOutsClient { ): Promise { const { operationOptions } = extractOperationOptions(options); return tracingClient.withSpan("OptOuts-Remove", operationOptions, async (updatedOptions) => { - const response = await this.api.optOuts.add(generateOptOutRequest(from, to), updatedOptions); + const response = await this.api.optOuts.remove( + generateOptOutRequest(from, to), + updatedOptions, + ); return response.value.map((optOutResponseItem: OptOutResponseItem) => { return {