Skip to content

fix: update to correct recurrence endpoint#127

Merged
aimensahnoun merged 1 commit intomainfrom
118-easyinvoice---cannot-cancel-recurring-invoice
Aug 18, 2025
Merged

fix: update to correct recurrence endpoint#127
aimensahnoun merged 1 commit intomainfrom
118-easyinvoice---cannot-cancel-recurring-invoice

Conversation

@aimensahnoun
Copy link
Member

@aimensahnoun aimensahnoun commented Aug 17, 2025

Problem:

Enable to cancel recurring invoices from EasyInvoice

Solution:

  • Update EasyInvoice to use correct V2 endpoint and pass the necessary body parameters.

Summary by CodeRabbit

  • New Features
    • None.
  • Bug Fixes
    • Improved reliability when stopping recurring invoices, with clearer status updates and consistent error handling.
  • Chores
    • Backend updated to use a standardized update flow for managing recurrence status.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 17, 2025

Walkthrough

Replaces the stop-recurrence endpoint call with a generic PATCH to /v2/request/{requestId} sending isRecurrenceStopped: true. Status checking and error handling remain the same. On success, the database is updated to set isRecurrenceStopped to true. No exported/public API signatures changed.

Changes

Cohort / File(s) Summary
Invoice router: switch stop-recurrence call to generic PATCH
src/server/routers/invoice.ts
Replace call to /v2/request/${requestId}/stop-recurrence with PATCH /v2/request/${requestId} and payload { isRecurrenceStopped: true }; retain existing status checks, error handling, and DB update to isRecurrenceStopped = true.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Server (invoice router)
  participant External API
  participant DB

  Client->>Server (invoice router): Stop recurrence for requestId
  Server (invoice router)->>External API: PATCH /v2/request/{requestId} { isRecurrenceStopped: true }
  External API-->>Server (invoice router): Response (status)
  alt Success
    Server (invoice router)->>DB: Update request.isRecurrenceStopped = true
    Server (invoice router)-->>Client: Success
  else Error
    Server (invoice router)-->>Client: Error response
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 118-easyinvoice---cannot-cancel-recurring-invoice

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@aimensahnoun aimensahnoun linked an issue Aug 17, 2025 that may be closed by this pull request
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (2)
src/server/routers/invoice.ts (2)

392-421: Add an authorization guard: any logged-in user can stop someone else’s recurrence

This mutation doesn’t verify ownership of the request before patching the API and updating the DB. A logged-in user could stop recurrence for any known requestId. Enforce that the current user is the issuer (or whatever rule applies here) before proceeding.

Apply this diff to fetch the invoice, ensure it exists, and check authorization:

   .mutation(async ({ ctx, input }) => {
-      const { requestId } = input;
+      const { db, user } = ctx;
+      const { requestId } = input;
+
+      // Authorization: ensure the authenticated user owns this request
+      const invoice = await db.query.requestTable.findFirst({
+        where: eq(requestTable.requestId, requestId),
+      });
+      if (!invoice) {
+        throw new TRPCError({
+          code: "NOT_FOUND",
+          message: "Invoice with this request ID not found",
+        });
+      }
+      if (invoice.userId !== user?.id) {
+        throw new TRPCError({
+          code: "FORBIDDEN",
+          message: "You are not allowed to stop recurrence for this invoice",
+        });
+      }
 
-      const request = await apiClient.patch(`/v2/request/${requestId}`, {
+      const response = await apiClient.patch(`/v2/request/${requestId}`, {
         isRecurrenceStopped: true,
       });
 
-      if (request.status !== 200) {
+      if (response.status < 200 || response.status >= 300) {
         throw new TRPCError({
           code: "BAD_REQUEST",
-          message: "Failed to stop recurrence",
+          message: `Failed to stop recurrence: ${response.data?.message ?? "Unknown error"}`,
         });
       }
       const updatedInvoice = await ctx.db
         .update(requestTable)
         .set({
           isRecurrenceStopped: true,
         })
         .where(eq(requestTable.requestId, requestId))
         .returning();

413-417: Fix misleading error message (“payment reference” vs “request ID”)

You’re filtering by requestId, but the message says “payment reference”. Update the message for accuracy.

-        throw new TRPCError({
-          code: "NOT_FOUND",
-          message: "Invoice with this payment reference not found",
-        });
+        throw new TRPCError({
+          code: "NOT_FOUND",
+          message: "Invoice with this request ID not found",
+        });
🧹 Nitpick comments (1)
src/server/routers/invoice.ts (1)

395-401: Accept any 2xx response and surface backend error; also rename “request” to “response”

PATCH endpoints often return 200 or 204. Restricting to 200 can misclassify success as failure. Also, “request” here is actually an Axios response; rename for clarity and include the backend’s error message.

-      const request = await apiClient.patch(`/v2/request/${requestId}`, {
+      const response = await apiClient.patch(`/v2/request/${requestId}`, {
         isRecurrenceStopped: true,
       });
 
-      if (request.status !== 200) {
+      if (response.status < 200 || response.status >= 300) {
         throw new TRPCError({
           code: "BAD_REQUEST",
-          message: "Failed to stop recurrence",
+          message: `Failed to stop recurrence: ${response.data?.message ?? "Unknown error"}`,
         });
       }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 3b41f40 and 9af3d2e.

📒 Files selected for processing (1)
  • src/server/routers/invoice.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-07-11T12:56:11.332Z
Learnt from: bassgeta
PR: RequestNetwork/easy-invoice#82
File: src/server/routers/recurring-payment.ts:143-148
Timestamp: 2025-07-11T12:56:11.332Z
Learning: The `v2/payouts/recurring/{externalPaymentId}` PATCH endpoint in the RequestNetwork/easy-invoice project specifically returns a 200 status code on success or an error status code - it does not return other 2xx success codes like 201 or 204. The status check `response.status !== 200` is correct for this specific endpoint.

Applied to files:

  • src/server/routers/invoice.ts
🧬 Code Graph Analysis (1)
src/server/routers/invoice.ts (1)
src/lib/axios.ts (1)
  • apiClient (3-8)

Copy link
Member

@rodrigopavezi rodrigopavezi left a comment

Choose a reason for hiding this comment

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

looks good 👍

@aimensahnoun aimensahnoun merged commit a0d7c45 into main Aug 18, 2025
9 checks passed
@aimensahnoun aimensahnoun deleted the 118-easyinvoice---cannot-cancel-recurring-invoice branch August 18, 2025 12:11
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.

EasyInvoice - Cannot cancel Recurring Invoice

2 participants