84 lines
3.9 KiB
Markdown
84 lines
3.9 KiB
Markdown
# Agrarmonitor Connector
|
|
|
|
TypeScript MVP connector for Agrarmonitor with shared cookie persistence, optional AES-GCM cookie encryption, automatic login, retry after expired sessions, device registration checks, and customer detail extraction.
|
|
|
|
Login uses Agrarmonitor's redirect flow only: the connector loads `/`, extracts the `nonce`, and posts credentials to `/redirect.php?id=benutzerverwaltung&action=login`.
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
## Build
|
|
|
|
```bash
|
|
npm run build
|
|
```
|
|
|
|
## Example
|
|
|
|
```ts
|
|
import {
|
|
AesGcmCookieEncryptor,
|
|
FileCookieStore,
|
|
createAgrarmonitorClient,
|
|
} from 'agrarmonitor-connector';
|
|
|
|
const agrarmonitor = await createAgrarmonitorClient({
|
|
baseUrl: 'https://admin7.agrarmonitor.de',
|
|
apiToken: process.env.AGRARMONITOR_API_TOKEN,
|
|
username: process.env.AGRARMONITOR_USERNAME ?? '',
|
|
password: process.env.AGRARMONITOR_PASSWORD ?? '',
|
|
cookieStore: new FileCookieStore(
|
|
process.env.AGRARMONITOR_COOKIE_PATH ?? './data/agrarmonitor-cookies.json',
|
|
{
|
|
encryptor: process.env.AGRARMONITOR_ENCRYPTION_KEY
|
|
? new AesGcmCookieEncryptor(process.env.AGRARMONITOR_ENCRYPTION_KEY)
|
|
: undefined,
|
|
logger: console,
|
|
}
|
|
),
|
|
logger: console,
|
|
});
|
|
|
|
const freischaltung = await agrarmonitor.checkFreigeschaltet();
|
|
console.log(freischaltung.freigeschaltet);
|
|
|
|
const registrierung = await agrarmonitor.checkRegistriert();
|
|
console.log(registrierung.registriert);
|
|
|
|
const response = await agrarmonitor.http.get('/kunden/detail/123');
|
|
console.log(response.status);
|
|
```
|
|
|
|
## Cookie Persistence
|
|
|
|
`FileCookieStore` keeps one shared `CookieJar` per file path inside the Node process. Multiple connector instances that use the same cookie file therefore reuse the same session and every successful request saves the latest cookies back to disk.
|
|
|
|
The store can read both the connector format and the older Telefonbuch cookie-array format.
|
|
|
|
## Useful Methods
|
|
|
|
- `checkFreigeschaltet()` checks whether Agrarmonitor redirects to `/freischaltung/`.
|
|
- `checkRegistriert()` checks whether the page still contains `Neues Gerät registrieren`.
|
|
- `registerDevice({ agrarmonitorId, pcName })` loads `/freischaltung/`, extracts the nonce, and posts the registration request.
|
|
- `fetchCustomers()` loads customers from `https://api.agrarmonitor.de/v1/kunden`.
|
|
- `getKunden2()` loads all customer pages and returns normalized customer/supplier fields.
|
|
- `getCustomerById(id)` loads one customer from the token-based Agrarmonitor API.
|
|
- `eingangsrechnungenLivesearch(suchstring)` searches invoice files and enriches matching Eingangsrechnungen with edit/detail metadata.
|
|
- `eingangsrechnungVorhanden(suchstring)` checks whether invoice file search has rows.
|
|
- `eingangsrechnungImDateieingangVorhanden(suchstring)` checks whether file inbox search has rows.
|
|
- `getRechnungsdaten(rechnungId)` reads editable invoice fields.
|
|
- `setRechnungsdaten(rechnungId, daten)` updates editable invoice fields.
|
|
- `setLieferscheinNummer(rechnungId, nummer)` updates only the delivery note number.
|
|
- `setEingangsdatum(rechnungId, datum)` updates the received date.
|
|
- `getMaschinenKategorien()` reads `/maschinen/kategorien` and returns ID, name, tractor flag, and mobile visibility.
|
|
- `getMaschinen(gruppenId, suchstring?)` reads `/module/maschinen/livesearch.php` and returns ID, number, name, license plate, and active flag.
|
|
- `getFirmen()` reads `/einstellungen` and returns company ID and description.
|
|
- `getArtikelEinheiten()` reads `/artikel/einheiten` and returns unit ID, description, and short label.
|
|
- `getArtikelKategorien()` reads `/artikel/kategorien` and returns product group ID and name.
|
|
- `getArtikel(artikelGruppe, suchstring?)` reads `/module/artikel/livesearch.php` and returns article ID, number, and description.
|
|
- `getMitarbeiter(suchstring?, mitarbeitergruppe?)` reads `/module/mitarbeiter/livesearch.php` and returns employee ID, number, last name, first name, and active flag.
|
|
- `saveSession()` explicitly persists the current cookie jar.
|