Root company app (src/, client/) — GL foundation, AR, and AP: - Segmented Chart of Accounts (Sage 300-style), double-entry PostingService with three-layer balance enforcement (app validation, CHECK constraints, deferred DB trigger), gap-free document numbering, RBAC with branch restrictions. - Accounts Receivable: Customers, Invoices, Credit Notes, Receipts with allocation, all posting through PostingService. - Accounts Payable: Suppliers, Supplier Invoices, Debit Notes, Payments with allocation, mirroring AR's posting pattern. - Tax Codes shared between AR and AP. - Company-app login is a token handoff from the Account Manager (RS256, single-use, company-scoped) rather than a local password. - Frontend reskinned to the approved mockup: navy sidebar, dashboard, tree-view Chart of Accounts, Customer/Supplier 360 pages. Account Manager (manager/, manager/client/) — central login gateway: - Password + TOTP 2FA + Google/Microsoft OAuth, company picker, and the RS256 handoff token issuance that the company app verifies. Covered by unit + integration tests against a real Postgres database (GL balance invariants, AR/AP posting correctness and allocation limits, the full handoff exchange including replay/cross-company/expiry/forgery rejection). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tq2DbEvzHzs13jDFhLaX44
156 lines
5.3 KiB
SQL
156 lines
5.3 KiB
SQL
-- CreateEnum
|
|
CREATE TYPE "OAuthProvider" AS ENUM ('GOOGLE', 'MICROSOFT');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "CompanyStatus" AS ENUM ('PROVISIONING', 'ACTIVE', 'STOPPED', 'FAILED', 'DECOMMISSIONED');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "ProvisioningAction" AS ENUM ('COMPANY_CREATED', 'DB_CREATED', 'MIGRATIONS_RUN', 'SEED_RUN', 'PM2_STARTED', 'PM2_STOPPED', 'PM2_RESTARTED', 'FRP_PROXY_REGISTERED', 'IMPORT_FILE_GENERATED', 'HANDOFF_ISSUED', 'COMPANY_DECOMMISSIONED');
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Person" (
|
|
"id" SERIAL NOT NULL,
|
|
"email" TEXT NOT NULL,
|
|
"fullName" TEXT NOT NULL,
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
"isOperator" BOOLEAN NOT NULL DEFAULT false,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "Person_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "PasswordCredential" (
|
|
"id" SERIAL NOT NULL,
|
|
"personId" INTEGER NOT NULL,
|
|
"passwordHash" TEXT NOT NULL,
|
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
|
|
CONSTRAINT "PasswordCredential_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "TwoFactorCredential" (
|
|
"id" SERIAL NOT NULL,
|
|
"personId" INTEGER NOT NULL,
|
|
"totpSecret" TEXT NOT NULL,
|
|
"backupCodes" TEXT[],
|
|
"isEnabled" BOOLEAN NOT NULL DEFAULT false,
|
|
"enabledAt" TIMESTAMP(3),
|
|
|
|
CONSTRAINT "TwoFactorCredential_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "OAuthAccount" (
|
|
"id" SERIAL NOT NULL,
|
|
"personId" INTEGER NOT NULL,
|
|
"provider" "OAuthProvider" NOT NULL,
|
|
"providerAccountId" TEXT NOT NULL,
|
|
"email" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "OAuthAccount_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Company" (
|
|
"id" SERIAL NOT NULL,
|
|
"name" TEXT NOT NULL,
|
|
"slug" TEXT NOT NULL,
|
|
"status" "CompanyStatus" NOT NULL DEFAULT 'PROVISIONING',
|
|
"port" INTEGER,
|
|
"databaseName" TEXT,
|
|
"databaseUrl" TEXT,
|
|
"pm2ProcessName" TEXT,
|
|
"frpProxyName" TEXT,
|
|
"frpToken" TEXT,
|
|
"publicUrl" TEXT,
|
|
"provisionedAt" TIMESTAMP(3),
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
|
|
CONSTRAINT "Company_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "PersonCompanyMembership" (
|
|
"id" SERIAL NOT NULL,
|
|
"personId" INTEGER NOT NULL,
|
|
"companyId" INTEGER NOT NULL,
|
|
"companyUserEmail" TEXT,
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "PersonCompanyMembership_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "ProvisioningAuditLog" (
|
|
"id" SERIAL NOT NULL,
|
|
"companyId" INTEGER,
|
|
"actorId" INTEGER,
|
|
"action" "ProvisioningAction" NOT NULL,
|
|
"detail" JSONB,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "ProvisioningAuditLog_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Person_email_key" ON "Person"("email");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "PasswordCredential_personId_key" ON "PasswordCredential"("personId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "TwoFactorCredential_personId_key" ON "TwoFactorCredential"("personId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "OAuthAccount_provider_providerAccountId_key" ON "OAuthAccount"("provider", "providerAccountId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Company_slug_key" ON "Company"("slug");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Company_port_key" ON "Company"("port");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Company_databaseName_key" ON "Company"("databaseName");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Company_pm2ProcessName_key" ON "Company"("pm2ProcessName");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Company_frpProxyName_key" ON "Company"("frpProxyName");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "PersonCompanyMembership_personId_companyId_key" ON "PersonCompanyMembership"("personId", "companyId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "ProvisioningAuditLog_companyId_idx" ON "ProvisioningAuditLog"("companyId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "ProvisioningAuditLog_action_idx" ON "ProvisioningAuditLog"("action");
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "PasswordCredential" ADD CONSTRAINT "PasswordCredential_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "TwoFactorCredential" ADD CONSTRAINT "TwoFactorCredential_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "OAuthAccount" ADD CONSTRAINT "OAuthAccount_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "PersonCompanyMembership" ADD CONSTRAINT "PersonCompanyMembership_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "PersonCompanyMembership" ADD CONSTRAINT "PersonCompanyMembership_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "ProvisioningAuditLog" ADD CONSTRAINT "ProvisioningAuditLog_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "ProvisioningAuditLog" ADD CONSTRAINT "ProvisioningAuditLog_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES "Person"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|