diff --git a/.drone.yml b/.drone.yml index 06db32b..65d7052 100644 --- a/.drone.yml +++ b/.drone.yml @@ -166,7 +166,7 @@ environment: steps: - name: renovate - image: renovate/renovate:42.71 + image: renovate/renovate:42.73 environment: RENOVATE_CONFIG_FILE: "renovate_backend_config.js" RENOVATE_TOKEN: diff --git a/docs/csv-member-import-v1.md b/docs/csv-member-import-v1.md new file mode 100644 index 0000000..2bdbe69 --- /dev/null +++ b/docs/csv-member-import-v1.md @@ -0,0 +1,666 @@ +# CSV Member Import v1 - Implementation Plan + +**Version:** 1.0 +**Date:** 2025-01-XX +**Status:** Ready for Implementation +**Related Documents:** +- [Feature Roadmap](./feature-roadmap.md) - Overall feature planning + +--- + +## Table of Contents + +- [Overview & Scope](#overview--scope) +- [UX Flow](#ux-flow) +- [CSV Specification](#csv-specification) +- [Technical Design Notes](#technical-design-notes) +- [Implementation Issues](#implementation-issues) +- [Rollout & Risks](#rollout--risks) + +--- + +## Overview & Scope + +### What We're Building + +A **basic CSV member import feature** that allows administrators to upload a CSV file and import new members into the system. This is a **v1 minimal implementation** focused on establishing the import structure without advanced features. + +**Core Functionality (v1 Minimal):** +- Upload CSV file via LiveView file upload +- Parse CSV with bilingual header support for core member fields (English/German) +- Auto-detect delimiter (`;` or `,`) using header recognition +- Map CSV columns to core member fields (`first_name`, `last_name`, `email`, `street`, `postal_code`, `city`) +- **Import custom field values** - Map CSV columns to existing custom fields by name (unknown custom field columns will be ignored with a warning) +- Validate each row (required field: `email`) +- Create members via Ash resource (one-by-one, **no background jobs**, processed in chunks of 200 rows via LiveView messages) +- Display import results: success count, error count, and error details +- Provide static CSV templates (EN/DE) + +**Key Constraints (v1):** +- ✅ **Admin-only feature** +- ✅ **No upsert** (create only) +- ✅ **No deduplication** (duplicate emails fail and show as errors) +- ✅ **No mapping wizard** (fixed header mapping via bilingual variants) +- ✅ **No background jobs** (progress via LiveView `handle_info`) +- ✅ **Best-effort import** (row-by-row, no rollback) +- ✅ **UI-only error display** (no error CSV export) +- ✅ **Safety limits** (10 MB, 1,000 rows, chunks of 200) + +### Out of Scope (v1) + +**Deferred to Future Versions:** +- ❌ Upsert/update existing members +- ❌ Advanced deduplication strategies +- ❌ Column mapping wizard UI +- ❌ Background job processing (Oban/GenStage) +- ❌ Transactional all-or-nothing import +- ❌ Error CSV export/download +- ❌ Batch validation preview before import +- ❌ Dynamic template generation +- ❌ Import history/audit log +- ❌ Import templates for other entities + +--- + +## UX Flow + +### Access & Location + +**Entry Point:** +- **Location:** Global Settings page (`/settings`) +- **UI Element:** New section "Import Members (CSV)" below "Custom Fields" section +- **Access Control:** Admin-only (enforced at LiveView event level, not entire `/settings` route) + +### User Journey + +1. **Navigate to Global Settings** +2. **Access Import Section** + - **Important notice:** Custom fields should be created in Mila before importing CSV files with custom field columns (unknown columns will be ignored with a warning) + - Upload area (drag & drop or file picker) + - Template download links (English / German) + - Help text explaining CSV format and custom field requirements +3. **Ensure Custom Fields Exist (if importing custom fields)** + - Navigate to Custom Fields section and create required custom fields + - Note the name/identifier for each custom field (used as CSV header) +4. **Download Template (Optional)** +5. **Prepare CSV File** + - Include custom field columns using the custom field name as header (e.g., `membership_number`, `birth_date`) +6. **Upload CSV** +7. **Start Import** + - Runs server-side via LiveView messages (may take up to ~30 seconds for large files) + - Warning messages if custom field columns reference non-existent custom fields (columns will be ignored) +8. **View Results** + - Success count + - Error count + - First 50 errors, each with: + - **CSV line number** (header is line 1, first data record begins at line 2) + - Error message + - Field name (if applicable) + +### Error Handling + +- **File too large:** Flash error before upload starts +- **Too many rows:** Flash error before import starts +- **Invalid CSV format:** Error shown in results +- **Partial success:** Results show both success and error counts + +--- + +## CSV Specification + +### Delimiter + +**Recommended:** Semicolon (`;`) +**Supported:** `;` and `,` + +**Auto-Detection (Header Recognition):** +- Remove UTF-8 BOM *first* +- Extract header record and try parsing with both delimiters +- For each delimiter, count how many recognized headers are present (via normalized variants) +- Choose delimiter with higher recognition; prefer `;` if tied +- If neither yields recognized headers, default to `;` + +### Quoting Rules + +- Fields may be quoted with double quotes (`"`) +- Escaped quotes: `""` inside quoted field represents a single `"` +- **v1 assumption:** CSV records do **not** contain embedded newlines inside quoted fields. (If they do, parsing may fail or line numbers may be inaccurate.) + +### Column Headers + +**v1 Supported Fields:** + +**Core Member Fields:** +- `first_name` / `Vorname` (optional) +- `last_name` / `Nachname` (optional) +- `email` / `E-Mail` (required) +- `street` / `Straße` (optional) +- `postal_code` / `PLZ` / `Postleitzahl` (optional) +- `city` / `Stadt` (optional) + +**Custom Fields:** +- Any custom field column using the custom field's **name** as the header (e.g., `membership_number`, `birth_date`) +- **Important:** Custom fields must be created in Mila before importing. The CSV header must match the custom field name exactly (same normalization as member fields). +- **Behavior:** If the CSV contains custom field columns that don't exist in Mila, a warning message will be shown and those columns will be ignored during import. + +**Member Field Header Mapping:** + +| Canonical Field | English Variants | German Variants | +|---|---|---| +| `first_name` | `first_name`, `firstname` | `Vorname`, `vorname` | +| `last_name` | `last_name`, `lastname`, `surname` | `Nachname`, `nachname`, `Familienname` | +| `email` | `email`, `e-mail`, `e_mail` | `E-Mail`, `e-mail`, `e_mail` | +| `street` | `street`, `address` | `Straße`, `strasse`, `Strasse` | +| `postal_code` | `postal_code`, `zip`, `postcode` | `PLZ`, `plz`, `Postleitzahl`, `postleitzahl` | +| `city` | `city`, `town` | `Stadt`, `stadt`, `Ort` | + +**Header Normalization (used consistently for both input headers AND mapping variants):** +- Trim whitespace +- Convert to lowercase +- Normalize Unicode: `ß` → `ss` (e.g., `Straße` → `strasse`) +- Replace hyphens/whitespace with underscores: `E-Mail` → `e_mail`, `phone number` → `phone_number` +- Collapse multiple underscores: `e__mail` → `e_mail` +- Case-insensitive matching + +**Unknown columns:** ignored (no error) + +**Required fields:** `email` + +**Custom Field Columns:** +- Custom field columns are identified by matching the normalized CSV header to the custom field `name` (not slug) +- Same normalization rules apply as for member fields (trim, lowercase, Unicode normalization, underscore replacement) +- Unknown custom field columns (non-existent names) will be ignored with a warning message + +### CSV Template Files + +**Location:** +- `priv/static/templates/member_import_en.csv` +- `priv/static/templates/member_import_de.csv` + +**Content:** +- Header row with required + common optional fields +- **Note:** Custom field columns are not included in templates by default (users add them based on their custom field configuration) +- One example row +- Uses semicolon delimiter (`;`) +- UTF-8 encoding **with BOM** (Excel compatibility) + +**Template Access:** +- Templates are static files in `priv/static/templates/` +- Served at: + - `/templates/member_import_en.csv` + - `/templates/member_import_de.csv` +- In LiveView, link them using Phoenix static path helpers (e.g. `~p` or `Routes.static_path/2`, depending on Phoenix version). + +### File Limits + +- **Max file size:** 10 MB +- **Max rows:** 1,000 rows (excluding header) +- **Processing:** chunks of 200 (via LiveView messages) +- **Encoding:** UTF-8 (BOM handled) + +--- + +## Technical Design Notes + +### Architecture Overview + +``` +┌─────────────────┐ +│ LiveView UI │ (GlobalSettingsLive or component) +│ - Upload area │ +│ - Progress │ +│ - Results │ +└────────┬────────┘ + │ prepare + ▼ +┌─────────────────────────────┐ +│ Import Service │ (Mv.Membership.Import.MemberCSV) +│ - parse + map + limit checks│ -> returns import_state +│ - process_chunk(chunk) │ -> returns chunk results +└────────┬────────────────────┘ + │ create + ▼ +┌─────────────────┐ +│ Ash Resource │ (Mv.Membership.Member) +│ - Create │ +└─────────────────┘ +``` + +### Technology Stack + +- **Phoenix LiveView:** file upload via `allow_upload/3` +- **NimbleCSV:** CSV parsing (add explicit dependency if missing) +- **Ash Resource:** member creation via `Membership.create_member/1` +- **Gettext:** bilingual UI/error messages + +### Module Structure + +**New Modules:** +- `lib/mv/membership/import/member_csv.ex` - import orchestration + chunk processing + custom field handling +- `lib/mv/membership/import/csv_parser.ex` - delimiter detection + parsing + BOM handling +- `lib/mv/membership/import/header_mapper.ex` - normalization + header mapping (core fields + custom fields) + +**Modified Modules:** +- `lib/mv_web/live/global_settings_live.ex` - render import section, handle upload/events/messages + +### Data Flow + +1. **Upload:** LiveView receives file via `allow_upload` +2. **Consume:** `consume_uploaded_entries/3` reads file content +3. **Prepare:** `MemberCSV.prepare/2` + - Strip BOM + - Detect delimiter (header recognition) + - Parse header + rows + - Map headers to canonical fields (core member fields) + - **Query existing custom fields and map custom field columns by name** (using same normalization as member fields) + - **Warn about unknown custom field columns** (non-existent names will be ignored with warning) + - Early abort if required headers missing + - Row count check + - Return `import_state` containing chunks, column_map, and custom_field_map +4. **Process:** LiveView drives chunk processing via `handle_info` + - For each chunk: validate + create member + create custom field values + collect errors +5. **Results:** LiveView shows progress + final summary + +### Types & Key Consistency + +- **Raw CSV parsing:** returns headers as list of strings, and rows **with csv line numbers** +- **Header mapping:** operates on normalized strings; mapping table variants are normalized once +- **Ash attrs:** built as atom-keyed map (`%{first_name: ..., ...}`) + +### Error Model + +```elixir +%{ + csv_line_number: 5, # physical line number in the CSV file + field: :email, # optional + message: "is not a valid email" +} +``` + +### CSV Line Numbers (Important) + +To keep error reporting user-friendly and accurate, **row errors must reference the physical line number in the original file**, even if empty lines are skipped. + +**Design decision:** the parser returns rows as: + +```elixir +rows :: [{csv_line_number :: pos_integer(), row_map :: map()}] +``` + +Downstream logic must **not** recompute line numbers from row indexes. + +### Authorization + +**Enforcement points:** +1. **LiveView event level:** check admin permission in `handle_event("start_import", ...)` +2. **UI level:** render import section only for admin users +3. **Static templates:** public assets (no authorization needed) + +Use `Mv.Authorization.PermissionSets` (preferred) instead of hard-coded string checks where possible. + +### Safety Limits + +- File size enforced by `allow_upload` (`max_file_size`) +- Row count enforced in `MemberCSV.prepare/2` before processing starts +- Chunking is done via **LiveView `handle_info` loop** (sequential, cooperative scheduling) + +--- + +## Implementation Issues + +### Issue #1: CSV Specification & Static Template Files + +**Dependencies:** None + +**Goal:** Define CSV contract and add static templates. + +**Tasks:** +- [ ] Finalize header mapping variants +- [ ] Document normalization rules +- [ ] Document delimiter detection strategy +- [ ] Create templates in `priv/static/templates/` (UTF-8 with BOM) +- [ ] Document template URLs and how to link them from LiveView +- [ ] Document line number semantics (physical CSV line numbers) + +**Definition of Done:** +- [ ] Templates open cleanly in Excel/LibreOffice +- [ ] CSV spec section complete + +--- + +### Issue #2: Import Service Module Skeleton + +**Dependencies:** None + +**Goal:** Create service API and error types. + +**API (recommended):** +- `prepare/2` — parse + map + limit checks, returns import_state +- `process_chunk/3` — process one chunk (pure-ish), returns per-chunk results + +**Tasks:** +- [ ] Create `lib/mv/membership/import/member_csv.ex` +- [ ] Define public function: `prepare/2 (file_content, opts \\ [])` +- [ ] Define public function: `process_chunk/3 (chunk_rows_with_lines, column_map, opts \\ [])` +- [ ] Define error struct: `%MemberCSV.Error{csv_line_number: integer, field: atom | nil, message: String.t}` +- [ ] Document module + API + +--- + +### Issue #3: CSV Parsing + Delimiter Auto-Detection + BOM Handling + +**Dependencies:** Issue #2 + +**Goal:** Parse CSV robustly with correct delimiter detection and BOM handling. + +**Tasks:** +- [ ] Verify/add NimbleCSV dependency (`{:nimble_csv, "~> 1.0"}`) +- [ ] Create `lib/mv/membership/import/csv_parser.ex` +- [ ] Implement `strip_bom/1` and apply it **before** any header handling +- [ ] Handle `\r\n` and `\n` line endings (trim `\r` on header record) +- [ ] Detect delimiter via header recognition (try `;` and `,`) +- [ ] Parse CSV and return: + - `headers :: [String.t()]` + - `rows :: [{csv_line_number, [String.t()]}]` or directly `[{csv_line_number, row_map}]` +- [ ] Skip completely empty records (but preserve correct physical line numbers) +- [ ] Return `{:ok, headers, rows}` or `{:error, reason}` + +**Definition of Done:** +- [ ] BOM handling works (Excel exports) +- [ ] Delimiter detection works reliably +- [ ] Rows carry correct `csv_line_number` + +--- + +### Issue #4: Header Normalization + Per-Header Mapping (No Language Detection) + +**Dependencies:** Issue #3 + +**Goal:** Map each header individually to canonical fields (normalized comparison). + +**Tasks:** +- [ ] Create `lib/mv/membership/import/header_mapper.ex` +- [ ] Implement `normalize_header/1` +- [ ] Normalize mapping variants once and compare normalized strings +- [ ] Build `column_map` (canonical field -> column index) +- [ ] **Early abort if required headers missing** (`email`) +- [ ] Ignore unknown columns (member fields only) +- [ ] **Separate custom field column detection** (by name, with normalization) + +**Definition of Done:** +- [ ] English/German headers map correctly +- [ ] Missing required columns fails fast + +--- + +### Issue #5: Validation (Required Fields) + Error Formatting + +**Dependencies:** Issue #4 + +**Goal:** Validate each row and return structured, translatable errors. + +**Tasks:** +- [ ] Implement `validate_row/3 (row_map, csv_line_number, opts)` +- [ ] Required field presence (`email`) +- [ ] Email format validation (EctoCommons.EmailValidator) +- [ ] Trim values before validation +- [ ] Gettext-backed error messages + +--- + +### Issue #6: Persistence via Ash Create + Per-Row Error Capture (Chunked Processing) + +**Dependencies:** Issue #5 + +**Goal:** Create members and capture errors per row with correct CSV line numbers. + +**Tasks:** +- [ ] Implement `process_chunk/3` in service: + - Input: `[{csv_line_number, row_map}]` + - Validate + create sequentially + - Collect counts + first 50 errors (per import overall; LiveView enforces cap across chunks) +- [ ] Implement Ash error formatter helper: + - Convert `Ash.Error.Invalid` into `%MemberCSV.Error{}` + - Prefer field-level errors where possible (attach `field` atom) + - Handle unique email constraint error as user-friendly message +- [ ] Map row_map to Ash attrs (`%{first_name: ..., ...}`) + +**Important:** **Do not recompute line numbers** in this layer—use the ones provided by the parser. + +--- + +### Issue #7: Admin Global Settings LiveView UI (Upload + Start Import + Results + Template Links) + +**Dependencies:** Issue #6 + +**Goal:** UI section with upload, progress, results, and template links. + +**Tasks:** +- [ ] Render import section only for admins +- [ ] **Add prominent UI notice about custom fields:** + - Display alert/info box: "Custom fields must be created in Mila before importing CSV files with custom field columns" + - Explain: "Use the custom field name as the CSV column header (same normalization as member fields applies)" + - Add link to custom fields management section +- [ ] Configure `allow_upload/3`: + - `.csv` only, `max_entries: 1`, `max_file_size: 10MB`, `auto_upload: false` +- [ ] `handle_event("start_import", ...)`: + - Admin permission check + - Consume upload -> read file content + - Call `MemberCSV.prepare/2` + - Store `import_state` in assigns (chunks + column_map + metadata) + - Initialize progress assigns + - `send(self(), {:process_chunk, 0})` +- [ ] `handle_info({:process_chunk, idx}, socket)`: + - Fetch chunk from `import_state` + - Call `MemberCSV.process_chunk/3` + - Merge counts/errors into progress assigns (cap errors at 50 overall) + - Schedule next chunk (or finish and show results) +- [ ] Results UI: + - Success count + - Failure count + - Error list (line number + message + field) + - **Warning messages for unknown custom field columns** (non-existent names) shown in results + +**Template links:** +- Link `/templates/member_import_en.csv` and `/templates/member_import_de.csv` via Phoenix static path helpers. + +--- + +### Issue #8: Authorization + Limits + +**Dependencies:** None (can be parallelized) + +**Goal:** Ensure admin-only access and enforce limits. + +**Tasks:** +- [ ] Admin check in start import event handler +- [ ] File size enforced in upload config +- [ ] Row limit enforced in `MemberCSV.prepare/2` (max_rows from config) +- [ ] Configuration: + ```elixir + config :mv, csv_import: [ + max_file_size_mb: 10, + max_rows: 1000 + ] + ``` + +--- + +### Issue #9: End-to-End LiveView Tests + Fixtures + +**Dependencies:** Issue #7 and #8 + +**Tasks:** +- [ ] Fixtures: + - valid EN/DE (core fields only) + - valid with custom fields + - invalid + - unknown custom field name (non-existent, should show warning) + - too many rows (1,001) + - BOM + `;` delimiter fixture + - fixture with empty line(s) to validate correct line numbers +- [ ] LiveView tests: + - admin sees section, non-admin does not + - upload + start import + - success + error rendering + - row limit + file size errors + - custom field import success + - custom field import warning (non-existent name, column ignored) + +--- + +### Issue #10: Documentation Polish (Inline Help Text + Docs) + +**Dependencies:** Issue #9 + +**Tasks:** +- [ ] UI help text + translations +- [ ] CHANGELOG entry +- [ ] Ensure moduledocs/docs + +--- + +### Issue #11: Custom Field Import + +**Dependencies:** Issue #6 (Persistence) + +**Priority:** High (Core v1 Feature) + +**Goal:** Support importing custom field values from CSV columns. Custom fields should exist in Mila before import for best results. + +**Important Requirements:** +- **Custom fields should be created in Mila first** - Unknown custom field columns will be ignored with a warning message +- CSV headers for custom fields must match the custom field **name** exactly (same normalization as member fields applies) +- Custom field values are validated according to the custom field type (string, integer, boolean, date, email) +- Unknown custom field columns (non-existent names) will be ignored with a warning - import continues + +**Tasks:** +- [ ] Extend `header_mapper.ex` to detect custom field columns by name (using same normalization as member fields) +- [ ] Query existing custom fields during `prepare/2` to map custom field columns +- [ ] Collect unknown custom field columns and add warning messages (don't fail import) +- [ ] Map custom field CSV values to `CustomFieldValue` creation in `process_chunk/3` +- [ ] Handle custom field type validation (string, integer, boolean, date, email) +- [ ] Create `CustomFieldValue` records linked to members during import +- [ ] Update error messages to include custom field validation errors +- [ ] Add UI help text explaining custom field requirements: + - "Custom fields must be created in Mila before importing" + - "Use the custom field name as the CSV column header (same normalization as member fields)" + - Link to custom fields management section +- [ ] Update CSV templates documentation to explain custom field columns +- [ ] Add tests for custom field import (valid, invalid name, type validation, warning for unknown) + +**Definition of Done:** +- [ ] Custom field columns are recognized by name (with normalization) +- [ ] Warning messages shown for unknown custom field columns (import continues) +- [ ] Custom field values are created and linked to members +- [ ] Type validation works for all custom field types +- [ ] UI clearly explains custom field requirements +- [ ] Tests cover custom field import scenarios (including warning for unknown names) + +--- + +## Rollout & Risks + +### Rollout Strategy +- Dev → Staging → Production (with anonymized real-world CSV tests) + +### Risks & Mitigations + +| Risk | Impact | Likelihood | Mitigation | +|---|---:|---:|---| +| Large import timeout | High | Medium | 10 MB + 1,000 rows, chunking via `handle_info` | +| Encoding issues | Medium | Medium | BOM stripping, templates with BOM | +| Invalid CSV format | Medium | High | Clear errors + templates | +| Duplicate emails | Low | High | Ash constraint error -> user-friendly message | +| Performance (no background jobs) | Medium | Low | Small limits, sequential chunk processing | +| Admin access bypass | High | Low | Event-level auth + UI hiding | +| Data corruption | High | Low | Per-row validation + best-effort | + +--- + +## Appendix + +### Module File Structure + +``` +lib/ +├── mv/ +│ └── membership/ +│ └── import/ +│ ├── member_csv.ex # prepare + process_chunk +│ ├── csv_parser.ex # delimiter detection + parsing + BOM handling +│ └── header_mapper.ex # normalization + header mapping +└── mv_web/ + └── live/ + └── global_settings_live.ex # add import section + LV message loop + +priv/ +└── static/ + └── templates/ + ├── member_import_en.csv + └── member_import_de.csv + +test/ +├── mv/ +│ └── membership/ +│ └── import/ +│ ├── member_csv_test.exs +│ ├── csv_parser_test.exs +│ └── header_mapper_test.exs +└── fixtures/ + ├── member_import_en.csv + ├── member_import_de.csv + ├── member_import_invalid.csv + ├── member_import_large.csv + └── member_import_empty_lines.csv +``` + +### Example Usage (LiveView) + +```elixir +def handle_event("start_import", _params, socket) do + assert_admin!(socket.assigns.current_user) + + [{_name, content}] = + consume_uploaded_entries(socket, :csv_file, fn %{path: path}, _entry -> + {:ok, File.read!(path)} + end) + + case Mv.Membership.Import.MemberCSV.prepare(content) do + {:ok, import_state} -> + socket = + socket + |> assign(:import_state, import_state) + |> assign(:import_progress, %{processed: 0, inserted: 0, failed: 0, errors: []}) + |> assign(:importing?, true) + + send(self(), {:process_chunk, 0}) + {:noreply, socket} + + {:error, reason} -> + {:noreply, put_flash(socket, :error, reason)} + end +end + +def handle_info({:process_chunk, idx}, socket) do + %{chunks: chunks, column_map: column_map} = socket.assigns.import_state + + case Enum.at(chunks, idx) do + nil -> + {:noreply, assign(socket, importing?: false)} + + chunk_rows_with_lines -> + {:ok, chunk_result} = + Mv.Membership.Import.MemberCSV.process_chunk(chunk_rows_with_lines, column_map) + + socket = merge_progress(socket, chunk_result) # caps errors at 50 overall + + send(self(), {:process_chunk, idx + 1}) + {:noreply, socket} + end +end +``` + +--- + +**End of Implementation Plan** \ No newline at end of file diff --git a/lib/membership/member.ex b/lib/membership/member.ex index 1d6d96e..6ae9307 100644 --- a/lib/membership/member.ex +++ b/lib/membership/member.ex @@ -5,7 +5,7 @@ defmodule Mv.Membership.Member do ## Overview Members are the core entity in the membership management system. Each member can have: - - Personal information (name, email, phone, address) + - Personal information (name, email, address) - Optional link to a User account (1:1 relationship) - Dynamic custom field values via CustomField system - Full-text searchable profile @@ -20,9 +20,8 @@ defmodule Mv.Membership.Member do - `has_one :user` - Optional authentication account link ## Validations - - Required: first_name, last_name, email + - Required: email (all other fields are optional) - Email format validation (using EctoCommons.EmailValidator) - - Phone number format: international format with 6-20 digits - Postal code format: exactly 5 digits (German format) - Date validations: join_date not in future, exit_date after join_date - Email uniqueness: prevents conflicts with unlinked users @@ -31,7 +30,7 @@ defmodule Mv.Membership.Member do Members have a `search_vector` attribute (tsvector) that is automatically updated via database trigger. Search includes name, email, notes, contact fields, and all custom field values. Custom field values are automatically included in - the search vector with weight 'C' (same as phone_number, city, etc.). + the search vector with weight 'C' (same as city, etc.). """ use Ash.Resource, domain: Mv.Membership, @@ -343,9 +342,7 @@ defmodule Mv.Membership.Member do validations do # Required fields are covered by allow_nil? false - # First name and last name must not be empty - validate present(:first_name) - validate present(:last_name) + # Email is required validate present(:email) # Email uniqueness check for all actions that change the email attribute @@ -396,11 +393,6 @@ defmodule Mv.Membership.Member do where: [present([:join_date, :exit_date])], message: "cannot be before join date" - # Phone number format (only if set) - validate match(:phone_number, ~r/^\+?[0-9\- ]{6,20}$/), - where: [present(:phone_number)], - message: "is not a valid phone number" - # Postal code format (only if set) validate match(:postal_code, ~r/^\d{5}$/), where: [present(:postal_code)], @@ -453,12 +445,12 @@ defmodule Mv.Membership.Member do uuid_v7_primary_key :id attribute :first_name, :string do - allow_nil? false + allow_nil? true constraints min_length: 1 end attribute :last_name, :string do - allow_nil? false + allow_nil? true constraints min_length: 1 end @@ -474,10 +466,6 @@ defmodule Mv.Membership.Member do constraints min_length: 5, max_length: 254 end - attribute :phone_number, :string do - allow_nil? true - end - attribute :join_date, :date do allow_nil? true end @@ -1073,7 +1061,6 @@ defmodule Mv.Membership.Member do expr( contains(postal_code, ^query) or contains(house_number, ^query) or - contains(phone_number, ^query) or contains(email, ^query) or contains(city, ^query) ) diff --git a/lib/mv/constants.ex b/lib/mv/constants.ex index c81dbd6..82a8400 100644 --- a/lib/mv/constants.ex +++ b/lib/mv/constants.ex @@ -7,7 +7,6 @@ defmodule Mv.Constants do :first_name, :last_name, :email, - :phone_number, :join_date, :exit_date, :notes, diff --git a/lib/mv_web/helpers/member_helpers.ex b/lib/mv_web/helpers/member_helpers.ex new file mode 100644 index 0000000..047bd12 --- /dev/null +++ b/lib/mv_web/helpers/member_helpers.ex @@ -0,0 +1,64 @@ +defmodule MvWeb.Helpers.MemberHelpers do + @moduledoc """ + Helper functions for member-related operations in the web layer. + + Provides utilities for formatting and displaying member information. + """ + + alias Mv.Membership.Member + + @doc """ + Returns a display name for a member. + + Combines first_name and last_name if available, otherwise falls back to email. + This ensures that members without names still have a meaningful display name. + + ## Examples + + iex> member = %Member{first_name: "John", last_name: "Doe", email: "john@example.com"} + iex> MvWeb.Helpers.MemberHelpers.display_name(member) + "John Doe" + + iex> member = %Member{first_name: nil, last_name: nil, email: "john@example.com"} + iex> MvWeb.Helpers.MemberHelpers.display_name(member) + "john@example.com" + + iex> member = %Member{first_name: "John", last_name: nil, email: "john@example.com"} + iex> MvWeb.Helpers.MemberHelpers.display_name(member) + "John" + """ + def display_name(%Member{} = member) do + name_parts = + [member.first_name, member.last_name] + |> Enum.reject(&blank?/1) + |> Enum.map_join(" ", &String.trim/1) + + if name_parts == "" do + member.email + else + name_parts + end + end + + @doc """ + Checks if a value is blank (nil, empty string, or only whitespace). + + ## Examples + + iex> MvWeb.Helpers.MemberHelpers.blank?(nil) + true + + iex> MvWeb.Helpers.MemberHelpers.blank?("") + true + + iex> MvWeb.Helpers.MemberHelpers.blank?(" ") + true + + iex> MvWeb.Helpers.MemberHelpers.blank?("John") + false + """ + def blank?(nil), do: true + def blank?(""), do: true + def blank?(value) when is_binary(value), do: String.trim(value) == "" + def blank?(_), do: false +end diff --git a/lib/mv_web/live/contribution_period_live/show.ex b/lib/mv_web/live/contribution_period_live/show.ex index 83d9207..b6a2574 100644 --- a/lib/mv_web/live/contribution_period_live/show.ex +++ b/lib/mv_web/live/contribution_period_live/show.ex @@ -36,7 +36,7 @@ defmodule MvWeb.ContributionPeriodLive.Show do <.mockup_warning /> <.header> - {gettext("Contributions for %{name}", name: "#{@member.first_name} #{@member.last_name}")} + {gettext("Contributions for %{name}", name: MvWeb.Helpers.MemberHelpers.display_name(@member))} <:subtitle> {gettext("Contribution type")}: {@member.contribution_type} diff --git a/lib/mv_web/live/custom_field_value_live/form.ex b/lib/mv_web/live/custom_field_value_live/form.ex index 9663927..4db2bed 100644 --- a/lib/mv_web/live/custom_field_value_live/form.ex +++ b/lib/mv_web/live/custom_field_value_live/form.ex @@ -289,6 +289,6 @@ defmodule MvWeb.CustomFieldValueLive.Form do end defp member_options(members) do - Enum.map(members, &{"#{&1.first_name} #{&1.last_name}", &1.id}) + Enum.map(members, &{MvWeb.Helpers.MemberHelpers.display_name(&1), &1.id}) end end diff --git a/lib/mv_web/live/member_live/form.ex b/lib/mv_web/live/member_live/form.ex index 53754aa..0a05e1f 100644 --- a/lib/mv_web/live/member_live/form.ex +++ b/lib/mv_web/live/member_live/form.ex @@ -43,7 +43,7 @@ defmodule MvWeb.MemberLive.Form do

