Files
harheimertc/server/utils/notification-settings.js
Torsten Schulz (local) 5da11d2e4d
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 7m50s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped
Fix in news, first android notification service
2026-06-10 13:47:33 +02:00

56 lines
1.8 KiB
JavaScript

export const DEFAULT_NOTIFICATION_SETTINGS = Object.freeze({
newNews: false,
newEvents: false,
eventsToday: false,
eventsTomorrow: false,
ownTeamMatches: false,
allTeamMatches: false,
birthdays: false,
newContactRequest: false,
newUserRegistration: false,
selectedTeamSlugs: [],
selectedTeamSeason: null,
notificationTime: '09:00'
})
function coerceBoolean(value) {
return value === true
}
export function sanitizeNotificationSettings(input = {}) {
const selectedTeamSlugs = Array.isArray(input.selectedTeamSlugs)
? input.selectedTeamSlugs
.map(value => String(value || '').trim())
.filter(Boolean)
.slice(0, 50)
: []
const selectedTeamSeason = typeof input.selectedTeamSeason === 'string' && input.selectedTeamSeason.trim()
? input.selectedTeamSeason.trim().slice(0, 30)
: null
const notificationTime = /^([01]\d|2[0-3]):[0-5]\d$/.test(String(input.notificationTime || ''))
? String(input.notificationTime)
: DEFAULT_NOTIFICATION_SETTINGS.notificationTime
return {
newNews: coerceBoolean(input.newNews),
newEvents: coerceBoolean(input.newEvents),
eventsToday: coerceBoolean(input.eventsToday),
eventsTomorrow: coerceBoolean(input.eventsTomorrow),
ownTeamMatches: coerceBoolean(input.ownTeamMatches),
allTeamMatches: coerceBoolean(input.allTeamMatches),
birthdays: coerceBoolean(input.birthdays),
newContactRequest: coerceBoolean(input.newContactRequest),
newUserRegistration: coerceBoolean(input.newUserRegistration),
selectedTeamSlugs: [...new Set(selectedTeamSlugs)],
selectedTeamSeason,
notificationTime
}
}
export function notificationSettingsForUser(user) {
return sanitizeNotificationSettings({
...DEFAULT_NOTIFICATION_SETTINGS,
...(user?.notificationSettings || user?.notifications || {})
})
}