Files
Compass/backend/api/src/star-profile.ts
2026-03-06 15:27:49 +01:00

53 lines
1.4 KiB
TypeScript

import {Row} from 'common/supabase/utils'
import {tryCatch} from 'common/util/try-catch'
import {createSupabaseDirectClient} from 'shared/supabase/init'
import {insert} from 'shared/supabase/utils'
import {log} from 'shared/utils'
import {APIErrors, APIHandler} from './helpers/endpoint'
export const starProfile: APIHandler<'star-profile'> = async (props, auth) => {
const {targetUserId, remove} = props
const creatorId = auth.uid
const pg = createSupabaseDirectClient()
if (remove) {
const {error} = await tryCatch(
pg.none('delete from profile_stars where creator_id = $1 and target_id = $2', [
creatorId,
targetUserId,
]),
)
if (error) {
throw APIErrors.internalServerError('Failed to remove star: ' + error.message)
}
return {status: 'success'}
}
// Check if star already exists
const {data: existing} = await tryCatch(
pg.oneOrNone<Row<'profile_stars'>>(
'select * from profile_stars where creator_id = $1 and target_id = $2',
[creatorId, targetUserId],
),
)
if (existing) {
log('star already exists, do nothing')
return {status: 'success'}
}
// Insert the new star
const {error} = await tryCatch(
insert(pg, 'profile_stars', {creator_id: creatorId, target_id: targetUserId}),
)
if (error) {
throw APIErrors.internalServerError('Failed to add star: ' + error.message)
}
return {status: 'success'}
}