mirror of https://github.com/buster-so/buster.git
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:
parent
077d6de476
commit
55af2727a1
|
@ -4,6 +4,15 @@ import { SLACK_OAUTH_SCOPES } from '../constants';
|
||||||
import * as slackHelpers from './slack-helpers';
|
import * as slackHelpers from './slack-helpers';
|
||||||
import { oauthStateStorage, tokenStorage } from './token-storage';
|
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
|
// Environment validation
|
||||||
const SlackEnvSchema = z.object({
|
const SlackEnvSchema = z.object({
|
||||||
SLACK_CLIENT_ID: z.string().min(1),
|
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
|
// Check for existing integration - allow re-installation if scopes don't match
|
||||||
const existing = await slackHelpers.getActiveIntegration(params.organizationId);
|
const existing = await slackHelpers.getActiveIntegration(params.organizationId);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
const currentScopes = existing.scope ? existing.scope.split(' ') : [];
|
if (validateScopes(existing.scope)) {
|
||||||
const requiredScopes = [...SLACK_OAUTH_SCOPES];
|
|
||||||
const hasAllRequiredScopes = requiredScopes.every((scope) => currentScopes.includes(scope));
|
|
||||||
|
|
||||||
if (hasAllRequiredScopes) {
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Organization already has an active Slack integration with current scopes'
|
'Organization already has an active Slack integration with current scopes'
|
||||||
);
|
);
|
||||||
|
@ -262,11 +267,7 @@ export class SlackOAuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate scopes
|
// Validate scopes
|
||||||
const currentScopes = integration.scope ? integration.scope.split(' ') : [];
|
if (!validateScopes(integration.scope)) {
|
||||||
const requiredScopes = [...SLACK_OAUTH_SCOPES];
|
|
||||||
const hasAllRequiredScopes = requiredScopes.every((scope) => currentScopes.includes(scope));
|
|
||||||
|
|
||||||
if (!hasAllRequiredScopes) {
|
|
||||||
return {
|
return {
|
||||||
connected: true,
|
connected: true,
|
||||||
status: 're_install_required',
|
status: 're_install_required',
|
||||||
|
|
Loading…
Reference in New Issue