48 lines
src/api/trials/activateTrial.ts
Creates a 14-day trial record for a new account and returns the expiry timestamp.
// POST /api/trials/activate — creates a 14-day free trial for a new account.import type { Request, Response } from "express";import { db } from "./db";const TRIAL_DAYS = 14;export interface TrialRecord { accountId: string; plan: "trial"; /** Unix timestamp in milliseconds — compared with Date.now() by access-gate services. */ trialEndsAt: number; createdAt: number;}
/** * Activates a 14-day free trial for a new account. * Returns 409 if a trial already exists for the account.*/
export async function activateTrial( req: Request, res: Response,): Promise<void> { const { accountId } = req.body as { accountId: string }; if (!accountId) { res.status(400).json({ error: "accountId is required" }); return;}
const existing = await db.trials.findByAccountId(accountId); if (existing) { res.status(409).json({ error: "Trial already active for this account" }); return;}
const now = Date.now(); const trialEndsAt = now + TRIAL_DAYS * 24 * 60 * 60; const trial: TrialRecord = {accountId,
plan: "trial",trialEndsAt,
createdAt: now,};
await db.trials.create(trial); res.status(201).json({ accountId, trialEndsAt, daysRemaining: TRIAL_DAYS });}