feat: Backend Scaling Part 49 — Audit Integrity, Per-Tenant Rate Limits & Resource Quotas - #377
Merged
Wilfred007 merged 20 commits intoJun 19, 2026
Conversation
…ly usage snapshots
…, access logs, quotas
… trail per request
…nto checkRateLimit
…nantConfigService
…-service quota visibility
…orcement on any route
Covers getQuotas (DB row present vs. fallback defaults) and assertEmployeeQuota (at-limit, over-limit, under-limit, error shape). DB pool is mocked via jest.mock so no live Postgres is needed.
scheduleDailyUsageSnapshots and scheduleNightlyIntegrityCheck are now started inside the server 'listen' callback alongside existing jobs so quota snapshots and audit-chain checks run automatically in production.
…otas Documents the migration, all new services/middleware/routes, background job schedule, and test matrix introduced by the backend-scaling-part-49 feature so reviewers and future contributors have a single reference.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #374.
This PR implements the three pillars described in the issue:
row_hash+chain_hashcolumns onapi_audit_logs, populated by apgcryptotrigger on every INSERT; a second trigger denies all UPDATE/DELETE; a nightly scheduled job walks the chain and emits a structured alert on any broken link.requests_per_minute/burst_sizeper organisation intenant_configurations; Redis caches them for 5 minutes. The circuit breaker automatically halves effective limits when an org's error rate exceeds 20 % in a 60-second rolling window.max_employees,max_monthly_transactions,max_storage_mbcolumns onorganization_settings; enforced before every employee/transaction INSERT; 80 % threshold warnings emitted as structured logs; daily snapshots stored intenant_usage_snapshotswith a 24-hour scheduled job.Files changed
backend/src/db/migrations/024_audit_integrity_and_quotas.sqlbackend/src/services/auditIntegrityService.tsbackend/src/services/tenantQuotaService.tsbackend/src/services/tenantRateLimitService.tsbackend/src/middleware/requireAdminJustification.tsx-admin-reasongatebackend/src/middleware/tenantQuotaMiddleware.tsquotaGuard()factorybackend/src/routes/adminRoutes.tsbackend/src/routes/tenantUsageRoutes.tsbackend/src/jobs/part49Jobs.tsbackend/src/app.ts/api/adminand/api/usagebackend/src/index.tsbackend/src/services/rateLimitService.tsbackend/src/middleware/advancedRateLimiting.tstenantIdtocheckRateLimitbackend/src/routes/employeeRoutes.tsquotaGuard('employees')middlewarebackend/src/services/tenantConfigService.tsbackend/src/middleware/enhancedTenantIsolation.tslogTenantAccessfire-and-forgetdocs/BACKEND_SCALING_PART_49.mdbackend/src/__tests__/backendScalingPart49.test.tsbackend/src/middleware/__tests__/requireAdminJustification.test.tsbackend/src/services/__tests__/tenantQuotaService.test.tsTest plan
npm test— all three new test files pass with DB pool mocked024_audit_integrity_and_quotas.sqlto staging DB and verify triggers firemax_employeeslimit → expect429withX-Quota-Resource: employeesGET /api/admin/tenants/:orgId/rate-limitswithoutx-admin-reason→ expect403200and a row inplatform_admin_access_logsapi_audit_logs→ expectERROR: audit log records are immutableGET /api/admin/audit/integritywith a tamperedchain_hashrow → expect a non-nullfirstBrokenLinkin the responsescheduleDailyUsageSnapshotsandscheduleNightlyIntegrityChecklog their first run on server start