Skip to content

feat: trigger prompt volume analysis after checkout - #582

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
BharadwajKanneveti:fix/auto-run-volume-analysis
Aug 3, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
BharadwajKanneveti:fix/auto-run-volume-analysis

Conversation

@BharadwajKanneveti

Copy link
Copy Markdown
Contributor

Summary

This PR automatically triggers prompt volume analysis after a successful Stripe checkout, so new users don't have to manually click Analyze prompts before seeing Volume and Competition data.

Changes

  • Extracted the shared prompt volume analysis logic into server/src/lib/volume-analysis.js.
  • Updated the manual /api/volumes/analyze-batch endpoint to reuse the shared implementation.
  • Added a new internal endpoint POST /api/internal/analyze-volumes secured with CRON_SECRET authentication.
  • The internal endpoint starts the analysis asynchronously and immediately returns 202 Accepted.
  • Triggered the new internal endpoint from the Stripe success flow immediately after the existing tracking kickoff.
  • Automatic onboarding analysis does not consume the user's monthly volume analysis quota; manual analysis behavior remains unchanged.

Related issue

Closes #541

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks — the shared volume-analysis.js extraction and the internal endpoint shape (CRON_SECRET + detached 202) are exactly right. Two things need fixing before this can land:

1. /analyze-batch no longer respects the caller's selection. The old endpoint analyzed the specific prompts array from the request body (the subset the user ticked in the Analyze dialog). The refactor resolves the brand from the first prompt and calls analyzeBrandVolumes(brandId), which analyzes every prompt in the brand:

  • Selecting 3 prompts on a 200-prompt brand now runs 200 serial LLM + DataForSEO calls inside one awaited request (cost + almost certain timeout).
  • volume_usage gets recorded for all analyzed prompts, not the user's selection.
  • The web client still sends locationCode / languageCode / force (web/src/lib/actions/volumes.ts → analyzePromptVolumesBatch), and the location/language overrides are now silently dropped.

Suggestion: give analyzeBrandVolumes optional { promptIds, locationCode, languageCode } and pass the request's subset + overrides through from /analyze-batch, keeping its old contract intact.

2. The auto-trigger isn't idempotent. The Stripe success route runs on every visit with a session_id (its only guard is payment_status === 'paid'), so a refresh of the success page re-fires the analysis — cached intents are reused, but fetchAndSaveVolumes still re-hits DataForSEO for every prompt. Add an onlyMissing: true mode for the internal path that skips prompts which already have a prompt_volumes row: the first post-checkout call analyzes everything (all prompts are new), and any repeat call becomes a no-op.

Minor: the two import lines from ../lib/volume-analysis.js in routes/volumes.js can be merged into one.

CI is green and the rest reads well — happy to approve once these two are in.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Both requested changes are addressed — analyze-batch now scopes to the selected prompt IDs, and the post-checkout trigger is idempotent via onlyMissing. Thanks for the quick turnaround!

@gkhngyk
gkhngyk merged commit 306a523 into ansvisor:main Aug 3, 2026
3 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.

Auto-run prompt volume analysis after checkout — drop the manual Analyze step for new users

2 participants