diff --git a/server/db/pg/schema/privateSchema.ts b/server/db/pg/schema/privateSchema.ts index 9d493ed9..6ff542de 100644 --- a/server/db/pg/schema/privateSchema.ts +++ b/server/db/pg/schema/privateSchema.ts @@ -140,6 +140,7 @@ export const limits = pgTable("limits", { }) .notNull(), value: real("value"), + override: boolean("override").default(false), description: text("description") }); diff --git a/server/db/sqlite/schema/privateSchema.ts b/server/db/sqlite/schema/privateSchema.ts index 2571a65a..265cee10 100644 --- a/server/db/sqlite/schema/privateSchema.ts +++ b/server/db/sqlite/schema/privateSchema.ts @@ -129,6 +129,7 @@ export const limits = sqliteTable("limits", { }) .notNull(), value: real("value"), + override: integer("override", { mode: "boolean" }).default(false), description: text("description") }); diff --git a/server/lib/billing/limitsService.ts b/server/lib/billing/limitsService.ts index a07f70b3..cd49a809 100644 --- a/server/lib/billing/limitsService.ts +++ b/server/lib/billing/limitsService.ts @@ -2,6 +2,7 @@ import { db, limits } from "@server/db"; import { and, eq } from "drizzle-orm"; import { LimitSet } from "./limitSet"; import { FeatureId } from "./features"; +import logger from "@server/logger"; class LimitService { async applyLimitSetToOrg(orgId: string, limitSet: LimitSet): Promise { @@ -13,6 +14,36 @@ class LimitService { for (const [featureId, entry] of limitEntries) { const limitId = `${orgId}-${featureId}`; const { value, description } = entry; + // get the limit first + const [limit] = await trx + .select() + .from(limits) + .where(eq(limits.limitId, limitId)) + .limit(1); + + if (!limit) { + logger.warn( + `Limit with ID ${limitId} not found for org ${orgId}...` + ); + continue; + } + + // check if its overriden + if (limit.override) { + logger.debug( + `Skipping limit ${limitId} for org ${orgId} since it is overridden...` + ); + continue; + } + + // dont write if the value is the same + if (limit.value === value) { + logger.debug( + `Skipping limit ${limitId} for org ${orgId} since the value is the same (${value})...` + ); + continue; + } + await trx .insert(limits) .values({ limitId, orgId, featureId, value, description });