feat(db): mesh data model — meshes, members, invites, audit log
- pgSchema "mesh" with 4 tables isolating the peer mesh domain - Enums: visibility, transport, tier, role - audit_log is metadata-only (E2E encryption enforced at broker/client) - Cascade on mesh delete, soft-delete via archivedAt/revokedAt Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
45
packages/db/src/schema/credit-transaction.ts
Normal file
45
packages/db/src/schema/credit-transaction.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { relations } from "drizzle-orm";
|
||||
import { integer, pgEnum, pgTable, text, timestamp } from "drizzle-orm/pg-core";
|
||||
|
||||
import { generateId } from "@turbostarter/shared/utils";
|
||||
|
||||
import { customer } from "./customer";
|
||||
|
||||
export const creditTransactionTypeEnum = pgEnum("credit_transaction_type", [
|
||||
"signup", // Initial free credits
|
||||
"purchase", // Bought via billing
|
||||
"usage", // Consumed by AI features
|
||||
"admin_grant", // Manually added by admin
|
||||
"admin_deduct", // Manually removed by admin
|
||||
"refund", // Refunded credits
|
||||
"promo", // Promotional credits
|
||||
"referral", // Referral bonus
|
||||
"expiry", // Credits expired
|
||||
]);
|
||||
|
||||
export const creditTransaction = pgTable("credit_transaction", {
|
||||
id: text().primaryKey().$defaultFn(generateId),
|
||||
customerId: text("customer_id")
|
||||
.notNull()
|
||||
.references(() => customer.id, { onDelete: "cascade" }),
|
||||
amount: integer().notNull(), // Positive = add, Negative = deduct
|
||||
type: creditTransactionTypeEnum().notNull(),
|
||||
reason: text(), // Human-readable description
|
||||
metadata: text(), // JSON for additional context (e.g., AI feature used)
|
||||
balanceAfter: integer("balance_after").notNull(), // Snapshot for reconciliation
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
createdBy: text("created_by"), // User ID who initiated (for admin actions)
|
||||
});
|
||||
|
||||
export const creditTransactionRelations = relations(
|
||||
creditTransaction,
|
||||
({ one }) => ({
|
||||
customer: one(customer, {
|
||||
fields: [creditTransaction.customerId],
|
||||
references: [customer.id],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
export type CreditTransaction = typeof creditTransaction.$inferSelect;
|
||||
export type InsertCreditTransaction = typeof creditTransaction.$inferInsert;
|
||||
Reference in New Issue
Block a user