Files
documenso/packages/trpc/server/admin-router/sync-organisation-subscription.ts
T
Lucas Smith 3887aa67c8 fix: rework stripe webhooks into idempotent subscription sync (#2977)
Replace per-event webhook handlers with a single sync function that
fetches the current state from Stripe and converges the local
subscription, claim, and organisation type.

- Create organisations upfront before checkout, restricted as
  "pending payment" until the first payment syncs
- Add rate-limited subscription sync route, triggered on checkout
  success so the UI doesn't wait on webhooks
- Surface pending payment state in banner, billing table, and limits
2026-06-12 16:01:03 +10:00

45 lines
1.3 KiB
TypeScript

import { syncStripeCustomerSubscription } from '@documenso/ee/server-only/stripe/sync-stripe-customer-subscription';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { prisma } from '@documenso/prisma';
import { adminProcedure } from '../trpc';
import {
ZSyncOrganisationSubscriptionRequestSchema,
ZSyncOrganisationSubscriptionResponseSchema,
} from './sync-organisation-subscription.types';
export const syncOrganisationSubscriptionRoute = adminProcedure
.input(ZSyncOrganisationSubscriptionRequestSchema)
.output(ZSyncOrganisationSubscriptionResponseSchema)
.mutation(async ({ input, ctx }) => {
const { organisationId, syncClaims } = input;
ctx.logger.info({
input: {
organisationId,
syncClaims,
},
});
const organisation = await prisma.organisation.findUnique({
where: { id: organisationId },
});
if (!organisation) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Organisation not found',
});
}
if (!organisation.customerId) {
throw new AppError(AppErrorCode.INVALID_REQUEST, {
message: 'Organisation has no Stripe customer to sync from',
});
}
await syncStripeCustomerSubscription({
customerId: organisation.customerId,
bypassClaimUpdate: !syncClaims,
});
});