Skip to content

Commit

Permalink
[communication]-[sms] Fix opt out remove action (#32298)
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
phermanov-msft authored Dec 19, 2024
1 parent 2aa1f31 commit 3366272
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 8 deletions.
9 changes: 3 additions & 6 deletions sdk/communication/communication-sms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/communication-sms/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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://<resource-name>.communication.azure.com/;<access-key>";

// create new client
const client = new SmsClient(connectionString);

// construct send parameters
const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-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 = ["<to-phone-number-1>", "<to-phone-number-2>"];
}

// 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);
});
Original file line number Diff line number Diff line change
@@ -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://<resource-name>.communication.azure.com/;<access-key>";

// create new client
const client = new SmsClient(connectionString);

// construct send parameters
const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-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 = ["<to-phone-number-1>", "<to-phone-number-2>"];
}

// 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);
});
Original file line number Diff line number Diff line change
@@ -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://<resource-name>.communication.azure.com/;<access-key>";

// create new client
const client = new SmsClient(connectionString);

// construct send parameters
const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-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 = ["<to-phone-number-1>", "<to-phone-number-2>"];
}

// 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);
});
5 changes: 4 additions & 1 deletion sdk/communication/communication-sms/src/optOutsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ export class OptOutsClient {
): Promise<OptOutRemoveResult[]> {
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 {
Expand Down

0 comments on commit 3366272

Please sign in to comment.