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
- Navigate to the Activities page
- Click on "Dokument generieren" button
- In the dialog:
- Select activity type (Email or Word Document)
- Select a contact from the dropdown
- Enter a description of what you want to communicate
- Click "Generieren"
- The system will:
- Use AI to generate professional content
- For Email: Open a mailto link with the content
- 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
-
DTOs (
libs/shared/dtos/src/lib/document-generation.dto.ts) -
GenerateActivityContentDto- Request DTO GenerateEmailResponseDto- Email response-
GenerateDocumentResponseDto- Document response -
OpenAI Service (
apps/api/src/app/util/openai/openai.service.ts) -
generateActivityContent()- Generates subject and content using GPT -
Document Generation Service (
apps/api/src/app/util/document-generation/document-generation.service.ts) -
createWordDocument()- Creates Word doc from template and uploads to OneDrive generateMailtoLink()- Generates mailto URL with parameters-
replacePlaceholders()- Replaces template placeholders -
Activities Controller (
apps/api/src/app/features/activities/activities.controller.ts) POST /activities/generate- Main endpoint for document generation
Frontend Components
-
Dialog Component (
apps/web/src/app/features/activities/document-generation-dialog/) -
Standalone Angular dialog component
- Contact selection
- Activity type selection
-
Description input with AI generation
-
API Service (
apps/web/src/app/data-access/api-activities.service.ts) -
generateActivityContent()- Calls backend API -
Activities List (
apps/web/src/app/features/activities/activities-list/) - "Dokument generieren" button
- 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_PATHis set correctly in.env - Ensure the file exists and the API has read access
"OpenAI API error"
- Verify
OPENAI_API_KEYis 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