From 3faa1f8e85535946641a487ab3190afafd4f9ac4 Mon Sep 17 00:00:00 2001 From: Jennie Robinson Faber Date: Sun, 5 Apr 2026 16:19:49 +0100 Subject: [PATCH] feat: add community-connections API endpoint and update profile handler New PATCH /api/members/me/community-connections endpoint following peer-support.patch.js pattern (requireAuth, validateBody, dot-notation $set, Slack user lookup when offerPeerSupport+slackHandle set, logActivity). Profile endpoint updated with craftTags handling, craftTagsPrivacy and communityConnectionsPrivacy in privacy fields, and craftTags in response. --- .../members/me/community-connections.patch.js | 95 +++++++++++++++++++ server/api/members/profile.patch.js | 8 ++ 2 files changed, 103 insertions(+) create mode 100644 server/api/members/me/community-connections.patch.js diff --git a/server/api/members/me/community-connections.patch.js b/server/api/members/me/community-connections.patch.js new file mode 100644 index 0000000..df9e401 --- /dev/null +++ b/server/api/members/me/community-connections.patch.js @@ -0,0 +1,95 @@ +import Member from '../../../models/member.js' +import { connectDB } from '../../../utils/mongoose.js' + +export default defineEventHandler(async (event) => { + await connectDB() + const member = await requireAuth(event) + + const body = await validateBody(event, communityConnectionsUpdateSchema) + + // Build update object for community connections settings + const updateData = { + 'communityConnections.topics': body.topics || [], + 'communityConnections.offerPeerSupport': body.offerPeerSupport || false, + 'communityConnections.availability': body.availability || '', + 'communityConnections.slackHandle': body.slackHandle || '', + 'communityConnections.personalMessage': body.personalMessage || '', + 'communityConnections.details': body.details || '', + } + + // If Slack handle provided and peer support offered, try to fetch Slack user ID and open DM + if (body.offerPeerSupport && body.slackHandle) { + try { + console.log( + `[Community Connections] Attempting to fetch Slack user ID for: ${body.slackHandle}`, + ) + + const { getSlackService } = await import('../../../utils/slack.ts') + const slackService = getSlackService() + + if (slackService) { + console.log('[Community Connections] Slack service initialized, looking up user...') + const slackUserId = await slackService.findUserIdByUsername(body.slackHandle) + + if (slackUserId) { + updateData['slackUserId'] = slackUserId + console.log( + `[Community Connections] ✓ Found Slack user ID for ${body.slackHandle}: ${slackUserId}`, + ) + + console.log('[Community Connections] Opening DM channel...') + const dmChannelId = await slackService.openDMChannel(slackUserId) + + if (dmChannelId) { + updateData['communityConnections.slackDMChannelId'] = dmChannelId + console.log(`[Community Connections] ✓ Got DM channel ID: ${dmChannelId}`) + } else { + console.warn('[Community Connections] Could not get DM channel ID') + } + } else { + console.warn( + `[Community Connections] Could not find Slack user ID for handle: ${body.slackHandle}`, + ) + } + } else { + console.log('[Community Connections] Slack service not configured, skipping user ID lookup') + } + } catch (error) { + console.error('[Community Connections] Error fetching Slack user ID:', error.message) + console.error('[Community Connections] Stack trace:', error.stack) + // Continue anyway - we'll still save the handle + } + } + + try { + const updated = await Member.findByIdAndUpdate( + member._id, + { $set: updateData }, + { new: true, runValidators: true }, + ) + + if (!updated) { + throw createError({ + statusCode: 404, + statusMessage: 'Member not found', + }) + } + + logActivity(member._id, 'community_connections_updated', { + topicCount: (body.topics || []).length, + offerPeerSupport: body.offerPeerSupport || false, + }) + + return { + success: true, + communityConnections: updated.communityConnections, + } + } catch (error) { + if (error.statusCode) throw error + console.error('Community connections update error:', error) + throw createError({ + statusCode: 500, + statusMessage: 'Failed to update community connections settings', + }) + } +}) diff --git a/server/api/members/profile.patch.js b/server/api/members/profile.patch.js index dd70ac1..760c1d8 100644 --- a/server/api/members/profile.patch.js +++ b/server/api/members/profile.patch.js @@ -33,6 +33,8 @@ export default defineEventHandler(async (event) => { "socialLinksPrivacy", "offeringPrivacy", "lookingForPrivacy", + "craftTagsPrivacy", + "communityConnectionsPrivacy", ]; // Build update object from validated data @@ -44,6 +46,11 @@ export default defineEventHandler(async (event) => { } }); + // Handle craftTags (simple array) + if (body.craftTags !== undefined) { + updateData.craftTags = body.craftTags; + } + // Handle offering and lookingFor separately (nested objects) if (body.offering !== undefined) { updateData.offering = { @@ -102,6 +109,7 @@ export default defineEventHandler(async (event) => { socialLinks: member.socialLinks, offering: member.offering, lookingFor: member.lookingFor, + craftTags: member.craftTags, showInDirectory: member.showInDirectory, notifications: member.notifications, };