feat: add connection API endpoints

Suggestions, create/confirm/hide/withdraw actions, my connections list,
and pending count for nav badge.
This commit is contained in:
Jennie Robinson Faber 2026-04-05 16:48:10 +01:00
parent d69d21abd6
commit dcb80e6006
7 changed files with 442 additions and 0 deletions

View file

@ -0,0 +1,43 @@
import mongoose from 'mongoose'
import Connection from '../../../models/connection.js'
import { requireAuth } from '../../../utils/auth.js'
export default defineEventHandler(async (event) => {
const member = await requireAuth(event)
const memberId = member._id
const connectionId = getRouterParam(event, 'id')
if (!mongoose.Types.ObjectId.isValid(connectionId)) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid connection ID'
})
}
const connection = await Connection.findById(connectionId)
if (!connection) {
throw createError({
statusCode: 404,
statusMessage: 'Connection not found'
})
}
// Only the initiator can withdraw
if (connection.initiator.toString() !== memberId.toString()) {
throw createError({
statusCode: 403,
statusMessage: 'Only the initiator can withdraw a connection request'
})
}
if (connection.status !== 'pending') {
throw createError({
statusCode: 400,
statusMessage: 'Can only withdraw pending connections'
})
}
await Connection.findByIdAndDelete(connectionId)
return { success: true }
})