# Notifications Service

The Notifications Service is responsible for sending email notifications when domain invitation events occur in the Magistrala platform.

## Overview

This service listens to invitation events from the domains service and sends email notifications to users when:
- They are invited to join a domain (`invitation.send`)
- Someone accepts their domain invitation (`invitation.accept`)
- Someone rejects their domain invitation (`invitation.reject`)

The service fetches user information from Atom entities and sends styled email notifications using SMTP.

## Features

- **Event-Driven**: Listens to invitation events from the event store (NATS/RabbitMQ)
- **Atom Integration**: Fetches user details (name, email) from Atom
- **Beautiful Email Templates**: Styled HTML email templates with Magistrala branding (#083662)
- **Configurable**: Email server settings and templates are fully configurable

## Architecture

```
domains service → event store → notifications service → Atom
                                         ↓
                                    SMTP Server → Email Recipients
```

## Configuration

The service is configured using environment variables:

### General Configuration
- `MG_NOTIFICATIONS_LOG_LEVEL` - Log level (default: "info")
- `MG_NOTIFICATIONS_INSTANCE_ID` - Instance ID for the service
- `MG_NOTIFICATIONS_DOMAIN_ALT_NAME` - Alternative name for domains such as, say, workspaces or tenants (default: "domains")
- `MG_ES_URL` - Event store URL (default: "nats://localhost:4222")

### Email Configuration
- `MG_EMAIL_HOST` - SMTP server host (default: "localhost")
- `MG_EMAIL_PORT` - SMTP server port (default: "25")
- `MG_EMAIL_USERNAME` - SMTP username
- `MG_EMAIL_PASSWORD` - SMTP password
- `MG_EMAIL_FROM_ADDRESS` - From email address (default: "noreply@magistrala.com")
- `MG_EMAIL_FROM_NAME` - From name (default: "Magistrala Notifications")

### Template Configuration
- `MG_EMAIL_INVITATION_TEMPLATE` - Path to invitation email template
- `MG_EMAIL_ACCEPTANCE_TEMPLATE` - Path to acceptance email template
- `MG_EMAIL_REJECTION_TEMPLATE` - Path to rejection email template

### Atom Configuration
- `ATOM_URL` - Atom HTTP URL
- `ATOM_SERVICE_TOKEN` - Service bearer token, if provisioned
- `ATOM_SERVICE_USERNAME` / `ATOM_SERVICE_SECRET` - Service credential fallback
- `ATOM_ADMIN_USERNAME` / `ATOM_ADMIN_SECRET` - Admin credential fallback
- `ATOM_TIMEOUT` - Atom HTTP timeout

## Running the Service

```bash
go run cmd/notifications/main.go
```

Or build and run:

```bash
go build -o notifications cmd/notifications/main.go
./notifications
```

## Email Templates

The service includes three beautifully styled email templates with Magistrala branding:

1. **Invitation Sent** (`invitation-sent-email.tmpl`) - Blue gradient header (#083662)
2. **Invitation Accepted** (`invitation-accepted-email.tmpl`) - Green gradient header
3. **Invitation Rejected** (`invitation-rejected-email.tmpl`) - Red gradient header

All templates are responsive and include:
- Professional styling
- Gradient headers
- Clear call-to-action sections
- Magistrala branding

## Testing

Run the tests:

```bash
go test ./notifications/... -v
```

To run email integration tests (requires SMTP server):

```bash
MG_RUN_EMAIL_TESTS=true go test ./notifications/emailer -v
```

## Development

The service consists of:
- `notifier.go` - Main service interface
- `emailer/emailer.go` - Email notification implementation
- `events/consumer.go` - Event consumer for invitation events
- `cmd/notifications/main.go` - Service entry point
- Tests with mocks for unit testing

## Dependencies

- Atom - for fetching user information
- Event store (NATS/RabbitMQ) - for receiving invitation events
- SMTP server - for sending emails
