---
phase: 11-agent-saved-signature-and-signing-workflow
plan: "02"
subsystem: pdf
tags: [pdf-lib, agent-signature, prepare-pipeline, drizzle-orm]

# Dependency graph
requires:
  - phase: 11-01
    provides: agentSignatureData column on users table; agent-signature field type in FieldPlacer
provides:
  - preparePdf() embeds agent PNG at agent-signature field coordinates via pdfDoc.embedPng + page.drawImage
  - prepare route fetches agentSignatureData from DB and passes to preparePdf()
  - 422 guard when agent-signature fields present but no signature saved
affects: [phase-12-preview, phase-13-ai-field-placement]

# Tech tracking
tech-stack:
  added: []
  patterns: [embed-once-draw-many for PDFImage reuse across field loop; 422 guard before preparePdf for missing agent signature]

key-files:
  created: []
  modified:
    - teressa-copeland-homes/src/lib/pdf/prepare-document.ts
    - teressa-copeland-homes/src/app/api/documents/[id]/prepare/route.ts

key-decisions:
  - "PDFImage embedded once before field loop and reused across all agent-sig fields — matches plan guidance, avoids repeated embed cost"
  - "Session guard strengthened to !session?.user?.id in prepare route — matches established pattern from agent/signature route; fixes TypeScript narrowing"
  - "422 guard fires only when agent-sig fields exist AND no signature is saved — documents with no agent-sig fields prepare normally"

patterns-established:
  - "embed-once-draw-many: call pdfDoc.embedPng() once before field loop, store PDFImage, reuse in drawImage calls inside loop"
  - "422-before-prepare: validate preconditions (agent sig present) before calling preparePdf() to prevent silent no-ops"

requirements-completed: [AGENT-04]

# Metrics
duration: 2min
completed: 2026-03-21
---

# Phase 11 Plan 02: Agent Signature Bake-In Summary

**preparePdf() embeds agent PNG at agent-signature field coordinates using pdfDoc.embedPng + page.drawImage; prepare route fetches agentSignatureData from DB with a 422 guard when fields exist but no signature is saved**

## Performance

- **Duration:** ~2 min
- **Started:** 2026-03-21T20:05:41Z
- **Completed:** 2026-03-21T20:07:05Z
- **Tasks:** 2
- **Files modified:** 2

## Accomplishments
- `preparePdf()` gains optional 5th parameter `agentSignatureData: string | null = null`; agent-signature stub replaced with real `page.drawImage()` call using field.x/y/width/height
- `PDFImage` imported from `@cantoo/pdf-lib`; image embedded once before field loop and reused across all agent-sig fields
- Prepare route fetches `agentSignatureData` from `users` table for the authenticated session; passes it as 5th arg to `preparePdf()`
- 422 guard with `{ error: 'agent-signature-missing' }` fires when any `agent-signature` field exists but no signature has been saved

## Task Commits

Each task was committed atomically:

1. **Task 1: preparePdf() — add agentSignatureData param and embed at agent-sig field coordinates** - `d9652e1` (feat)
2. **Task 2: prepare route — fetch agentSignatureData, 422 guard, pass to preparePdf** - `b2e9810` (feat)

## Files Created/Modified
- `teressa-copeland-homes/src/lib/pdf/prepare-document.ts` - Added `agentSignatureData` param, `PDFImage` import, embed-once-draw-many block, replaced agent-signature stub with `drawImage`
- `teressa-copeland-homes/src/app/api/documents/[id]/prepare/route.ts` - Added `users`/`getFieldType` imports, strengthened session guard, DB fetch, 422 guard, 5-arg `preparePdf()` call

## Decisions Made
- PDFImage embedded once before field loop and reused — avoids re-embedding cost per field and matches plan guidance
- Session guard strengthened from `!session` to `!session?.user?.id` — required for TypeScript narrowing so `session.user.id` is a `string` (not `string | undefined`) when passed to `eq()`
- 422 guard fires only when `hasAgentSigFields && !agentSignatureData` — documents with no agent-signature fields prepare normally, preserving backward compatibility

## Deviations from Plan

### Auto-fixed Issues

**1. [Rule 1 - Bug] Strengthened session guard to enable TypeScript narrowing**
- **Found during:** Task 2 (prepare route — fetch agentSignatureData, 422 guard, pass to preparePdf)
- **Issue:** Original `if (!session)` guard left `session.user` and `session.user.id` as possibly-undefined; TypeScript TS2769 + TS18048 errors when passing `session.user.id` to `eq(users.id, ...)`
- **Fix:** Changed `if (!session)` to `if (!session?.user?.id)` — matches established pattern from `src/app/api/agent/signature/route.ts` (11-01)
- **Files modified:** `teressa-copeland-homes/src/app/api/documents/[id]/prepare/route.ts`
- **Verification:** `npx tsc --noEmit` passed with zero errors after fix
- **Committed in:** `b2e9810` (part of Task 2 commit)

---

**Total deviations:** 1 auto-fixed (Rule 1 - type-narrowing bug in session guard)
**Impact on plan:** Minimal — single-line guard change consistent with established codebase pattern. No scope creep.

## Issues Encountered
- TypeScript TS2769/TS18048 errors on `session.user.id` due to weaker session guard than established pattern. Resolved immediately by adopting `!session?.user?.id` guard (same as agent/signature route added in 11-01).

## User Setup Required
None - no external service configuration required.

## Next Phase Readiness
- AGENT-04 complete: agent's saved PNG is baked into prepared PDFs before reaching the client
- Client signing session unaffected — `isClientVisibleField()` already excludes `agent-signature` fields
- Phase 12 (preview route) can proceed; Phase 13 (AI field placement) has no dependency on this plan
- Functional verification (draw → save → prepare → download PDF and inspect embedded signature) is recommended before Phase 12

---
*Phase: 11-agent-saved-signature-and-signing-workflow*
*Completed: 2026-03-21*
