Architecture Deep Dive PPAT Docs DOCX · R2 · NestJS Nov 24, 2026 · ~6 min read

Stop Fighting Your Document Templates: Four Ways to Manage PPAT DOCX Templates in Production

Adding a single new letter like Surat Keterangan Hibah used to touch six backend files, two frontend files, and still break in production with a 404 Template not found until someone manually uploaded the .docx file. Here is why that happened—and how four different architectures trade off simplicity, deploy safety, and Word customizability.

TL;DR Recommendation

Move to Bundled Defaults + Optional Custom Overrides: ship default templates/*.docx inside the Docker image as the built-in fallback, use Postgres + R2 only when staff uploads a customized version, and let the client read labels directly from the API. New document types then go live on deploy with zero manual steps—while keeping full Word customizability and a one-click Reset to Default.

01 · Diagnosis

Why Managing Templates Felt Hard

When templates live in an awkward halfway house between code (git-tracked files in api/templates/) and runtime data (rows in prisma.template pointing to Cloudflare R2), neither side owns the full lifecycle, creating four recurring headaches:

1. Four-Place Registration Drift

Maintenance

Every new document had to be registered in ppat-document.types.ts, ppat-documents.config.ts, templates.service.ts (two separate arrays), plus two hardcoded arrays in client.

2. Missing Files in Production Image

Deploy Gap

Dockerfile copied dist/, generated/, and prisma/, but never copied templates/. Committed templates were invisible in production, forcing manual uploads after every deploy.

3. Orphan R2 Objects on Re-Seed

Storage Waste

populateFromLocal() uploaded every local .docx to R2 before calling createMany({ skipDuplicates: true }), leaving unused duplicate files in R2 on every run.

4. Fragile Deploy Ordering

Runtime 404

Upload a template before deploying the API and TemplateValidationService rejects new tags; deploy first and forget to upload and staff hit a 404 when clicking Generate.

02 · Architectures

Comparing Four Template Management Approaches

Select an approach below to inspect its runtime flow, pros, cons, and concrete impact on this codebase.

03 · Side-by-Side

Trade-Off Matrix

Criteria 1. Bundled + Overrides ★ 2. Boot Auto-Sync 3. Code-Generated 4. Manual DB + R2
Zero-step prod deploy Yes — instant Yes — on boot Yes — instant No — manual upload
Staff can edit layout in Word Yes (via override) Yes No — needs dev PR Yes
One-click "Reset to Default" Built-in (delete row) Needs extra logic N/A (always code) No — re-upload file
Works if R2 is slow/down on boot Yes (reads local disk) Boot sync fails/delays Yes (no R2 read) Generation fails
Complex branching (multi-anak, AJB) Medium (presenter helps) Medium (presenter helps) Excellent (full TS) Medium
Git tracks default template updates Auto-applies if uncustomized Stale once seeded Auto-applies always Manual re-upload
Refactor effort Small (1–2 hrs) Small (~1 hr) High (rewrite templates) None

04 · Interactive Guide

Which Approach Matches Your Workflow?

Toggle the priorities that matter most for your Notaris/PPAT office:

05 · Concrete Migration Steps

The Four-Step Migration Plan

How we rolled out Bundled Defaults + Optional Overrides without a database migration:

  1. 1

    Bundle templates/ in Dockerfile + point config to it

    Add COPY --from=builder --chown=node:node /home/node/templates ./templates in api/Dockerfile, and add defaultTemplatePath to each entry in PPAT_DOCUMENTS_CONFIG.

  2. 2

    Add fallback lookup in GeneratedDocumentService & PengecekanService

    Check prisma.template first (R2 custom override). If missing and defaultTemplatePath is configured, read directly from disk via fs.readFile().

  3. 3

    Surface Default vs. Custom in /template

    Merge bundled defaults with DB overrides (source: "DEFAULT" | "CUSTOM"), expose preview & download for defaults, and add POST /templates/:id/reset.

  4. 4

    Make client 100% API-driven

    Widen docType in client to z.string().min(1) and render PpatDocumentLabels[doc.docType] ?? doc.displayName.

See how we implemented this architecture on feat/bundled-template-defaults: Read: Inside Our New PPAT Template Architecture →