import { Controller, Get, Query, Res, Logger } from "@nestjs/common"; import { Response } from "express"; import { IntegrationsService } from "./integrations.service"; @Controller("api/integrations") export class IntegrationsController { private readonly logger = new Logger(IntegrationsController.name); constructor(private readonly integrationsService: IntegrationsService) {} @Get("google/callback") async googleCallback( @Query("code") code: string, @Query("state") state: string, @Query("error") error: string, @Res() res: Response, ) { if (error) { this.logger.warn(`Google OAuth error: ${error}`); return res.redirect(`${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=google_calendar&status=error`); } try { const result = await this.integrationsService.handleOAuthCallback(code, state); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=${result.provider}&status=${result.success ? "connected" : "error"}`, ); } catch (err: any) { this.logger.error(`Google OAuth callback failed: ${err.message}`); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=google_calendar&status=error`, ); } } @Get("outlook/callback") async outlookCallback( @Query("code") code: string, @Query("state") state: string, @Query("error") error: string, @Res() res: Response, ) { if (error) { this.logger.warn(`Outlook OAuth error: ${error}`); return res.redirect(`${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=outlook&status=error`); } try { const result = await this.integrationsService.handleOAuthCallback(code, state); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=${result.provider}&status=${result.success ? "connected" : "error"}`, ); } catch (err: any) { this.logger.error(`Outlook OAuth callback failed: ${err.message}`); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=outlook&status=error`, ); } } @Get("zoom/callback") async zoomCallback( @Query("code") code: string, @Query("state") state: string, @Query("error") error: string, @Res() res: Response, ) { if (error) { this.logger.warn(`Zoom OAuth error: ${error}`); return res.redirect(`${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=zoom&status=error`); } try { const result = await this.integrationsService.handleOAuthCallback(code, state); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=${result.provider}&status=${result.success ? "connected" : "error"}`, ); } catch (err: any) { this.logger.error(`Zoom OAuth callback failed: ${err.message}`); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=zoom&status=error`, ); } } @Get("riverside/callback") async riversideCallback( @Query("code") code: string, @Query("state") state: string, @Query("error") error: string, @Res() res: Response, ) { if (error) { this.logger.warn(`Riverside OAuth error: ${error}`); return res.redirect(`${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=riverside&status=error`); } try { const result = await this.integrationsService.handleOAuthCallback(code, state); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=${result.provider}&status=${result.success ? "connected" : "error"}`, ); } catch (err: any) { this.logger.error(`Riverside OAuth callback failed: ${err.message}`); return res.redirect( `${process.env.WEBAPP_URL || "http://localhost:3000"}/settings?integration=riverside&status=error`, ); } } }