<%= if @member do %> - {@member.first_name} {@member.last_name} + {MvWeb.Helpers.MemberHelpers.display_name(@member)} <% else %> {gettext("New Member")} <% end %> @@ -82,10 +82,10 @@ defmodule MvWeb.MemberLive.Form do <%!-- Name Row --%>
- <.input field={@form[:first_name]} label={gettext("First Name")} required /> + <.input field={@form[:first_name]} label={gettext("First Name")} />
- <.input field={@form[:last_name]} label={gettext("Last Name")} required /> + <.input field={@form[:last_name]} label={gettext("Last Name")} />
@@ -110,11 +110,6 @@ defmodule MvWeb.MemberLive.Form do <.input field={@form[:email]} label={gettext("Email")} required type="email" /> - <%!-- Phone --%> -
- <.input field={@form[:phone_number]} label={gettext("Phone")} type="tel" /> -
- <%!-- Membership Dates Row --%>
diff --git a/lib/mv_web/live/member_live/index.html.heex b/lib/mv_web/live/member_live/index.html.heex index c8ba7e4..1557ed9 100644 --- a/lib/mv_web/live/member_live/index.html.heex +++ b/lib/mv_web/live/member_live/index.html.heex @@ -239,24 +239,6 @@ > {member.city} - <:col - :let={member} - :if={:phone_number in @member_fields_visible} - label={ - ~H""" - <.live_component - module={MvWeb.Components.SortHeaderComponent} - id={:sort_phone_number} - field={:phone_number} - label={gettext("Phone Number")} - sort_field={@sort_field} - sort_order={@sort_order} - /> - """ - } - > - {member.phone_number} - <:col :let={member} :if={:join_date in @member_fields_visible} diff --git a/lib/mv_web/live/member_live/show.ex b/lib/mv_web/live/member_live/show.ex index c2af0a9..997cb1a 100644 --- a/lib/mv_web/live/member_live/show.ex +++ b/lib/mv_web/live/member_live/show.ex @@ -35,7 +35,7 @@ defmodule MvWeb.MemberLive.Show do

