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 <dallinbentley98@gmail.com>
This commit is contained in:
Devin AI 2025-07-17 20:03:25 +00:00
parent 077d6de476
commit 55af2727a1
1 changed files with 11 additions and 10 deletions

View File

@ -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',