mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-05-12 09:09:01 -04:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import {createSupabaseDirectClient} from 'shared/supabase/init'
|
|
|
|
import {APIErrors, APIHandler} from './helpers/endpoint'
|
|
|
|
export const saveSubscription: APIHandler<'save-subscription'> = async (body, auth) => {
|
|
const {subscription} = body
|
|
|
|
if (!subscription?.endpoint || !subscription?.keys) {
|
|
throw APIErrors.badRequest('Invalid subscription object')
|
|
}
|
|
|
|
const userId = auth?.uid
|
|
|
|
try {
|
|
const pg = createSupabaseDirectClient()
|
|
// Check if a subscription already exists
|
|
const exists = await pg.oneOrNone('select id from push_subscriptions where endpoint = $1', [
|
|
subscription.endpoint,
|
|
])
|
|
|
|
if (exists) {
|
|
// Already exists, optionally update keys and userId
|
|
await pg.none('update push_subscriptions set keys = $1, user_id = $2 where id = $3', [
|
|
subscription.keys,
|
|
userId,
|
|
exists.id,
|
|
])
|
|
} else {
|
|
await pg.none(
|
|
`insert into push_subscriptions(endpoint, keys, user_id) values($1, $2, $3)
|
|
on conflict(endpoint) do update set keys = excluded.keys
|
|
`,
|
|
[subscription.endpoint, subscription.keys, userId],
|
|
)
|
|
}
|
|
|
|
return {success: true}
|
|
} catch (err) {
|
|
console.error('Error saving subscription', err)
|
|
throw APIErrors.internalServerError('Failed to save subscription')
|
|
}
|
|
}
|