# Triggered Sends Lambda

This Lambda function submits triggered sends to the Marketing Cloud API based on fan campaign form responses.

## Logic

### Trigger
The Lambda function is triggered by an S3 event when a new file containing Triggered Send information is uploaded to the S3 bucket.

### S3 File Description

The file contains the triggered send information grouped by the business unit ID and the triggered send definition key. The file is in NDJSON format, where each line is a JSON object with the following structure:

```json
{
  "business_unit_id": "12345",
  "triggered_send_definition_key": "example-key",
  "recipients": [
    {
      "email_address": "user1@example.com",
      "profile_uuid": "uuid-1",
      "source_event_id": "event-1",
      "first_name": "John",
      "subscriber_key": "subscriber-key-1",
      "last_name": "Doe",
      "postal_code": "12345"
    },
    {
      "email_address": "user2@example.com",
      "profile_uuid": "uuid-2",
      "source_event_id": "event-2",
      "first_name": "Jane",
      "subscriber_key": "subscriber-key-2",
      "last_name": "Smith",
      "postal_code": "67890"
    }
  ]
}
```

### Marketing Cloud API

The Marketing Cloud API uses OAuth 2.0 for authentication. Also, the API requirement is to issue access tokens based on the account id. In order to reuse the tokens across multiple API calls, the Lambda function caches the tokens in a Redis instance.

The Lambda uses batch endpoint for sending triggered sends. Unfortunately there is no documentation for this endpoint, but it works quite similar to the [regular triggered send endpoint.](https://developer.salesforce.com/docs/marketing/marketing-cloud/references/mc_rest_messaging/messageDefinitionSends.html)

### Audit Trail

The lambda submits results of all attempts (successful and not) of triggered send processing to Kafka topics. This data is synchronised to Snowflake and is used for monitoring and retries if needed.  

### Error Handling

The lambda logic implies catching all type of the failures for further processing.
