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: redis json parsing (#608)

Sid62e035e04ec2
diff --git a/apps/erp/app/modules/shared/shared.server.ts b/apps/erp/app/modules/shared/shared.server.ts
index 16b47b1359..3dd28530f2 100644
--- a/apps/erp/app/modules/shared/shared.server.ts
+++ b/apps/erp/app/modules/shared/shared.server.ts
@@ -60,7 +60,10 @@ export async function getCustomFieldsSchemas(
let schema: CustomFieldsTableType[] | null = null;
try {
- schema = (await redis.get(key)) as CustomFieldsTableType[] | null;
+ const cachedSchema = await redis.get(key);
+ if (cachedSchema) {
+ schema = JSON.parse(cachedSchema) as CustomFieldsTableType[];
+ }
} finally {
if (schema) {
return {
diff --git a/apps/erp/app/modules/users/users.server.ts b/apps/erp/app/modules/users/users.server.ts
index db60812cb0..6949b94f8e 100644
--- a/apps/erp/app/modules/users/users.server.ts
+++ b/apps/erp/app/modules/users/users.server.ts
@@ -594,9 +594,15 @@ export async function getUserClaims(userId: string, companyId: string) {
} | null = null;
try {
- claims = JSON.parse(
- (await redis.get(getPermissionCacheKey(userId))) || "null"
- );
+ const cachedClaims = await redis.get(getPermissionCacheKey(userId));
+ if (cachedClaims) {
+ claims = JSON.parse(cachedClaims) as {
+ permissions: Record<string, Permission>;
+ role: string | null;
+ };
+ }
+ } catch (e) {
+ console.error("Failed to get claims from redis", e);
} finally {
// if we don't have permissions from redis, get them from the database
if (!claims) {
diff --git a/packages/auth/src/config/env.ts b/packages/auth/src/config/env.ts
index a5ec99305d..779eb67525 100644
--- a/packages/auth/src/config/env.ts
+++ b/packages/auth/src/config/env.ts
@@ -273,7 +273,8 @@ export const STRIPE_BYPASS_USER_IDS = getEnv("STRIPE_BYPASS_USER_IDS", {
isRequired: false
});
export const REDIS_URL = getEnv("REDIS_URL", {
- isRequired: false
+ isRequired: true,
+ isSecret: true
});
export const SESSION_MAX_AGE = 60 * 60 * 24 * 7; // 7 days;
export const REFRESH_ACCESS_TOKEN_THRESHOLD = 60 * 10; // 10 minutes left before token expires
diff --git a/packages/auth/src/services/users.server.ts b/packages/auth/src/services/users.server.ts
index 16b1070b86..01c99681e6 100644
--- a/packages/auth/src/services/users.server.ts
+++ b/packages/auth/src/services/users.server.ts
@@ -28,10 +28,13 @@ export async function getUserClaims(userId: string, companyId: string) {
} | null = null;
try {
- claims = (await redis.get(getPermissionCacheKey(userId))) as {
- permissions: Record<string, Permission>;
- role: string | null;
- };
+ const cachedClaims = await redis.get(getPermissionCacheKey(userId));
+ if (cachedClaims) {
+ claims = JSON.parse(cachedClaims) as {
+ permissions: Record<string, Permission>;
+ role: string | null;
+ };
+ }
} catch (e) {
console.error("Failed to get claims from redis", e);
} finally {
diff --git a/packages/stripe/src/stripe.server.ts b/packages/stripe/src/stripe.server.ts
index fa44fd7d31..d084144fbb 100644
--- a/packages/stripe/src/stripe.server.ts
+++ b/packages/stripe/src/stripe.server.ts
@@ -170,7 +170,8 @@ export async function getStripeCustomer(customerId: string) {
}
const customer = await redis.get(`stripe:customer:${customerId}`);
- return KvStripeCustomerSchema.nullish().parse(customer);
+ if (!customer) return null;
+ return KvStripeCustomerSchema.parse(JSON.parse(customer));
}
const KvStripeUserSchema = z.string().nullish();
@@ -547,7 +548,7 @@ export async function syncStripeDataToKV(
};
const [, companyPlan] = await Promise.all([
- redis.set(key, subData),
+ redis.set(key, JSON.stringify(subData)),
upsertCompanyPlan(serviceRole, companyPlanData)
]);
diff --git a/scripts/migrations/stripe-customer-migrations.ts b/scripts/migrations/stripe-customer-migrations.ts
index c1dd149e8e..38b97bb2fd 100644
--- a/scripts/migrations/stripe-customer-migrations.ts
+++ b/scripts/migrations/stripe-customer-migrations.ts
@@ -171,7 +171,7 @@ const KvStripeCustomerSchema = z.object({
};
const [, companyPlan] = await Promise.all([
- redis.set(customerKey, subData),
+ redis.set(customerKey, JSON.stringify(subData)),
upsertCompanyPlan(client, companyPlanData),
updateCompanyOwner(client, companyId, company.ownerId),
]);