Skip to content

feat: Google Drive integration for analytics exports (Phase 7) - #42

Merged
ChitkulLakshya merged 7 commits into
mainfrom
feature/google-drive-integration
Aug 15, 2026
Merged

feat: Google Drive integration for analytics exports (Phase 7)#42
ChitkulLakshya merged 7 commits into
mainfrom
feature/google-drive-integration

Conversation

@ChitkulLakshya

Copy link
Copy Markdown
Collaborator

Phase 7: Google Drive Integration for Analytics Exports

Summary

Adds Google Drive integration so users can save analytics exports (CSV, Google Sheets, JSON) directly to their Google Drive. The integration is separate from the login OAuth flow — users connect Google Drive from Settings → Integrations after logging in.

User Flow

  1. User logs in (any method — email, Google, GitHub)
  2. Goes to Settings → Integrations → Google Drive card
  3. Clicks "Connect Google Drive" → redirected to Google consent screen
  4. Grants permission → redirected back to settings with "Connected" status
  5. Goes to Analytics page → clicks "Save to Drive" button
  6. File uploaded to their Google Drive as a Google Sheet (CSV) or JSON file
  7. Success banner shows file name + "Open" link to view in Google Drive

Changes

Backend (4 new files, 1 modified):

File Description
server/models/GoogleDriveToken.js MongoDB model for encrypted token storage
server/utils/googleDriveOAuth.js OAuth flow, token exchange, refresh, revoke
server/utils/googleDriveUpload.js CSV/JSON upload to Drive via googleapis
server/index.js 6 new endpoints (auth, callback, status, disconnect, 2x export)

Frontend (4 modified):

File Description
src/settings/IntegrationsSection.jsx Google Drive card with connect/disconnect
src/pages/AnalyticsPage.jsx "Save to Drive" button in overview header
src/pages/TemplateDetailAnalytics.jsx "Save to Drive" button in detail header
src/pages/AnalyticsPage.css Drive button + result banner styles

Docs (1 new):

File Description
docs/analytics/phase7-google-drive-integration.md Full architecture documentation

API Endpoints

Endpoint Method Description
/api/google-drive/auth GET Returns Google OAuth URL
/api/google-drive/callback GET OAuth callback — stores tokens, redirects to frontend
/api/google-drive/status GET Returns connection status + email
/api/google-drive/disconnect DELETE Revokes tokens + deletes from DB
/api/analytics/export/overview/drive POST Upload overview analytics to Drive
/api/analytics/templates/:draftId/export/drive POST Upload template detail to Drive

Security

  • Tokens encrypted with AES-256-GCM (ENCRYPTION_KEY env var)
  • drive.file scope only (non-sensitive — no Google verification needed)
  • OAuth state parameter prevents CSRF
  • Token revocation on disconnect
  • No tokens exposed to frontend

Prerequisites

  • GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET (already exist for Gmail)
  • Add drive.file scope to Google OAuth consent screen
  • Add redirect URI: https://yourdomain.com/api/google-drive/callback
  • Enable Google Drive API in Google Cloud Console
  • Optional: GOOGLE_DRIVE_REDIRECT_URI and FRONTEND_URL env vars for production

Build Verified

✓ built in 10.25s

Backend:
- New MongoDB model: GoogleDriveToken (encrypted token storage)
- New utility: googleDriveOAuth.js (OAuth flow, token refresh, revoke)
- New utility: googleDriveUpload.js (CSV/JSON upload to Drive)
- 6 new API endpoints: auth, callback, status, disconnect, 2x export-to-drive
- CSV exports auto-convert to Google Sheets format
- Tokens encrypted with AES-256-GCM, drive.file scope only

Frontend:
- Google Drive card in Settings > Integrations (connect/disconnect)
- 'Save to Drive' button in AnalyticsPage overview header
- 'Save to Drive' button in TemplateDetailAnalytics detail header
- Success banner with 'Open' link to uploaded file in Google Drive
- Green-themed Drive button styling

Docs:
- docs/analytics/phase7-google-drive-integration.md with full architecture
Copilot AI lite review requested due to automatic review settings August 15, 2026 10:18
@netlify

netlify Bot commented Aug 15, 2026

Copy link
Copy Markdown

Deploy Preview for leddger-ai ready!

Name Link
🔨 Latest commit 535e846
🔍 Latest deploy log https://app.netlify.com/projects/leddger-ai/deploys/6a806ee7d0a44e0008e25e2f
😎 Deploy Preview https://deploy-preview-42--leddger-ai.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

- Redirect to /dashboard/settings/integrations (not /dashboard/settings)
  so the frontend route map matches and user lands on Integrations tab
- IntegrationsSection now reads ?drive=connected|error query param
  from OAuth callback and shows success/error toast
- Clean URL after reading param via history.replaceState
12 end-to-end connection maps in docs/connections/:
- README.md: index + shared infrastructure overview
- 01-auth-login-flow: Login → Supabase OAuth → JWT → route guard
- 02-github-oauth-connect: Settings → Supabase identity linking
- 03-google-calendar-connect: Settings → Supabase identity linking
- 04-google-drive-oauth-connect: Backend OAuth → token storage → connect/disconnect
- 05-analytics-overview-load: 4 parallel API calls → MongoDB aggregation → charts
- 06-analytics-detail-load: Template detail + submissions + GitHub analysis
- 07-analytics-sync: Supabase → MongoDB backfill
- 08-analytics-export-to-drive: CSV/JSON → Google Drive upload
- 09-email-campaign-schedule: Agenda scheduler → nodemailer → send log
- 10-template-draft-lifecycle: Create → schedule → activate → submit → expire
- 11-account-deletion: Cascade across Supabase + MongoDB + Cloudinary
- 12-avatar-upload: Sharp compress → Cloudinary → Supabase URL

Each map includes ASCII flow diagram, file-by-file trace with line numbers,
shared dependencies, error paths, and environment variables.
- DELETE /api/user/data now calls revokeTokens() to revoke Google tokens
  and delete GoogleDriveToken from MongoDB
- DELETE /api/user/account same fix
- Updated connection map doc to reflect the fix
- Found during connection map analysis (was flagged as known gap)
…der)

Problem: Render 512MB instance was loading googleapis, sharp, and multer
at server startup even when those features were never used.

Changes:
- emailService.js: googleapis + nodemailer now lazy-loaded via
  getOAuth2() and getNodemailer() on first email send (~40-60MB saved)
- index.js: sharp now lazy-loaded inside compressToTargetSize()
  on first avatar upload (~20-30MB saved)
- index.js: multer now lazy-loaded via getUpload() on first file
  upload (~5MB saved)
- Route handlers updated to use getUpload().single('file') wrapper

Already optimized (no changes needed):
- googleapis in googleDriveOAuth.js, googleDriveUpload.js, scheduler.js
- cloudinary via getCloudinary()
- agenda via getAgenda()
- app.listen guarded by require.main === module

Result: Startup RAM reduced from ~140MB to ~70MB (~50% reduction)
- First email send: +50MB (one-time, cached by Node)
- First avatar upload: +25MB (one-time, cached by Node)

Added docs/connections/13-ram-optimization.md with full analysis.
@ChitkulLakshya
ChitkulLakshya merged commit 4973faa into main Aug 15, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants