Files
Compass/backend/api/src/update-notif-setting.ts
Martin Braquet ba9b3cfb06 Add pretty formatting (#29)
* Test

* Add pretty formatting

* Fix Tests

* Fix Tests

* Fix Tests

* Fix

* Add pretty formatting fix

* Fix

* Test

* Fix tests

* Clean typeckech

* Add prettier check

* Fix api tsconfig

* Fix api tsconfig

* Fix tsconfig

* Fix

* Fix

* Prettier
2026-02-20 17:32:27 +01:00

30 lines
988 B
TypeScript

import {createSupabaseDirectClient} from 'shared/supabase/init'
import {updatePrivateUser} from 'shared/supabase/users'
import {broadcastUpdatedPrivateUser} from 'shared/websockets/helpers'
import {type APIHandler} from './helpers/endpoint'
export const updateNotifSettings: APIHandler<'update-notif-settings'> = async (
{type, medium, enabled},
auth,
) => {
const pg = createSupabaseDirectClient()
if (type === 'opt_out_all' && medium === 'mobile') {
await updatePrivateUser(pg, auth.uid, {
interestedInPushNotifications: !enabled,
})
} else {
// deep update array at data.notificationPreferences[type]
await pg.none(
`update private_users
set data = jsonb_set(data, '{notificationPreferences, $1:raw}',
coalesce(data->'notificationPreferences'->$1, '[]'::jsonb)
${enabled ? `|| '[$2:name]'::jsonb` : `- $2`}
)
where id = $3`,
[type, medium, auth.uid],
)
broadcastUpdatedPrivateUser(auth.uid)
}
}