- {@member.first_name} {@member.last_name} + {MvWeb.Helpers.MemberHelpers.display_name(@member)}

<.button variant="primary" navigate={~p"/members/#{@member}/edit?return_to=show"}> @@ -104,11 +104,6 @@ defmodule MvWeb.MemberLive.Show do
- <%!-- Phone --%> -
- <.data_field label={gettext("Phone")} value={@member.phone_number} /> -
- <%!-- Membership Dates Row --%>
<.data_field diff --git a/lib/mv_web/live/user_live/form.ex b/lib/mv_web/live/user_live/form.ex index 0639e75..f0cc1ce 100644 --- a/lib/mv_web/live/user_live/form.ex +++ b/lib/mv_web/live/user_live/form.ex @@ -131,7 +131,7 @@ defmodule MvWeb.UserLive.Form do

- {@user.member.first_name} {@user.member.last_name} + {MvWeb.Helpers.MemberHelpers.display_name(@user.member)}

{@user.member.email}

@@ -210,7 +210,7 @@ defmodule MvWeb.UserLive.Form do ) ]} > -

{member.first_name} {member.last_name}

+

{MvWeb.Helpers.MemberHelpers.display_name(member)}

{member.email}

<% end %> @@ -438,7 +438,7 @@ defmodule MvWeb.UserLive.Form do member_name = if selected_member, - do: "#{selected_member.first_name} #{selected_member.last_name}", + do: MvWeb.Helpers.MemberHelpers.display_name(selected_member), else: "" # Store the selected member ID and name in socket state and clear unlink flag diff --git a/lib/mv_web/live/user_live/index.html.heex b/lib/mv_web/live/user_live/index.html.heex index 9a98159..e7fd72e 100644 --- a/lib/mv_web/live/user_live/index.html.heex +++ b/lib/mv_web/live/user_live/index.html.heex @@ -51,7 +51,7 @@ <:col :let={user} label={gettext("Linked Member")}> <%= if user.member do %> - {user.member.first_name} {user.member.last_name} + {MvWeb.Helpers.MemberHelpers.display_name(user.member)} <% else %> {gettext("No member linked")} <% end %> diff --git a/lib/mv_web/live/user_live/show.ex b/lib/mv_web/live/user_live/show.ex index 777def1..9eaa4fa 100644 --- a/lib/mv_web/live/user_live/show.ex +++ b/lib/mv_web/live/user_live/show.ex @@ -57,7 +57,7 @@ defmodule MvWeb.UserLive.Show do class="text-blue-600 underline hover:text-blue-800" > <.icon name="hero-users" class="inline w-4 h-4 mr-1" /> - {@user.member.first_name} {@user.member.last_name} + {MvWeb.Helpers.MemberHelpers.display_name(@user.member)} <% else %> {gettext("No member linked")} diff --git a/lib/mv_web/translations/member_fields.ex b/lib/mv_web/translations/member_fields.ex index f10e0d2..2d6834a 100644 --- a/lib/mv_web/translations/member_fields.ex +++ b/lib/mv_web/translations/member_fields.ex @@ -20,7 +20,6 @@ defmodule MvWeb.Translations.MemberFields do def label(:first_name), do: gettext("First Name") def label(:last_name), do: gettext("Last Name") def label(:email), do: gettext("Email") - def label(:phone_number), do: gettext("Phone") def label(:join_date), do: gettext("Join Date") def label(:exit_date), do: gettext("Exit Date") def label(:notes), do: gettext("Notes") diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index ef28ae8..9467ed7 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -150,11 +150,6 @@ msgstr "Notizen" msgid "Paid" msgstr "Bezahlt" -#: lib/mv_web/live/member_live/index.html.heex -#, elixir-autogen, elixir-format -msgid "Phone Number" -msgstr "Telefonnummer" - #: lib/mv_web/live/member_live/form.ex #: lib/mv_web/live/member_live/index.html.heex #: lib/mv_web/translations/member_fields.ex @@ -842,13 +837,6 @@ msgstr "Zahlungen" msgid "Personal Data" msgstr "Persönliche Daten" -#: lib/mv_web/live/member_live/form.ex -#: lib/mv_web/live/member_live/show.ex -#: lib/mv_web/translations/member_fields.ex -#, elixir-autogen, elixir-format -msgid "Phone" -msgstr "Telefon" - #: lib/mv_web/live/member_live/form.ex #: lib/mv_web/live/member_live/show/membership_fees_component.ex #, elixir-autogen, elixir-format @@ -1903,6 +1891,18 @@ msgstr "Nicht gesetzt" #~ msgid "Pending" #~ msgstr "Ausstehend" +#~ #: lib/mv_web/live/member_live/form.ex +#~ #: lib/mv_web/live/member_live/show.ex +#~ #: lib/mv_web/translations/member_fields.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Phone" +#~ msgstr "Telefon" + +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Phone Number" +#~ msgstr "Telefonnummer" + #~ #: lib/mv_web/live/contribution_settings_live.ex #~ #, elixir-autogen, elixir-format #~ msgid "Quarterly Interval - Joining Period Excluded" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index be36eb6..77931d4 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -151,11 +151,6 @@ msgstr "" msgid "Paid" msgstr "" -#: lib/mv_web/live/member_live/index.html.heex -#, elixir-autogen, elixir-format -msgid "Phone Number" -msgstr "" - #: lib/mv_web/live/member_live/form.ex #: lib/mv_web/live/member_live/index.html.heex #: lib/mv_web/translations/member_fields.ex @@ -843,13 +838,6 @@ msgstr "" msgid "Personal Data" msgstr "" -#: lib/mv_web/live/member_live/form.ex -#: lib/mv_web/live/member_live/show.ex -#: lib/mv_web/translations/member_fields.ex -#, elixir-autogen, elixir-format -msgid "Phone" -msgstr "" - #: lib/mv_web/live/member_live/form.ex #: lib/mv_web/live/member_live/show/membership_fees_component.ex #, elixir-autogen, elixir-format diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 9c2dc9a..5846f7b 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -151,11 +151,6 @@ msgstr "" msgid "Paid" msgstr "" -#: lib/mv_web/live/member_live/index.html.heex -#, elixir-autogen, elixir-format -msgid "Phone Number" -msgstr "" - #: lib/mv_web/live/member_live/form.ex #: lib/mv_web/live/member_live/index.html.heex #: lib/mv_web/translations/member_fields.ex @@ -843,13 +838,6 @@ msgstr "" msgid "Personal Data" msgstr "" -#: lib/mv_web/live/member_live/form.ex -#: lib/mv_web/live/member_live/show.ex -#: lib/mv_web/translations/member_fields.ex -#, elixir-autogen, elixir-format, fuzzy -msgid "Phone" -msgstr "" - #: lib/mv_web/live/member_live/form.ex #: lib/mv_web/live/member_live/show/membership_fees_component.ex #, elixir-autogen, elixir-format, fuzzy @@ -1827,46 +1815,62 @@ msgstr "" msgid "Not set" msgstr "" +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Show current cycle" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Unpaid in last cycle" +#~ msgstr "" + +#~ #: lib/mv_web/live/custom_field_live/index_component.ex +#~ #, elixir-autogen, elixir-format, fuzzy +#~ msgid "New Custom field" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Show Last/Current Cycle Payment Status" +#~ msgstr "" + #~ #: lib/mv_web/live/components/payment_filter_component.ex #~ #, elixir-autogen, elixir-format #~ msgid "All payment statuses" #~ msgstr "" -#~ #: lib/mv_web/live/custom_field_live/show.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Auto-generated identifier (immutable)" -#~ msgstr "" - -#~ #: lib/mv_web/live/contribution_settings_live.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Configure global settings for membership contributions." -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/form.ex -#~ #: lib/mv_web/live/member_live/show.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Contribution" -#~ msgstr "" - -#~ #: lib/mv_web/components/layouts/navbar.ex -#~ #: lib/mv_web/live/contribution_settings_live.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Contribution Settings" -#~ msgstr "" - -#~ #: lib/mv_web/live/contribution_settings_live.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Contribution start" -#~ msgstr "" - #~ #: lib/mv_web/live/member_live/index.html.heex #~ #, elixir-autogen, elixir-format #~ msgid "Copy emails" #~ msgstr "" +#~ #: lib/mv_web/live/member_live/form.ex +#~ #: lib/mv_web/live/member_live/show.ex +#~ #: lib/mv_web/translations/member_fields.ex +#~ #, elixir-autogen, elixir-format, fuzzy +#~ msgid "Phone" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/show.ex +#~ #, elixir-autogen, elixir-format, fuzzy +#~ msgid "Pending" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/show.ex +#~ #, elixir-autogen, elixir-format, fuzzy +#~ msgid "Payment Cycle" +#~ msgstr "" + #~ #: lib/mv_web/live/contribution_settings_live.ex #~ #, elixir-autogen, elixir-format -#~ msgid "Default Contribution Type" +#~ msgid "View Example Member" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/form.ex +#~ #: lib/mv_web/live/member_live/show.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "This data is for demonstration purposes only (mockup)." #~ msgstr "" #~ #: lib/mv_web/live/contribution_settings_live.ex @@ -1875,6 +1879,11 @@ msgstr "" #~ msgid "Edit amount" #~ msgstr "" +#~ #: lib/mv_web/live/contribution_settings_live.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Quarterly Interval - Joining Period Excluded" +#~ msgstr "" + #~ #: lib/mv_web/live/contribution_settings_live.ex #~ #, elixir-autogen, elixir-format #~ msgid "Example: Member Contribution View" @@ -1885,20 +1894,20 @@ msgstr "" #~ msgid "Failed to delete some cycles: %{errors}" #~ msgstr "" +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Switch to current cycle" +#~ msgstr "" + #~ #: lib/mv_web/live/membership_fee_settings_live.ex #~ #, elixir-autogen, elixir-format #~ msgid "Failed to save settings. Please check the errors below." #~ msgstr "" -#~ #: lib/mv_web/live/user_live/index.html.heex -#~ #: lib/mv_web/live/user_live/show.ex +#~ #: lib/mv_web/components/layouts/navbar.ex +#~ #: lib/mv_web/live/contribution_settings_live.ex #~ #, elixir-autogen, elixir-format -#~ msgid "Generated periods" -#~ msgstr "" - -#~ #: lib/mv_web/live/custom_field_live/form_component.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Immutable" +#~ msgid "Contribution Settings" #~ msgstr "" #~ #: lib/mv_web/live/contribution_settings_live.ex @@ -1906,80 +1915,9 @@ msgstr "" #~ msgid "Include joining period" #~ msgstr "" -#~ #: lib/mv_web/live/custom_field_live/index_component.ex -#~ #, elixir-autogen, elixir-format, fuzzy -#~ msgid "New Custom field" -#~ msgstr "" - -#~ #: lib/mv_web/live/components/payment_filter_component.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Not paid" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/show.ex -#~ #, elixir-autogen, elixir-format, fuzzy -#~ msgid "Payment Cycle" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/show.ex -#~ #, elixir-autogen, elixir-format, fuzzy -#~ msgid "Pending" -#~ msgstr "" - #~ #: lib/mv_web/live/contribution_settings_live.ex #~ #, elixir-autogen, elixir-format -#~ msgid "Quarterly Interval - Joining Period Excluded" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/index.html.heex -#~ #, elixir-autogen, elixir-format -#~ msgid "Show Last/Current Cycle Payment Status" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/index.html.heex -#~ #, elixir-autogen, elixir-format -#~ msgid "Show current cycle" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/index.html.heex -#~ #, elixir-autogen, elixir-format -#~ msgid "Show last completed cycle" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/index.html.heex -#~ #, elixir-autogen, elixir-format -#~ msgid "Switch to current cycle" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/index.html.heex -#~ #, elixir-autogen, elixir-format -#~ msgid "Switch to last completed cycle" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/form.ex -#~ #: lib/mv_web/live/member_live/show.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "This data is for demonstration purposes only (mockup)." -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/index.html.heex -#~ #, elixir-autogen, elixir-format -#~ msgid "Unpaid in current cycle" -#~ msgstr "" - -#~ #: lib/mv_web/live/member_live/index.html.heex -#~ #, elixir-autogen, elixir-format -#~ msgid "Unpaid in last cycle" -#~ msgstr "" - -#~ #: lib/mv_web/live/contribution_settings_live.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "View Example Member" -#~ msgstr "" - -#~ #: lib/mv_web/live/contribution_settings_live.ex -#~ #, elixir-autogen, elixir-format -#~ msgid "Yearly Interval - Joining Period Included" +#~ msgid "Contribution start" #~ msgstr "" #~ #: lib/mv_web/live/member_live/form.ex @@ -1988,7 +1926,69 @@ msgstr "" #~ msgid "monthly" #~ msgstr "" +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Show last completed cycle" +#~ msgstr "" + +#~ #: lib/mv_web/live/components/payment_filter_component.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Not paid" +#~ msgstr "" + +#~ #: lib/mv_web/live/contribution_settings_live.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Yearly Interval - Joining Period Included" +#~ msgstr "" + +#~ #: lib/mv_web/live/custom_field_live/form_component.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Immutable" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/form.ex +#~ #: lib/mv_web/live/member_live/show.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Contribution" +#~ msgstr "" + +#~ #: lib/mv_web/live/user_live/index.html.heex +#~ #: lib/mv_web/live/user_live/show.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Generated periods" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Switch to last completed cycle" +#~ msgstr "" + +#~ #: lib/mv_web/live/contribution_settings_live.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Configure global settings for membership contributions." +#~ msgstr "" + +#~ #: lib/mv_web/live/custom_field_live/show.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Auto-generated identifier (immutable)" +#~ msgstr "" + +#~ #: lib/mv_web/live/contribution_settings_live.ex +#~ #, elixir-autogen, elixir-format +#~ msgid "Default Contribution Type" +#~ msgstr "" + #~ #: lib/mv_web/live/member_live/form.ex #~ #, elixir-autogen, elixir-format #~ msgid "yearly" #~ msgstr "" + +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Phone Number" +#~ msgstr "" + +#~ #: lib/mv_web/live/member_live/index.html.heex +#~ #, elixir-autogen, elixir-format +#~ msgid "Unpaid in current cycle" +#~ msgstr "" diff --git a/priv/repo/migrations/20260102155350_remove_phone_number_and_make_fields_optional.exs b/priv/repo/migrations/20260102155350_remove_phone_number_and_make_fields_optional.exs new file mode 100644 index 0000000..5943b78 --- /dev/null +++ b/priv/repo/migrations/20260102155350_remove_phone_number_and_make_fields_optional.exs @@ -0,0 +1,404 @@ +defmodule Mv.Repo.Migrations.RemovePhoneNumberAndMakeFieldsOptional do + @moduledoc """ + Removes phone_number field from members table and makes first_name/last_name optional. + + This migration: + 1. Removes phone_number column from members table + 2. Makes first_name and last_name columns nullable + 3. Updates members_search_vector_trigger() function to remove phone_number + 4. Updates update_member_search_vector_from_custom_field_value() function to remove phone_number + 5. Updates existing search_vector values for all members + """ + + use Ecto.Migration + + def up do + # Update the main trigger function to remove phone_number + execute(""" + CREATE OR REPLACE FUNCTION members_search_vector_trigger() RETURNS trigger AS $$ + DECLARE + custom_values_text text; + BEGIN + -- Aggregate all custom field values for this member + -- Support both formats: _union_type/_union_value (Ash format) and type/value (legacy) + -- ->> operator always returns TEXT directly (no need for -> + ::text fallback) + SELECT string_agg( + CASE + WHEN value ? '_union_value' THEN value->>'_union_value' + WHEN value ? 'value' THEN value->>'value' + ELSE '' + END, + ' ' + ) + INTO custom_values_text + FROM custom_field_values + WHERE member_id = NEW.id AND value IS NOT NULL; + + -- Build search_vector with member fields and custom field values + NEW.search_vector := + setweight(to_tsvector('simple', coalesce(NEW.first_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(NEW.last_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(NEW.email, '')), 'B') || + setweight(to_tsvector('simple', coalesce(NEW.join_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(NEW.exit_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(NEW.notes, '')), 'B') || + setweight(to_tsvector('simple', coalesce(NEW.city, '')), 'C') || + setweight(to_tsvector('simple', coalesce(NEW.street, '')), 'C') || + setweight(to_tsvector('simple', coalesce(NEW.house_number::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(NEW.postal_code::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(custom_values_text, '')), 'C'); + RETURN NEW; + END + $$ LANGUAGE plpgsql; + """) + + # Update trigger function to remove phone_number + execute(""" + CREATE OR REPLACE FUNCTION update_member_search_vector_from_custom_field_value() RETURNS trigger AS $$ + DECLARE + member_id_val uuid; + member_first_name text; + member_last_name text; + member_email text; + member_join_date date; + member_exit_date date; + member_notes text; + member_city text; + member_street text; + member_house_number text; + member_postal_code text; + custom_values_text text; + old_value_text text; + new_value_text text; + BEGIN + -- Get member ID from trigger context + member_id_val := COALESCE(NEW.member_id, OLD.member_id); + + -- Optimization: For UPDATE operations, check if value actually changed + -- If value hasn't changed, we can skip the expensive re-aggregation + IF TG_OP = 'UPDATE' THEN + -- Extract OLD value for comparison (handle both JSONB formats) + -- ->> operator always returns TEXT directly + old_value_text := COALESCE( + NULLIF(OLD.value->>'_union_value', ''), + NULLIF(OLD.value->>'value', ''), + '' + ); + + -- Extract NEW value for comparison (handle both JSONB formats) + new_value_text := COALESCE( + NULLIF(NEW.value->>'_union_value', ''), + NULLIF(NEW.value->>'value', ''), + '' + ); + + -- Check if value, member_id, or custom_field_id actually changed + -- If nothing changed, skip expensive re-aggregation + IF (old_value_text IS NOT DISTINCT FROM new_value_text) AND + (OLD.member_id IS NOT DISTINCT FROM NEW.member_id) AND + (OLD.custom_field_id IS NOT DISTINCT FROM NEW.custom_field_id) THEN + RETURN COALESCE(NEW, OLD); + END IF; + END IF; + + -- Fetch only required fields instead of full record (performance optimization) + SELECT + first_name, + last_name, + email, + join_date, + exit_date, + notes, + city, + street, + house_number, + postal_code + INTO + member_first_name, + member_last_name, + member_email, + member_join_date, + member_exit_date, + member_notes, + member_city, + member_street, + member_house_number, + member_postal_code + FROM members + WHERE id = member_id_val; + + -- Aggregate all custom field values for this member + -- Support both formats: _union_type/_union_value (Ash format) and type/value (legacy) + -- ->> operator always returns TEXT directly + SELECT string_agg( + CASE + WHEN value ? '_union_value' THEN value->>'_union_value' + WHEN value ? 'value' THEN value->>'value' + ELSE '' + END, + ' ' + ) + INTO custom_values_text + FROM custom_field_values + WHERE member_id = member_id_val AND value IS NOT NULL; + + -- Update the search_vector for the affected member + UPDATE members + SET search_vector = + setweight(to_tsvector('simple', coalesce(member_first_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(member_last_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(member_email, '')), 'B') || + setweight(to_tsvector('simple', coalesce(member_join_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(member_exit_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(member_notes, '')), 'B') || + setweight(to_tsvector('simple', coalesce(member_city, '')), 'C') || + setweight(to_tsvector('simple', coalesce(member_street, '')), 'C') || + setweight(to_tsvector('simple', coalesce(member_house_number::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(member_postal_code::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(custom_values_text, '')), 'C') + WHERE id = member_id_val; + + RETURN COALESCE(NEW, OLD); + END + $$ LANGUAGE plpgsql; + """) + + # Update existing search_vector values for all members + execute(""" + UPDATE members m + SET search_vector = + setweight(to_tsvector('simple', coalesce(m.first_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(m.last_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(m.email, '')), 'B') || + setweight(to_tsvector('simple', coalesce(m.join_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(m.exit_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(m.notes, '')), 'B') || + setweight(to_tsvector('simple', coalesce(m.city, '')), 'C') || + setweight(to_tsvector('simple', coalesce(m.street, '')), 'C') || + setweight(to_tsvector('simple', coalesce(m.house_number::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(m.postal_code::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce( + (SELECT string_agg( + CASE + WHEN value ? '_union_value' THEN value->>'_union_value' + WHEN value ? 'value' THEN value->>'value' + ELSE '' + END, + ' ' + ) + FROM custom_field_values + WHERE member_id = m.id AND value IS NOT NULL), + '' + )), 'C') + """) + + # Make first_name and last_name nullable + execute("ALTER TABLE members ALTER COLUMN first_name DROP NOT NULL") + execute("ALTER TABLE members ALTER COLUMN last_name DROP NOT NULL") + + # Remove phone_number column + alter table(:members) do + remove :phone_number + end + end + + def down do + # Set default values for NULL fields before restoring NOT NULL constraint + # This prevents the migration from failing if NULL values exist + execute("UPDATE members SET first_name = '' WHERE first_name IS NULL") + execute("UPDATE members SET last_name = '' WHERE last_name IS NULL") + + # Restore first_name and last_name as NOT NULL + execute("ALTER TABLE members ALTER COLUMN first_name SET NOT NULL") + execute("ALTER TABLE members ALTER COLUMN last_name SET NOT NULL") + + # Add phone_number column back + alter table(:members) do + add :phone_number, :text + end + + # Restore trigger functions with phone_number + execute(""" + CREATE OR REPLACE FUNCTION members_search_vector_trigger() RETURNS trigger AS $$ + DECLARE + custom_values_text text; + BEGIN + -- Aggregate all custom field values for this member + -- Support both formats: _union_type/_union_value (Ash format) and type/value (legacy) + -- ->> operator always returns TEXT directly (no need for -> + ::text fallback) + SELECT string_agg( + CASE + WHEN value ? '_union_value' THEN value->>'_union_value' + WHEN value ? 'value' THEN value->>'value' + ELSE '' + END, + ' ' + ) + INTO custom_values_text + FROM custom_field_values + WHERE member_id = NEW.id AND value IS NOT NULL; + + -- Build search_vector with member fields and custom field values + NEW.search_vector := + setweight(to_tsvector('simple', coalesce(NEW.first_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(NEW.last_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(NEW.email, '')), 'B') || + setweight(to_tsvector('simple', coalesce(NEW.phone_number, '')), 'C') || + setweight(to_tsvector('simple', coalesce(NEW.join_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(NEW.exit_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(NEW.notes, '')), 'B') || + setweight(to_tsvector('simple', coalesce(NEW.city, '')), 'C') || + setweight(to_tsvector('simple', coalesce(NEW.street, '')), 'C') || + setweight(to_tsvector('simple', coalesce(NEW.house_number::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(NEW.postal_code::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(custom_values_text, '')), 'C'); + RETURN NEW; + END + $$ LANGUAGE plpgsql; + """) + + execute(""" + CREATE OR REPLACE FUNCTION update_member_search_vector_from_custom_field_value() RETURNS trigger AS $$ + DECLARE + member_id_val uuid; + member_first_name text; + member_last_name text; + member_email text; + member_phone_number text; + member_join_date date; + member_exit_date date; + member_notes text; + member_city text; + member_street text; + member_house_number text; + member_postal_code text; + custom_values_text text; + old_value_text text; + new_value_text text; + BEGIN + -- Get member ID from trigger context + member_id_val := COALESCE(NEW.member_id, OLD.member_id); + + -- Optimization: For UPDATE operations, check if value actually changed + -- If value hasn't changed, we can skip the expensive re-aggregation + IF TG_OP = 'UPDATE' THEN + -- Extract OLD value for comparison (handle both JSONB formats) + -- ->> operator always returns TEXT directly + old_value_text := COALESCE( + NULLIF(OLD.value->>'_union_value', ''), + NULLIF(OLD.value->>'value', ''), + '' + ); + + -- Extract NEW value for comparison (handle both JSONB formats) + new_value_text := COALESCE( + NULLIF(NEW.value->>'_union_value', ''), + NULLIF(NEW.value->>'value', ''), + '' + ); + + -- Check if value, member_id, or custom_field_id actually changed + -- If nothing changed, skip expensive re-aggregation + IF (old_value_text IS NOT DISTINCT FROM new_value_text) AND + (OLD.member_id IS NOT DISTINCT FROM NEW.member_id) AND + (OLD.custom_field_id IS NOT DISTINCT FROM NEW.custom_field_id) THEN + RETURN COALESCE(NEW, OLD); + END IF; + END IF; + + -- Fetch only required fields instead of full record (performance optimization) + SELECT + first_name, + last_name, + email, + phone_number, + join_date, + exit_date, + notes, + city, + street, + house_number, + postal_code + INTO + member_first_name, + member_last_name, + member_email, + member_phone_number, + member_join_date, + member_exit_date, + member_notes, + member_city, + member_street, + member_house_number, + member_postal_code + FROM members + WHERE id = member_id_val; + + -- Aggregate all custom field values for this member + -- Support both formats: _union_type/_union_value (Ash format) and type/value (legacy) + -- ->> operator always returns TEXT directly + SELECT string_agg( + CASE + WHEN value ? '_union_value' THEN value->>'_union_value' + WHEN value ? 'value' THEN value->>'value' + ELSE '' + END, + ' ' + ) + INTO custom_values_text + FROM custom_field_values + WHERE member_id = member_id_val AND value IS NOT NULL; + + -- Update the search_vector for the affected member + UPDATE members + SET search_vector = + setweight(to_tsvector('simple', coalesce(member_first_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(member_last_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(member_email, '')), 'B') || + setweight(to_tsvector('simple', coalesce(member_phone_number, '')), 'C') || + setweight(to_tsvector('simple', coalesce(member_join_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(member_exit_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(member_notes, '')), 'B') || + setweight(to_tsvector('simple', coalesce(member_city, '')), 'C') || + setweight(to_tsvector('simple', coalesce(member_street, '')), 'C') || + setweight(to_tsvector('simple', coalesce(member_house_number::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(member_postal_code::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(custom_values_text, '')), 'C') + WHERE id = member_id_val; + + RETURN COALESCE(NEW, OLD); + END + $$ LANGUAGE plpgsql; + """) + + # Update existing search_vector values to include phone_number + execute(""" + UPDATE members m + SET search_vector = + setweight(to_tsvector('simple', coalesce(m.first_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(m.last_name, '')), 'A') || + setweight(to_tsvector('simple', coalesce(m.email, '')), 'B') || + setweight(to_tsvector('simple', coalesce(m.phone_number, '')), 'C') || + setweight(to_tsvector('simple', coalesce(m.join_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(m.exit_date::text, '')), 'D') || + setweight(to_tsvector('simple', coalesce(m.notes, '')), 'B') || + setweight(to_tsvector('simple', coalesce(m.city, '')), 'C') || + setweight(to_tsvector('simple', coalesce(m.street, '')), 'C') || + setweight(to_tsvector('simple', coalesce(m.house_number::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce(m.postal_code::text, '')), 'C') || + setweight(to_tsvector('simple', coalesce( + (SELECT string_agg( + CASE + WHEN value ? '_union_value' THEN value->>'_union_value' + WHEN value ? 'value' THEN value->>'value' + ELSE '' + END, + ' ' + ) + FROM custom_field_values + WHERE member_id = m.id AND value IS NOT NULL), + '' + )), 'C') + """) + end +end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index fb102f4..4f99e5b 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -147,7 +147,6 @@ member_attrs_list = [ last_name: "Müller", email: "hans.mueller@example.de", join_date: ~D[2023-01-15], - phone_number: "+49301234567", city: "München", street: "Hauptstraße", house_number: "42", @@ -160,7 +159,6 @@ member_attrs_list = [ last_name: "Schmidt", email: "greta.schmidt@example.de", join_date: ~D[2023-02-01], - phone_number: "+49309876543", city: "Hamburg", street: "Lindenstraße", house_number: "17", @@ -174,7 +172,6 @@ member_attrs_list = [ last_name: "Wagner", email: "friedrich.wagner@example.de", join_date: ~D[2022-11-10], - phone_number: "+49301122334", city: "Berlin", street: "Kastanienallee", house_number: "8", @@ -186,7 +183,6 @@ member_attrs_list = [ last_name: "Wagner", email: "marianne.wagner@example.de", join_date: ~D[2022-11-10], - phone_number: "+49301122334", city: "Berlin", street: "Kastanienallee", house_number: "8" @@ -299,7 +295,6 @@ linked_members = [ last_name: "Weber", email: "maria.weber@example.de", join_date: ~D[2023-03-15], - phone_number: "+49301357924", city: "Frankfurt", street: "Goetheplatz", house_number: "5", @@ -313,7 +308,6 @@ linked_members = [ last_name: "Klein", email: "thomas.klein@example.de", join_date: ~D[2023-04-01], - phone_number: "+49302468135", city: "Köln", street: "Rheinstraße", house_number: "23", diff --git a/test/membership/member_test.exs b/test/membership/member_test.exs index 1c4beb1..258d8be 100644 --- a/test/membership/member_test.exs +++ b/test/membership/member_test.exs @@ -7,7 +7,6 @@ defmodule Mv.Membership.MemberTest do first_name: "John", last_name: "Doe", email: "john@example.com", - phone_number: "+49123456789", join_date: ~D[2020-01-01], exit_date: nil, notes: "Test note", @@ -17,16 +16,14 @@ defmodule Mv.Membership.MemberTest do postal_code: "12345" } - test "First name is required and must not be empty" do - attrs = Map.put(@valid_attrs, :first_name, "") - assert {:error, %Ash.Error.Invalid{errors: errors}} = Membership.create_member(attrs) - assert error_message(errors, :first_name) =~ "must be present" + test "First name is optional" do + attrs = Map.delete(@valid_attrs, :first_name) + assert {:ok, _member} = Membership.create_member(attrs) end - test "Last name is required and must not be empty" do - attrs = Map.put(@valid_attrs, :last_name, "") - assert {:error, %Ash.Error.Invalid{errors: errors}} = Membership.create_member(attrs) - assert error_message(errors, :last_name) =~ "must be present" + test "Last name is optional" do + attrs = Map.delete(@valid_attrs, :last_name) + assert {:ok, _member} = Membership.create_member(attrs) end test "Email is required" do @@ -41,14 +38,6 @@ defmodule Mv.Membership.MemberTest do assert error_message(errors, :email) =~ "is not a valid email" end - test "Phone number is optional but must have a valid format if specified" do - attrs = Map.put(@valid_attrs, :phone_number, "abc") - assert {:error, %Ash.Error.Invalid{errors: errors}} = Membership.create_member(attrs) - assert error_message(errors, :phone_number) =~ "is not a valid phone number" - attrs2 = Map.delete(@valid_attrs, :phone_number) - assert {:ok, _member} = Membership.create_member(attrs2) - end - test "Join date cannot be in the future" do attrs = Map.put(@valid_attrs, :join_date, Date.utc_today() |> Date.add(1)) diff --git a/test/mv_web/components/sort_header_component_test.exs b/test/mv_web/components/sort_header_component_test.exs index e199635..6d23ab4 100644 --- a/test/mv_web/components/sort_header_component_test.exs +++ b/test/mv_web/components/sort_header_component_test.exs @@ -24,7 +24,6 @@ defmodule MvWeb.Components.SortHeaderComponentTest do :house_number, :postal_code, :city, - :phone_number, :join_date ] @@ -101,7 +100,6 @@ defmodule MvWeb.Components.SortHeaderComponentTest do assert has_element?(view, "[data-testid='street'] .opacity-40") assert has_element?(view, "[data-testid='house_number'] .opacity-40") assert has_element?(view, "[data-testid='postal_code'] .opacity-40") - assert has_element?(view, "[data-testid='phone_number'] .opacity-40") assert has_element?(view, "[data-testid='join_date'] .opacity-40") end diff --git a/test/mv_web/helpers/member_helpers_test.exs b/test/mv_web/helpers/member_helpers_test.exs new file mode 100644 index 0000000..7a11235 --- /dev/null +++ b/test/mv_web/helpers/member_helpers_test.exs @@ -0,0 +1,141 @@ +defmodule MvWeb.Helpers.MemberHelpersTest do + @moduledoc """ + Tests for the display_name/1 helper function in MemberHelpers. + """ + use Mv.DataCase, async: true + + alias Mv.Membership.Member + alias MvWeb.Helpers.MemberHelpers + + describe "display_name/1" do + test "returns full name when both first_name and last_name are present" do + member = %Member{ + first_name: "John", + last_name: "Doe", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John Doe" + end + + test "returns email when both first_name and last_name are nil" do + member = %Member{ + first_name: nil, + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "returns first_name only when last_name is nil" do + member = %Member{ + first_name: "John", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John" + end + + test "returns last_name only when first_name is nil" do + member = %Member{ + first_name: nil, + last_name: "Doe", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "Doe" + end + + test "returns email when first_name and last_name are empty strings" do + member = %Member{ + first_name: "", + last_name: "", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "returns email when first_name and last_name are whitespace only" do + member = %Member{ + first_name: " ", + last_name: " \t ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "trims whitespace from name parts" do + member = %Member{ + first_name: " John ", + last_name: " Doe ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John Doe" + end + + test "handles one empty string and one nil" do + member = %Member{ + first_name: "", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "handles one nil and one empty string" do + member = %Member{ + first_name: nil, + last_name: "", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "handles one whitespace and one nil" do + member = %Member{ + first_name: " ", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "handles one valid name and one whitespace" do + member = %Member{ + first_name: "John", + last_name: " ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John" + end + + test "handles member with only first_name containing whitespace" do + member = %Member{ + first_name: " John ", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John" + end + + test "handles member with only last_name containing whitespace" do + member = %Member{ + first_name: nil, + last_name: " Doe ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "Doe" + end + end +end diff --git a/test/mv_web/member_live/index_display_name_test.exs b/test/mv_web/member_live/index_display_name_test.exs new file mode 100644 index 0000000..7a11235 --- /dev/null +++ b/test/mv_web/member_live/index_display_name_test.exs @@ -0,0 +1,141 @@ +defmodule MvWeb.Helpers.MemberHelpersTest do + @moduledoc """ + Tests for the display_name/1 helper function in MemberHelpers. + """ + use Mv.DataCase, async: true + + alias Mv.Membership.Member + alias MvWeb.Helpers.MemberHelpers + + describe "display_name/1" do + test "returns full name when both first_name and last_name are present" do + member = %Member{ + first_name: "John", + last_name: "Doe", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John Doe" + end + + test "returns email when both first_name and last_name are nil" do + member = %Member{ + first_name: nil, + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "returns first_name only when last_name is nil" do + member = %Member{ + first_name: "John", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John" + end + + test "returns last_name only when first_name is nil" do + member = %Member{ + first_name: nil, + last_name: "Doe", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "Doe" + end + + test "returns email when first_name and last_name are empty strings" do + member = %Member{ + first_name: "", + last_name: "", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "returns email when first_name and last_name are whitespace only" do + member = %Member{ + first_name: " ", + last_name: " \t ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "trims whitespace from name parts" do + member = %Member{ + first_name: " John ", + last_name: " Doe ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John Doe" + end + + test "handles one empty string and one nil" do + member = %Member{ + first_name: "", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "handles one nil and one empty string" do + member = %Member{ + first_name: nil, + last_name: "", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "handles one whitespace and one nil" do + member = %Member{ + first_name: " ", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "john@example.com" + end + + test "handles one valid name and one whitespace" do + member = %Member{ + first_name: "John", + last_name: " ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John" + end + + test "handles member with only first_name containing whitespace" do + member = %Member{ + first_name: " John ", + last_name: nil, + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "John" + end + + test "handles member with only last_name containing whitespace" do + member = %Member{ + first_name: nil, + last_name: " Doe ", + email: "john@example.com" + } + + assert MemberHelpers.display_name(member) == "Doe" + end + end +end diff --git a/test/mv_web/member_live/index_member_fields_display_test.exs b/test/mv_web/member_live/index_member_fields_display_test.exs index 6b4f50c..c6fd39f 100644 --- a/test/mv_web/member_live/index_member_fields_display_test.exs +++ b/test/mv_web/member_live/index_member_fields_display_test.exs @@ -16,7 +16,6 @@ defmodule MvWeb.MemberLive.IndexMemberFieldsDisplayTest do house_number: "123", postal_code: "12345", city: "Berlin", - phone_number: "+49123456789", join_date: ~D[2020-01-15] }) |> Ash.create() diff --git a/test/mv_web/member_live/index_test.exs b/test/mv_web/member_live/index_test.exs index d4f5644..acca9bf 100644 --- a/test/mv_web/member_live/index_test.exs +++ b/test/mv_web/member_live/index_test.exs @@ -121,7 +121,6 @@ defmodule MvWeb.MemberLive.IndexTest do :house_number, :postal_code, :city, - :phone_number, :join_date ]