# Running a receiver import for Jonathan (`receivers:import-file`)

Card #5205. Agents may import receivers into a Nuhet account **only when Jonathan asks for that
import**, naming the company and the file (or a card that does). The command creates the same
kind of import as the import pages: the `ImportReceivers` job processes it, and the customer sees
it in their import list, report page and notice, as if they had uploaded it themselves.

## What the command does

```text
php artisan receivers:import-file <company id> <file> --email=<column> [options]
```

| Option | Meaning |
| --- | --- |
| `--email=`, `--phone=` | Column by header name (case-insensitive; an exact header name wins) or 0-based number. `--email` is required to commit: capacity is checked by address, so phone-only lists go through the import page. |
| `--list=<id>` / `--new-list="<name>"` | Existing list of that company, or a new list (refused if the company already has a list with that name). One of them is required with `--commit`. |
| `--prop="Column"` / `--prop="Column=Property"` | Fill an existing property of the company from a column. |
| `--new-prop="Column"` / `--new-prop="Column=New name"` | Create a property from a column (refused if the company already has a property with that name; use `--prop`). |
| `--overwrite` | Update receivers that already exist (the checkbox on the mapping page). Default off. |
| `--delimiter=`, `--no-headers` | CSV only. The delimiter is detected otherwise. `.xlsx` needs neither. |
| `--confirm-consent` | Confirms the addresses may receive newsletters from the company (the page's agreement). |
| `--note="<who, where>"` | Who asked and on which card. Saved with the import and logged. |
| `--commit` | Queue the import. **Without it the command is a dry run and changes nothing.** |

The dry run prints JSON: the sheet chosen (for `.xlsx`), headers (column numbers when the file has
no header row, so no receiver is printed), data rows, rows shorter than the mapping, the resolved
mapping, address counts (valid, blank, invalid line numbers in the converted file, duplicates,
already in the account, new) and the package capacity after the import. `--commit` is refused
without consent and a note, without a list or an e-mail column, when a row is shorter than the
mapping, when the company has no package, or when the import would exceed the package. Nothing is
written in those cases, and a commit that fails part-way removes its import and stored file.

Retries are only partly protected: a second `--commit` with the same `--new-list` or `--new-prop` is
refused, but a retry into an existing `--list` makes a second import. If the first run's result is
uncertain (for example an SSH timeout), read the company's newest imports and lists first; if the
import exists, do not run it again.

## Procedure

1. **Scope.** Confirm the request names the company (ID, checked against its name), the file and the
   list. Read the card. Do not infer consent: if it is unclear that the addresses may receive the
   company's newsletters, ask Jonathan.
2. **Stage the file.** `sftp_put` it to the `Nuhet` host under a private task folder, for example
   `/tmp/nuhet-import-<card>/`, then give `www-data` its own copy (run with sudo):
   `install -d -o www-data -g www-data -m 700 /tmp/nuhet-import-<card>-www` and
   `install -o www-data -g www-data -m 600 /tmp/nuhet-import-<card>/<file> /tmp/nuhet-import-<card>-www/`.
3. **Dry run** as the web user, from `/var/www/html`:
   `sudo -u www-data -- php artisan receivers:import-file 45310 /tmp/nuhet-import-4615-www/file.xlsx --email="E-postadress, primär" --new-list="Medlemmar"`
   Put the counts (not the addresses) in the card's agent comment. Report blockers such as capacity or
   unexpected columns to Jonathan instead of working around them.
4. **Commit** with the same arguments plus `--confirm-consent --note="Jonathan, card #NNNN" --commit`,
   only for the scope Jonathan approved. Each production command is approved on the confirm-required host.
5. **Check the result.** The job runs every minute. Open the printed `report_url` (Jonathan can) or read
   the import row (`imports.id`, `status` 4 = done, 5 = failed, 6 = package limit reached, json `created_reciever_count`, `updated_reciever_count`,
   `skipped_reciever_count`, `errors`). Compare with the dry run.
6. **Clean up** both staged folders and record the import ID, list and counts on the card.

## Notes

- Always run PHP on production as `www-data`. Files and logs created as another user break later imports.
- The command only filters rows by address validity. Splitting a file (for example by membership date)
  needs a prepared file per list, made from the original and kept with the card's evidence.
- There is no undo for a finished import. Once the job has published it (status 4), "Ta bort rapport"
  only removes the report: the receivers stay, because publishing clears their `import_id`. It removes
  receivers only for imports that were not published yet (waiting, queued, working, failed or
  stopped at the package limit).
  Removing receivers after a finished import is a separate, explicitly approved action for Jonathan
  to decide; do not improvise it.
- Staff users can have `override_company_id` set (acting as a customer). The command takes the company
  ID explicitly and does not depend on any user.
