Refactor Slack integration update endpoint to support more flexible settings

Co-authored-by: dallin <dallin@buster.so>
This commit is contained in:
Cursor Agent 2025-07-03 20:50:53 +00:00
parent ba01e881e6
commit 151662f300
2 changed files with 17 additions and 15 deletions

View File

@ -20,9 +20,13 @@ const OAuthCallbackSchema = z.object({
state: z.string(), state: z.string(),
}); });
const UpdateDefaultChannelSchema = z.object({ const UpdateIntegrationSchema = z.object({
name: z.string().min(1), defaultChannel: z
id: z.string().min(1), .object({
name: z.string().min(1),
id: z.string().min(1),
})
.optional(),
}); });
// Custom error class // Custom error class
@ -317,10 +321,10 @@ export class SlackHandler {
} }
/** /**
* PUT /api/v2/slack/integration/default-channel * PUT /api/v2/slack/integration
* Update default channel for Slack integration * Update Slack integration settings
*/ */
async updateDefaultChannel(c: Context) { async updateIntegration(c: Context) {
try { try {
// Get service instance (lazy initialization) // Get service instance (lazy initialization)
const slackOAuthService = this.getSlackOAuthService(); const slackOAuthService = this.getSlackOAuthService();
@ -350,7 +354,7 @@ export class SlackHandler {
// Parse request body // Parse request body
const body = await c.req.json(); const body = await c.req.json();
const parsed = UpdateDefaultChannelSchema.safeParse(body); const parsed = UpdateIntegrationSchema.safeParse(body);
if (!parsed.success) { if (!parsed.success) {
throw new SlackError( throw new SlackError(
@ -361,21 +365,19 @@ export class SlackHandler {
} }
// Get active integration // Get active integration
const { getActiveIntegration, updateDefaultChannel } = await import( const { getActiveIntegration, updateIntegration } = await import('./services/slack-helpers');
'./services/slack-helpers'
);
const integration = await getActiveIntegration(organizationGrant.organizationId); const integration = await getActiveIntegration(organizationGrant.organizationId);
if (!integration) { if (!integration) {
throw new SlackError('No active Slack integration found', 404, 'INTEGRATION_NOT_FOUND'); throw new SlackError('No active Slack integration found', 404, 'INTEGRATION_NOT_FOUND');
} }
// Update default channel // Update integration settings
await updateDefaultChannel(integration.id, parsed.data); await updateIntegration(integration.id, parsed.data);
return c.json({ return c.json({
message: 'Default channel updated successfully', message: 'Integration updated successfully',
defaultChannel: parsed.data, ...parsed.data,
}); });
} catch (error) { } catch (error) {
console.error('Failed to update default channel:', error); console.error('Failed to update default channel:', error);

View File

@ -9,8 +9,8 @@ const app = new Hono()
.get('/auth/callback', (c) => slackHandler.handleOAuthCallback(c)) .get('/auth/callback', (c) => slackHandler.handleOAuthCallback(c))
// Protected endpoints // Protected endpoints
.get('/integration', requireAuth, (c) => slackHandler.getIntegration(c)) .get('/integration', requireAuth, (c) => slackHandler.getIntegration(c))
.put('/integration', requireAuth, (c) => slackHandler.updateIntegration(c))
.delete('/integration', requireAuth, (c) => slackHandler.removeIntegration(c)) .delete('/integration', requireAuth, (c) => slackHandler.removeIntegration(c))
.put('/integration/default-channel', requireAuth, (c) => slackHandler.updateDefaultChannel(c))
// Error handling // Error handling
.onError((e, c) => { .onError((e, c) => {
if (e instanceof SlackError) { if (e instanceof SlackError) {