mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-04-04 23:03:45 -04:00
53 lines
1.4 KiB
TypeScript
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'}
|
|
}
|