# Policies

Policies organize two project-scoped email controls: DNE records for exact email addresses and domain rules for exact domains. Create a policy group first, then include its `group_id` or `group_name` on every child-route request.

## Authentication and project scope

Every route requires a Bearer access token for an active admitted `member`,
`user`, `admin`, or `root` principal, `project_id` (UUID), and the route's exact
capability through active membership in that project. Organization requests also
apply current verified Management grants. See [project access](https://m7.org/docs/api/api.post.m7.org/projects.md).
Resources and name lookups are scoped to the selected project. Do not send
`owner`, `project`, `principal_id`, or `actor_id` overrides.

## Policy groups

| Route | Required fields | Optional fields and behavior |
| --- | --- | --- |
| `POST /v1/members/policy/get` | `project_id`; `id` or `name` | `group_id` is an alias for `id`; `group_name` is an alias for `name`. |
| `POST /v1/members/policy/list` | `project_id`; no other fields | Exact filters: `id`, `name`; supports [shared pagination](https://m7.org/docs/api/api.post.m7.org/README.md#pagination). |
| `POST /v1/members/policy/save` | `project_id`; `name` | `id` updates a project-scoped group; `description` and `metadata` are optional. |
| `POST /v1/members/policy/delete` | `project_id`; `id` or `name` | Deletes the project-scoped group record. |

### Save a policy group

| Field | Required | Type | Description |
| --- | --- | --- | --- |
| `project_id` | Yes | UUID | Authorized project scope for every request. |
| `id` | No | UUID | Existing project-scoped group to update. |
| `name` | Yes | String | Stable project-scoped group name. |
| `description` | No | String or `null` | Optional description. |
| `metadata` | No | Object or `null` | Application-defined data. |

Save writes the complete editable record, so an omitted `description` or `metadata` clears that field on update.

```bash
curl -sS https://api.post.service.m7.org/v1/members/policy/save \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "project_id": "PROJECT_UUID",
    "name": "transactional-email",
    "description": "Rules for transactional recipients"
  }'
```

```json
{
  "status": 1,
  "comment": "OK",
  "data": {
    "saved": true,
    "item": {
      "id": "POLICY_GROUP_UUID",
      "name": "transactional-email",
      "description": "Rules for transactional recipients"
    }
  }
}
```

## Child policy scope

Every DNE and domain route requires one policy-group reference:

| Field | Required | Type | Behavior |
| --- | --- | --- | --- |
| `group_id` | Conditional | UUID | Uses a project-scoped policy-group ID. |
| `group_name` | Conditional | String | Resolves a project-scoped policy-group name to its ID. |

Send either field. If both are supplied, they must resolve to the same group or the request fails with a conflict. The API applies the resolved group and the selected project scope to every child lookup, including lookups by record ID.

## DNE records

A DNE record suppresses one exact email address in a policy group.

| Route | Required fields | Optional fields and behavior |
| --- | --- | --- |
| `POST /v1/members/policy/dne/get` | `project_id`; Group scope; `id` or `email` | Returns `data.item`, which is `null` when no scoped record matches. |
| `POST /v1/members/policy/dne/check` | `project_id`; Group scope; `id` or `email` | Returns `data.match` and `data.item`. |
| `POST /v1/members/policy/dne/list` | `project_id`; Group scope | Exact filters: `id`, `email`, `domain`, `type`, `source`, `reason_code`; supports [shared pagination](https://m7.org/docs/api/api.post.m7.org/README.md#pagination). |
| `POST /v1/members/policy/dne/save` | `project_id`; Group scope, `email` | Creates or updates the record scoped to that group and email. |
| `POST /v1/members/policy/dne/delete` | `project_id`; Group scope; `id` or `email` | Deletes the scoped record. |

### DNE save fields

| Field | Required | Type | Description |
| --- | --- | --- | --- |
| `project_id` | Yes | UUID | Authorized project scope for every request. |
| `email` | Yes | String | Email address. The API lowercases it and derives `domain`. |
| `type` | No | String | `soft` (default) or `hard`. |
| `source` | No | String | `manual` (default), `bounce`, `deferred`, `complaint`, `import`, or `system`. |
| `reason_code` | No | String or `null` | Machine-readable reason. |
| `reason` | No | String or `null` | Human-readable reason. |
| `metadata` | No | Object or `null` | Application-defined data. |

Save writes the complete editable DNE record. Omitted `type` and `source` use their defaults; omitted `reason_code`, `reason`, and `metadata` become `null` on update.

```bash
curl -sS https://api.post.service.m7.org/v1/members/policy/dne/save \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "project_id": "PROJECT_UUID",
    "group_name": "transactional-email",
    "email": "recipient@example.test",
    "type": "soft",
    "source": "manual",
    "reason_code": "user_request"
  }'
```

The response contains `data.saved: true` and `data.item`, including `id`, `email`, derived `domain`, `group`, `type`, and `source`.

## Domain policies

A domain policy stores an `allow` or `block` decision for one exact domain in a policy group.

| Route | Required fields | Optional fields and behavior |
| --- | --- | --- |
| `POST /v1/members/policy/domain/get` | `project_id`; Group scope; `id` or `domain` | Returns `data.item`, which is `null` when no scoped record matches. |
| `POST /v1/members/policy/domain/check` | `project_id`; Group scope; `id` or `domain` | Returns `data.match` and `data.item`. |
| `POST /v1/members/policy/domain/list` | `project_id`; Group scope | Exact filters: `id`, `domain`, `policy`, `include_subdomains`, `source`, `reason_code`; supports [shared pagination](https://m7.org/docs/api/api.post.m7.org/README.md#pagination). |
| `POST /v1/members/policy/domain/save` | `project_id`; Group scope, `domain`, `policy` | Creates or updates the record scoped to that group and domain. |
| `POST /v1/members/policy/domain/delete` | `project_id`; Group scope; `id` or `domain` | Deletes the scoped record. |

### Domain-policy save fields

| Field | Required | Type | Description |
| --- | --- | --- | --- |
| `project_id` | Yes | UUID | Authorized project scope for every request. |
| `domain` | Yes | String | Domain name without an `@` or spaces. The API lowercases it. |
| `policy` | Yes | String | `allow` or `block`. |
| `include_subdomains` | No | Boolean | Defaults to `true`; stored with the record. |
| `source` | No | String | `manual` (default), `import`, or `system`. |
| `reason_code` | No | String or `null` | Machine-readable reason. |
| `reason` | No | String or `null` | Human-readable reason. |
| `metadata` | No | Object or `null` | Application-defined data. |

Save writes the complete editable domain-policy record. Omitted `include_subdomains` defaults to `true`, `source` defaults to `manual`, and omitted `reason_code`, `reason`, and `metadata` become `null` on update.

```bash
curl -sS https://api.post.service.m7.org/v1/members/policy/domain/save \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "project_id": "PROJECT_UUID",
    "group_id": "POLICY_GROUP_UUID",
    "domain": "example.test",
    "policy": "block",
    "include_subdomains": true,
    "source": "manual"
  }'
```

The response contains `data.saved: true` and `data.item`, including `id`, `group`, `domain`, `policy`, and `include_subdomains`.

`get` and `check` match the exact requested `domain`. They do not expand a lookup to subdomains; use `include_subdomains` as the stored policy attribute.

## Errors and scope

- A missing, out-of-project, or mismatched group reference fails before the child operation.
- A child record ID from another group is treated as not found.
- `get` and `check` return a successful response with a `null` item or `match: false` when no scoped record exists; `delete` returns a not-found error in that case.
