Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: add fallbacks to stripe methods (#609)

Sid664593d34069
diff --git a/packages/stripe/src/stripe.server.ts b/packages/stripe/src/stripe.server.ts
index d084144fbb..28f3d92876 100644
--- a/packages/stripe/src/stripe.server.ts
+++ b/packages/stripe/src/stripe.server.ts
@@ -169,9 +169,19 @@ export async function getStripeCustomer(customerId: string) {
return null;
}
- const customer = await redis.get(`stripe:customer:${customerId}`);
- if (!customer) return null;
- return KvStripeCustomerSchema.parse(JSON.parse(customer));
+ const cached = await redis.get(`stripe:customer:${customerId}`);
+ if (cached) return KvStripeCustomerSchema.parse(JSON.parse(cached));
+
+ // Fallback: fetch from Stripe API and re-populate cache (self-heals after Redis migration data loss)
+ if (!stripe) return null;
+
+ try {
+ const result = await syncStripeDataToKV(customerId);
+ return result?.data ?? null;
+ } catch (error) {
+ console.error("Failed to sync stripe data from API fallback:", error);
+ return null;
+ }
}
const KvStripeUserSchema = z.string().nullish();
@@ -181,9 +191,25 @@ export async function getStripeCustomerId(companyId: string) {
return null;
}
- return KvStripeUserSchema.parse(
+ const cached = KvStripeUserSchema.parse(
await redis.get(`stripe:company:${companyId}`)
);
+ if (cached) return cached;
+
+ // Fallback: check companyPlan table
+ const serviceRole = getCarbonServiceRole();
+ const { data } = await serviceRole
+ .from("companyPlan")
+ .select("stripeCustomerId")
+ .eq("id", companyId)
+ .single();
+
+ const customerId = data?.stripeCustomerId;
+ if (customerId) {
+ await redis.set(`stripe:company:${companyId}`, customerId);
+ }
+
+ return customerId ?? null;
}
function getStripeWebhookEvent({