Skip to content

[codex] Fix workflow queue dedupe - #444

Merged
tomas-cm1 merged 8 commits into
masterfrom
feature/queue-fix
Jun 17, 2026
Merged

[codex] Fix workflow queue dedupe#444
tomas-cm1 merged 8 commits into
masterfrom
feature/queue-fix

Conversation

@tomas-cm1

@tomas-cm1 tomas-cm1 commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR cleans up the workflow queue schema so the generic queue table no longer carries an order-specific order_id column. Product review reminder jobs now keep the order id in workflow arguments and use a generic dedupe_key for duplicate prevention.

Problem

workflow_queue_item is intended to be generic infrastructure for delayed workflow execution, but the table contained a nullable order_id column and an index on (workflow, order_id). That leaked order-domain details into the queue schema and made future workflow types less clean.

Changes

  • Remove order_id from the workflow queue model.
  • Add nullable dedupe_key to workflow_queue_item.
  • Add a unique partial index on (workflow, dedupe_key) for active rows where dedupe_key is present.
  • Store product review reminder identity as:
    • workflow = send-product-review-request
    • dedupe_key = send-review-reminder-{orderId}
    • arguments = { order_id }
  • Update product review queue dedupe lookup to use dedupe_key instead of querying arguments->>'order_id'.
  • Add migrations to drop the old order_id column/index and add/backfill dedupe_key.
  • Update the initial workflow queue migration so fresh databases create the new schema directly.

Validation

  • Ran Biome on changed workflow queue files successfully.
  • Ran local Medusa migrations successfully; Migration20260610110000 and Migration20260610120000 applied.
  • Restarted local backend and verified it became healthy.
  • Created test orders and verified queue rows contain the expected generic shape.
  • Ran bunx nx run medusa-be:build; backend compilation completed, but the full build failed during admin/frontend bundling because Rolldown could not resolve @medusajs/admin-shared from medusa-order-dashboard-plugin. This appears unrelated to the workflow queue changes.

Summary by CodeRabbit

Release Notes

  • New Features

    • Product review verification: customers can now only review products they have previously purchased with completed payment status.
  • Improvements

    • Optimised product review workflow queue system for improved performance and reduced duplicate request processing.
  • Chores

    • Updated database schema to support enhanced review request queue management.

@vercel

vercel Bot commented Jun 14, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
new-engine-ui-storybook Ready Ready Preview, Comment Jun 17, 2026 11:32am

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/queue-fix
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch feature/queue-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps

greptile-apps Bot commented Jun 14, 2026

Copy link
Copy Markdown

Greptile Summary

This PR refactors the workflow_queue_item schema to replace the domain-specific order_id column with a generic dedupe_key, and adds a new ensureCustomerPurchasedProduct guard so that only customers with captured orders can submit reviews without a review token.

  • Schema cleanup: order_id is removed from the model and replaced by a nullable dedupe_key with a unique partial index on (workflow, dedupe_key) WHERE dedupe_key IS NOT NULL. The product-review-request job now stores its identity as dedupe_key = send-review-reminder-{orderId} and keeps order_id in the workflow arguments.
  • Dedupe lookup update: hasQueuedReviewRequest switches from query.graph (filtering by order_id) to listWorkflowQueueItems (filtering by dedupe_key), consistent with the new schema.
  • Purchase verification: ensureCustomerPurchasedProduct is called in the review POST handler for non-token requests, querying orders filtered by customer_id, items.product_id, and payment_status.

Confidence Score: 3/5

Not safe to merge until the migration handles existing databases correctly.

The consolidated migration uses CREATE TABLE IF NOT EXISTS, which silently skips schema creation for any database that ran the previous migrations. It then attempts to create a unique index on (workflow, dedupe_key) — a column that doesn't exist in the old schema — causing the migration to error out entirely. Any developer or environment with the old table in place cannot migrate forward without manual intervention or a DB reset, and post-failure the application code that references dedupe_key will break every queue operation.

apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617111954.ts needs ALTER TABLE guards to add dedupe_key and drop order_id before the index creation for databases that already have the old schema.

Important Files Changed

