# LMS Personas — Admin Panel Integration Guide This guide documents how to integrate the **LMS persona catalog** into the Yimaru admin panel. Personas are coach/character profiles (name, description, avatar, gender) stored centrally and linked to **practice shells** in Learn English (LMS) and exam-prep via `persona_id`. > **Naming note:** This feature is **`/api/v1/personas`** (catalog rows in `lms_personas`). It is **not** the same as **question-set user personas** (`/question-sets/:id/personas`), which assign real users to a question set for role-play scenarios. Both are covered below; the admin **Personas** menu usually means the catalog. --- ## Table of contents 1. [Feature overview](#feature-overview) 2. [Architecture](#architecture) 3. [Authentication & permissions](#authentication--permissions) 4. [RBAC setup & troubleshooting 403](#rbac-setup--troubleshooting-403) 5. [Data model](#data-model) 6. [Response envelopes](#response-envelopes) 7. [Persona catalog API reference](#persona-catalog-api-reference) 8. [Linking personas to practices](#linking-personas-to-practices) 9. [Recommended admin UI screens](#recommended-admin-ui-screens) 10. [TypeScript types & API client](#typescript-types--api-client) 11. [Question-set user personas (separate feature)](#question-set-user-personas-separate-feature) 12. [Error handling](#error-handling) 13. [Integration checklist](#integration-checklist) 14. [Limitations & future work](#limitations--future-work) 15. [Quick reference](#quick-reference) --- ## Feature overview | Capability | Admin panel | Learner app | |------------|-------------|-------------| | Create / edit / delete catalog personas | Yes — `/personas` CRUD | No | | List personas (picker / management) | Yes | Uses practice payload (`persona_id` resolved server-side) | | Assign persona to practice | Yes — `persona_id` on practice create/update | Sees persona in practice content | | Upload profile image via persona API | **No** — URL string only | Displays `profile_picture` URL | | Deactivate persona (hide from pickers) | Yes — `is_active: false` | Inactive personas excluded when `active_only=true` | ### Default seeded personas (migration `000063`) After migration, three catalog rows are inserted with stable IDs: | ID | Name | Description | |----|------|-------------| | `1` | Friendly Coach | Warm, encouraging tutor for everyday conversational practice. | | `2` | Exam Coach | Structured, exam-style guidance and clear checkpoints. | | `3` | Story Narrator | Story-led scenarios with character-driven prompts. | Use `GET /personas?active_only=false` to verify seeds in a new environment. --- ## Architecture ```mermaid flowchart LR subgraph AdminPanel["Admin panel"] PersonaList[Persona list / CRUD] PersonaForm[Create / edit form] PracticeEditor[Practice editor persona picker] end subgraph API["Yimaru API"] PersonaRoutes["/api/v1/personas CRUD"] LmsPractice["/api/v1/practices*"] EpPractice["/api/v1/exam-prep/practices*"] end subgraph DB["PostgreSQL"] PersonasTable["lms_personas"] LmsPractices["lms_practices.persona_id"] EpPractices["exam_prep.lesson_practices.persona_id"] end PersonaList --> PersonaRoutes PersonaForm --> PersonaRoutes PracticeEditor --> PersonaRoutes PracticeEditor --> LmsPractice PracticeEditor --> EpPractice PersonaRoutes --> PersonasTable LmsPractice --> LmsPractices EpPractice --> EpPractices LmsPractices --> PersonasTable EpPractices --> PersonasTable ``` - **Migrations:** - `000063_lms_personas` — table, seeds, FK on `lms_practices` and `exam_prep.lesson_practices` (`ON DELETE SET NULL`) - `000064_lms_personas_profile_picture` — renames `avatar_url` → `profile_picture` - `000065_lms_personas_gender` — adds `gender` column - **Postman:** `postman/LMS-Personas.postman_collection.json` - **Handlers:** `internal/web_server/handlers/lms_persona_handler.go` - **Service:** `internal/services/personas/service.go` Routes live at **`/api/v1/personas`** (not under `/admin/`). Staff authenticate with team or platform admin tokens. --- ## Authentication & permissions All persona catalog endpoints require: ```http Authorization: Bearer Content-Type: application/json ``` ### How to obtain a token | Login flow | Endpoint | Typical use | |------------|----------|-------------| | **Team member (recommended)** | `POST /api/v1/team/login` | Admin panel staff | | **Platform admin** | `POST /api/v1/auth/admin-login` | `users.role` = `ADMIN` / `SUPER_ADMIN` | ### RBAC permission keys (catalog) | Action | Permission | HTTP | |--------|------------|------| | List personas | `personas.list` | `GET /personas` | | Get one persona | `personas.get` | `GET /personas/:id` | | Create persona | `personas.create` | `POST /personas` | | Update persona | `personas.update` | `PUT /personas/:id` | | Delete persona | `personas.delete` | `DELETE /personas/:id` | ### Default role access | Role | Catalog CRUD | |------|----------------| | `ADMIN` | Full (middleware bypass) | | `SUPER_ADMIN` | Full (bypass) | | `INSTRUCTOR` | Full (`personas.*` in seeds) | | `SUPPORT` | **Read only** (`personas.list`, `personas.get`) | | `STUDENT` / `OPEN_LEARNER` | No persona admin permissions by default | ### UI gating - Show **Personas** menu when user has `personas.list` (or is `ADMIN` / `SUPER_ADMIN`). - Hide create/edit/delete for roles with list/get only. - Practice editor persona dropdown: call `GET /personas?active_only=true` (requires `personas.list`). --- ## RBAC setup & troubleshooting 403 If persona endpoints return **403 Forbidden**: 1. **Decode JWT `Role`** — confirm staff role (`ADMIN`, `INSTRUCTOR`, etc.). 2. **Apply migrations** — `SELECT to_regclass('public.lms_personas');` must not be `NULL`. 3. **Sync RBAC:** ```http POST /api/v1/rbac/permissions/sync Authorization: Bearer ``` 4. **Re-login** after sync. 5. For custom roles, assign `personas.list`, `personas.create`, etc. via RBAC UI. --- ## Data model ### Persona object (`data` in responses) ```json { "id": 1, "name": "Friendly Coach", "description": "Warm, encouraging tutor for everyday conversational practice.", "profile_picture": null, "gender": null, "is_active": true, "created_at": "2026-04-01T10:00:00Z", "updated_at": null } ``` | Field | Type | Required | Description | |-------|------|----------|-------------| | `id` | int64 | — | Primary key | | `name` | string | Yes (create) | Display name; trimmed; cannot be empty on create/update | | `description` | string \| null | No | Longer bio / coaching style text | | `profile_picture` | string \| null | No | **Image URL** (HTTPS or MinIO public URL). Key always present in JSON (`null` when unset) | | `gender` | string \| null | No | Free-text label (e.g. `female`, `male`). Key always present (`null` when unset) | | `is_active` | boolean | No | Default `true` on create. Inactive personas hidden when listing with `active_only=true` | | `created_at` | string (time) | — | Server timestamp | | `updated_at` | string \| null | — | Set on first update | ### Create body (`CreateLmsPersonaInput`) ```json { "name": "Business Mentor", "description": "Formal tone for workplace English.", "profile_picture": "https://cdn.example.com/personas/business-mentor.png", "gender": "neutral", "is_active": true } ``` | Field | Validation | |-------|------------| | `name` | Required, non-empty after trim | | `description` | Optional | | `profile_picture` | Optional URL string | | `gender` | Optional; whitespace-only omitted on create | | `is_active` | Optional; defaults to `true` | ### Update body (`UpdateLmsPersonaInput`) All fields optional — **partial update**. Only sent fields change. ```json { "name": "Business Mentor (Updated)", "is_active": false } ``` | Behavior | Detail | |----------|--------| | Clear `gender` | Send `"gender": ""` (stored as empty string) | | Clear `description` / `profile_picture` | Send `""` for that field | | Omit field | Unchanged | ### List payload (`data` for `GET /personas`) ```json { "personas": [ /* persona objects */ ], "total_count": 3, "limit": 20, "offset": 0 } ``` Sort order: **`name ASC`, then `created_at DESC`**. --- ## Response envelopes ### Success (`domain.Response`) ```json { "message": "Persona created successfully", "data": { }, "success": true, "status_code": 201 } ``` Rely on **HTTP status code** as source of truth. ### Error (`domain.ErrorResponse`) ```json { "message": "Validation failed", "error": "name is required" } ``` --- ## Persona catalog API reference Base path: `{API_BASE_URL}/api/v1` ### 1. List personas ``` GET /personas Permission: personas.list ``` #### Query parameters | Param | Default | Description | |-------|---------|-------------| | `active_only` | `true` | Pass `false` to include inactive personas. Any value other than literal `false` keeps active-only filter | | `limit` | `20` | Page size (max **200** server-side) | | `offset` | `0` | Pagination offset | #### Example — admin management table (all personas) ```http GET /api/v1/personas?active_only=false&limit=50&offset=0 Authorization: Bearer ``` #### Example — practice editor dropdown (active only) ```http GET /api/v1/personas?active_only=true&limit=200&offset=0 Authorization: Bearer ``` #### Response `200` ```json { "message": "Personas retrieved successfully", "data": { "personas": [ { "id": 1, "name": "Friendly Coach", "description": "Warm, encouraging tutor...", "profile_picture": null, "gender": null, "is_active": true, "created_at": "2026-04-01T10:00:00.000000 +0000 UTC", "updated_at": null } ], "total_count": 3, "limit": 50, "offset": 0 }, "success": true, "status_code": 200 } ``` --- ### 2. Get persona by ID ``` GET /personas/:id Permission: personas.get ``` #### Example ```http GET /api/v1/personas/1 Authorization: Bearer ``` #### Response `200` Returns single persona in `data`. #### Errors | HTTP | When | |------|------| | `400` | Invalid ID | | `404` | Persona not found | | `403` | Missing permission | --- ### 3. Create persona ``` POST /personas Permission: personas.create ``` #### Example ```http POST /api/v1/personas Authorization: Bearer Content-Type: application/json { "name": "Postman Coach", "description": "Smoke-test persona from admin panel", "profile_picture": "https://cdn.example.com/personas/postman-coach.png", "gender": "female", "is_active": true } ``` #### Response `201` ```json { "message": "Persona created successfully", "data": { "id": 4, "name": "Postman Coach", "description": "Smoke-test persona from admin panel", "profile_picture": "https://cdn.example.com/personas/postman-coach.png", "gender": "female", "is_active": true, "created_at": "...", "updated_at": null }, "success": true, "status_code": 201 } ``` #### Errors | HTTP | When | |------|------| | `400` | Validation failed / empty name | | `403` | Missing `personas.create` | --- ### 4. Update persona ``` PUT /personas/:id Permission: personas.update ``` Partial update — include only fields to change. #### Example — deactivate ```http PUT /api/v1/personas/4 Authorization: Bearer Content-Type: application/json { "is_active": false } ``` #### Response `200` ```json { "message": "Persona updated successfully", "data": { /* updated persona */ } } ``` #### Errors | HTTP | When | |------|------| | `400` | Empty `name` on update | | `404` | Persona not found | --- ### 5. Delete persona ``` DELETE /personas/:id Permission: personas.delete ``` Hard delete from `lms_personas`. #### Database side effect Foreign keys on practices use **`ON DELETE SET NULL`**: - `lms_practices.persona_id` → cleared when persona deleted - `exam_prep.lesson_practices.persona_id` → cleared when persona deleted Learner-facing practices remain; they simply lose the assigned coach until re-linked. #### Example ```http DELETE /api/v1/personas/4 Authorization: Bearer ``` #### Response `200` ```json { "message": "Persona deleted successfully", "success": true, "status_code": 200 } ``` **Note:** Delete currently returns **200 even if the ID does not exist** (no row-affected check). Prefer confirming ID via `GET` first, or refresh the list after delete. Show a confirmation dialog in the admin UI — deletion affects any practices that referenced this persona. --- ## Linking personas to practices Personas are **not embedded** in practice JSON. Practices store **`persona_id`** (FK to `lms_personas.id`). ### LMS (Learn English) practices When creating or updating an LMS practice, set `persona_id`: ```http PUT /api/v1/practices/:id Authorization: Bearer Content-Type: application/json { "persona_id": 1 } ``` Or on create: ```http POST /api/v1/practices Content-Type: application/json { "parent_kind": "LESSON", "parent_id": 42, "question_set_id": 10, "persona_id": 2, "title": "Speaking practice" } ``` Clear assignment: send `"persona_id": null` or omit on update depending on your practice update API semantics (check practice handler — `persona_id` uses COALESCE on update). **Validation:** If `persona_id` is set to a non-existent catalog ID, practice create/update returns an error (`persona not found`). ### Exam-prep practices Same field on exam-prep practice endpoints: ```http PUT /api/v1/exam-prep/practices/:id Content-Type: application/json { "persona_id": 2 } ``` Permissions: `exam_prep.practices.create` / `exam_prep.practices.update` (not `personas.*` alone). ### Admin UI pattern — persona picker 1. On practice create/edit screen load: ```http GET /api/v1/personas?active_only=true&limit=200&offset=0 ``` 2. Render `