mirror of
https://github.com/documenso/documenso.git
synced 2026-06-22 04:12:06 +10:00
3887aa67c8
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
45 lines
1.3 KiB
TypeScript
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,
|
|
});
|
|
});
|