Library Utilities

126 utility modules in backend/src/lib/, providing foundational building blocks used across services and routes. These are not services themselves — they are stateless helpers, clients, and infrastructure components.

By Category

Authentication & Encryption

FilePurpose
auth.tsPassword hashing (argon2id via Bun.password.hash()), JWT creation
tokens.tsToken generation (portal, magic link, 2FA)
encryption.tsAES-256-GCM encryption (encryptToken/decryptToken)

Key: ENCRYPTION_MASTER_KEY — see Authentication, HubSpot Integration for encrypted token storage.

Validation & Sanitization

FilePurpose
validation.tsShared Zod schemas
sanitization-helpers.tsstrictTextField(), sanitizedLocalizedText(), optionalSanitizedLocalizedText()
schema-validator.tsRuntime schema validation

See Validation Pattern for usage rules.

File Handling

FilePurpose
file-operations.tsFile read/write operations
file-validation.tsFile type/size validation
mime-validation.tsMIME type allowlist
storage.tsS3/Tigris object storage client

See Files for the file management domain.

Business Logic

FilePurpose
funding-calculator.tsFunding amount calculations
u-value-calculator.tsThermal transmittance (U-value) calculations

See Financial Calculations, Energy Calculations.

Access Control

FilePurpose
permissions.tsPermission definitions and checks
ownership-guards.tsverifyProjectAccess(), verifyBuildingAccess(), verifyScenarioAccess()
department-access.tsDepartment-scoped data access

Always use RBAC-aware functions from ownership-guards.ts. See RBAC Authorization.

External API Clients

FilePurposeIntegration
hubspot.tsHubSpot API clientHubSpot Integration
dataverse-client.tsMicrosoft Dataverse clientDataverse
docusign-client.tsDocuSign eSignature clientDocuSign

Communication

Directory/FilePurpose
email/Email sending via Resend
email-templates/Email template definitions
sms/SMS via MessageBird

See Resend Email, MessageBird SMS, Notifications.

Infrastructure

FilePurpose
logger.tsStructured logging
sentry.tsSentry error tracking setup
cache.tsIn-memory caching
redis/Redis client and utilities
distributed-lock.tsDistributed locking via Redis

Background Jobs

DirectoryPurpose
jobs/Job factory, config, schedulers, processors

See Background Jobs for the job processing architecture.

Utilities

FilePurpose
pagination.tsCursor/offset pagination helpers
sort.tsMulti-column sort builder
search-utils.tsFull-text search helpers
number-utils.tsNumber formatting
pdf-generator.tsPDF document creation