# Insurer, staff and branch management The expert panel now supports setting up a fresh deployment without creating insurer/staff accounts through Swagger. No existing case, assignment, Fanavaran or reporting routes were removed. ## Starting with only a super-admin 1. Sign in through the existing super-admin login. 2. Open **شرکت‌های بیمه** (`/insurers`), and add the Persian/English insurer names, numeric client code and expert collaboration mode. 3. Add a **حساب بیمه** with name, email and password. This creates an existing `company` actor attached to that insurer; its login identifier is its email. 4. Use the insurer's **شعبه‌ها** and **اعضا** actions to select the insurer and create branches and staff. 5. The insurer's own account can manage its branches and all six staff roles. It cannot create or edit insurers or insurer login accounts. The **مدیریت** tabs in `/branches` and `/expert-list` provide creation and editing. Existing date/search filters and performance tables remain under **آمار عملکرد**. The super-admin's existing insurer selector remains available. ## API surface All endpoints require bearer authentication and strictly validate their DTOs. Swagger groups are `super-admin-management` and `insurer-management`. | Operation | Super-admin | Insurer | | --- | --- | --- | | List/create insurers | `GET/POST /super-admin/management/clients` | Unavailable | | Edit insurer metadata | `PATCH /super-admin/management/clients/:id` | Unavailable | | List/create insurer login accounts | `GET/POST /super-admin/management/clients/:clientId/accounts` | Unavailable | | Edit insurer login account | `PATCH /super-admin/management/clients/:clientId/accounts/:id` | Unavailable | | List/create staff | `GET/POST /super-admin/management/staff?clientId=...` | `GET/POST /expert-insurer/management/staff` | | Edit staff | `PATCH /super-admin/management/staff/:role/:id?clientId=...` | `PATCH /expert-insurer/management/staff/:role/:id` | | List/create branches | `GET/POST /super-admin/management/branches?clientId=...` | `GET/POST /expert-insurer/management/branches` | | Edit branch | `PATCH /super-admin/management/branches/:id?clientId=...` | `PATCH /expert-insurer/management/branches/:id` | Staff list queries accept `search`, `role`, `page` and `limit` (default 20, capped at 100). Responses contain `list`, `total`, `page` and `limit`. Branch responses contain `list`; clients contain `clients`. Account lists share the staff pagination contract. ## Fields `role` is one of `expert`, `damage_expert`, `field_expert`, `file_maker`, `file_reviewer`, or `call_center`, and is selected only during creation. These use the existing collections and actor login mechanism. Staff creation requires `firstName`, `lastName`, a ten-digit `nationalCode` and a password of at least six characters. Email and mobile are optional for field experts, file makers, file reviewers and call-center agents, matching the seed format. Blame and claim experts require email and mobile to match their existing account contracts. Seed-style accounts without email log in using their national code. National-code and email aliases cannot be duplicated across new managed accounts. Common optional properties are `branchId`, `phone`, `mobile`, `email`, `city`, `state`, `address` and `expertCode`. A branch, when supplied, must belong to the account's insurer. `expertCode` and the role-specific Fanavaran IDs are positive integer strings, matching existing persistence and mapping contracts. | Role | Additional optional fields | | --- | --- | | Blame, claim and field expert | All four Fanavaran codes below | | FileMaker | `ThirdPartyClaimExpertId`, `CarBodyClaimExpertId`, `locations` | | FileReviewer | `ThirdPartyExpertiseClaim`, `CarBodyExpertiseClaim`, `locations` | | Call-center agent | None of the four product/stage codes | | Field | Persian label in the panel | | --- | --- | | `ThirdPartyClaimExpertId` | کارشناس مسئول پرونده مالی | | `CarBodyClaimExpertId` | کارشناس مسئول پرونده بدنه | | `ThirdPartyExpertiseClaim` | کارشناس ارزیاب خسارت ثالث مالی | | `CarBodyExpertiseClaim` | کارشناس ارزیاب خسارت بدنه | All four codes are optional, editable and clearable for blame, claim and field experts. FileMaker accounts accept only the first two, FileReviewer accounts accept only the last two, and call-center accounts reject all four through the API. The panel presents these four named fields rather than the legacy generic `expertCode` input; existing `expertCode` values are preserved. `locations` is an editable array of `{id, name}`. Both properties must be nonempty, and IDs must be unique within the array. These are Fanavaran location IDs, rather than branch codes. Shared locations participate in the existing FileMaker/FileReviewer matching; management does not alter that matching logic. Branches require `code`, `name`, `city`, `state`, and `address`. `fullName`, `phoneNumber`, `isActive` (default true) and `activityStartDate` are optional and editable. Branch codes remain unique within an insurer. Insurer login accounts support name, email, password and optional contact/address fields; their role and tenant are assigned by the server. PATCH sends only changed fields. Omit `password` to preserve it; supply a replacement to change it. Nullable optional staff properties can be cleared with `null`; send `locations: []` to clear locations. Required identity and branch properties cannot be cleared. The form preserves the full stored activity timestamp when its date is unchanged. ## Access and compatibility - An insurer's scope is taken from the authenticated actor's `clientKey`. A supplied tenant identifier never overrides it. - Super-admin changes require an existing selected insurer. Foreign branch IDs and cross-tenant account/branch edits are rejected. The tenant predicate is retained on the final atomic update. - Staff roles, client association, arbitrary fields and MongoDB update operators cannot be edited through these endpoints. - Passwords are hashed through the existing `HashService`; passwords, OTPs, tokens and insurer SMS credentials are excluded from management responses. - Existing client settings and integrations are preserved when editing client metadata. This UI does not replace the existing system/settings or inquiry configuration screens. - Existing insurer-create DTOs also accept `expertCode` and the four Fanavaran codes, restricted to the applicable fields for FileMaker/FileReviewer DTOs. The branch schema and existing create DTO accept `fullName`. - No migration, deletion, mass reassignment or change to case workflow is required. ## Verification `npm test -- --runInBand` includes schema-backed management tests for all six roles, optional seed fields, role-specific IDs, locations, tenant isolation, branch updates, account passwords, protected fields and strict DTO validation. The route contract keeps the 383 existing routes and adds 18 guarded management routes. The expert UI's `npm run test:ui` includes management proxy and payload regression checks. Desktop/mobile browser checks use mocked local API responses, rather than modifying a real insurer. Live credential login, database indexes under concurrent writes and outbound Fanavaran submission remain integration QA responsibilities.