( workosUserId: string, slackUserId: string )
| 349 | * Checks which channels they're in and adds them to corresponding committees. |
| 350 | */ |
| 351 | export async function syncUserToChaptersFromSlackChannels( |
| 352 | workosUserId: string, |
| 353 | slackUserId: string |
| 354 | ): Promise<SyncUserChaptersResult> { |
| 355 | const result: SyncUserChaptersResult = { |
| 356 | workos_user_id: workosUserId, |
| 357 | slack_user_id: slackUserId, |
| 358 | channels_checked: 0, |
| 359 | chapters_joined: 0, |
| 360 | chapters_already_member: 0, |
| 361 | chapters: [], |
| 362 | errors: [], |
| 363 | }; |
| 364 | |
| 365 | if (!isSlackConfigured()) { |
| 366 | result.errors.push('Slack is not configured (ADDIE_BOT_TOKEN missing)'); |
| 367 | return result; |
| 368 | } |
| 369 | |
| 370 | try { |
| 371 | logger.info( |
| 372 | { workosUserId, slackUserId }, |
| 373 | 'Starting user committee sync from Slack channels' |
| 374 | ); |
| 375 | |
| 376 | // Get all channels the user is a member of |
| 377 | let userChannelIds: string[]; |
| 378 | try { |
| 379 | userChannelIds = await getUserChannels(slackUserId); |
| 380 | } catch (error) { |
| 381 | const errorMessage = error instanceof Error ? error.message : 'Unknown error'; |
| 382 | |
| 383 | // If Slack says the user doesn't exist, mark them deleted so we don't keep trying |
| 384 | if (errorMessage.includes('user_not_found')) { |
| 385 | logger.warn({ slackUserId }, 'Slack user no longer exists — marking as deleted'); |
| 386 | await slackDb.markSlackUserDeleted(slackUserId); |
| 387 | result.errors.push('Slack user no longer exists'); |
| 388 | return result; |
| 389 | } |
| 390 | |
| 391 | result.errors.push(`Failed to fetch user channels: ${errorMessage}`); |
| 392 | logger.error({ error, slackUserId }, 'Failed to fetch user channels from Slack'); |
| 393 | return result; |
| 394 | } |
| 395 | result.channels_checked = userChannelIds.length; |
| 396 | |
| 397 | if (userChannelIds.length === 0) { |
| 398 | logger.info({ slackUserId }, 'User is not a member of any channels'); |
| 399 | return result; |
| 400 | } |
| 401 | |
| 402 | // Get all committees that have Slack channels configured |
| 403 | const workingGroups = await workingGroupDb.listWorkingGroupsWithSlackChannel(); |
| 404 | |
| 405 | // Get user's Slack info for membership record |
| 406 | const slackMapping = await slackDb.getBySlackUserId(slackUserId); |
| 407 | |
| 408 | // Check each group with a Slack channel |
no test coverage detected