openapi: 3.0.0
info:
  title: Role
  version: 2.0.0
  description: >
    A **Role** represents a safety organization role defined in an office.

    Examples: safety officer (RSPP), workers' safety representative (RLS),

    fire warden, first aid officer, supervisor.


    Belongs to an office. People are assigned to roles via EntityEmployee.

    A role can be subject to check actions on the compliance schedule

    (ActionSubscription with `subscriber_type: ROLE`).


    Use this API to:

    - Define the safety organization roles for an office

    - Get the `office_role_id` needed to create compliance schedule entries or
    certificates

    - Browse the roles of an office (filter by `office_id`)


    Supports historicization.
servers:
  - url: http://service.4hse.com/
security:
  - OAuth2: []
  - AccessToken: []
paths:
  /v2/role/index:
    post:
      summary: index
      operationId: indexRole
      tags:
        - Roles
      description: |
        Returns a paginated list of roles.

        Most useful filters:
        - `office_id`: all roles in a specific office
        - `name`: search by role name
      requestBody:
        description: Parameters for searching roles
        required: false
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/RoleIndexParams"
            example:
              filter:
                office_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
              per-page: 20
              page: 1
              sort: name
      responses:
        "200":
          description: List of roles
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/ViewRole"
          headers:
            X-Pagination-Current-Page:
              schema:
                type: integer
              description: Current page
            X-Pagination-Page-Count:
              schema:
                type: integer
              description: Total number of pages
            X-Pagination-Per-Page:
              schema:
                type: integer
              description: Number of items per page
            X-Pagination-Total-Count:
              schema:
                type: integer
              description: Total number of items
  /v2/role/create:
    post:
      summary: create
      operationId: createRole
      tags:
        - Roles
      description: |
        Creates a new safety role in an office.

        Requires `office_id`, `project_id`, and `name`.
        The `office_role_id` is auto-generated if not provided.
      requestBody:
        description: Role to create
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Role"
      responses:
        "201":
          description: Role created successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Role"
  /v2/role/view/{id}:
    get:
      summary: view
      operationId: viewRole
      tags:
        - Roles
      description: Retrieves a single role by its ID.
      parameters:
        - name: id
          in: path
          description: The office_role_id to retrieve.
          required: true
          schema:
            type: string
            format: uuid
      responses:
        "200":
          description: Role found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ViewRole"
        "404":
          description: Role not found
  /v2/role/update/{id}:
    put:
      summary: update
      operationId: updateRole
      tags:
        - Roles
      description: Updates an existing role.
      parameters:
        - name: id
          in: path
          description: The office_role_id to update.
          required: true
          schema:
            type: string
            format: uuid
      requestBody:
        description: Fields to update
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Role"
      responses:
        "200":
          description: Role updated successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Role"
        "404":
          description: Role not found
  /v2/role/delete/{id}:
    delete:
      summary: delete
      operationId: deleteRole
      tags:
        - Roles
      description: |
        Deletes a role.

        If `force=false` (default), the response lists child entities.
        If `force=true`, the role and all related entities are deleted.
      parameters:
        - name: id
          in: path
          description: The office_role_id to delete.
          required: true
          schema:
            type: string
            format: uuid
        - name: force
          in: query
          description: If true, deletes the role and all child entities.
          schema:
            type: boolean
            default: false
      responses:
        "204":
          description: Role deleted successfully
        "400":
          description: Preview of entities that would be deleted (returned when force=false)
components:
  schemas:
    Role:
      type: object
      required:
        - office_id
        - project_id
        - name
      properties:
        office_role_id:
          type: string
          format: uuid
          description: >
            Unique identifier. Auto-generated if not provided. Used as
            `subscriber_id` (with `subscriber_type: ROLE`) in the compliance
            schedule and as `resource_id` in certificates.
        office_id:
          type: string
          format: uuid
          description: The office this role belongs to. Pass the `office_id`.
        code:
          type: string
          maxLength: 45
          description: Identifier code.
        name:
          type: string
          maxLength: 255
          description: Name of the role.
        description:
          type: string
          nullable: true
          description: Optional description.
        project_id:
          type: string
          format: uuid
          description: The project (company). Pass the `project_id`.
    ViewRole:
      type: object
      properties:
        office_role_id:
          type: string
          format: uuid
          description: Unique identifier. Used as `subscriber_id` (ROLE) or `resource_id`.
        office_id:
          type: string
          format: uuid
        project_id:
          type: string
          format: uuid
        code:
          type: string
        name:
          type: string
        description:
          type: string
        office_name:
          type: string
          description: Name of the office.
        project_name:
          type: string
          description: Name of the project.
        project_type:
          type: string
          enum:
            - safety
            - template
        owned_active:
          type: boolean
          nullable: true
          description: Whether this role is currently active.
        parent_active:
          type: boolean
          nullable: true
          description: Whether the parent office is currently active.
    RoleIndexParams:
      type: object
      properties:
        filter:
          $ref: "#/components/schemas/ViewRole"
        per-page:
          type: integer
          minimum: 1
          default: 100
        page:
          type: integer
          minimum: 1
          default: 1
        sort:
          type: string
          enum:
            - code
            - name
            - office_name
            - project_name
        history:
          type: boolean
          description: If true, includes historicized entries.
      example:
        filter:
          office_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
        per-page: 100
        page: 1
        sort: name
  securitySchemes:
    OAuth2:
      type: oauth2
      flows:
        password:
          tokenUrl: https://auth.4hse.com/realms/4hse/protocol/openid-connect/token
          scopes: {}
    AccessToken:
      type: apiKey
      in: query
      name: access-token
