import Fastify from 'fastify'
import {
FlowgladServer,
type CreateUsageEventParams,
} from '@flowglad/server'
import { getSessionUser } from './auth'
import { flowglad } from '@/utils/flowglad'
const fastify = Fastify()
fastify.post('/api/usage', async (request, reply) => {
const {
amount,
priceId,
subscriptionId,
usageMeterId,
transactionId,
usageDate,
properties,
} = request.body as CreateUsageEventParams
// Extract customerExternalId from your auth/session
// This should be YOUR app's user/organization ID, NOT Flowglad's customer ID
const userId = await getUserIdFromRequest(request)
const usageEvent = await flowglad(userId).createUsageEvent({
amount,
priceId,
subscriptionId,
usageMeterId,
transactionId,
usageDate: usageDate ?? Date.now(),
properties,
})
reply.send({ usageEvent })
})
fastify.listen({ port: 3000 })