Add trial system

This commit is contained in:
Owen
2026-04-18 13:40:50 -07:00
parent b2d5a1ffdf
commit 55989c2019
18 changed files with 631 additions and 20 deletions

View File

@@ -12,9 +12,10 @@
*/
import Stripe from "stripe";
import { customers, db } from "@server/db";
import { customers, db, subscriptions } from "@server/db";
import { eq } from "drizzle-orm";
import logger from "@server/logger";
import { generateId } from "@server/auth/sessions/app";
export async function handleCustomerCreated(
customer: Stripe.Customer
@@ -38,14 +39,31 @@ export async function handleCustomerCreated(
return;
}
await db.insert(customers).values({
customerId: customer.id,
orgId: customer.metadata.orgId,
email: customer.email || null,
name: customer.name || null,
createdAt: customer.created,
updatedAt: customer.created
await db.transaction(async (trx) => {
await trx.insert(customers).values({
customerId: customer.id,
orgId: customer.metadata.orgId,
email: customer.email || null,
name: customer.name || null,
createdAt: customer.created,
updatedAt: customer.created
});
// Insert a 14-day trial subscription at tier3
const now = Math.floor(Date.now() / 1000);
const trialExpiresAt = now + 10 * 24 * 60 * 60;
const subscriptionId = `trial-${generateId(15)}`;
await trx.insert(subscriptions).values({
subscriptionId,
customerId: customer.id,
status: "active",
type: "tier3",
createdAt: now,
expiresAt: trialExpiresAt,
trial: true
});
});
logger.info(`Customer with ID ${customer.id} created successfully.`);
} catch (error) {
logger.error(