openapi: 3.0.0
info:
  title: Substance
  version: 2.0.0
  description: >
    A **Substance** represents a hazardous or chemical substance registered in
    an office.

    Examples: solvents, acids, powders, compressed gases.


    Belongs to an office and can be subject to check actions on the compliance
    schedule

    (ActionSubscription with `subscriber_type: SUBSTANCE`).


    Use this API to:

    - Register substances present in an office

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

    - Browse the substance inventory for an office (filter by `office_id`)


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

        Most useful filters:
        - `office_id`: all substances in a specific office
        - `name`: search by name
      requestBody:
        description: Parameters for searching substances
        required: false
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/SubstanceIndexParams"
            example:
              filter:
                office_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
              per-page: 20
              page: 1
              sort: name
      responses:
        "200":
          description: List of substances
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/ViewSubstance"
          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/substance/create:
    post:
      summary: create
      operationId: createSubstance
      tags:
        - Substances
      description: |
        Registers a new substance in an office.

        Requires `office_id`, `project_id`, and `name`.
        The `office_substance_id` is auto-generated if not provided.
      requestBody:
        description: Substance to register
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Substance"
      responses:
        "201":
          description: Substance registered successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Substance"
  /v2/substance/view/{id}:
    get:
      summary: view
      operationId: viewSubstance
      tags:
        - Substances
      description: Retrieves a single substance by its ID.
      parameters:
        - name: id
          in: path
          description: The office_substance_id to retrieve.
          required: true
          schema:
            type: string
            format: uuid
      responses:
        "200":
          description: Substance found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ViewSubstance"
        "404":
          description: Substance not found
  /v2/substance/update/{id}:
    put:
      summary: update
      operationId: updateSubstance
      tags:
        - Substances
      description: Updates an existing substance.
      parameters:
        - name: id
          in: path
          description: The office_substance_id to update.
          required: true
          schema:
            type: string
            format: uuid
      requestBody:
        description: Fields to update
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Substance"
      responses:
        "200":
          description: Substance updated successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Substance"
        "404":
          description: Substance not found
  /v2/substance/delete/{id}:
    delete:
      summary: delete
      operationId: deleteSubstance
      tags:
        - Substances
      description: |
        Deletes a substance.

        If `force=false` (default), the response lists child entities.
        If `force=true`, the substance and all related entities are deleted.
      parameters:
        - name: id
          in: path
          description: The office_substance_id to delete.
          required: true
          schema:
            type: string
            format: uuid
        - name: force
          in: query
          description: If true, deletes the substance and all child entities.
          schema:
            type: boolean
            default: false
      responses:
        "204":
          description: Substance deleted successfully
        "400":
          description: Preview of entities that would be deleted (returned when force=false)
components:
  schemas:
    Substance:
      type: object
      required:
        - office_id
        - project_id
        - name
      properties:
        office_substance_id:
          type: string
          format: uuid
          description: >
            Unique identifier. Auto-generated if not provided. Used as
            `subscriber_id` (with `subscriber_type: SUBSTANCE`) in the
            compliance schedule and as `resource_id` in certificates.
        office_id:
          type: string
          format: uuid
          description: The office this substance belongs to. Pass the `office_id`.
        code:
          type: string
          maxLength: 45
          description: Identifier code.
        name:
          type: string
          maxLength: 255
          description: Name of the substance.
        description:
          type: string
          nullable: true
          description: Optional description.
        project_id:
          type: string
          format: uuid
          description: The project (company). Pass the `project_id`.
    ViewSubstance:
      type: object
      properties:
        office_substance_id:
          type: string
          format: uuid
          description: Unique identifier. Used as `subscriber_id` (SUBSTANCE) 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 substance is currently active.
        parent_active:
          type: boolean
          nullable: true
          description: Whether the parent office is currently active.
    SubstanceIndexParams:
      type: object
      properties:
        filter:
          $ref: "#/components/schemas/ViewSubstance"
        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
