Skip to content

Add cancel subscription functionality to user dashboard#37

Merged
DavidASix merged 23 commits into
mainfrom
copilot/fix-33
Aug 11, 2025
Merged

Add cancel subscription functionality to user dashboard#37
DavidASix merged 23 commits into
mainfrom
copilot/fix-33

Conversation

Copilot AI commented Aug 10, 2025

Copy link
Copy Markdown
Contributor

This PR implements the ability for users to cancel their active subscriptions directly from their account dashboard. The implementation includes:

New API Endpoint

  • POST /api/purchases/cancel-subscription - Cancels all active Stripe subscriptions for the authenticated user
  • Uses existing type-safe API pattern with Zod schema validation
  • Returns success status and count of cancelled subscriptions
  • Includes comprehensive error handling for edge cases (no customer ID, no active subscriptions, Stripe API errors)

Enhanced Subscription Page UI

  • Added a "Cancel Subscription" button for users with active subscriptions
  • Implemented confirmation dialog using shadcn/ui AlertDialog component
  • Added loading states and proper success/error messaging with toast notifications
  • Automatically refreshes subscription status after cancellation using TanStack Query

Key Implementation Details

  • No database changes: As requested, the cancellation only affects Stripe subscriptions, leaving payment history intact
  • Authentication required: Uses existing withAuth middleware for user session validation
  • Graceful error handling: Handles cases where users have no Stripe customer ID or no active subscriptions
  • Consistent patterns: Follows existing codebase conventions for API endpoints, middleware usage, and UI components

Code Quality

  • Added comprehensive test coverage for the new API schema
  • All existing tests continue to pass
  • TypeScript compilation successful with full type safety
  • ESLint and Prettier checks passing
  • Production build verified

Future Enhancement Note

Added entry to TODO.md for the suggested future feature of adding a subscription cancellation flag to user accounts for UI nudging to re-subscribe.

The implementation provides a clean, user-friendly way to cancel subscriptions while maintaining data integrity and following established patterns in the codebase.

Fixes #33.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 10, 2025 14:10
…d tests

Co-authored-by: DavidASix <3901710+DavidASix@users.noreply.github.com>
Co-authored-by: DavidASix <3901710+DavidASix@users.noreply.github.com>
Copilot AI changed the title [WIP] Add cancel subscription option Add cancel subscription functionality to user dashboard Aug 10, 2025
Copilot AI requested a review from DavidASix August 10, 2025 14:14
@DavidASix
DavidASix requested a review from Copilot August 11, 2025 02:06

Copilot AI 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.

Pull Request Overview

This PR implements a comprehensive cancel subscription functionality that allows authenticated users to cancel their active Stripe subscriptions directly from their dashboard. The implementation maintains data integrity by only affecting Stripe subscriptions without modifying the database.

  • Added a new authenticated API endpoint for subscription cancellation with proper error handling
  • Enhanced the subscription page UI with a confirmation dialog and loading states
  • Added comprehensive test coverage for the new API schema validation

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/app/api/purchases/cancel-subscription/schema.ts Defines type-safe API schema with Zod validation for the cancellation endpoint
src/app/api/purchases/cancel-subscription/route.ts Implements the POST endpoint with authentication, Stripe integration, and error handling
src/app/(product)/subscription/page.tsx Adds cancel subscription UI with confirmation dialog and query cache invalidation
src/tests/cancel-subscription.test.ts Provides test coverage for the API schema validation
TODO.md Updates project status and adds future enhancement note

},
);

await Promise.all(cancellationPromises);

Copilot AI Aug 11, 2025

Copy link

Choose a reason for hiding this comment

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

The cancelledCount variable is being modified inside an async map callback, which creates a race condition. Multiple promises could increment the counter simultaneously, leading to an incorrect final count. Consider using Promise.allSettled() and counting the fulfilled results, or use a more thread-safe approach.

Suggested change
await Promise.all(cancellationPromises);
const cancellationPromises = subscriptions.data.map(
async (subscription) => {
try {
await stripe.subscriptions.cancel(subscription.id);
return true;
} catch (error) {
console.error(
`Failed to cancel subscription ${subscription.id}:`,
error,
);
return false;
}
},
);
const results = await Promise.allSettled(cancellationPromises);
const cancelledCount = results.filter(
(result) => result.status === "fulfilled" && result.value === true
).length;

Copilot uses AI. Check for mistakes.
Comment thread src/app/api/purchases/cancel-subscription/route.ts
Comment thread src/app/api/purchases/cancel-subscription/route.ts
@DavidASix
DavidASix marked this pull request as ready for review August 11, 2025 16:25
@DavidASix
DavidASix merged commit 0e857a5 into main Aug 11, 2025
2 checks passed
@DavidASix
DavidASix deleted the copilot/fix-33 branch August 17, 2025 13:57
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.

Add cancel subscription option

3 participants