# daemon-notifications-delivery

This application is the final step of [The Orchard Notification System](https://docs.google.com/document/d/1rX5Qxdc8Sm6cbIPgTF3BUJ8WuzfH1-j5kJfGp6l5Iyw/edit?usp=sharing)

## Requirements
* docker (with compose and buildX plugins)
* [AWS profiles](https://www.notion.so/0bb005a6ec2b499a8190682d121617e4?v=d6a9e7b57f6443758b08053661f947fd) for `Orchard Prod` `Orchard Dev` and `Shared Services`

## Running

Run the application:

To run the application in docker:
1. At least next variables should be set in `.env` file
    ```
     Environment
     SQS_QUEUE_URL
     POEDITOR_PROJECT_ID
     POEDITOR_API_TOKEN
    ```
2. Run
    ```bash
    make dev
    ```

To run the application in docker with datadog agent:
1. Set `DD_SITE` and `DD_API_KEY` environment variables
2. Run
    ```bash
    make dev/datadog
    ```


To send the message to the SQS for processing run
```bash
python qmail_queue.py
```

## Linting and Testing

Run linter and tests:

```bash
make pre-commit
```

## Getting translations

POEDITOR_API_TOKEN= POEDITOR_PROJECT_ID=202965 ./i18n.sh

## Sending a general email

To use the general email system, send an SQS message to the queue with the following required keys
"type": "GeneralNotification",
"subject": "Host Read Ad Assigned", # emali subject
"to": ["jodonnell@theorchard.com"], # list of email addresses to send to
"template": "host_read_ad_assigned", # template file in notifications_delivery/templates
"lang": "en", # localization languague
"variables": {
"key": "value" # whatever you send in here is passed to your template
}

```python
message = {
    "type": "GeneralNotification",
    "subject": "Host Read Ad Assigned",
    "to": ["jodonnell@theorchard.com"],
    "template": "host_read_ad_assigned",
    "lang": "en",
    # "sender": '"Sony Music Podcasts" <no-reply@sonymusic.com>',
    "sender": '"The Orchard Podcasts" <ratoui@dev.theorchard.io>',
    "variables": {
        "organization": "orchard",
        "from": "networkadmin@sonymusic.com",
        "podcast_title": "[PROD] Muschio Selvaggio",
        "episode_title": "Ep.14 Questa è la volta buona con Sdrumox Homyatol e Panetti",
        "episode_url": "https://podcast.qaorch.com/podcast/6536/episode/3322",
    },
}
sqs_client.send_message(
            QueueUrl=config.SQS_QUEUE_URL,
            MessageBody=json.dumps(message),
            DelaySeconds=0)

```

## Email Testing

To test an email, follow these steps:

1. Run the `email_preview.py` script like:

    ```bash
    python email_preview.py spike_detector
    ```

2. The `email_preview.py` has options to generate an HTML file and send the email to a specified email address using SMTP.

    _Note: You will need to use one of your own SMTP server to send the test email.<br>
    You can use a personal GMail [(instructions)](https://support.google.com/mail/answer/7126229?hl=en) or use a one-off service like [Ethereal](https://ethereal.email/)._

    ```bash
    python email_preview.py spike_detector -s
    ```

3. For now there are several types of supported templates:
   * `spike_detector`
   * `analytics_digest`

4. You can check all options by:
    ```bash
    python email_preview.py --help
    ```

5. It is highly recommended to test your generated emails in various email clients to ensure proper rendering. Consider testing in the following clients:

    - **Web client**: Use popular email web clients like Gmail.
    - **Mobile client**: Test on mobile email clients such as the iOS Mail app.
    - **Desktop client**: Verify compatibility with desktop clients like Outlook or Thunderbird.

    Note that testing emails solely in the browser is not sufficient. Email clients have different rendering capabilities, so it is important to use simpler "old school" CSS techniques instead of relying on modern CSS3 features like flexbox for better compatibility across clients.

#### "Real-time" development

You can achieve a nearly "real time" development experience while modifying the email templates by running

```bash
   while true; do python email_preview.py; sleep 2; done;
```

or you can install `entr` file watcher `brew install entr` and it will render (and send if specified) e-mail after each change in template
```bash
  find . -name '*.jinja' | entr python email_preview.py -b awal spike_detector
```

along with a browser/IDE extension that refreshes the page every time the file changes, like

-   Chrome: [Easy Auto Refresh](https://chrome.google.com/webstore/detail/easy-auto-refresh/aabcgdmkeabbnleenpncegpcngjpnjkc?hl=es)
-   VSCode's [LiveServer](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer).
