Skip to main content
Module: Platform (PF)
Feature ID: PF-64
Version: 1.0.0
Last Updated: 2026-01-30
Status: Production Ready

Executive Summary

The Organization Document Templates module provides centralized management of letterheads, document templates, and approval chain templates for cross-core document generation. This enables consistent, professional document output across GR (Governance & Risk), HR, and other modules.

Architecture Overview

Module Purpose and Scope

PF-64 provides:
  • Letterhead Management: Organization branding for document headers/footers
  • Document Templates: Structure templates for policies, procedures, letters
  • Approval Chain Templates: Reusable approval workflows
  • PDF Generation: Server-side document rendering with letterhead integration

Key Design Decisions

  1. Multi-tenant with System Templates: Organizations can create custom templates while also accessing system-provided templates
  2. Version Control: Document templates maintain immutable version history
  3. Status-based Deprecation: Templates use status fields instead of soft-delete to preserve audit trails
  4. Dynamic Approver Resolution: Approval chains integrate with HR module for manager/department head lookup

Database Design

Entity Relationship Diagram

Table Specifications

pf_letterheads

pf_document_templates

pf_document_template_versions

pf_approval_chain_templates

JSONB Schemas

Section Definition

Template Config

Approval Step

Index Strategy


RLS Policy Summary

Multi-Tenant Isolation

All tables implement organization-based RLS:

System Template Protection

System templates (is_system = true) are read-only:

Version History Immutability

Template versions are append-only:

Hook Architecture

Query Key Factory Pattern

Mutation Pattern with Cache Invalidation

Error Handling Conventions


Edge Function Design

generate-templated-pdf Architecture

PDF-lib Usage Patterns

Performance Considerations


Security Model

Permission Requirements

Multi-Tenant Isolation

  1. RLS on all tables: Every query filtered by organization_id
  2. System template protection: is_system templates read-only
  3. Edge function validation: Verify org membership before processing
  4. Storage bucket policies: Organization-scoped paths

Data Protection

  1. No PHI in templates: Templates contain structure, not patient data
  2. Audit trail: Version history preserved, no hard deletes
  3. Signed URLs: Time-limited access to generated documents

Consumer Integration Guide

GR-01 Policy Integration

GR-11 Procedure Integration

HR Offer Letter Integration

Custom Integration Pattern


Testing Strategy

Test Files

Coverage Targets

  • RLS Tests: 100%
  • Unit Tests: 80%+
  • Integration Tests: Critical paths covered

Appendix

Feature Flag

Enable via organization settings:
  • GR-01: Policy Management (consumer)
  • GR-11: Procedure Management (consumer)
  • HR: Offer Letters (consumer)
  • FW: Workflow Framework (approval integration)

Migration Notes

PF-64 was implemented in phases:
  • Phase 1: Database schema and types
  • Phase 2: RLS policies
  • Phase 3: React hooks and query layer
  • Phase 4: PDF generation edge function
  • Phase 5: Testing and documentation

Document Version History