Filename Overview
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617111954.ts Replaces two deleted migrations as a single fresh-start CREATE TABLE; silently skips table creation for existing databases then fails when trying to create the unique index on the non-existent dedupe_key column.
apps/medusa-be/src/api/store/reviews/helpers.ts Adds ensureCustomerPurchasedProduct guarding review creation; filters on payment_status: ["captured", "completed"] where "completed" is not a valid Medusa v2 payment status value (flagged in previous review thread).
apps/medusa-be/src/api/store/reviews/route.ts Correctly gates ensureCustomerPurchasedProduct behind !tokenRecord; review-token path is unaffected.
apps/medusa-be/src/utils/product-review-request-queue.ts Migrates dedupe check from query.graph to listWorkflowQueueItems using dedupe_key; generates key as send-review-reminder-{orderId}; still has a narrow race between the pre-check and insert (flagged in prior outside-diff comment).
apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts Replaces order_id with nullable dedupe_key; adds unique partial index on (workflow, dedupe_key) where dedupe_key IS NOT NULL; model definition is correct.
apps/medusa-be/src/modules/workflow-queue/migrations/.snapshot-workflow-queue.json Snapshot correctly reflects the new schema: dedupe_key (nullable text) replaces order_id, and the unique partial index replaces the old non-unique compound index.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Client
    participant ReviewPOST as POST /store/reviews
    participant DB as Database

    Client->>ReviewPOST: "POST {product_id, review_token?}"
    ReviewPOST->>DB: retrieveReviewToken (if token provided)
    ReviewPOST->>DB: ensureProductExists
    alt No review token
        ReviewPOST->>DB: "query.graph(order, filters={customer_id, items.product_id, payment_status})"
        DB-->>ReviewPOST: matching orders
        ReviewPOST-->>Client: 403 NOT_ALLOWED (if no orders found)
    end
    ReviewPOST->>DB: ensureReviewDoesNotExist
    ReviewPOST->>DB: createReviewWorkflow.run()
    ReviewPOST-->>Client: "200 {review}"

    Note over ReviewPOST,DB: Queue side-path (separate flow)
    participant Scheduler
    participant QueueService as WorkflowQueueService
    Scheduler->>QueueService: "listWorkflowQueueItems({dedupe_key, workflow})"
    QueueService-->>Scheduler: existing items
    alt Not already queued/sent
        Scheduler->>QueueService: "createWorkflowQueueItems({workflow, dedupe_key, run_at, arguments:{order_id}})"
        Note right of QueueService: Unique index on (workflow, dedupe_key) enforces DB-level deduplication
    end
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Client
    participant ReviewPOST as POST /store/reviews
    participant DB as Database

    Client->>ReviewPOST: "POST {product_id, review_token?}"
    ReviewPOST->>DB: retrieveReviewToken (if token provided)
    ReviewPOST->>DB: ensureProductExists
    alt No review token
        ReviewPOST->>DB: "query.graph(order, filters={customer_id, items.product_id, payment_status})"
        DB-->>ReviewPOST: matching orders
        ReviewPOST-->>Client: 403 NOT_ALLOWED (if no orders found)
    end
    ReviewPOST->>DB: ensureReviewDoesNotExist
    ReviewPOST->>DB: createReviewWorkflow.run()
    ReviewPOST-->>Client: "200 {review}"

    Note over ReviewPOST,DB: Queue side-path (separate flow)
    participant Scheduler
    participant QueueService as WorkflowQueueService
    Scheduler->>QueueService: "listWorkflowQueueItems({dedupe_key, workflow})"
    QueueService-->>Scheduler: existing items
    alt Not already queued/sent
        Scheduler->>QueueService: "createWorkflowQueueItems({workflow, dedupe_key, run_at, arguments:{order_id}})"
        Note right of QueueService: Unique index on (workflow, dedupe_key) enforces DB-level deduplication
    end
Loading

Fix All in Codex

Reviews (7): Last reviewed commit: "fix(queue-fix): Workflow queue migration" | Re-trigger Greptile

Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts Outdated
Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

Code Review Summary

Status: 5 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 2
WARNING 2
SUGGESTION 1
Issue Details (click to expand)

CRITICAL

File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617111954.ts 6 Migration uses drop constraint but IDX_workflow_queue_item_workflow_dedupe_key_unique is a UNIQUE INDEX, not a constraint. Should use DROP INDEX IF EXISTS instead.

WARNING

File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617111954.ts 11 CREATE UNIQUE INDEX without CONCURRENTLY will lock the table exclusively. On a populated workflow_queue_item table, this can cause downtime.
apps/medusa-be/src/api/store/reviews/helpers.ts 214 Purchase verification query is unbounded and should use pagination: { take: 1 } because only existence is needed.

SUGGESTION

