fix: await stripe setup during org creation

This commit is contained in:
Matti Nannt
2026-02-20 09:57:48 +01:00
parent c8bc04fd6b
commit abd844c70c
2 changed files with 44 additions and 4 deletions

View File

@@ -204,6 +204,41 @@ describe("Organization Service", () => {
expect(ensureCloudStripeSetupForOrganization).toHaveBeenCalledWith("org1");
});
test("should still return organization when Stripe setup fails", async () => {
const mockOrganization = {
id: "org1",
name: "Test Org",
createdAt: new Date(),
updatedAt: new Date(),
billing: {
billingMode: "stripe" as const,
plan: PROJECT_FEATURE_KEYS.FREE,
limits: {
projects: BILLING_LIMITS.FREE.PROJECTS,
monthly: {
responses: BILLING_LIMITS.FREE.RESPONSES,
miu: BILLING_LIMITS.FREE.MIU,
},
},
stripeCustomerId: null,
periodStart: new Date(),
period: "monthly" as const,
},
isAIEnabled: false,
whitelabel: false,
};
vi.mocked(prisma.organization.create).mockResolvedValue(mockOrganization);
vi.mocked(ensureCloudStripeSetupForOrganization).mockRejectedValueOnce(
new Error("stripe temporarily unavailable")
);
const result = await createOrganization({ name: "Test Org" });
expect(result).toEqual(mockOrganization);
expect(ensureCloudStripeSetupForOrganization).toHaveBeenCalledWith("org1");
});
test("should throw DatabaseError on prisma error", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Database error", {
code: "P2002",

View File

@@ -145,10 +145,15 @@ export const createOrganization = async (
select,
});
// Stripe setup is best-effort and should not block organization creation.
void Promise.resolve(ensureCloudStripeSetupForOrganization(organization.id)).catch((error) => {
logger.warn({ error, organizationId: organization.id }, "Background Stripe setup failed");
});
// Stripe setup is best-effort but should be attempted before we return.
try {
await ensureCloudStripeSetupForOrganization(organization.id);
} catch (error) {
logger.warn(
{ error, organizationId: organization.id },
"Stripe setup failed after organization creation"
);
}
return organization;
} catch (error) {