Add normalized kunden2 lookup
This commit is contained in:
Vendored
+60
@@ -145,6 +145,10 @@ class AgrarmonitorConnector {
|
||||
}
|
||||
return responseData.data;
|
||||
}
|
||||
async getKunden2(options = {}) {
|
||||
const customers = await this.fetchAllCustomers(options);
|
||||
return customers.map(customer => this.mapKunde2(customer));
|
||||
}
|
||||
async eingangsrechnungenLivesearch(suchstring) {
|
||||
const response = await this.http.get('/module/dateien/livesearch.php', {
|
||||
params: this.createDateienLivesearchParams(suchstring),
|
||||
@@ -348,6 +352,45 @@ class AgrarmonitorConnector {
|
||||
const client = apiToken === this.options.apiToken ? this.apiHttp : this.createApiHttpClient(apiToken);
|
||||
return client.get(url, axiosConfig);
|
||||
}
|
||||
async fetchAllCustomers(options) {
|
||||
const customers = [];
|
||||
let page = 1;
|
||||
let lastPage = 1;
|
||||
do {
|
||||
const response = await this.apiRequest('/kunden', {
|
||||
params: {
|
||||
per_page: options.perPage ?? 99999,
|
||||
page,
|
||||
},
|
||||
apiToken: options.apiToken,
|
||||
});
|
||||
if (!response.data || !Array.isArray(response.data.data)) {
|
||||
throw new Error('Ungueltige Agrarmonitor API-Antwort');
|
||||
}
|
||||
customers.push(...response.data.data);
|
||||
lastPage = this.parsePositiveNumber(response.data.meta?.last_page, page);
|
||||
page++;
|
||||
} while (page <= lastPage);
|
||||
return customers;
|
||||
}
|
||||
mapKunde2(customer) {
|
||||
return {
|
||||
ID: this.parseNumber(customer.id),
|
||||
ist_Kunde: this.toBoolean(customer.ist_kunde),
|
||||
Kundennummer: this.toStringValue(customer.kundennummer),
|
||||
ist_Lieferant: this.toBoolean(customer.ist_lieferant),
|
||||
Lieferantennummer: this.toStringValue(customer.lieferantennummer),
|
||||
Firma: this.toStringValue(customer.firma),
|
||||
Anrede: this.toStringValue(customer.anrede),
|
||||
Vorname: this.toStringValue(customer.vorname),
|
||||
Nachname: this.toStringValue(customer.nachname),
|
||||
Strasse: this.toStringValue(customer.strasse),
|
||||
Plz: this.toStringValue(customer.plz),
|
||||
Ort: this.toStringValue(customer.ort),
|
||||
Land: this.toStringValue(customer.land_iso_3166),
|
||||
ist_aktiv: this.toBoolean(customer.ist_aktiv),
|
||||
};
|
||||
}
|
||||
async performLogin() {
|
||||
if (!this.options.username || !this.options.password) {
|
||||
throw new Error('Agrarmonitor-Credentials nicht konfiguriert');
|
||||
@@ -479,6 +522,23 @@ class AgrarmonitorConnector {
|
||||
const numberValue = Number(String(value ?? '').trim());
|
||||
return Number.isFinite(numberValue) ? numberValue : 0;
|
||||
}
|
||||
parsePositiveNumber(value, fallback) {
|
||||
const numberValue = this.parseNumber(value);
|
||||
return numberValue > 0 ? numberValue : fallback;
|
||||
}
|
||||
toStringValue(value) {
|
||||
return value === null || typeof value === 'undefined' ? '' : String(value);
|
||||
}
|
||||
toBoolean(value) {
|
||||
if (typeof value === 'boolean') {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
return value === 1;
|
||||
}
|
||||
const normalized = String(value ?? '').trim().toLowerCase();
|
||||
return normalized === '1' || normalized === 'true' || normalized === 'ja';
|
||||
}
|
||||
parseJaNein(value) {
|
||||
return value.trim().toLowerCase() === 'ja';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user