File Line Issue
apps/medusa-be/src/api/store/reviews/helpers.ts 222 Purchase check can accept canceled/archived/draft orders because it filters only by payment_status and does not exclude invalid final order statuses.
Other Observations (not in diff)

Issues found in unchanged code that cannot receive inline comments:

File Line Issue
apps/medusa-be/src/utils/product-review-request-queue.ts 189 Queue insert race can surface as an unhandled unique-constraint error because the pre-check and insert are still not atomic.
Files Reviewed (6 files)
  • apps/medusa-be/src/api/store/reviews/helpers.ts - 2 issues (carried forward)
  • apps/medusa-be/src/api/store/reviews/route.ts - no issues
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617111954.ts - 2 issues (new file)
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts - no issues
  • apps/medusa-be/src/utils/product-review-request-queue.ts - 1 issue (carried forward)
  • apps/medusa-be/src/modules/workflow-queue/migrations/.snapshot-workflow-queue.json - generated snapshot file
Previous Review Summaries (5 snapshots, latest commit 9a77a8f)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 9a77a8f)

Status: 7 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 1
WARNING 4
SUGGESTION 2
Issue Details (click to expand)

CRITICAL

File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts 9 Column rename will fail because order_id is already dropped by Migration20260610110000.ts.

WARNING

File Line Issue
apps/medusa-be/src/api/store/reviews/helpers.ts 222 Purchase check can accept canceled/archived/draft orders because it filters only by payment_status and does not exclude invalid final order statuses.
apps/medusa-be/src/utils/product-review-request-queue.ts 189 Queue insert race can surface as an unhandled unique-constraint error because the pre-check and insert are still not atomic.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 15 Unique index creation may lock a populated queue table because it is not CONCURRENTLY.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts 6 Constraint name refers to an index that hasn't been created yet when this migration runs.

SUGGESTION

File Line Issue
apps/medusa-be/src/api/store/reviews/helpers.ts 214 Purchase verification query is unbounded and should use pagination: { take: 1 } because only existence is needed.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 12 Duplicate cleanup hard-deletes queue rows; soft-deleting would preserve operational history while still satisfying the partial unique index.
Files Reviewed (7 files)
  • apps/medusa-be/src/api/store/reviews/helpers.ts - 2 issues
  • apps/medusa-be/src/api/store/reviews/route.ts - no new issues found
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts - no new issues found
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts - 2 issues
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts - 2 issues (new file)
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts - no new issues found
  • apps/medusa-be/src/utils/product-review-request-queue.ts - 1 issue

Previous review (commit c8a6609)

Status: 7 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 1
WARNING 4
SUGGESTION 2
Issue Details (click to expand)

CRITICAL

File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts 9 Column rename will fail because order_id is already dropped by Migration20260610110000.ts.

WARNING

File Line Issue
apps/medusa-be/src/api/store/reviews/helpers.ts 222 Purchase check can accept canceled/archived/draft orders because it filters only by payment_status and does not exclude invalid final order statuses.
apps/medusa-be/src/utils/product-review-request-queue.ts 189 Queue insert race can surface as an unhandled unique-constraint error because the pre-check and insert are still not atomic.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 15 Unique index creation may lock a populated queue table because it is not CONCURRENTLY.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts 6 Constraint name refers to an index that hasn't been created yet when this migration runs.

SUGGESTION

File Line Issue
apps/medusa-be/src/api/store/reviews/helpers.ts 214 Purchase verification query is unbounded and should use pagination: { take: 1 } because only existence is needed.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 12 Duplicate cleanup hard-deletes queue rows; soft-deleting would preserve operational history while still satisfying the partial unique index.
Files Reviewed (7 files)
  • apps/medusa-be/src/api/store/reviews/helpers.ts - 2 issues
  • apps/medusa-be/src/api/store/reviews/route.ts - no new issues found
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts - no new issues found
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts - 2 issues
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts - 2 issues (new file)
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts - no new issues found
  • apps/medusa-be/src/utils/product-review-request-queue.ts - 1 issue

Previous review (commit 50ce286)

Status: 5 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 3
SUGGESTION 2
Issue Details (click to expand)

WARNING

File Line Issue
apps/medusa-be/src/api/store/reviews/helpers.ts 222 Purchase check can accept canceled/archived/draft orders because it filters only by payment_status and does not exclude invalid final order statuses.
apps/medusa-be/src/utils/product-review-request-queue.ts 189 Queue insert race can surface as an unhandled unique-constraint error because the pre-check and insert are still not atomic.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 15 Unique index creation may lock a populated queue table because it is not CONCURRENTLY.

