AddOn/Datenexport_Zeiterfassung/test.html aktualisiert
This commit is contained in:
parent
408bb3ae26
commit
c4291374b9
|
|
@ -2,7 +2,7 @@
|
|||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>TimeTracking API WebUI - Test</title>
|
||||
<title>TimeTracking API WebUI - Test mit Polling</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; margin: 20px; max-width: 600px; }
|
||||
label, select, input, button { display: block; width: 100%; margin-top: 10px; padding: 8px; }
|
||||
|
|
@ -48,43 +48,26 @@
|
|||
<script>
|
||||
let baseUrl = '';
|
||||
let token = '';
|
||||
let apiSite = ''; // Optional, falls die API-URL schon api beinhaltet, kann leer bleiben.
|
||||
let apiSite = '';
|
||||
|
||||
async function apiRequest(baseUrl, token, apiSite, method, endpoint, body = null) {
|
||||
let url = baseUrl.replace(/\/+$/, '') + (apiSite ? '/' + apiSite.replace(/^\/+|\/+$/g, '') : '') + '/' + endpoint.replace(/^\/+/, '');
|
||||
|
||||
const headers = {
|
||||
'Authorization': token,
|
||||
};
|
||||
if (body) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
const options = {
|
||||
method: method.toUpperCase(),
|
||||
headers,
|
||||
};
|
||||
if (body) {
|
||||
options.body = JSON.stringify(body);
|
||||
}
|
||||
|
||||
const url = baseUrl.replace(/\/+$/g, '') + (apiSite ? '/' + apiSite.replace(/^\/+|\/+$/g, '') : '') + '/' + endpoint.replace(/^\/+/, '');
|
||||
const headers = { 'Authorization': token };
|
||||
if (body) headers['Content-Type'] = 'application/json';
|
||||
const options = { method: method.toUpperCase(), headers };
|
||||
if (body) options.body = JSON.stringify(body);
|
||||
const response = await fetch(url, options);
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`API Fehler ${response.status}: ${errorText}`);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
if (contentType.includes('application/json')) {
|
||||
return response.json();
|
||||
}
|
||||
return response.text();
|
||||
return contentType.includes('application/json') ? response.json() : response.text();
|
||||
}
|
||||
|
||||
document.getElementById('connectBtn').addEventListener('click', async () => {
|
||||
baseUrl = document.getElementById('apiUrl').value.trim();
|
||||
token = document.getElementById('token').value.trim();
|
||||
apiSite = ''; // Kann erweitert werden, falls nötig
|
||||
|
||||
if (!baseUrl || !token) {
|
||||
alert('Bitte WebAPI URL und Token eingeben.');
|
||||
|
|
@ -92,9 +75,7 @@
|
|||
}
|
||||
|
||||
try {
|
||||
// OE laden
|
||||
const orgUnits = await apiRequest(baseUrl, token, apiSite, 'GET', 'api/organization/');
|
||||
// Export-Definitionen laden
|
||||
const exportDefs = await apiRequest(baseUrl, token, apiSite, 'GET', 'api/export/');
|
||||
|
||||
const orgSelect = document.getElementById('organizationUnit');
|
||||
|
|
@ -110,24 +91,19 @@
|
|||
exportSelect.innerHTML = '';
|
||||
exportDefs.forEach(def => {
|
||||
const opt = document.createElement('option');
|
||||
//opt.value = def.Id || def.id || def.ExportDefinitionId || def.ExportDefinition || '';
|
||||
opt.value = def.ObjectId || '';
|
||||
opt.textContent = def.ObjectNaming || def.ObjectPatternNumber || opt.ObjectShortTerm;
|
||||
opt.textContent = def.ObjectNaming || def.ObjectPatternNumber || def.ObjectShortTerm;
|
||||
exportSelect.appendChild(opt);
|
||||
});
|
||||
|
||||
// Schritt 2 einblenden
|
||||
document.getElementById('step1').style.display = 'none';
|
||||
document.getElementById('step2').style.display = 'block';
|
||||
|
||||
// Datum Default setzen (letzte Woche)
|
||||
const dateFromInput = document.getElementById('dateFrom');
|
||||
const dateUntilInput = document.getElementById('dateUntil');
|
||||
const today = new Date();
|
||||
const lastWeek = new Date(today);
|
||||
lastWeek.setDate(today.getDate() - 7);
|
||||
dateFromInput.value = lastWeek.toISOString().split('T')[0];
|
||||
dateUntilInput.value = today.toISOString().split('T')[0];
|
||||
document.getElementById('dateFrom').value = lastWeek.toISOString().split('T')[0];
|
||||
document.getElementById('dateUntil').value = today.toISOString().split('T')[0];
|
||||
|
||||
} catch (e) {
|
||||
alert('Fehler beim Laden der Daten: ' + e.message);
|
||||
|
|
@ -146,27 +122,32 @@
|
|||
}
|
||||
|
||||
try {
|
||||
// Export anfragen
|
||||
const result = await apiRequest(baseUrl, token, apiSite, 'POST', 'api/export', {
|
||||
const guid = await apiRequest(baseUrl, token, apiSite, 'POST', 'api/export', {
|
||||
ExportDefinition: exportDefinitionId,
|
||||
organizationUnit: organizationUnitId,
|
||||
DateFrom: dateFrom,
|
||||
DateUntil: dateUntil
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
throw new Error('Kein Result im Antwortobjekt ('+result+')');
|
||||
}
|
||||
|
||||
// Exportdaten abrufen (Result ist GUID)
|
||||
const exportData = await apiRequest(baseUrl, token, apiSite, 'GET', `api/export/${result}`);
|
||||
|
||||
const pre = document.getElementById('exportResult');
|
||||
if (typeof exportData === 'object') {
|
||||
pre.textContent = JSON.stringify(exportData, null, 2);
|
||||
} else {
|
||||
pre.textContent = exportData;
|
||||
pre.textContent = 'Job gestartet (GUID: ' + guid + '), warte auf Ergebnis...';
|
||||
|
||||
const poll = async () => {
|
||||
try {
|
||||
const data = await apiRequest(baseUrl, token, apiSite, 'GET', `api/export/${guid}`);
|
||||
if (typeof data === 'object') {
|
||||
pre.textContent = JSON.stringify(data, null, 2);
|
||||
if (data.Result === 'JobPending') {
|
||||
setTimeout(poll, 60000);
|
||||
}
|
||||
} else {
|
||||
pre.textContent = data;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Polling-Fehler:', err);
|
||||
}
|
||||
};
|
||||
setTimeout(poll, 10000);
|
||||
|
||||
} catch (e) {
|
||||
alert('Fehler beim Export: ' + e.message);
|
||||
|
|
|
|||
Loading…
Reference in New Issue