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

71 lines
1.9 KiB
TypeScript

import {Row} from 'common/supabase/utils'
import {tryCatch} from 'common/util/try-catch'
import {createProfileLikeNotification} from 'shared/create-profile-notification'
import {createSupabaseDirectClient} from 'shared/supabase/init'
import {log} from 'shared/utils'
import {getHasFreeLike} from './has-free-like'
import {APIErrors, APIHandler} from './helpers/endpoint'
export const likeProfile: APIHandler<'like-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_likes where creator_id = $1 and target_id = $2', [
creatorId,
targetUserId,
]),
)
if (error) {
throw APIErrors.internalServerError('Failed to remove like: ' + error.message)
}
return {status: 'success'}
}
// Check if like already exists
const {data: existing} = await tryCatch(
pg.oneOrNone<Row<'profile_likes'>>(
'select * from profile_likes where creator_id = $1 and target_id = $2',
[creatorId, targetUserId],
),
)
if (existing) {
log('Like already exists, do nothing')
return {status: 'success'}
}
const hasFreeLike = await getHasFreeLike(creatorId)
if (!hasFreeLike) {
// Charge for like.
throw APIErrors.forbidden('You already liked someone today!')
}
// Insert the new like
const {data, error} = await tryCatch(
pg.one<Row<'profile_likes'>>(
'insert into profile_likes (creator_id, target_id) values ($1, $2) returning *',
[creatorId, targetUserId],
),
)
if (error) {
throw APIErrors.internalServerError('Failed to add like: ' + error.message)
}
const continuation = async () => {
await createProfileLikeNotification(data)
}
return {
result: {status: 'success'},
continue: continuation,
}
}