diff --git a/src/components/Dashboard/DashboardAusweis.svelte b/src/components/Dashboard/DashboardAusweis.svelte index bf36b78a..c14d80d7 100644 --- a/src/components/Dashboard/DashboardAusweis.svelte +++ b/src/components/Dashboard/DashboardAusweis.svelte @@ -160,35 +160,10 @@ if (ausweisart === Enums.Ausweisart.BedarfsausweisWohnen) { const results: {data: string, name: string, type: "Sonstiges" | "Ausweis"}[] = [] - for (const file of bedarfsausweisAdditionalInput.files || []) { - const reader = new FileReader(); + let i = 0 + const dateien = [...(bedarfsausweisAdditionalInput.files || []), ...(bedarfsausweisFileInput.files || [])]; - reader.onload = (ev) => { - const result = reader.result; - - if (!result) { - addNotification({ - message: `Die Datei ${file.name} konnte nicht verarbeitet werden.`, - timeout: 3000, - type: "error", - dismissable: true - }) - } - - results.push({ - data: result as string, - name: file.name, - type: "Sonstiges" - }) - } - reader.readAsDataURL(file) - } - - // Jetzt holen wir uns noch den Ausweis - const files = bedarfsausweisFileInput.files - const reader = new FileReader() - - if (files === null || (files.length === 0)) { + if (dateien.length === 0) { addNotification({ message: "Bitte laden sie vor dem Ausstellen einen Ausweis hoch.", timeout: 3000, @@ -198,50 +173,57 @@ return; } - reader.onload = (ev) => { - const result = reader.result; + for (const datei of dateien) { + const reader = new FileReader(); + + reader.onload = async (ev) => { + const result = reader.result; if (!result) { addNotification({ - message: `Die Datei ${files[0].name} konnte nicht verarbeitet werden.`, + message: `Die Datei ${datei.name} konnte nicht verarbeitet werden.`, timeout: 3000, type: "error", dismissable: true }) } - results.push({ + results.push({ data: result as string, - name: files[0].name, - type: "Ausweis" + name: datei.name, + type: i == dateien.length - 1 ? "Ausweis" : "Sonstiges" }) - } + i++ - reader.readAsDataURL(files[0]) - - try { - await api.admin["bedarfsausweis-ausstellen"].POST.fetch({ - id_ausweis: ausweis.id, - post, - files: results - }, { - headers: { - "Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}` + if (i === dateien.length) { + try { + await api.admin["bedarfsausweis-ausstellen"].POST.fetch({ + id_ausweis: ausweis.id, + post, + files: results + }, { + headers: { + "Content-Type": "application/json", + "Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}` + } + }) + updateNotification(notification, { + message: "Ausweis ausgestellt.", + subtext: "Der Ausweis wurde erfolgreich ausgestellt.", + timeout: 3000, + type: "success", + }) + } catch(e) { + updateNotification(notification, { + message: "Das hat nicht geklappt.", + subtext: e as string, + timeout: 3000, + type: "error", + }) + } } - }) - updateNotification(notification, { - message: "Ausweis ausgestellt.", - subtext: "Der Ausweis wurde erfolgreich ausgestellt.", - timeout: 3000, - type: "success", - }) - } catch(e) { - updateNotification(notification, { - message: "Das hat nicht geklappt.", - subtext: e as string, - timeout: 3000, - type: "error", - }) + } + reader.readAsDataURL(datei) } } else { try { diff --git a/src/pages/api/admin/bedarfsausweis-ausstellen.ts b/src/pages/api/admin/bedarfsausweis-ausstellen.ts index 943643a8..4edb9311 100644 --- a/src/pages/api/admin/bedarfsausweis-ausstellen.ts +++ b/src/pages/api/admin/bedarfsausweis-ausstellen.ts @@ -131,7 +131,6 @@ export const POST = defineApiRoute({ } // Hier müssen wir warten, damit wir sichergehen können, dass die Rechnung bei LexOffice existiert. - setTimeout(async () => { const [pdfRechnung, pdfRechnungError] = await tryCatch( getLexOfficeRechnung(rechnung) ); @@ -143,7 +142,7 @@ export const POST = defineApiRoute({ }); } - const processedFiles: Mail.Attachment[] = [] + const processedFiles: Mail.Attachment[] = [] for (const file of files) { const { data, name, type } = file; @@ -356,6 +355,5 @@ export const POST = defineApiRoute({ }, }); } - }, 3000); }, -}); +}); \ No newline at end of file