Zum Inhalt

Document Generation Feature

Overview

The document generation feature allows users to create AI-powered activity documents (emails or Word documents) using OpenAI. The system generates professional content based on a user's description and automatically populates contact and project information.

Features

Email Generation

  • AI generates professional email subject and content based on user description
  • Automatically populates recipient from selected contact
  • Subject is prefixed with "aai#" as per system requirements
  • Opens mailto link with pre-filled subject, recipient, and content
  • User can edit the email before sending in their email client

Word Document Generation

  • AI generates professional letter/document content
  • Creates Word document from a template file
  • Automatically fills placeholders with:
  • Contact information (company, name, address)
  • Document subject and AI-generated content
  • User signature (name, email, phone, fax)
  • Uploads document to user's OneDrive
  • Opens document in Word on the web for editing

Configuration

Environment Variables

Add the following to your .env file:

# OpenAI API key (required for AI content generation)
OPENAI_API_KEY=your-openai-api-key

# Path to Word document template file
# Template should contain placeholders as described below
WORD_TEMPLATE_PATH=/path/to/template/document-template.docx

Word Template

Create a Word document template with the following placeholders:

Contact Information

  • {{company}} - Contact's company name
  • {{title}} - Contact's title (e.g., Mr., Mrs., Dr.)
  • {{firstName}} - Contact's first name
  • {{lastName}} - Contact's last name
  • {{street}} - Contact's street address
  • {{houseNumber}} - Contact's house number
  • {{zip}} - Contact's postal code
  • {{city}} - Contact's city

Document Content

  • {{subject}} - Generated document subject
  • {{content}} - AI-generated document content

User Signature

  • {{userFirstName}} - Creating user's first name
  • {{userLastName}} - Creating user's last name
  • {{userEmail}} - Creating user's email
  • {{userPhone}} - Creating user's phone
  • {{userFax}} - Creating user's fax
  • {{userCompany}} - Creating user's company name
  • {{userStreet}} - Creating user's company street
  • {{userPostalCode}} - Creating user's company postalCode
  • {{userCity}} - Creating user's company city

Template Example

{{company}}
{{title}} {{firstName}} {{lastName}}
{{street}} {{houseNumber}}
{{zip}} {{city}}

Betreff: {{subject}}

Sehr geehrte Damen und Herren,

{{content}}

Mit freundlichen Grüßen

{{userFirstName}} {{userLastName}}
{{userEmail}}
{{userPhone}}
{{userFax}}

Usage

Web Interface

  1. Navigate to the Activities page
  2. Click on "Dokument generieren" button
  3. In the dialog:
  4. Select activity type (Email or Word Document)
  5. Select a contact from the dropdown
  6. Enter a description of what you want to communicate
  7. Click "Generieren"
  8. The system will:
  9. Use AI to generate professional content
  10. For Email: Open a mailto link with the content
  11. For Document: Create and upload the document to OneDrive, then open it

API Endpoint

POST /api/v1/activities/generate

Request Body

{
  "activityType": "Email" | "Document",
  "contactId": "uuid-of-contact",
  "description": "Description of what to communicate",
  "projectId": "optional-project-id"
}

Response

For Email:

{
  "subject": "Generated subject",
  "content": "Generated email content",
  "mailtoLink": "mailto:recipient@example.com?subject=aai%23Subject&body=Content"
}

For Document:

{
  "subject": "Generated subject",
  "content": "Generated document content",
  "documentUrl": "https://onedrive.live.com/...",
  "downloadUrl": "https://..."
}

Technical Details

Backend Components

  1. DTOs (libs/shared/dtos/src/lib/document-generation.dto.ts)

  2. GenerateActivityContentDto - Request DTO

  3. GenerateEmailResponseDto - Email response
  4. GenerateDocumentResponseDto - Document response

  5. OpenAI Service (apps/api/src/app/util/openai/openai.service.ts)

  6. generateActivityContent() - Generates subject and content using GPT

  7. Document Generation Service (apps/api/src/app/util/document-generation/document-generation.service.ts)

  8. createWordDocument() - Creates Word doc from template and uploads to OneDrive

  9. generateMailtoLink() - Generates mailto URL with parameters
  10. replacePlaceholders() - Replaces template placeholders

  11. Activities Controller (apps/api/src/app/features/activities/activities.controller.ts)

  12. POST /activities/generate - Main endpoint for document generation

Frontend Components

  1. Dialog Component (apps/web/src/app/features/activities/document-generation-dialog/)

  2. Standalone Angular dialog component

  3. Contact selection
  4. Activity type selection
  5. Description input with AI generation

  6. API Service (apps/web/src/app/data-access/api-activities.service.ts)

  7. generateActivityContent() - Calls backend API

  8. Activities List (apps/web/src/app/features/activities/activities-list/)

  9. "Dokument generieren" button
  10. Opens generation dialog

Permissions

Users need the following permissions to use this feature:

  • canCreateActivities - To generate documents

Notes

  • The current implementation uses a simple text-based placeholder replacement for Word documents
  • For production use with complex Word templates, consider using a library like docxtemplater
  • AI-generated content is in German (configurable in OpenAI service)
  • Email subject is automatically prefixed with "aai#" to identify AI-generated content

Troubleshooting

"Word template not found" error

  • Check that WORD_TEMPLATE_PATH is set correctly in .env
  • Ensure the file exists and the API has read access

"OpenAI API error"

  • Verify OPENAI_API_KEY is set and valid
  • Check OpenAI API quotas and usage limits

"Contact does not have an email address"

  • For email generation, the selected contact must have an email address
  • Add email to the contact or select a different contact