
# Link Filter Feature Documentation

## Overview

The Link Filter feature allows users to filter entities by their relationships to other entities. For example, you can filter all real estate properties that are linked to a specific contact like "Yannic Buchwald".

## How It Works

### Automatic Activation
The link filter automatically appears in the filter panel when:
1. The current entity type has `linksConfiguration.enabled = true`
2. The type has defined `allowedTypes` or relationships to other entity types

### User Interface
The link filter provides a two-step selection process:

1. **Type Selection**: First dropdown allows you to select which type of entity you want to filter by (e.g., "Contacts")
2. **Entity Selection**: Second dropdown allows you to select the specific entity (e.g., "Yannic Buchwald")

### Filter Logic
When a link filter is applied:
1. The system queries the relations collection to find all relationships involving the selected entity
2. It extracts the IDs of entities that are linked to the selected entity
3. The main data view is filtered to show only those linked entities

## Example Use Cases

### Real Estate and Contacts
- **Scenario**: You have real estate properties linked to contacts (agents, owners, managers)
- **Usage**: Filter all properties where "Yannic Buchwald" is linked as an agent
- **Result**: Only properties with Yannic as a linked contact are displayed

### Projects and Team Members
- **Scenario**: You have projects linked to team members
- **Usage**: Filter all projects where "John Doe" is assigned
- **Result**: Only projects with John as a team member are displayed

## Technical Implementation

### Components
- `LinksFilterWidget.svelte`: The main filter UI component
- `FilterComponent.svelte`: Automatically includes links filter when appropriate
- `DataViewWidget.svelte`: Handles the special links filtering logic

### Database Queries
The filter converts to MongoDB queries that:
1. Query the relations collection: `{ "links.collectionName": targetType, "links.id": selectedEntityId }`
2. Extract linked entity IDs from relations
3. Filter main collection: `{ id: { $in: [linkedEntityIds] } }`

### Translation Keys
- `widgets.dataView.filter.selectType`: "Select type..."
- `widgets.dataView.filter.selectEntity`: "Select entity..."

## Configuration

### Enabling Links for a Type
```typescript
const myType: Type = {
  // ... other properties
  linksConfiguration: {
    enabled: true,
    allowedTypes: ["tenant-projectId-contactType"],
    descriptions: {
      "contactType": [
        { de: "Immobilienmakler", en: "Real Estate Agent" },
        { de: "Eigentümer", en: "Owner" }
      ]
    }
  }
};
```

### Template Integration
The feature works seamlessly with existing templates like the Real Estate template, which already defines link configurations for contacts.

## Testing

### Unit Tests
Located in: `packages/client/src/data-widgets/links/LinksFilterWidget.test.ts`

### E2E Tests
Located in: `apps/backoffice/src/routes/projects/[projectId]/link-filter.e2e.ts`

### Manual Testing Steps
1. Create a project with the Real Estate template
2. Add some contacts (e.g., "Yannic Buchwald", "Max Mustermann") 
3. Add real estate properties and link them to contacts
4. Navigate to the real estate data view
5. Click the filter button to open filters
6. Look for "Verknüpfungen" (Links) filter
7. Select "Kontakte" in the first dropdown
8. Select a specific contact in the second dropdown
9. Verify only linked properties are displayed

## Browser Compatibility
The feature uses standard web technologies and should work in all modern browsers that support the base application.