56 lines
1.8 KiB
JavaScript
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 || {})
|
|
})
|
|
}
|