SUGGESTION

File Line Issue
apps/medusa-be/src/api/store/reviews/helpers.ts 214 Purchase verification query is unbounded and should use pagination: { take: 1 } because only existence is needed.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 12 Duplicate cleanup hard-deletes queue rows; soft-deleting would preserve operational history while still satisfying the partial unique index.
Resolved Issues (click to expand)

CRITICAL

File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 9 Added dedupe_key backfill from arguments->>'order_id', resolving the prior missing-backfill issue for existing product review queue rows.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 12 Added duplicate cleanup before creating the unique index, resolving the prior risk that pre-existing duplicate active rows would block the migration.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 9 Backfill now uses arguments->>'order_id', so Migration20260610110000 dropping the physical order_id column before this migration no longer blocks the backfill.
Existing Inline Comments (carried forward)
File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 16 Existing Greptile comment: unique index creation will fail if duplicates exist from the old schema. The latest commit adds duplicate deletion, but the active comment remains stale.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 16 Existing Greptile comment: missing dedupe_key backfill. The latest commit adds backfill, but the active comment remains stale.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610080000.ts N/A Existing human comment: changing an existing migration is risky if it has already been applied locally or in another environment.
Files Reviewed (6 files)
  • apps/medusa-be/src/api/store/reviews/helpers.ts - 2 issues
  • apps/medusa-be/src/api/store/reviews/route.ts - no new issues found
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts - no new issues found
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts - 2 issues
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts - no new issues found
  • apps/medusa-be/src/utils/product-review-request-queue.ts - 1 issue

Fix these issues in Kilo Cloud

Previous review (commit 45135c4)

Status: No New Issues Found | Recommendation: Previous critical issues appear resolved; existing inline comments should be reviewed before merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 0
Resolved Issues (click to expand)

CRITICAL

File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 9 Added dedupe_key backfill from arguments->>'order_id', resolving the prior missing-backfill issue for existing product review queue rows.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 12 Added duplicate cleanup before creating the unique index, resolving the prior risk that pre-existing duplicate active rows would block the migration.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 9 Backfill now uses arguments->>'order_id', so Migration20260610110000 dropping the physical order_id column before this migration no longer blocks the backfill.
Existing Inline Comments (carried forward)
File Line Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts N/A Existing Greptile comment: unique index creation will fail if duplicates exist from the old schema. The latest commit adds duplicate deletion, but the active comment remains.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts 16 Existing Greptile comment: missing dedupe_key backfill. The latest commit adds backfill, but the active comment remains.
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610080000.ts N/A Existing human comment: changing an existing migration is risky if it has already been applied locally or in another environment. The latest commit reverts the initial migration to the old schema, but the active comment remains unresolved.
Files Reviewed (2 files)
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610080000.ts - no new issues found; reverts the initial migration to create the old order_id schema before later cleanup migrations.
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts - no new issues found; adds dedupe_key backfill and duplicate cleanup before the unique index.

Previous review (commit da1c74a)

Status: 2 Critical Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 2
Issue Details (click to expand)

CRITICAL

File Issue
apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts Missing backfill of dedupe_key column. Existing workflow_queue_item rows with workflow='send-product-review-request' will have dedupe_key=NULL after migration runs, making them invisible to the new deduplication lookup in hasQueuedReviewRequest(). This could result in duplicate emails being sent for the same order.
Migration order Migration20260610110000 drops the order_id column before Migration20260610120000 can read from it to backfill dedupe_key. If a backfill is needed, the migrations must be reordered or the backfill logic must be in Migration20260610110000 before dropping the column.
Other Observations (not in diff)

No additional issues found outside the diff.

Files Reviewed (4 files)
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts - model changes look correct
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610080000.ts - initial schema updated correctly
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts - drops order_id column
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts - adds dedupe_key column and unique index
  • apps/medusa-be/src/utils/product-review-request-queue.ts - uses dedupe_key correctly

Reviewed by laguna-m.1-20260312:free · 2,352,302 tokens

Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610080000.ts Outdated
@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Caution

Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted.

Error details
{}

Comment thread apps/medusa-be/src/api/store/reviews/helpers.ts
Comment thread apps/medusa-be/src/api/store/reviews/helpers.ts
Comment thread apps/medusa-be/src/api/store/reviews/helpers.ts
Comment thread apps/medusa-be/src/utils/product-review-request-queue.ts
Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts Outdated
Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts Outdated
@redeyecz

