# Jobs

Use jobs to deliver a notification or to require a recipient to complete an
email proof flow. Create a draft job after its message group has been prepared,
then activate it.

## 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.

## Routes

| Route | Required fields | Optional fields and behavior |
| --- | --- | --- |
| `POST /v1/members/job/create` | `project_id`; On creation: `purpose`, `proof_policy`; on update: `id` | `message_group`, `email`, `sms`, `metadata`, and draft `status`. |
| `POST /v1/members/job/get` | `project_id`; `id` | Returns the project-scoped job and its delivery requests. |
| `POST /v1/members/job/list` | `project_id`; no other fields | Filters: `id`, `message_group`, `proof_policy`, `purpose`, `status`, and shared pagination. |
| `POST /v1/members/job/activate` | `project_id`; `id` | Activates a draft job after its group, templates, and contacts pass activation rules. |
| `POST /v1/members/job/cancel` | `project_id`; `id` | Cancels a draft or active job. |
| `POST /v1/members/job/retry` | `project_id`; `id` | Creates a new draft clone of a completed, failed, or cancelled job. |

## Create or update a draft job

`/v1/members/job/create` creates a job when `id` is omitted. With a project-scoped
draft-job `id`, it updates that draft. A non-draft job cannot be changed by this
route.

### Request fields

| Field | Required | Type | Description |
| --- | --- | --- | --- |
| `project_id` | Yes | UUID | Authorized project scope for every request. |
| `id` | No | UUID | Existing project-scoped draft job to update. |
| `purpose` | Yes on create | String | The purpose shown to the recipient and used in message snapshots. An update may omit it to retain the existing value. |
| `proof_policy` | Yes on create | String | `single`, `any`, or `none`. An update may omit it to retain the existing value. See [activation](https://m7.org/docs/api/api.post.m7.org/jobs.md#activate-a-job). |
| `message_group` | No at creation; yes at activation | UUID or String | Project-scoped message-group ID or name. |
| `email` | No at creation; conditional at activation | String or array of strings | Recipient email addresses. A newline-separated string is also accepted. |
| `sms` | No at creation | String or array of strings | SMS contact values. A newline-separated string is also accepted. |
| `metadata` | No | Object or `null` | Application-defined job data. |
| `status` | No | String | Only `draft` is accepted on this route. New jobs are `draft`. |

Supplying `email` or `sms` replaces every contact method of that type on the
draft job. On an update, omitted job fields retain their existing values.

```bash
curl -sS https://api.post.service.m7.org/v1/members/job/create \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "project_id": "PROJECT_UUID",
    "purpose": "Confirm an email address",
    "proof_policy": "single",
    "message_group": "email-proof",
    "email": ["recipient@example.test"],
    "metadata": { "source": "signup" }
  }'
```

```json
{
  "status": 1,
  "comment": "OK",
  "data": {
    "created": true,
    "item": {
      "id": "JOB_UUID",
      "message_group": "MESSAGE_GROUP_UUID",
      "purpose": "Confirm an email address",
      "proof_policy": "single",
      "status": "draft",
      "email": ["recipient@example.test"],
      "sms": [],
      "checks": {
        "dne": null,
        "domain": null,
        "blocked": false
      }
    }
  }
}
```

Store `data.item.id`; activation and the proof flow use it.

## Read and list jobs

`/get` requires `id` and returns `data.item`. Along with the job fields shown
above, it returns `email_requests` for the job. `/list` returns the shared
paged object in `data`; each item includes `email`, `sms`, and `checks`.

`message_group` accepts an ID or a project-scoped message-group name when used as a
list filter. `status` accepts `draft`, `active`, `completed`, `failed`, or
`cancelled`; `all` removes the status filter. The legacy values `prepare` and
`complete` normalize to `draft` and `completed` respectively.

## Activate a job

`/v1/members/job/activate` requires `project_id` and `id`:

```json
{ "project_id": "PROJECT_UUID", "id": "JOB_UUID" }
```

Activation is allowed only for a `draft` job. The job must have a project-scoped
message group, and that group must contain a template for the delivery
channel. At least one contact method is required. Email values must be valid
email addresses.

| `proof_policy` | Activation result |
| --- | --- |
| `single` | Requires exactly one contact method, which must be email. The job becomes `active`; its email challenge is sent automatically. |
| `any` | Requires at least one contact method and an eligible email method. The job becomes `active`; the first eligible email method is armed. |
| `none` | Requires at least one email method. The notification is sent and the job becomes `completed`. |

The response places `activated: true` and the updated job in `data.item`.
For an `active` proof job, continue with the [proof flow](https://m7.org/docs/api/api.post.m7.org/proof.md).

## Cancel or retry

`/cancel` accepts a project-scoped job `id`. Only `draft` and `active` jobs can be
cancelled. Its response contains `data.cancelled: true` and the cancelled
`data.item`.

`/retry` accepts a project-scoped job `id`. Only `completed`, `failed`, and `cancelled`
jobs can be retried. It returns `data.retried: true` and a new `draft` job in
`data.item`; the new job has a new ID and carries the original contact methods.

## Errors and state

- A missing or out-of-project `id` returns `Job not found`.
- Editing or activating a job outside the allowed state fails with a conflict.
- Activation fails when its message group is missing, belongs to another
  project, has no eligible template, or its proof-policy contact rule is not
  met.
- A proof job remains `active` until [proof consumption](https://m7.org/docs/api/api.post.m7.org/proof.md#consume-a-proof)
  succeeds. Consumption changes it to `completed`.
