SEPA-Exportdatei aus Mandaten erzeugen #182

This commit is contained in:
2026-05-15 18:36:48 +02:00
parent 9592e2b062
commit c6a0d59c29
3 changed files with 373 additions and 121 deletions

View File

@@ -67,6 +67,45 @@ const createDatevExport = async (server:FastifyInstance,req:any,startDate,endDat
}
const createSepaExport = async (server: FastifyInstance, req: any, idsToExport: number[], creditorBankaccountId: number) => {
const exportData = await createSEPAExport(server, idsToExport, req.user.tenant_id, creditorBankaccountId)
const fileKey = `${req.user.tenant_id}/exports/SEPA_${dayjs().format("YYYY-MM-DD")}_${randomUUID()}.xml`
await s3.send(
new PutObjectCommand({
Bucket: secrets.S3_BUCKET,
Key: fileKey,
Body: exportData.buffer,
ContentType: "application/xml",
})
)
const url = await getSignedUrl(
s3,
new GetObjectCommand({
Bucket: secrets.S3_BUCKET,
Key: fileKey,
}),
{ expiresIn: 60 * 60 * 24 }
)
const inserted = await server.db
.insert(generatedexports)
.values({
tenantId: req.user.tenant_id,
startDate: exportData.startDate,
endDate: exportData.endDate,
validUntil: dayjs().add(24, "hours").toDate(),
filePath: fileKey,
url,
type: "sepa",
})
.returning()
console.log(inserted[0])
}
export default async function exportRoutes(server: FastifyInstance) {
//Export DATEV
@@ -94,17 +133,24 @@ export default async function exportRoutes(server: FastifyInstance) {
})
server.post("/exports/sepa", async (req, reply) => {
const { idsToExport } = req.body as {
const { idsToExport, creditorBankaccountId } = req.body as {
idsToExport: Array<number>
creditorBankaccountId: number
}
if (!idsToExport?.length || !creditorBankaccountId) {
return reply.send({
success: false,
message: "Belege und Gläubigerkonto sind Pflichtfelder."
})
}
reply.send({success:true})
setImmediate(async () => {
try {
await createSEPAExport(server, idsToExport, req.user.tenant_id)
await createSepaExport(server, req, idsToExport, creditorBankaccountId)
console.log("Job done ✅")
} catch (err) {
console.error("Job failed ❌", err)