> For the complete documentation index, see [llms.txt](https://developer.dsquares.com/technical-design-document-tdd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.dsquares.com/technical-design-document-tdd/mazaya-rabbitmq-integration/reward-rabbitmq-consumer.md).

# Reward RabbitMQ Consumer

**Overview**

The `CustomerRewardConsumer` class is responsible for consuming messages from a RabbitMQ queue named `RewardQueue` and updating customer details based on the received messages.

**Constructor**

```csharp
public CustomerRewardConsumer(IOptions<RabbitMQCredentials> options, IServiceProvider services)
```

* **Parameters:**
  * `options`: An instance of `IOptions<RabbitMQCredentials>` used to retrieve RabbitMQ connection credentials.
  * `services`: An instance of `IServiceProvider` used for dependency injection.

**Properties**

```csharp
private readonly RabbitMQCredentials _rabbitMQCredentials;
public IServiceProvider _services { get; }
private readonly IModel _channel;
```

* **Description:**
  * `_rabbitMQCredentials`: Stores RabbitMQ connection credentials.
  * `_services`: Stores the service provider for dependency injection.
  * `_channel`: Represents the RabbitMQ channel for communication.

**ExecuteAsync Method**

```csharp
protected override Task ExecuteAsync(CancellationToken stoppingToken)
```

* **Description:**
  * Overrides the `ExecuteAsync` method of the `BackgroundService` class to consume messages from the RabbitMQ queue asynchronously.
* **Parameters:**
  * `stoppingToken`: A `CancellationToken` to monitor for cancellation requests.
* **Return Type:**
  * `Task`: Represents the asynchronous operation.

**Private Methods**

**UpdateCustomerDetails Method**

```csharp
private async Task UpdateCustomerDetails(string message)
```

* **Description:**
  * Parses the incoming message, updates customer details, and adds them to the database.
* **Parameters:**
  * `message`: The message received from the RabbitMQ queue.
* **Return Type:**
  * `Task`: Represents the asynchronous operation.

#### Example Usage

```csharp
// Configure RabbitMQ credentials
services.Configure<RabbitMQCredentials>(Configuration.GetSection("RabbitMQ"));

// Register CustomerRewardConsumer as a hosted service
services.AddHostedService<CustomerRewardConsumer>();
```

#### RabbitMQ Configuration (appsettings.json)

```json
{
  "RabbitMQ": {
    "HostName": "your_rabbitmq_host",
    "UserName": "your_username",
    "Password": "your_password"
  }
}
```

#### Notes

* This implementation assumes that there is a service named `ICustomerServices` registered in the service collection that handles customer-related operations.
* Messages are deserialized using JSON.NET (`Newtonsoft.Json`).
* The RabbitMQ queue name is hardcoded as `RabbitMQQueueNames.RewardQueue`.
* The RabbitMQ connection is not declared as durable and does not automatically delete the queue.
* The consumer automatically acknowledges received messages.
