# `SwooshCloudflare.Event`
[🔗](https://github.com/devaction-labs/swoosh_cloudflare/blob/v0.7.1/lib/swoosh_cloudflare/event.ex#L1)

A Cloudflare Email Sending lifecycle event, as published to a Cloudflare Queue by an
[event subscription](https://developers.cloudflare.com/email-service/reference/event-subscriptions/).

Use `parse/1` on the decoded message body (a map) or on the raw JSON string:

    {:ok, %SwooshCloudflare.Event{type: :bounced, recipient: "user@example.net"}} =
      SwooshCloudflare.Event.parse(json_or_map)

Use `SwooshCloudflare.Queues.pull/3` to receive these events over HTTP.

## Fields

  * `:type` - one of `:delivered`, `:deferred`, `:bounced`, `:failed`, `:rejected`,
    `:complained`, or `:unknown` for an event type this library does not know.
  * `:raw_type` - the type string as received (for example
    `"cf.email.sending.message.delivered"`).
  * `:event_id`, `:message_id`, `:sender`, `:recipient`, `:subject`, `:terminal` -
    from `payload` (`eventId`, `messageId`, ...). `:subject` is absent for some events.
  * `:delivery`, `:bounce`, `:failure`, `:rejection`, `:complaint` - the corresponding
    `payload` sub-maps with their original string keys (for example
    `%{"status" => "bounced", "smtpStatusCode" => "550"}`), or `nil` when absent.
  * `:metadata` - the `metadata` map (`accountId`, `eventSubscriptionId`,
    `eventSchemaVersion`, `eventTimestamp`) and `:source` - the `source` map
    (`type`, `zoneId`, `domain`).
  * `:timestamp` - `metadata.eventTimestamp` as a `DateTime`, or `nil`.
  * `:raw` - the full decoded map, so fields this library does not model are not lost.

## Correlating an event with a sent email

Events carry `payload.messageId` (for example `"0101018f7d0c4d9a-msg-deadbeef"`).
The Cloudflare documentation does not say whether this value equals the `message_id`
returned by the `/send` endpoint (the Swoosh `:id` in the delivery result), nor whether
it is wrapped in angle brackets like an RFC 5322 `Message-ID`. This is **UNVERIFIED**:
the REST API documentation shows no `message_id` in the `/send` response, and the
documented event examples use bare values without brackets. Compare a real `/send`
result with a real event before relying on it, and consider stripping `<` and `>`
from both sides when matching.

# `event_type`

```elixir
@type event_type() ::
  :delivered | :deferred | :bounced | :failed | :rejected | :complained
```

# `t`

```elixir
@type t() :: %SwooshCloudflare.Event{
  bounce: map() | nil,
  complaint: map() | nil,
  delivery: map() | nil,
  event_id: String.t() | nil,
  failure: map() | nil,
  message_id: String.t() | nil,
  metadata: map() | nil,
  raw: map(),
  raw_type: String.t(),
  recipient: String.t() | nil,
  rejection: map() | nil,
  sender: String.t() | nil,
  source: map() | nil,
  subject: String.t() | nil,
  terminal: boolean() | nil,
  timestamp: DateTime.t() | nil,
  type: event_type() | :unknown
}
```

# `parse`

```elixir
@spec parse(map() | binary()) :: {:ok, t()} | {:error, :invalid_json | :invalid_event}
```

Parses a decoded event map or a JSON string into an `Event`.

Returns `{:error, :invalid_json}` for a string that is not valid JSON and
`{:error, :invalid_event}` for anything that does not have the event envelope
(a `"type"` string and a `"payload"` map). Never raises.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