Copy link
Copy Markdown
Collaborator

nemas tam ani snapshot DB ani aktualni schemu vuci modelu ... soude dle tech migration namingach tak to bylo generated ne skrze MODULE=workflow_queue mise run dev:medusa:migration:generate, pls fix

Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts Outdated

@coderabbitai coderabbitai Bot 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.

♻️ Duplicate comments (1)
apps/medusa-be/src/api/store/reviews/helpers.ts (1)

214-223: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Tighten purchase-eligibility filtering in the guard query.

Line 222 only filters by payment_status, so cancelled/archived/draft orders can still satisfy this authorisation check. Please align this guard with the existing paid-order eligibility contract and bound the existence query to a single match.

Also applies to: 226-231

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/medusa-be/src/api/store/reviews/helpers.ts` around lines 214 - 223, The
query.graph call in the purchase-eligibility check is filtering only by
payment_status but allows cancelled, archived, or draft orders to pass the
authorization check. Extend the filters object in the query.graph method call to
explicitly exclude unwanted order statuses (cancelled, archived, draft) or
restrict to only allowed statuses that align with the existing paid-order
eligibility contract. Additionally, add a limit parameter to the query.graph
call to bound the existence check to a single match, ensuring the query only
returns one order result.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@apps/medusa-be/src/api/store/reviews/helpers.ts`:
- Around line 214-223: The query.graph call in the purchase-eligibility check is
filtering only by payment_status but allows cancelled, archived, or draft orders
to pass the authorization check. Extend the filters object in the query.graph
method call to explicitly exclude unwanted order statuses (cancelled, archived,
draft) or restrict to only allowed statuses that align with the existing
paid-order eligibility contract. Additionally, add a limit parameter to the
query.graph call to bound the existence check to a single match, ensuring the
query only returns one order result.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: a90b1a1f-dbf4-4847-b72b-55cdd4a97819

📥 Commits

Reviewing files that changed from the base of the PR and between 52c2096 and c8a6609.

📒 Files selected for processing (8)
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/.snapshot-workflow-queue.json
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
  • apps/medusa-be/src/utils/product-review-request-queue.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Greptile Review
  • GitHub Check: main
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Kilo Code Review
🧰 Additional context used
📓 Path-based instructions (14)
apps/medusa-be/src/modules/**/*

📄 CodeRabbit inference engine (CLAUDE.md)

Place custom Medusa modules under apps/medusa-be/src/modules

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/medusa-be/**/*.{ts,tsx}

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

apps/medusa-be/**/*.{ts,tsx}: Use TypeScript for type checking - run npx tsc --noEmit for validation
Forbidden: Non-null assertions (!) - always use type guards and validation instead
Annotate generic field access with explicit unknown type before type guards - const v: unknown = result[field]
Don't use as Type without validation - always validate before casting
Use Modules.* and ContainerRegistrationKeys.* constants instead of hardcoding strings
Batch operations with CHUNK_SIZE to avoid unbounded operations
Extract pure functions to separate files for testability without runtime dependencies

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/utils/product-review-request-queue.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/medusa-be/**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

apps/medusa-be/**/*.{ts,tsx,js,jsx}: Use Biome linter with ultracite preset - run bunx biome check --write . and always use braces in conditionals
Comments should explain 'why', never 'what' - use self-documenting code via clear naming
Always use const per declaration - const a = 1; const b = 2 is correct, one variable per line
Use nullish coalescing (??) operator instead of logical OR (||) for default values

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/utils/product-review-request-queue.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/medusa-be/src/modules/**/*.ts

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

apps/medusa-be/src/modules/**/*.ts: Module directories use hyphens (my-module/), module keys use underscores (my_module), and export module key as constant
Loaders cannot resolve cross-module dependencies - use __hooks.onApplicationStart for deferred initialization instead

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/medusa-be/src/{api,modules}/**/*.ts

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

Use MedusaError with proper error types - INVALID_DATA(400), NOT_FOUND(404), UNAUTHORIZED(401), NOT_ALLOWED(400), DUPLICATE_ERROR(422), CONFLICT(409)

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/medusa-be/src/{modules,api}/**/*.ts

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

