Merge branch 'dev'

This commit is contained in:
Moritz Utcke
2025-04-09 21:35:43 -04:00
3 changed files with 118 additions and 24 deletions

View File

@@ -1,8 +1,6 @@
name: Production Pipeline
on:
pull_request:
branches: [main]
push:
branches: [main]

View File

@@ -280,3 +280,51 @@ export async function createInvoice(
voucherNumber: abfrage_response["voucherNumber"],
};
}
/**
* Ládt das Reechnungs PDF von LexOffice runter.
*
* @export
* @async
* @param {Rechnung} rechnung
* @returns {ArrayBuffer}
* @throws Falls eine der requests fehlschlägt.
*/
export async function getLexOfficeRechnung(rechnung: Rechnung) {
const response = await fetch(`https://api.lexoffice.io/v1/invoices/${rechnung.lex_office_id}/document`, {
method: "GET",
headers: {
"Accept": `application/json`,
"Authorization": `Bearer ${LEX_OFFICE_API_KEY}`,
"Content-Type": `application/json`
}
})
const body = await response.json()
if(!("documentFileId" in body)){
throw new Error("documentFileId nicht in request.")
}
const file_id = body["documentFileId"];
const fileRequest = await fetch(`https://api.lexoffice.io/v1/files/${file_id}`, {
method: "GET",
headers: {
"Accept": `application/pdf`,
"Authorization": `Bearer ${LEX_OFFICE_API_KEY}`,
}
})
if (fileRequest.status !== 200) {
throw new Error("File request hat nicht funktioniert.")
}
const file = await fileRequest.arrayBuffer()
return file;
}

View File

@@ -18,7 +18,7 @@ import { BASE_URI } from "#lib/constants.js";
import { getAnsichtsausweis, getDatenblatt } from "#lib/server/ausweis.js";
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { s3Client } from "#lib/s3.js";
import { createInvoice } from "#lib/server/invoice.js";
import { createInvoice, getLexOfficeRechnung } from "#lib/server/invoice.js";
import { tryCatch } from "#lib/tryCatch.js";
import {
getBedarfsausweisWohnenKomplett,
@@ -121,32 +121,60 @@ export const GET = defineApiRoute({
ausweis.aufnahme.objekt.benutzer
);
if (pdfAusweis) {
const command = new PutObjectCommand({
Bucket: "ibc-pdfs",
Key: `ID_${ausweis.id}_Energieausweis.pdf`,
Body: pdfAusweis,
ACL: "private",
});
const pdfRechnung = await getLexOfficeRechnung(rechnung);
await s3Client.send(command);
if (!pdfAusweis) {
throw new APIError({
code: "INTERNAL_SERVER_ERROR",
message: "Ausweis PDF konnte nicht generiert werden."
})
}
if (pdfDatenblatt) {
const command = new PutObjectCommand({
Bucket: "ibc-pdfs",
Key: `ID_${ausweis.id}_Datenblatt.pdf`,
Body: pdfDatenblatt,
ACL: "private",
});
await s3Client.send(command);
if (!pdfDatenblatt) {
throw new APIError({
code: "INTERNAL_SERVER_ERROR",
message: "Datenblatt PDF konnte nicht generiert werden."
})
}
let text: string;
if (!pdfRechnung) {
throw new APIError({
code: "INTERNAL_SERVER_ERROR",
message: "Rechnungs PDF konnte nicht generiert werden."
})
}
const ausweisCommand = new PutObjectCommand({
Bucket: "ibc-pdfs",
Key: `ID_${ausweis.id}_Energieausweis.pdf`,
Body: pdfAusweis,
ACL: "private",
});
await s3Client.send(ausweisCommand);
const datenblattCommand = new PutObjectCommand({
Bucket: "ibc-pdfs",
Key: `ID_${ausweis.id}_Datenblatt.pdf`,
Body: pdfDatenblatt,
ACL: "private",
});
await s3Client.send(datenblattCommand);
const rechnungsCommand = new PutObjectCommand({
Bucket: "ibc-pdfs",
Key: `ID_${ausweis.id}_Rechnung.pdf`,
Body: pdfDatenblatt,
ACL: "private",
});
await s3Client.send(rechnungsCommand);
let html: string;
if (rechnung.status === Enums.Rechnungsstatus.paid) {
text = `
html = `
<p>Sehr geehrte*r ${user.vorname} ${user.name},</p>
<p>im Anhang finden Sie Ihren geprüften Energieusweis inkl. Rechnung als PDF-Datei. Den Rechnungsbetrag haben Sie bereits bezahlt. Vielen Dank.</p>
@@ -173,7 +201,7 @@ fon 040 · 209339850
fax 040 · 209339859
</p>`;
} else {
text = `
html = `
<p>Sehr geehrte*r ${user.vorname} ${user.name},</p>
<p>im Anhang finden Sie Ihren geprüften Energieusweis inkl. Rechnung als PDF-Datei. Nachfolgend finden Sie unsere Bankverbindung. Bitte geben Sie als Verwendungszweck die Rechnungsnummer an (siehe unten). Vielen Dank.</p>
@@ -229,8 +257,28 @@ fax 040 · 209339859
await transport.sendMail({
from: `"IBCornelsen" <info@online-energieausweis.org>`,
to: user.email,
bcc: "info@online-energieausweis.org",
subject: `Ihr Originalausweis vom Ingenieurbüro Cornelsen (ID: ${ausweis.id})`,
text,
html,
attachments: [{
filename: `ID_${ausweis.id}_Ansichtsausweis.pdf`,
encoding: "binary",
content: Buffer.from(pdfAusweis),
contentType: "application/pdf",
contentDisposition: "attachment",
}, {
filename: `ID_${ausweis.id}_Datenblatt.pdf`,
encoding: "binary",
content: Buffer.from(pdfDatenblatt),
contentType: "application/pdf",
contentDisposition: "attachment",
}, {
filename: `ID_${ausweis.id}_Rechnung.pdf`,
encoding: "binary",
content: Buffer.from(pdfRechnung),
contentType: "application/pdf",
contentDisposition: "attachment",
}]
});
if (ausweisart === Enums.Ausweisart.VerbrauchsausweisWohnen) {