📄 Business Plan Templates – Firebase Upload + Styled DOCX Pipeline
What we’re doing (at a glance)

Upload the General Business Plan Template DOCX to Firebase Storage.

Create a Firestore manifest for it (title, tags, sections, renders).

Hook it into the existing styled preview UI.

Mark it as the master DOCX for future BP generation.

Lock in naming/versioning so future updates don’t break links.

1) Storage structure (authoritative)
/templates/
  business-plan/
    docs/
      general-business-plan/
        general-v1.docx              ← current master DOCX
        general-v1.sha256.txt        ← integrity record (optional)
        general-v1.upload.json       ← upload log (optional)
        general-v2.docx              ← future upgrades
    previews/
      general-business-plan/
        general-v1-preview.webp      ← full-width preview (styled)
        general-v1-thumb.webp        ← card thumbnail (styled)
    manifests/
      general-business-plan/
        general-v1.json              ← frozen manifest snapshot for v1 (optional backup)


Naming convention

slug: general-business-plan

version: start at v1 and increment (v2, v3, …)

current master: the DOCX whose isMaster: true in Firestore (see below)

2) Firestore schema (authoritative)

Collections & docs

templates (collection)
  business-plan (document)
    types (collection)
      general-business-plan (document)           ← "slug" doc
        title: "General Business Plan Template"
        slug: "general-business-plan"
        category: "business-plan"
        isActive: true
        isMaster: true                           ← only one per category should be true
        currentVersion: "v1"
        storagePaths:
          docx: "templates/business-plan/docs/general-business-plan/general-v1.docx"
          preview: "templates/business-plan/previews/general-business-plan/general-v1-preview.webp"
          thumb: "templates/business-plan/previews/general-business-plan/general-v1-thumb.webp"
        manifestRef: "templates/business-plan/manifests/general-business-plan/general-v1.json"
        tags: ["general", "foundational", "SMB", "lender-ready"]
        sections: [ ...see manifest sample below... ]
        createdAt: <serverTimestamp>
        updatedAt: <serverTimestamp>

        versions (subcollection)
          v1 (document)
            version: "v1"
            isMaster: true
            notes: "Initial release of General BP master."
            storagePaths: { docx, preview, thumb }
            sections: [ ...snapshot... ]
            createdAt: <serverTimestamp>


Global settings guard (optional, recommended)

settings (collection)
  templates (document)
    business-plan:
      masterSlug: "general-business-plan"
      masterVersion: "v1"
      lastRotatedAt: <serverTimestamp>


This makes it trivial for the app to resolve “the master business plan” without scanning.

3) Upload pipeline (front-end → Firestore)

Assumes envs already set (Vite):
VITE_FIREBASE_API_KEY, VITE_FIREBASE_PROJECT_ID, VITE_FIREBASE_APP_ID, VITE_FIREBASE_STORAGE_BUCKET

Pick file

Accept .docx only.

Enforce max size (e.g., 20 MB).

Resolve slug & version

Slug = general-business-plan

Version = v1 (if existing master is v1, keep; else increment).

If replacing, do not overwrite; add new version and flip isMaster flags atomically (see step 5).

Upload DOCX → Storage

Path: templates/business-plan/docs/general-business-plan/general-v1.docx

On success, optionally compute SHA256 client-side and upload as general-v1.sha256.txt.

Create/Update Firestore doc

Upsert templates/business-plan/types/general-business-plan with:

title, slug, category, isActive: true, currentVersion: "v1"

storagePaths.docx to the uploaded path.

tags, sections (from manifest).

isMaster: true (if this is the master).

Atomic master-flag enforcement

Run a Firestore transaction:

Set isMaster: false on any other template in category "business-plan" where it’s true.

Set isMaster: true on general-business-plan.

Update settings/templates.business-plan.masterSlug/masterVersion.

Preview assets

Use your existing styled preview renderer (blue gradient card).
Generate & upload:

previews/general-business-plan/general-v1-preview.webp (full)

previews/general-business-plan/general-v1-thumb.webp (card)

Update storagePaths.preview & storagePaths.thumb.

If you don’t have an automated rasterizer for DOCX: keep previews UI-driven from the manifest (section titles + bullets), not from the DOCX binary. That’s what you’ve been doing: the styled Preview renders from structured data, not the file itself.

4) Minimal security rules (tight but practical)

Firestore (only admins can write templates):

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    function isAdmin() {
      return request.auth != null && request.auth.token.admin == true;
    }

    match /templates/{doc=**} {
      allow read: if true;              // public can read template manifests
      allow write: if isAdmin();        // only admin (custom claim) can create/update
    }

    match /settings/{doc=**} {
      allow read: if true;
      allow write: if isAdmin();
    }
  }
}


Storage (only admins can write under /templates):

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    function isAdmin() {
      return request.auth != null && request.auth.token.admin == true;
    }

    // public read for previews; locked writes
    match /templates/{allPaths=**} {
      allow read: if true;
      allow write: if isAdmin();
    }
  }
}


(You already planned the admin claim setup; this matches that.)

5) UI wiring (what Replit connects)

Upload page / admin modal

Inputs: File (.docx), Title (preset), Slug (locked: general-business-plan), Version (auto), Tags (chips), Sections (JSON form).

Button: Upload + Publish as Master → runs the transaction in Step 3–5.

Preview component (existing)

Source: Firestore doc templates/business-plan/types/general-business-plan

If storagePaths.preview exists, show that image; else render from sections into the styled card on the fly.

Master badge

If isMaster === true, show “Master Template” chip in UI.

Consumer pages

