openapi: 3.1.0
info:
  title: Labels API
  version: "1.0.0"
  description: |
    Kleine REST-API der Labels-Plattform.

    **Anmeldung:** `Authorization: Bearer <API-Schlüssel>`. Schlüssel erzeugt ein Admin unter
    „Verwaltung → API-Schlüssel“ (`lbl_…`). Ein Schlüssel gehört zu genau einer Organisation und
    erreicht nur deren Daten; fremde IDs ergeben `404`. Die Route unter `/operator` verlangt einen
    Betreiber-Schlüssel (`lblop_…`).

    **Code-Kennung:** Überall, wo `{code}` steht, gilt der Kurzcode (`7KQ4X2`) oder die lesbare
    Nummer (`LBL-004512`).

    **Ratenbegrenzung:** je Schlüssel pro Minute (Standard 300); bei Überschreitung `429` mit
    `Retry-After`.
servers:
  - url: /api/v1
security:
  - apiKey: []
tags:
  - name: Codes
  - name: Objekte
  - name: Ziele
  - name: Scans
  - name: Betreiber
paths:
  /codes/{code}:
    get:
      tags: [Codes]
      summary: Code nachschlagen
      operationId: getCode
      parameters: [{ $ref: "#/components/parameters/Code" }]
      responses:
        "200":
          description: Der Code mit Charge, Objekt und geltendem Ziel.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Code" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }
  /codes/{code}/link:
    post:
      tags: [Codes]
      summary: Code mit einem vorhandenen Objekt verknüpfen oder umhängen
      description: |
        `unused` → `active`. Ein aktiver Code wird auf das neue Objekt umgehängt (protokolliert).
        Ein gesperrter Code ergibt `409`.
      operationId: linkCode
      parameters: [{ $ref: "#/components/parameters/Code" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [objectId]
              properties:
                objectId: { type: string, format: uuid }
      responses:
        "200":
          description: Verknüpft.
          content:
            application/json:
              schema:
                type: object
                required: [change, code]
                properties:
                  change: { type: string, enum: [linked, relinked, unchanged] }
                  code: { $ref: "#/components/schemas/Code" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/Conflict" }
  /codes/{code}/assignment:
    put:
      tags: [Codes]
      summary: Sticker zuordnen (Kategorie und Ort, Firma oder Person)
      description: |
        Hält fest, was der Sticker kennzeichnet und wo es ist bzw. wem es gehört. Ein freier
        Sticker (`unused`) wird dabei `active` und zählt zum Kontingent; bei einem aktiven werden
        nur die Angaben geändert. Ein gesperrter Code ergibt `409`.
      operationId: assignCode
      parameters: [{ $ref: "#/components/parameters/Code" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [category]
              properties:
                category: { type: string, maxLength: 120, examples: [Feuerlöscher] }
                assigneeKind: { type: [string, "null"], enum: [place, company, person, null] }
                assignee:
                  { type: [string, "null"], maxLength: 400, examples: ["Argentinierstraße 71, 1040 Wien"] }
                note: { type: [string, "null"], maxLength: 500 }
      responses:
        "200":
          description: Zugeordnet.
          content:
            application/json:
              schema:
                type: object
                required: [change, code]
                properties:
                  change: { type: string, enum: [installed, updated] }
                  code: { $ref: "#/components/schemas/Code" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/Conflict" }
  /codes/{code}/destination:
    put:
      tags: [Codes]
      summary: Eigenes Ziel eines Codes setzen
      description: "`null` entfernt das eigene Ziel; dann gilt das Ziel der Charge bzw. der Organisation. Die Änderung wirkt sofort."
      operationId: setCodeDestination
      parameters: [{ $ref: "#/components/parameters/Code" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [destinationId]
              properties:
                destinationId: { type: [string, "null"], format: uuid }
      responses:
        "200":
          description: Der aktualisierte Code.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Code" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /codes/{code}/void:
    post:
      tags: [Codes]
      summary: Code endgültig sperren
      description: Ein gesperrter Code kann nicht wieder aktiviert werden. Bereits gesperrt ergibt `409`.
      operationId: voidCode
      parameters: [{ $ref: "#/components/parameters/Code" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [reason]
              properties:
                reason: { type: string, minLength: 3, maxLength: 300 }
      responses:
        "200":
          description: Der gesperrte Code.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Code" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/Conflict" }
  /codes/{code}/replace:
    post:
      tags: [Codes]
      summary: Label ersetzen
      description: Das Label wird gesperrt; `newCode` (ein freies Label) übernimmt Objekt, eigenen Inhalt, Kategorie, Zuordnung und fixe ID. Die Historie des Objekts bleibt erhalten.
      operationId: replaceCode
      parameters: [{ $ref: "#/components/parameters/Code" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [newCode]
              properties:
                newCode: { type: string, description: Kurzcode, Nummer oder fixe ID des neuen Labels }
      responses:
        "200":
          description: Das neue Label.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Code" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /codes/{code}/reset:
    post:
      tags: [Codes]
      summary: Zuordnung zurücksetzen
      description: Das Label ist danach wieder frei und ohne Objekt. Bei Smart Learning erscheint beim nächsten Scan das Formular.
      operationId: resetCode
      parameters: [{ $ref: "#/components/parameters/Code" }]
      responses:
        "200":
          description: Das zurückgesetzte Label.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Code" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /codes/{code}/fixed-id:
    put:
      tags: [Codes]
      summary: Fixe ID setzen
      description: Eigene Kennung des Kunden, z. B. die Inventarnummer aus SAP oder Excel. Je Organisation eindeutig; danach lässt sich das Label überall auch mit dieser ID ansprechen. `null` entfernt sie.
      operationId: setFixedId
      parameters: [{ $ref: "#/components/parameters/Code" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [fixedId]
              properties:
                fixedId: { type: [string, "null"], maxLength: 80 }
      responses:
        "200":
          description: Das Label.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Code" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /codes/bulk:
    post:
      tags: [Codes]
      summary: Massenaktion
      description: |
        Jede Einzelaktion für viele Labels (höchstens 1000): `assign` (je Label ein Objekt mit Objekttyp und Standort),
        `destination` (eigenen Inhalt setzen, `null` = erbt), `template` bzw. `site` (für die Objekte der Labels),
        `void` (sperren), `reset` (Zuordnung zurücksetzen). Unbekannte Labels ergeben `404`, dann ändert sich nichts.
      operationId: bulkCodes
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [codes, action]
              properties:
                codes:
                  type: array
                  maxItems: 1000
                  items: { type: string, description: Kurzcode, Nummer oder fixe ID }
                action: { type: string, enum: [assign, destination, template, site, void, reset] }
                templateId: { type: [string, "null"], format: uuid }
                siteId: { type: [string, "null"], format: uuid }
                destinationId: { type: [string, "null"], format: uuid }
                reason: { type: string, maxLength: 200 }
      responses:
        "200":
          description: Wie viele Labels geändert und wie viele übersprungen wurden.
          content:
            application/json:
              schema:
                type: object
                properties:
                  done: { type: integer }
                  skipped: { type: integer }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /objects:
    post:
      tags: [Objekte]
      summary: Objekt anlegen und optional verknüpfen
      description: Mit `code` wird das neue Objekt in derselben Transaktion mit dem Code verknüpft.
      operationId: createObject
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/ObjectInput" }
      responses:
        "201":
          description: Angelegt.
          content:
            application/json:
              schema:
                type: object
                required: [object, code]
                properties:
                  object: { $ref: "#/components/schemas/Object" }
                  code:
                    oneOf:
                      - { $ref: "#/components/schemas/Code" }
                      - { type: "null" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/Conflict" }
        "422":
          description: Fachlich abgelehnt, z. B. externe Referenz bereits vergeben.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
  /destinations:
    get:
      tags: [Ziele]
      summary: Ziele auflisten
      operationId: listDestinations
      responses:
        "200":
          description: Alle Ziele der Organisation.
          content:
            application/json:
              schema:
                type: object
                required: [data]
                properties:
                  data:
                    type: array
                    items:
                      type: object
                      required: [id, name, type, url, isOrganizationDefault]
                      properties:
                        id: { type: string, format: uuid }
                        name: { type: string }
                        type: { type: string, enum: [redirect, page, passport], description: "redirect = Weiterleitung, page = Infoseite, passport = digitaler Produktpass." }
                        url: { type: [string, "null"] }
                        isOrganizationDefault: { type: boolean }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /scans:
    get:
      tags: [Scans]
      summary: Scan-Ereignisse abrufen
      description: Aufsteigend nach ID. Für die nächste Seite `after` auf `nextAfter` setzen.
      operationId: listScans
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time }, description: "Standard: 30 Tage vor `to`." }
        - { name: to, in: query, schema: { type: string, format: date-time }, description: "Standard: jetzt." }
        - { name: code, in: query, schema: { type: string }, description: Nur Ereignisse dieses Codes. }
        - { name: after, in: query, schema: { type: integer, minimum: 0 } }
        - { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 1000, default: 100 } }
      responses:
        "200":
          description: Eine Seite Scan-Ereignisse. Sie enthalten keine personenbezogenen Daten.
          content:
            application/json:
              schema:
                type: object
                required: [data, nextAfter]
                properties:
                  data:
                    type: array
                    items: { $ref: "#/components/schemas/Scan" }
                  nextAfter: { type: [integer, "null"] }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /operator/sdm/keys:
    post:
      tags: [Betreiber]
      summary: Schlüssel für einen Sticker einer geprüften Charge abrufen (Codierstation)
      description: |
        Verlangt einen **Betreiber-Schlüssel**. Ordnet die UID dem nächsten freien Code der Charge zu
        (wiederholte Abrufe derselben UID liefern denselben Code) und gibt die Schlüssel zurück, die
        die Codierstation auf den Chip schreibt. Jeder Abruf wird protokolliert. Die Antwort enthält
        Schlüsselmaterial: nur über TLS abrufen, nicht speichern, nicht protokollieren.
      operationId: issueStickerKeys
      security:
        - operatorKey: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [batchId, uid]
              properties:
                batchId: { type: string, format: uuid }
                uid: { type: string, pattern: "^[0-9A-Fa-f]{14}$", description: 7-Byte-UID des Chips, hexadezimal. }
      responses:
        "200":
          description: Schlüssel und URL-Vorlage für diesen Sticker.
          headers:
            Cache-Control: { schema: { type: string, const: no-store } }
          content:
            application/json:
              schema: { $ref: "#/components/schemas/StickerKeys" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403":
          description: Kein Betreiber-Schlüssel.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "404": { $ref: "#/components/responses/NotFound" }
        "422":
          description: Charge ist nicht geprüft oder hat keinen freien Code mehr.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
components:
  securitySchemes:
    apiKey:
      type: http
      scheme: bearer
      description: Organisations-Schlüssel (`lbl_…`).
    operatorKey:
      type: http
      scheme: bearer
      description: Betreiber-Schlüssel (`lblop_…`).
  parameters:
    Code:
      name: code
      in: path
      required: true
      description: Kurzcode oder lesbare Nummer.
      schema: { type: string }
      examples:
        kurzcode: { value: 7KQ4X2 }
        nummer: { value: LBL-004512 }
  responses:
    BadRequest:
      description: Ungültige Anfrage.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    Unauthorized:
      description: API-Schlüssel fehlt, ist ungültig oder widerrufen.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    NotFound:
      description: Nicht gefunden – auch für Einträge anderer Organisationen.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    Conflict:
      description: Zustandswechsel nicht erlaubt (z. B. Code ist gesperrt).
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    RateLimited:
      description: Zu viele Anfragen.
      headers:
        Retry-After: { schema: { type: integer } }
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
  schemas:
    Error:
      type: object
      required: [error]
      properties:
        error:
          type: object
          required: [code, message]
          properties:
            code: { type: string, examples: [not_found, invalid_request, invalid_state, rate_limited] }
            message: { type: string }
    Object:
      type: object
      required: [id, type, name, externalRef, serial, address, attributes, createdAt, updatedAt]
      properties:
        id: { type: string, format: uuid }
        type: { type: string, enum: [appliance, location, touchpoint] }
        name: { type: string }
        externalRef: { type: [string, "null"] }
        serial: { type: [string, "null"] }
        address: { type: [string, "null"] }
        attributes: { type: object, additionalProperties: { type: string } }
        templateId: { type: [string, "null"], format: uuid, description: Objektvorlage. }
        siteId: { type: [string, "null"], format: uuid, description: Standort. }
        displayId: { type: [string, "null"], description: "Lesbare ID aus Standort-Kürzel und laufender Nummer, z. B. HBF-0007." }
        validUntil: { type: [string, "null"], format: date, description: Gültig bis; die Bedeutung legt die Vorlage fest. }
        createdAt: { type: string, format: date-time }
        updatedAt: { type: string, format: date-time }
    ObjectInput:
      type: object
      required: [type, name]
      properties:
        type: { type: string, enum: [appliance, location, touchpoint] }
        name: { type: string, minLength: 1, maxLength: 200 }
        externalRef: { type: [string, "null"], maxLength: 120, description: Je Organisation eindeutig. }
        serial: { type: [string, "null"], maxLength: 120 }
        address: { type: [string, "null"], maxLength: 400 }
        attributes: { type: object, additionalProperties: { type: string }, maxProperties: 30 }
        templateId: { type: [string, "null"], format: uuid, description: "Mit Vorlage gilt deren Art, und ihre Pflichtfelder müssen in attributes stehen (Schlüssel = Feldschlüssel)." }
        siteId: { type: [string, "null"], format: uuid, description: Der Standort vergibt beim ersten Mal die lesbare ID. }
        validUntil: { type: [string, "null"], format: date }
        installedBy: { type: [string, "null"], maxLength: 160 }
        installedAt: { type: [string, "null"], format: date }
        code: { type: string, description: Kurzcode oder Nummer des Codes, der verknüpft werden soll. }
    Code:
      type: object
      required: [id, shortCode, number, url, state, batch, object, destinationId, effectiveDestination]
      properties:
        id: { type: string, format: uuid }
        shortCode: { type: string, examples: [7KQ4X2] }
        number: { type: string, examples: [LBL-004512] }
        url: { type: string, format: uri }
        state: { type: string, enum: [unused, active, void] }
        fixedId: { type: [string, "null"], description: "Eigene Kennung des Kunden, z. B. Inventarnummer" }
        awaitingAssignment: { type: boolean, description: "Smart Learning: Formular übersprungen, wartet auf Zuordnung" }
        replacedById: { type: [string, "null"], format: uuid, description: "Ersetzt durch dieses Label" }
        category:
          type: [string, "null"]
          description: Was der Sticker kennzeichnet, z. B. Feuerlöscher.
        assigneeKind: { type: [string, "null"], enum: [place, company, person, null] }
        assignee:
          type: [string, "null"]
          description: Adresse, Firma oder Person – je nach `assigneeKind`.
        note: { type: [string, "null"] }
        voidReason: { type: [string, "null"] }
        activatedAt: { type: [string, "null"], format: date-time }
        voidedAt: { type: [string, "null"], format: date-time }
        batch:
          type: object
          required: [id, name, carrier, verified]
          properties:
            id: { type: string, format: uuid }
            name: { type: string }
            carrier: { type: string, enum: [qr, nfc, qr_nfc] }
            verified: { type: boolean }
            purpose:
              type: string
              enum: [object, passport]
              description: "`object`: neutrale Sticker, `passport`: digitaler Produktpass."
        object:
          oneOf:
            - { $ref: "#/components/schemas/Object" }
            - { type: "null" }
        destinationId: { type: [string, "null"], format: uuid, description: Eigenes Ziel des Codes. }
        effectiveDestination:
          description: Das Ziel, das beim Aufruf gilt, und woher es stammt.
          oneOf:
            - type: object
              required: [id, name, type, source]
              properties:
                id: { type: string, format: uuid }
                name: { type: string }
                type: { type: string, enum: [redirect, page, passport], description: "redirect = Weiterleitung, page = Infoseite, passport = digitaler Produktpass." }
                source: { type: string, enum: [code, object, template, batch, organization] }
            - { type: "null" }
    Scan:
      type: object
      required: [id, at, number, shortCode, batch, result, outcome, country, deviceType]
      properties:
        id: { type: integer }
        at: { type: string, format: date-time }
        number: { type: string }
        shortCode: { type: string }
        batch: { type: string }
        result: { type: string, enum: [unverified, verified, replayed, invalid] }
        outcome: { type: string, enum: [redirect, page, inactive, void, no_destination, rejected] }
        country: { type: [string, "null"], description: ISO-3166-1 alpha-2 }
        deviceType: { type: string, enum: [mobile, tablet, desktop, bot, other] }
    StickerKeys:
      type: object
      required: [uid, number, shortCode, keyVersion, urlTemplate, sdmMetaReadKey, sdmFileReadKey]
      properties:
        uid: { type: string }
        number: { type: string, description: Lesbare Nummer des zugeordneten Codes. }
        shortCode: { type: string }
        keyVersion: { type: integer }
        urlTemplate: { type: string, description: "NDEF-URL mit Platzhaltern für die gespiegelten PICC-Daten (p) und die SDMMAC (m)." }
        sdmMetaReadKey: { type: string, description: "AES-128, hex. Leseschlüssel der Charge (SDMMetaReadKey)." }
        sdmFileReadKey: { type: string, description: "AES-128, hex. Schlüssel dieses Stickers (SDMFileReadKey), abgeleitet nach AN10922." }
