mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-04-03 22:44:35 -04:00
34 lines
940 B
TypeScript
34 lines
940 B
TypeScript
import {createSupabaseDirectClient} from 'shared/supabase/init'
|
|
|
|
import {APIErrors, APIHandler} from './helpers/endpoint'
|
|
|
|
export const saveSubscriptionMobile: APIHandler<'save-subscription-mobile'> = async (
|
|
body,
|
|
auth,
|
|
) => {
|
|
const {token} = body
|
|
|
|
if (!token) {
|
|
throw APIErrors.badRequest('Invalid subscription object')
|
|
}
|
|
|
|
const userId = auth?.uid
|
|
|
|
try {
|
|
const pg = createSupabaseDirectClient()
|
|
await pg.none(
|
|
`
|
|
insert into push_subscriptions_mobile(token, platform, user_id)
|
|
values ($1, $2, $3)
|
|
on conflict(token) do update set platform = excluded.platform,
|
|
user_id = excluded.user_id
|
|
`,
|
|
[token, 'android', userId],
|
|
)
|
|
return {success: true}
|
|
} catch (err) {
|
|
console.error('Error saving subscription', err)
|
|
throw APIErrors.internalServerError('Failed to save subscription')
|
|
}
|
|
}
|