Files
paperlessmanager/paperless-backend/src/agrarmonitor/agrarmonitor.service.ts
T
bjoernpoettker 9f39578471
Build and Push Multi-Platform Images / build-and-push (push) Successful in 43s
fix: disable auto-retry and improve error messages in Agrarmonitor
- Set autoRetry: false and timeoutMs: 10000 in AgrarmonitorService
- Show specific error message on timeout or backend error in frontend

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-21 22:10:36 +02:00

88 lines
2.9 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
createAgrarmonitorClient,
FileCookieStore,
AesGcmCookieEncryptor,
type AgrarmonitorConnectorResult,
} from 'agrarmonitor-connector';
export interface AgrarmonitorStatusDto {
connected: boolean;
registriert: boolean | null;
freigeschaltet: boolean | null;
error?: string;
}
export interface AgrarmonitorRegisterResultDto {
success: boolean;
message: string;
}
@Injectable()
export class AgrarmonitorService {
private readonly logger = new Logger(AgrarmonitorService.name);
private client: AgrarmonitorConnectorResult | null = null;
constructor(private readonly configService: ConfigService) {}
async getClient(): Promise<AgrarmonitorConnectorResult> {
if (this.client) return this.client;
const username = this.configService.get<string>('AGRARMONITOR_USERNAME', '');
const password = this.configService.get<string>('AGRARMONITOR_PASSWORD', '');
const baseUrl = this.configService.get<string>('AGRARMONITOR_BASE_URL', 'https://admin7.agrarmonitor.de');
const apiBaseUrl = this.configService.get<string>('AGRARMONITOR_API_BASE_URL', 'https://api.agrarmonitor.de');
const apiToken = this.configService.get<string>('AGRARMONITOR_API_TOKEN');
const cookiePath = this.configService.get<string>('AGRARMONITOR_COOKIE_PATH', './data/agrarmonitor-cookies.json');
const encryptionKey = this.configService.get<string>('AGRARMONITOR_ENCRYPTION_KEY');
const encryptor = encryptionKey ? new AesGcmCookieEncryptor(encryptionKey) : undefined;
const cookieStore = new FileCookieStore(cookiePath, { encryptor, logger: this.logger });
this.client = await createAgrarmonitorClient({
baseUrl,
apiBaseUrl,
apiToken,
username,
password,
cookieStore,
autoLogin: true,
autoRetry: false,
timeoutMs: 10000,
logger: this.logger,
});
return this.client;
}
async getStatus(): Promise<AgrarmonitorStatusDto> {
try {
const client = await this.getClient();
const [registrierungStatus, freigeschaltetStatus] = await Promise.all([
client.checkRegistriert(),
client.checkFreigeschaltet(),
]);
return {
connected: true,
registriert: registrierungStatus.registriert,
freigeschaltet: freigeschaltetStatus.freigeschaltet,
};
} catch (err: any) {
this.client = null;
return {
connected: false,
registriert: null,
freigeschaltet: null,
error: err?.message ?? 'Verbindung fehlgeschlagen',
};
}
}
async registerDevice(pcName: string, agrarmonitorId: string): Promise<AgrarmonitorRegisterResultDto> {
const client = await this.getClient();
const result = await client.registerDevice({ agrarmonitorId, pcName });
return { success: result.success, message: result.message };
}
}