Initial commit: multi-company financial management system
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
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
-- 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;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
@@ -0,0 +1,129 @@
|
||||
// Central Account Manager — a platform-wide identity store (Person) and company registry,
|
||||
// fully separate from any single company's own isolated database and User table.
|
||||
// See the implementation plan, "Phase A — Account Manager: foundation & handoff".
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
model Person {
|
||||
id Int @id @default(autoincrement())
|
||||
email String @unique
|
||||
fullName String
|
||||
isActive Boolean @default(true)
|
||||
isOperator Boolean @default(false)
|
||||
credential PasswordCredential?
|
||||
twoFactor TwoFactorCredential?
|
||||
oauthAccounts OAuthAccount[]
|
||||
memberships PersonCompanyMembership[]
|
||||
auditLogs ProvisioningAuditLog[]
|
||||
createdAt DateTime @default(now())
|
||||
}
|
||||
|
||||
model PasswordCredential {
|
||||
id Int @id @default(autoincrement())
|
||||
personId Int @unique
|
||||
person Person @relation(fields: [personId], references: [id])
|
||||
passwordHash String
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model TwoFactorCredential {
|
||||
id Int @id @default(autoincrement())
|
||||
personId Int @unique
|
||||
person Person @relation(fields: [personId], references: [id])
|
||||
totpSecret String
|
||||
backupCodes String[] // hashed, one-time use
|
||||
isEnabled Boolean @default(false)
|
||||
enabledAt DateTime?
|
||||
}
|
||||
|
||||
enum OAuthProvider {
|
||||
GOOGLE
|
||||
MICROSOFT
|
||||
}
|
||||
|
||||
model OAuthAccount {
|
||||
id Int @id @default(autoincrement())
|
||||
personId Int
|
||||
person Person @relation(fields: [personId], references: [id])
|
||||
provider OAuthProvider
|
||||
providerAccountId String
|
||||
email String
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([provider, providerAccountId])
|
||||
}
|
||||
|
||||
enum CompanyStatus {
|
||||
PROVISIONING
|
||||
ACTIVE
|
||||
STOPPED
|
||||
FAILED
|
||||
DECOMMISSIONED
|
||||
}
|
||||
|
||||
model Company {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
slug String @unique
|
||||
status CompanyStatus @default(PROVISIONING)
|
||||
port Int? @unique
|
||||
databaseName String? @unique
|
||||
databaseUrl String? // plain column for now — see plan's "later hardening" note
|
||||
pm2ProcessName String? @unique
|
||||
frpProxyName String? @unique
|
||||
frpToken String?
|
||||
publicUrl String?
|
||||
provisionedAt DateTime?
|
||||
memberships PersonCompanyMembership[]
|
||||
auditLogs ProvisioningAuditLog[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model PersonCompanyMembership {
|
||||
id Int @id @default(autoincrement())
|
||||
personId Int
|
||||
person Person @relation(fields: [personId], references: [id])
|
||||
companyId Int
|
||||
company Company @relation(fields: [companyId], references: [id])
|
||||
companyUserEmail String? // override; defaults to Person.email when resolving the company-local User
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([personId, companyId])
|
||||
}
|
||||
|
||||
enum ProvisioningAction {
|
||||
COMPANY_CREATED
|
||||
DB_CREATED
|
||||
MIGRATIONS_RUN
|
||||
SEED_RUN
|
||||
PM2_STARTED
|
||||
PM2_STOPPED
|
||||
PM2_RESTARTED
|
||||
FRP_PROXY_REGISTERED
|
||||
IMPORT_FILE_GENERATED
|
||||
HANDOFF_ISSUED
|
||||
COMPANY_DECOMMISSIONED
|
||||
}
|
||||
|
||||
model ProvisioningAuditLog {
|
||||
id Int @id @default(autoincrement())
|
||||
companyId Int?
|
||||
company Company? @relation(fields: [companyId], references: [id])
|
||||
actorId Int?
|
||||
actor Person? @relation(fields: [actorId], references: [id])
|
||||
action ProvisioningAction
|
||||
detail Json?
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@index([companyId])
|
||||
@@index([action])
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
console.log('Seeding operator Person...');
|
||||
const operatorEmail = 'joosth2002@icloud.com';
|
||||
const passwordHash = await bcrypt.hash('ChangeMe123!', 12);
|
||||
|
||||
const operator = await prisma.person.upsert({
|
||||
where: { email: operatorEmail },
|
||||
create: { email: operatorEmail, fullName: 'System Administrator', isOperator: true },
|
||||
update: { isOperator: true },
|
||||
});
|
||||
|
||||
await prisma.passwordCredential.upsert({
|
||||
where: { personId: operator.id },
|
||||
create: { personId: operator.id, passwordHash },
|
||||
update: {},
|
||||
});
|
||||
|
||||
console.log('Seeding the one manually-provisioned company (Phase A)...');
|
||||
const company = await prisma.company.upsert({
|
||||
where: { slug: 'demo' },
|
||||
create: {
|
||||
name: 'Demo Company',
|
||||
slug: 'demo',
|
||||
status: 'ACTIVE',
|
||||
port: 5000,
|
||||
publicUrl: 'http://localhost:5173',
|
||||
provisionedAt: new Date(),
|
||||
},
|
||||
update: { status: 'ACTIVE', publicUrl: 'http://localhost:5173' },
|
||||
});
|
||||
|
||||
await prisma.personCompanyMembership.upsert({
|
||||
where: { personId_companyId: { personId: operator.id, companyId: company.id } },
|
||||
create: { personId: operator.id, companyId: company.id },
|
||||
update: { isActive: true },
|
||||
});
|
||||
|
||||
console.log(`Seed complete. Sign in as ${operatorEmail} with temporary password "ChangeMe123!" — change it after first login.`);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user