Updated stuff
All checks were successful
Code Analysis and Production Deploy / analyze (push) Successful in 6m4s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Successful in 2m10s

This commit is contained in:
Torsten Schulz (local)
2026-06-12 16:49:00 +02:00
parent 44d441811c
commit e537839e28
10 changed files with 197 additions and 157 deletions

View File

@@ -146,9 +146,9 @@ function isVorstandUser(user) {
return roles.includes('admin') || roles.includes('vorstand')
}
export async function sendPushToUsers({ title, body, data = {}, predicate, failureLabel = 'FCM-Push' }) {
export async function sendPushToUsers({ title, body, data = {}, predicate, bodyForUser, dataForUser, failureLabel = 'FCM-Push' }) {
const serviceAccount = await readServiceAccount()
if (!serviceAccount) {
if (serviceAccount == null) {
console.warn('FCM nicht konfiguriert: FCM_SERVICE_ACCOUNT_JSON oder GOOGLE_APPLICATION_CREDENTIALS fehlt.')
return { sent: 0, failed: 0, removed: 0, recipients: 0, tokenCount: 0, skipped: true }
}
@@ -160,30 +160,34 @@ export async function sendPushToUsers({ title, body, data = {}, predicate, failu
let recipients = 0
let tokenCount = 0
let changed = false
const payload = {
...Object.fromEntries(Object.entries(data).map(([key, value]) => [key, String(value ?? '')])),
title: String(title || 'Harheimer TC'),
body: String(body || '').slice(0, 240),
notificationId: String(data.notificationId || notificationIdFor(`${data.type || 'push'}:${title}:${body}`))
}
const baseData = Object.fromEntries(Object.entries(data).map(([key, value]) => [key, String(value ?? '')]))
for (const user of users) {
if (isHiddenUser(user)) continue
const settings = notificationSettingsForUser(user)
if (predicate && !predicate(user, settings)) continue
const userBody = String(bodyForUser ? bodyForUser(user, settings) : body || '').slice(0, 240)
const userData = dataForUser ? dataForUser(user, settings) : {}
const payload = {
...baseData,
...Object.fromEntries(Object.entries(userData || {}).map(([key, value]) => [key, String(value ?? '')])),
title: String(title || 'Harheimer TC'),
body: userBody,
notificationId: String((userData && userData.notificationId) || data.notificationId || notificationIdFor([data.type || 'push', title, userBody].join(':')))
}
recipients += 1
const tokens = pushTokensForUser(user)
tokenCount += tokens.length
const validTokens = []
for (const entry of tokens) {
try {
await sendFcmMessage({ serviceAccount, accessToken, token: entry.token, title, body, data: payload })
await sendFcmMessage({ serviceAccount, accessToken, token: entry.token, title, body: userBody, data: payload })
sent += 1
validTokens.push(entry)
} catch (error) {
failed += 1
console.error('FCM Push fehlgeschlagen:', { failureLabel, message: error.message })
if (!/UNREGISTERED|NOT_FOUND|INVALID_ARGUMENT/.test(String(error.message))) {
if (/UNREGISTERED|NOT_FOUND|INVALID_ARGUMENT/.test(String(error.message)) === false) {
validTokens.push(entry)
} else {
removed += 1
@@ -191,7 +195,7 @@ export async function sendPushToUsers({ title, body, data = {}, predicate, failu
}
}
}
if (validTokens.length !== tokens.length) {
if (validTokens.length < tokens.length) {
user.pushTokens = validTokens
changed = true
}