Skip to content

Commit 784fe70

Browse files
committed
regen migrations
1 parent fdd1da3 commit 784fe70

7 files changed

Lines changed: 17523 additions & 22 deletions

File tree

apps/sim/lib/billing/calculations/usage-monitor.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { member, userStats } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { toError } from '@sim/utils/errors'
55
import { and, eq } from 'drizzle-orm'
6+
import { defaultBillingPeriod } from '@/lib/billing/core/billing-period'
67
import {
78
getHighestPrioritySubscription,
89
type HighestPrioritySubscription,
@@ -22,13 +23,6 @@ const logger = createLogger('UsageMonitor')
2223

2324
const WARNING_THRESHOLD = 80
2425

25-
function defaultBillingPeriod(): { start: Date; end: Date } {
26-
return {
27-
start: new Date(0),
28-
end: new Date(Date.UTC(9999, 11, 31)),
29-
}
30-
}
31-
3226
interface UsageData {
3327
percentUsed: number
3428
isWarning: boolean
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const OPEN_BILLING_PERIOD_START = new Date(0)
2+
const OPEN_BILLING_PERIOD_END = new Date(Date.UTC(9999, 11, 31))
3+
4+
export function defaultBillingPeriod(): { start: Date; end: Date } {
5+
return {
6+
start: OPEN_BILLING_PERIOD_START,
7+
end: OPEN_BILLING_PERIOD_END,
8+
}
9+
}

apps/sim/lib/billing/core/usage-log.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import { createLogger } from '@sim/logger'
55
import { toError } from '@sim/utils/errors'
66
import { generateId } from '@sim/utils/id'
77
import { and, desc, eq, gte, lte, sql } from 'drizzle-orm'
8+
import { defaultBillingPeriod } from '@/lib/billing/core/billing-period'
89
import { getHighestPrioritySubscription } from '@/lib/billing/core/plan'
910
import { isOrgScopedSubscription } from '@/lib/billing/subscriptions/utils'
1011
import { isBillingEnabled } from '@/lib/core/config/feature-flags'
1112

1213
const logger = createLogger('UsageLog')
13-
const OPEN_BILLING_PERIOD = {
14-
start: new Date(0),
15-
end: new Date(Date.UTC(9999, 11, 31)),
16-
} as const
1714

1815
/**
1916
* Usage log category types
@@ -97,10 +94,6 @@ function stableEventKey(parts: Record<string, unknown>): string {
9794
return createHash('sha256').update(payload).digest('hex')
9895
}
9996

100-
function defaultBillingPeriod(): { start: Date; end: Date } {
101-
return { ...OPEN_BILLING_PERIOD }
102-
}
103-
10497
async function resolveBillingContext(
10598
userId: string,
10699
billingEntity?: BillingEntity,

apps/sim/lib/billing/core/usage.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
renderUsageThresholdEmail,
1111
} from '@/components/emails'
1212
import { getEffectiveBillingStatus } from '@/lib/billing/core/access'
13+
import { defaultBillingPeriod } from '@/lib/billing/core/billing-period'
1314
import {
1415
getHighestPrioritySubscription,
1516
type HighestPrioritySubscription,
@@ -38,13 +39,6 @@ import { getEmailPreferences } from '@/lib/messaging/email/unsubscribe'
3839

3940
const logger = createLogger('UsageManagement')
4041

41-
function defaultBillingPeriod(): { start: Date; end: Date } {
42-
return {
43-
start: new Date(0),
44-
end: new Date(Date.UTC(9999, 11, 31)),
45-
}
46-
}
47-
4842
export interface OrgUsageLimitResult {
4943
limit: number
5044
minimum: number
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TYPE "public"."billing_entity_type" AS ENUM('user', 'organization');--> statement-breakpoint
2+
ALTER TABLE "usage_log" ADD COLUMN "event_key" text;--> statement-breakpoint
3+
ALTER TABLE "usage_log" ADD COLUMN "billing_entity_type" "billing_entity_type";--> statement-breakpoint
4+
ALTER TABLE "usage_log" ADD COLUMN "billing_entity_id" text;--> statement-breakpoint
5+
ALTER TABLE "usage_log" ADD COLUMN "billing_period_start" timestamp;--> statement-breakpoint
6+
ALTER TABLE "usage_log" ADD COLUMN "billing_period_end" timestamp;--> statement-breakpoint
7+
CREATE UNIQUE INDEX "usage_log_event_key_unique" ON "usage_log" USING btree ("event_key") WHERE "usage_log"."event_key" IS NOT NULL;--> statement-breakpoint
8+
CREATE INDEX "usage_log_billing_entity_period_idx" ON "usage_log" USING btree ("billing_entity_type","billing_entity_id","billing_period_start","billing_period_end") WHERE "usage_log"."billing_entity_type" IS NOT NULL;--> statement-breakpoint
9+
ALTER TABLE "usage_log" ADD CONSTRAINT "usage_log_billing_scope_all_or_none" CHECK ((
10+
("usage_log"."billing_entity_type" IS NULL AND "usage_log"."billing_entity_id" IS NULL AND "usage_log"."billing_period_start" IS NULL AND "usage_log"."billing_period_end" IS NULL)
11+
OR
12+
("usage_log"."billing_entity_type" IS NOT NULL AND "usage_log"."billing_entity_id" IS NOT NULL AND "usage_log"."billing_period_start" IS NOT NULL AND "usage_log"."billing_period_end" IS NOT NULL AND "usage_log"."billing_period_start" < "usage_log"."billing_period_end")
13+
)) NOT VALID;

0 commit comments

Comments
 (0)