|
| 1 | +import { stripe } from '@/actions/organization/lib/stripe'; |
| 2 | +import { db } from '@comp/db'; |
| 3 | +import { SubscriptionType } from '@comp/db/types'; |
| 4 | +import { NextResponse } from 'next/server'; |
| 5 | +import { syncStripeDataToKV } from '../syncStripeDataToKv'; |
| 6 | + |
| 7 | +// Type for request body |
| 8 | +interface RepairStripeDataRequest { |
| 9 | + org_id: string; |
| 10 | + stripe_sub_id: string; |
| 11 | +} |
| 12 | + |
| 13 | +// Error response helper |
| 14 | +function errorResponse(message: string, status: number) { |
| 15 | + return new Response(JSON.stringify({ error: message }), { |
| 16 | + status, |
| 17 | + headers: { 'Content-Type': 'application/json' }, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +// Success response helper |
| 22 | +function successResponse(message: string, data?: any) { |
| 23 | + return new Response(JSON.stringify({ message, data }), { |
| 24 | + status: 200, |
| 25 | + headers: { 'Content-Type': 'application/json' }, |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * POST /api/stripe/repair |
| 31 | + * |
| 32 | + * Repairs Stripe data for an organization by: |
| 33 | + * 1. Retrieving the Stripe subscription |
| 34 | + * 2. Updating the organization with the correct customer ID |
| 35 | + * 3. Syncing the data to KV store |
| 36 | + */ |
| 37 | +export async function POST(req: Request) { |
| 38 | + // Validate authentication |
| 39 | + const retoolCompApiSecret = process.env.RETOOL_COMP_API_SECRET; |
| 40 | + if (!retoolCompApiSecret) { |
| 41 | + return errorResponse('Server configuration error: retool comp api secret not configured', 500); |
| 42 | + } |
| 43 | + |
| 44 | + const authHeader = req.headers.get('authorization'); |
| 45 | + const token = authHeader?.split(' ')[1]; |
| 46 | + if (!token || token !== retoolCompApiSecret) { |
| 47 | + return NextResponse.json( |
| 48 | + { |
| 49 | + success: false, |
| 50 | + error: 'Unauthorized', |
| 51 | + }, |
| 52 | + { status: 401 }, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + // Parse and validate request body |
| 57 | + let body: RepairStripeDataRequest; |
| 58 | + try { |
| 59 | + body = await req.json(); |
| 60 | + } catch (error) { |
| 61 | + return errorResponse('Invalid JSON in request body', 400); |
| 62 | + } |
| 63 | + |
| 64 | + const { org_id: organizationId, stripe_sub_id: stripeSubscriptionId } = body; |
| 65 | + |
| 66 | + if (!organizationId) { |
| 67 | + return errorResponse('Missing required field: org_id', 400); |
| 68 | + } |
| 69 | + |
| 70 | + if (!stripeSubscriptionId) { |
| 71 | + return errorResponse('Missing required field: stripe_sub_id', 400); |
| 72 | + } |
| 73 | + |
| 74 | + // Fetch organization from database |
| 75 | + const organization = await db.organization.findUnique({ |
| 76 | + where: { id: organizationId }, |
| 77 | + }); |
| 78 | + |
| 79 | + if (!organization) { |
| 80 | + return errorResponse(`Organization not found: ${organizationId}`, 404); |
| 81 | + } |
| 82 | + |
| 83 | + // Retrieve Stripe subscription |
| 84 | + let subscription; |
| 85 | + try { |
| 86 | + subscription = await stripe.subscriptions.retrieve(stripeSubscriptionId); |
| 87 | + } catch (error) { |
| 88 | + return errorResponse(`Stripe subscription not found: ${stripeSubscriptionId}`, 404); |
| 89 | + } |
| 90 | + |
| 91 | + // Retrieve Stripe customer |
| 92 | + let customer; |
| 93 | + try { |
| 94 | + customer = await stripe.customers.retrieve(subscription.customer as string); |
| 95 | + } catch (error) { |
| 96 | + return errorResponse(`Stripe customer not found: ${subscription.customer}`, 404); |
| 97 | + } |
| 98 | + |
| 99 | + // Update organization with Stripe data |
| 100 | + const updatedOrganization = await db.organization.update({ |
| 101 | + where: { id: organizationId }, |
| 102 | + data: { |
| 103 | + stripeCustomerId: customer.id, |
| 104 | + subscriptionType: SubscriptionType.MANAGED, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + // Sync updated data to KV store |
| 109 | + await syncStripeDataToKV(updatedOrganization.id); |
| 110 | + |
| 111 | + return successResponse('Stripe data successfully repaired', { |
| 112 | + organizationId: updatedOrganization.id, |
| 113 | + stripeCustomerId: customer.id, |
| 114 | + subscriptionType: SubscriptionType.MANAGED, |
| 115 | + }); |
| 116 | +} |
0 commit comments