Resolve master via settings/templates.business-plan.masterSlug and masterVersion, then fetch the doc & version subdoc.

6) Sample manifest JSON (General BP)

Use this for sections, either embedded in the Firestore doc or saved as a versioned manifest JSON under /manifests.

{
  "title": "General Business Plan Template",
  "slug": "general-business-plan",
  "category": "business-plan",
  "tags": ["general", "foundational", "SMB", "lender-ready", "pitch"],
  "sections": [
    {
      "id": "cover",
      "label": "Cover",
      "fields": [
        {"key": "CompanyName", "type": "docProperty", "placeholder": "Company Name"},
        {"key": "OwnerName", "type": "docProperty", "placeholder": "Owner / Author"},
        {"key": "Date", "type": "auto", "format": "MMMM d, yyyy"}
      ]
    },
    {
      "id": "executive-summary",
      "label": "Executive Summary",
      "blocks": [
        {"type": "heading", "text": "Executive Summary"},
        {"type": "paragraph", "text": "Brief overview of the business, market opportunity, solution, and financial highlights."},
        {"type": "bullets", "items": [
          "Problem & Opportunity",
          "Solution & Value Proposition",
          "Target Market",
          "Business Model",
          "Traction & Milestones",
          "Financial Highlights",
          "Funding Ask (if applicable)"
        ]}
      ]
    },
    {
      "id": "company-overview",
      "label": "Company Overview",
      "blocks": [
        {"type": "heading", "text": "Company Overview"},
        {"type": "keyvalues", "items": [
          {"key":"Legal Structure","value":"LLC / Corp / Sole Prop"},
          {"key":"Location","value":"City, State/Country"},
          {"key":"Founded","value":"Year"},
          {"key":"Owners","value":"OwnerName"}
        ]}
      ]
    },
    {
      "id": "market-analysis",
      "label": "Market Analysis",
      "blocks": [
        {"type": "heading", "text": "Market Analysis"},
        {"type": "paragraph", "text": "Total addressable market (TAM), serviceable available market (SAM), and serviceable obtainable market (SOM)."},
        {"type": "bullets", "items": [
          "Customer Segments",
          "Market Size & Growth",
          "Trends & Drivers",
          "Competitive Landscape"
        ]}
      ]
    },
    {
      "id": "products-services",
      "label": "Products & Services",
      "blocks": [
        {"type": "heading", "text": "Products & Services"},
        {"type": "bullets", "items": [
          "Core Offering",
          "Differentiators",
          "Pricing",
          "Roadmap"
        ]}
      ]
    },
    {
      "id": "go-to-market",
      "label": "Go-To-Market",
      "blocks": [
        {"type": "heading", "text": "Go-To-Market Strategy"},
        {"type": "bullets", "items": [
          "Positioning & Messaging",
          "Channels (Paid/Organic/Partnerships)",
          "Sales Motion",
          "Retention & Lifecycle"
        ]}
      ]
    },
    {
      "id": "operations",
      "label": "Operations",
      "blocks": [
        {"type": "heading", "text": "Operations Plan"},
        {"type": "bullets", "items": [
          "Facilities & Tech Stack",
          "Suppliers & Key Partners",
          "Regulatory/Compliance",
          "KPIs & Reporting Cadence"
        ]}
      ]
    },
    {
      "id": "team",
      "label": "Team",
      "blocks": [
        {"type": "heading", "text": "Management & Team"},
        {"type": "bullets", "items": [
          "Leadership Bios",
          "Advisors",
          "Hiring Plan"
        ]}
      ]
    },
    {
      "id": "financials",
      "label": "Financials",
      "blocks": [
        {"type": "heading", "text": "Financial Plan"},
        {"type": "bullets", "items": [
          "Revenue Model & Assumptions",
          "3–5 Year Projections",
          "Break-even Analysis",
          "Cash Flow",
          "Use of Funds"
        ]}
      ]
    },
    {
      "id": "appendix",
      "label": "Appendix",
      "blocks": [
        {"type": "heading", "text": "Appendix"},
        {"type": "bullets", "items": [
          "Supporting Docs",
          "References",
          "Additional Charts"
        ]}
      ]
    }
  ]
}

7) Exact step-by-step for Replit to execute

Create Storage folders exactly as listed in Section 1.

Upload general-v1.docx to
templates/business-plan/docs/general-business-plan/general-v1.docx.

(Optional) Upload general-v1.sha256.txt (hash) and general-v1.upload.json (who/when).

Generate previews using the existing styled preview component:

Render from the manifest (sections) and export to WebP (1024px wide for preview, ~360px for thumb).

Upload to:

templates/business-plan/previews/general-business-plan/general-v1-preview.webp

templates/business-plan/previews/general-business-plan/general-v1-thumb.webp

Create/Upsert Firestore doc
templates/business-plan/types/general-business-plan with fields from Section 2.

Write version subdoc
templates/business-plan/types/general-business-plan/versions/v1.

Run a transaction to:

Set isMaster: false on any other business-plan template with isMaster: true.

Set isMaster: true here and currentVersion: "v1".

Update settings/templates.business-plan.masterSlug and masterVersion.

Verify UI

Admin view shows Master Template chip.

Public library lists “General Business Plan Template” with styled card using the uploaded preview, falls back to live render if images absent.

Lock rules

Deploy the Firestore & Storage rules above so only admins can write under /templates/**.

8) Tiny sample manifest file (if you want to save it alongside)

Path:
templates/business-plan/manifests/general-business-plan/general-v1.json

Use the JSON from Section 6.

Reference it via manifestRef in the Firestore doc.