From 55af2727a10932d7529f1fb9a11947d41b93f1a1 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 17 Jul 2025 20:03:25 +0000 Subject: [PATCH] refactor(slack): extract shared scope validation helper function - Create validateScopes helper to avoid code duplication - Use shared function in both initiateOAuth and getIntegrationStatus - Addresses code review feedback from greptile-apps[bot] Co-Authored-By: Dallin Bentley --- .../v2/slack/services/slack-oauth-service.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/server/src/api/v2/slack/services/slack-oauth-service.ts b/apps/server/src/api/v2/slack/services/slack-oauth-service.ts index f7400a89f..303a5e993 100644 --- a/apps/server/src/api/v2/slack/services/slack-oauth-service.ts +++ b/apps/server/src/api/v2/slack/services/slack-oauth-service.ts @@ -4,6 +4,15 @@ import { SLACK_OAUTH_SCOPES } from '../constants'; import * as slackHelpers from './slack-helpers'; import { oauthStateStorage, tokenStorage } from './token-storage'; +/** + * Validates if an integration has all required OAuth scopes + */ +function validateScopes(currentScopeString?: string | null): boolean { + const currentScopes = currentScopeString ? currentScopeString.split(' ') : []; + const requiredScopes = [...SLACK_OAUTH_SCOPES]; + return requiredScopes.every((scope) => currentScopes.includes(scope)); +} + // Environment validation const SlackEnvSchema = z.object({ SLACK_CLIENT_ID: z.string().min(1), @@ -89,11 +98,7 @@ export class SlackOAuthService { // Check for existing integration - allow re-installation if scopes don't match const existing = await slackHelpers.getActiveIntegration(params.organizationId); if (existing) { - const currentScopes = existing.scope ? existing.scope.split(' ') : []; - const requiredScopes = [...SLACK_OAUTH_SCOPES]; - const hasAllRequiredScopes = requiredScopes.every((scope) => currentScopes.includes(scope)); - - if (hasAllRequiredScopes) { + if (validateScopes(existing.scope)) { throw new Error( 'Organization already has an active Slack integration with current scopes' ); @@ -262,11 +267,7 @@ export class SlackOAuthService { } // Validate scopes - const currentScopes = integration.scope ? integration.scope.split(' ') : []; - const requiredScopes = [...SLACK_OAUTH_SCOPES]; - const hasAllRequiredScopes = requiredScopes.every((scope) => currentScopes.includes(scope)); - - if (!hasAllRequiredScopes) { + if (!validateScopes(integration.scope)) { return { connected: true, status: 're_install_required',