MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / tryAutoLinkWebsiteUserToSlack

Function tryAutoLinkWebsiteUserToSlack

server/src/slack/sync.ts:488–575  ·  view source on GitHub ↗
(
  workosUserId: string,
  email: string
)

Source from the content-addressed store, hash-verified

486 * with the same email and links them if found.
487 */
488export async function tryAutoLinkWebsiteUserToSlack(
489 workosUserId: string,
490 email: string
491): Promise<AutoLinkResult> {
492 try {
493 // Find Slack user with this email
494 const slackUser = await slackDb.findByEmail(email);
495
496 if (!slackUser) {
497 logger.debug({ email }, 'No Slack user found for website user email');
498 return { linked: false, reason: 'no_slack_user' };
499 }
500
501 // Check if Slack user is already mapped to a different WorkOS user
502 if (slackUser.workos_user_id && slackUser.workos_user_id !== workosUserId) {
503 logger.debug(
504 { email, existingWorkosUserId: slackUser.workos_user_id, newWorkosUserId: workosUserId },
505 'Slack user already mapped to different WorkOS account'
506 );
507 return { linked: false, reason: 'slack_user_already_mapped' };
508 }
509
510 // Check if this WorkOS user is already mapped to a different Slack user
511 const existingMapping = await slackDb.getByWorkosUserId(workosUserId);
512 if (existingMapping && existingMapping.slack_user_id !== slackUser.slack_user_id) {
513 logger.debug(
514 { workosUserId, existingSlackUserId: existingMapping.slack_user_id },
515 'WorkOS user already mapped to different Slack account'
516 );
517 return { linked: false, reason: 'workos_user_already_mapped' };
518 }
519
520 // Already linked (same accounts)
521 if (slackUser.workos_user_id === workosUserId) {
522 logger.debug({ workosUserId, slackUserId: slackUser.slack_user_id }, 'Users already linked');
523 return { linked: false, reason: 'already_linked' };
524 }
525
526 // Skip bots and deleted users
527 if (slackUser.slack_is_bot) {
528 return { linked: false, reason: 'slack_user_is_bot' };
529 }
530 if (slackUser.slack_is_deleted) {
531 return { linked: false, reason: 'slack_user_is_deleted' };
532 }
533
534 // Link the accounts
535 await slackDb.mapUser({
536 slack_user_id: slackUser.slack_user_id,
537 workos_user_id: workosUserId,
538 mapping_source: 'email_auto',
539 });
540
541 logger.info(
542 { workosUserId, slackUserId: slackUser.slack_user_id, email },
543 'Auto-linked website user to Slack account by email'
544 );
545

Callers 2

setupAuthRoutesMethod · 0.85

Calls 6

findByEmailMethod · 0.80
getByWorkosUserIdMethod · 0.80
mapUserMethod · 0.80

Tested by

no test coverage detected