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.
This commit is contained in:
parent
06ee77592f
commit
3faa1f8e85
2 changed files with 103 additions and 0 deletions
95
server/api/members/me/community-connections.patch.js
Normal file
95
server/api/members/me/community-connections.patch.js
Normal file
|
|
@ -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',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
@ -33,6 +33,8 @@ export default defineEventHandler(async (event) => {
|
||||||
"socialLinksPrivacy",
|
"socialLinksPrivacy",
|
||||||
"offeringPrivacy",
|
"offeringPrivacy",
|
||||||
"lookingForPrivacy",
|
"lookingForPrivacy",
|
||||||
|
"craftTagsPrivacy",
|
||||||
|
"communityConnectionsPrivacy",
|
||||||
];
|
];
|
||||||
|
|
||||||
// Build update object from validated data
|
// 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)
|
// Handle offering and lookingFor separately (nested objects)
|
||||||
if (body.offering !== undefined) {
|
if (body.offering !== undefined) {
|
||||||
updateData.offering = {
|
updateData.offering = {
|
||||||
|
|
@ -102,6 +109,7 @@ export default defineEventHandler(async (event) => {
|
||||||
socialLinks: member.socialLinks,
|
socialLinks: member.socialLinks,
|
||||||
offering: member.offering,
|
offering: member.offering,
|
||||||
lookingFor: member.lookingFor,
|
lookingFor: member.lookingFor,
|
||||||
|
craftTags: member.craftTags,
|
||||||
showInDirectory: member.showInDirectory,
|
showInDirectory: member.showInDirectory,
|
||||||
notifications: member.notifications,
|
notifications: member.notifications,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue