> ## Documentation Index
> Fetch the complete documentation index at: https://system.muzemus.online/llms.txt
> Use this file to discover all available pages before exploring further.

# Entity relationships

> ER diagram, cascade behaviour, and unique constraints

## Entity relationship diagram

```mermaid theme={null}
erDiagram
    Client ||--o{ Region : has
    Client ||--o{ Store : has
    Client ||--o{ Department : has
    Client ||--o{ UniformCategory : has
    Client ||--o{ Product : has
    Client ||--o{ EntitlementRuleSet : has

    Region ||--o{ Store : contains
    Department ||--o{ Employee : categorizes
    Store ||--o{ Employee : employs
    Store ||--o{ StoreOrder : generates

    UniformCategory ||--o{ Employee : governs
    Product ||--o{ ProductSize : offers

    Employee ||--o| EmployeeSizeProfile : has
    Employee ||--o{ EntitlementPeriod : has
    Employee ||--o{ Order : receives

    EntitlementRuleSet ||--o{ EntitlementRuleItem : allocates
    EntitlementRuleSet ||--o{ EntitlementRuleCriteria : defines
    EntitlementRuleSet ||--o{ EntitlementPeriod : generates

    StoreOrder ||--o{ Order : batches
    Order ||--o{ OrderItem : contains
    Order ||--o{ OrderStatusHistory : tracks

    Product ||--o{ OrderItem : ordered_as
    Product ||--o{ EntitlementRuleItem : allocated_in
    EntitlementPeriod ||--o{ OrderItem : consumes

    User ||--o{ UserRole : has
    User ||--o{ Session : has
    User ||--o{ Notification : receives
    User ||--o{ AuditLog : performs
    User ||--o{ Order : submits
```

## Cascade behaviour

| Parent               | Child                                            | On parent delete |
| -------------------- | ------------------------------------------------ | ---------------- |
| `User`               | `UserRole`, `Session`, `Account`, `Notification` | Cascade          |
| `Employee`           | `EmployeeSizeProfile`, `EntitlementPeriod`       | Cascade          |
| `EntitlementRuleSet` | `EntitlementRuleItem`, `EntitlementRuleCriteria` | Cascade          |
| `Product`            | `ProductSize`                                    | Cascade          |
| `Order`              | `OrderItem`, `OrderStatusHistory`                | Cascade          |

All other relations restrict deletes. That is why permanent deletes of stores, clients, or products run through batched transactions that remove dependent records first (the `POST /<resource>/bulk-permanent` endpoints).

## Unique constraints

| Model                 | Fields               | Purpose                         |
| --------------------- | -------------------- | ------------------------------- |
| `User`                | `email`              | One account per email           |
| `Session`             | `token`              | Session uniqueness              |
| `Region`              | `[clientId, code]`   | Region codes per client         |
| `Department`          | `[clientId, code]`   | Department codes per client     |
| `Product`             | `[clientId, code]`   | Product codes per client        |
| `ProductSize`         | `[productId, value]` | One size value per product      |
| `Employee`            | `employeeNumber`     | One record per employee number  |
| `EmployeeSizeProfile` | `employeeId`         | One profile per employee        |
| `Store`               | `storeNumber`        | One record per store number     |
| `StoreOrder`          | `storeOrderNumber`   | One batch number per submission |
| `Order`               | `orderNumber`        | Order number uniqueness         |
| `SystemSetting`       | `key`                | One value per setting           |
| `NumberSequence`      | `key`                | One counter per sequence        |

`UserRole` has no compound unique constraint; role assignment integrity is enforced by the service layer when creating accounts.

## Index strategy

Prisma-managed indexes on the columns used by scoped queries:

| Model                                                | Indexed fields                                        | Purpose                            |
| ---------------------------------------------------- | ----------------------------------------------------- | ---------------------------------- |
| `Region`, `Department`, `UniformCategory`, `Product` | `clientId`                                            | Client-scoped list queries         |
| `Employee`                                           | `storeId`, `uniformCategoryId`, `managerId`, `userId` | Store directory and category joins |
| `Order`                                              | `employeeId`, `storeId`, `status`, `storeOrderId`     | Order lists and batch grouping     |
| `OrderItem`                                          | `orderId`, `productId`, `entitlementPeriodId`         | Consumption lookups                |
| `EntitlementPeriod`                                  | `[employeeId, periodType]`, `ruleSetId`               | Balance and phase lookups          |
| `EntitlementRuleItem`, `EntitlementRuleCriteria`     | `ruleSetId`                                           | Rule set expansion                 |
| `Notification`                                       | `[recipientId, readAt]`                               | Unread badge queries               |
| `AuditLog`                                           | `actorId`, `[entityType, entityId]`                   | Audit filters                      |