Provider ID format in DB: {identifier}_{id} (e.g., my_shipping_default), in container: fp_{identifier}_{id}

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Import UI components using the @techsio/ui-kit namespace, not @libs/ui, for runtime apps

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/utils/product-review-request-queue.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Run Biome linting and formatting only on changed files using 'bunx biome check --write path/to/file'

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/utils/product-review-request-queue.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/medusa-be/src/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Medusa backend custom logic should be organized in api/, modules/, workflows/, admin/, subscribers/, and jobs/ directories under apps/medusa-be/src/

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/utils/product-review-request-queue.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/**/!(medusa-be)/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use modern React patterns and React 19 for frontend applications in the monorepo

Files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
  • apps/medusa-be/src/utils/product-review-request-queue.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
apps/medusa-be/src/api/**/*

📄 CodeRabbit inference engine (CLAUDE.md)

Place custom Medusa backend API endpoints under apps/medusa-be/src/api

Files:

  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
apps/medusa-be/src/{api,jobs}/**/*.{ts,tsx}

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

Use Query service for data retrieval - access via container.resolve<Query>(ContainerRegistrationKeys.QUERY) and use query.graph()

Files:

  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
apps/medusa-be/src/api/**/*.ts

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

apps/medusa-be/src/api/**/*.ts: Colocate validators.ts, middlewares.ts, and route.ts together - export route middlewares as MiddlewareRoute[] array
Route handlers should access validated request data via req.validatedBody - it is type-safe and pre-validated

Files:

  • apps/medusa-be/src/api/store/reviews/helpers.ts
  • apps/medusa-be/src/api/store/reviews/route.ts
apps/medusa-be/src/modules/**/models/*.ts

📄 CodeRabbit inference engine (apps/medusa-be/CLAUDE.md)

apps/medusa-be/src/modules/**/models/*.ts: Data layer indexes must be soft-delete safe - use where: { deleted_at: null } for unique constraints
Model definitions: Use model.text() for strings, model.float() for decimals, model.bigNumber() for high-precision (money)
Soft-delete index format: { on: ["handle"], unique: true, where: { deleted_at: null } } (DML format)
Module checks: Use checks() method with named SQL expressions for column constraints

Files:

  • apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts
🧠 Learnings (1)
📚 Learning: 2026-05-18T16:32:35.366Z
Learnt from: redeyecz
Repo: TechsioCZ/new-engine PR: 409
File: apps/medusa-be/src/modules/producer/migrations/Migration20260518122326.ts:9-35
Timestamp: 2026-05-18T16:32:35.366Z
Learning: In `apps/medusa-be` MikroORM migration files under `src/modules/**/migrations/`, keep SQL schema-agnostic: do not explicitly qualify tables/indexes with a schema prefix (e.g., avoid `

Applied to files:

  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts
  • apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts
🔇 Additional comments (10)
apps/medusa-be/src/api/store/reviews/route.ts (1)

4-5: LGTM!

Also applies to: 29-31

apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610120000.ts (2)

11-13: Previous reviewer noted: hard-deleting duplicates removes queue history.

Soft-deleting would satisfy the unique index whilst preserving auditability. This concern was raised in an earlier review.


14-16: Previous reviewer noted: non-concurrent index creation may lock the table.

CREATE UNIQUE INDEX without CONCURRENTLY can hold an exclusive lock on a populated table during deployment. Consider a concurrent index strategy or a scheduled deployment window. This concern was raised in an earlier review.

apps/medusa-be/src/utils/product-review-request-queue.ts (3)

185-203: Previous reviewer noted: race window between pre-check and insert.

The unique index prevents duplicates, but a concurrent caller passing hasQueuedReviewRequest() can fail the insert with an unhandled unique-constraint error. Consider catching and handling the constraint violation as "already queued". This concern was raised in an earlier review.


17-67: LGTM!


116-134: LGTM!

apps/medusa-be/src/modules/workflow-queue/models/workflow-queue-item.ts (1)

1-30: LGTM!

apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260610110000.ts (1)

1-25: LGTM!

apps/medusa-be/src/modules/workflow-queue/migrations/.snapshot-workflow-queue.json (1)

1-190: LGTM!

apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts (1)

3-20: Unable to complete verification due to technical limitations. The review comment's critical assertions about the migration chain (specifically that Migration20260610110000 drops order_id, Migration20260610120000 adds dedupe_key, and Migration20260617104357 attempts to rename a non-existent column) cannot be independently verified. Access to the migration files is required to confirm or refute these claims.

Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts Outdated
Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts Outdated
Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts Outdated
Comment thread apps/medusa-be/src/modules/workflow-queue/migrations/Migration20260617104357.ts Outdated
@tomas-cm1
tomas-cm1 merged commit 5858213 into master Jun 17, 2026
7 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants