AG Scroll Settings Budget Push und Rechnungsdokumente umsetzen
All checks were successful
CI / Build and Deploy (push) Successful in 2m20s

This commit is contained in:
jan
2026-05-05 21:57:20 +02:00
parent 99d4f6dd22
commit f87a82e02f
21 changed files with 885 additions and 323 deletions

View File

@@ -1,5 +1,5 @@
import webpush from "web-push";
import type { ApprovalType } from "@prisma/client";
import type { ApprovalType, BudgetReleaseNotifyTarget } from "@prisma/client";
import { approvalLabel } from "@/lib/domain";
import prisma from "@/lib/prisma";
@@ -10,6 +10,14 @@ type PushTargetExpense = {
amount: number;
};
type PushTargetBudgetRelease = {
id: string;
name: string;
workingGroupId: string;
workingGroupName: string;
releasedAmount: number;
};
function configureWebPush() {
const publicKey = process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY;
const privateKey = process.env.VAPID_PRIVATE_KEY;
@@ -92,3 +100,52 @@ export async function notifyApprovalRequest(expense: PushTargetExpense, approval
})
);
}
export async function notifyBudgetRelease(budget: PushTargetBudgetRelease, target: BudgetReleaseNotifyTarget) {
if (!configureWebPush()) {
return;
}
const subscriptions = await prisma.pushSubscription.findMany({
where: {
user: {
workingGroupId: budget.workingGroupId,
...(target === "GROUP_MEMBERS_ONLY" ? { role: "MEMBER" } : {})
}
}
});
await Promise.all(
subscriptions.map(async (subscription) => {
const payload = JSON.stringify({
title: "Budget freigegeben",
body: `${budget.workingGroupName}: ${budget.name} wurde mit ${budget.releasedAmount.toFixed(2)} EUR freigegeben.`,
url: `/?budget=${encodeURIComponent(budget.id)}`,
tag: `budget-release-${budget.id}`
});
try {
await webpush.sendNotification(
{
endpoint: subscription.endpoint,
keys: {
p256dh: subscription.p256dh,
auth: subscription.auth
}
},
payload
);
} catch (error) {
const statusCode = typeof error === "object" && error && "statusCode" in error ? error.statusCode : null;
if (statusCode === 404 || statusCode === 410) {
await prisma.pushSubscription.delete({
where: {
endpoint: subscription.endpoint
}
});
}
}
})
);
}