-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from giselles-ai/send-user-seat-meter-event
Send user seat meter event
- Loading branch information
Showing
9 changed files
with
2,234 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { toUTCDate } from "@/lib/date"; | ||
import { reportUserSeatUsage } from "@/services/usage-based-billing"; | ||
import type Stripe from "stripe"; | ||
import invariant from "tiny-invariant"; | ||
|
||
export async function handleSubscriptionCycleInvoice(invoice: Stripe.Invoice) { | ||
if (invoice.status !== "draft") { | ||
/** | ||
* User seat usage must be reported while the invoice is in draft status. | ||
* If this error occurs in production, consider adjusting the invoice finalization grace period: | ||
* https://docs.stripe.com/billing/subscriptions/usage-based/configure-grace-period | ||
* | ||
* Note: Extending the grace period will delay the invoice delivery to customers. | ||
* To minimize billing delays, we should consider manually calling finalizeInvoice() | ||
* immediately after reporting the usage. | ||
*/ | ||
throw new Error("Invoice is not in draft status"); | ||
} | ||
|
||
const subscription = invoice.subscription; | ||
invariant(subscription, "Invoice is missing a subscription ID"); | ||
const subscriptionId = | ||
typeof subscription === "string" ? subscription : subscription.id; | ||
|
||
const customer = invoice.customer; | ||
invariant(customer, "Invoice is missing a customer ID"); | ||
const customerId = typeof customer === "string" ? customer : customer.id; | ||
const periodEnd = new Date(invoice.period_end * 1000); | ||
const periodEndUTC = toUTCDate(periodEnd); | ||
|
||
await reportUserSeatUsage(subscriptionId, customerId, periodEndUTC); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function toUTCDate(date: Date): Date { | ||
return new Date( | ||
Date.UTC( | ||
date.getUTCFullYear(), | ||
date.getUTCMonth(), | ||
date.getUTCDate(), | ||
date.getUTCHours(), | ||
date.getUTCMinutes(), | ||
date.getUTCSeconds(), | ||
date.getUTCMilliseconds(), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CREATE TABLE IF NOT EXISTS "user_seat_usage_reports" ( | ||
"db_id" serial PRIMARY KEY NOT NULL, | ||
"team_db_id" integer NOT NULL, | ||
"user_db_id_list" integer[] NOT NULL, | ||
"stripe_meter_event_id" text NOT NULL, | ||
"created_at" timestamp DEFAULT now() NOT NULL | ||
); | ||
--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "user_seat_usage_reports" ADD CONSTRAINT "user_seat_usage_reports_team_db_id_teams_db_id_fk" FOREIGN KEY ("team_db_id") REFERENCES "public"."teams"("db_id") ON DELETE cascade ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; | ||
--> statement-breakpoint | ||
CREATE INDEX IF NOT EXISTS "user_seat_usage_reports_team_db_id_index" ON "user_seat_usage_reports" USING btree ("team_db_id");--> statement-breakpoint | ||
CREATE INDEX IF NOT EXISTS "user_seat_usage_reports_created_at_index" ON "user_seat_usage_reports" USING btree ("created_at");--> statement-breakpoint | ||
CREATE INDEX IF NOT EXISTS "user_seat_usage_reports_stripe_meter_event_id_index" ON "user_seat_usage_reports" USING btree ("stripe_meter_event_id"); |
Oops, something went wrong.