openapi: 3.0.0
info:
  title: Rule engine
  description: This document lists Autopass rule engine endpoints.
  version: 1.0.0
servers:
  - url: https://api.autopass.pro
    description: Live server
    x-environment: live
  - url: https://api-prelive.autopass.pro
    description: Prelive server
    x-environment: prelive
paths:
  /rules-api/rule-types:
    get:
      summary: List rule types
      description: List rule types and their associated families and tables.
      operationId: list-rule-types
      tags:
        - rule-type
      responses:
        200:
          description: Successfully listed the rule types.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/RuleTypeCollection"
        401:
          description: Unauthorized.
  /rules-api/rule-types/{rule_type_id}:
    get:
      summary: Get a rule type
      description: Get a rule type for a given id, with its associated families and tables.
      operationId: get-rule-type
      tags:
        - rule-type
      parameters:
        - in: path
          name: rule_type_id
          required: true
          schema:
            type: string
            example: 470
      responses:
        200:
          description: Successfully got the rule type.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/RuleType"
        401:
          description: Unauthorized.
        400:
          description: rule_type_id must be an integer.
        404:
          description: No rule type found for this rule_type_id.

  /rules-api/tables:
    post:
      summary: Create a new table
      description: Creation of a new table with a set of rules.
      operationId: create-table
      tags:
        - table
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TableCreation"
      responses:
        201:
          description: Successfully created the table.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TableCreationResponse"
        401:
          description: Unauthorized.
        400:
          description: Request body could not be read properly.

  /rules-api/tables/{table_id}:
    get:
      summary: Get a table
      description: Get a table for a given id, with its associated rules.
      operationId: get-table
      tags:
        - table
      parameters:
        - in: path
          name: table_id
          required: true
          schema:
            type: string
            example: 470
      responses:
        200:
          description: Successfully got the table.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: "#/components/schemas/Table"
                  - properties:
                      rules:
                        $ref: "#/components/schemas/Rules"
        401:
          description: Unauthorized.
        400:
          description: table_id must be an integer.
        404:
          description: No table found for this table_id.
    put:
      summary: Update a table
      description: Update the properties of a table.
      operationId: update-table
      tags:
        - table
      parameters:
        - in: path
          name: table_id
          required: true
          schema:
            type: string
            example: 470
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TableModification"
      responses:
        200:
          description: Successfully modified the table.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Table"
        401:
          description: Unauthorized.
        400:
          description: table_id must be an integer.
        404:
          description: No table found for this table_id.

  /rules-api/tables/{table_id}/convert-file:
    post:
      summary: Convert file
      description: Verify the content of a csv file, then returns the transformed rules and their status.
      operationId: convert-file
      tags:
        - table
      parameters:
        - in: path
          name: table_id
          required: true
          schema:
            type: string
            example: 470
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        200:
          description: Successfully converted the rules from the csv file.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ConvertedRules"
        401:
          description: Unauthorized.
        400:
          description: table_id must be an integer.
        404:
          description: No table found for this table_id.

  /rules-api/tables/{table_id}/rules:
    post:
      summary: Deploy a new version
      description: Deploy a new version of a given decision table
      operationId: deploy-version
      tags:
        - tables
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: integer
        - in: query
          name: active
          required: false
          schema:
            type: boolean
            default: true
          description: >
            Indique si la version déployée doit être activée immédiatement.

            Mettre à `false` si vous ne souhaitez pas activer automatiquement
            cette version après le déploiement.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/PostDecisionTable2"
      responses:
        "201":
          description: Successfully deployed the decision table

        401:
          description: Unauthorized.
        400:
          description: table_id must be an integer.
        404:
          description: No table found for this table_id.

  /rules-api/tables/{table_id}/versions:
    get:
      summary: List versions
      description: Return a list of the versions of a table with their differences.
      operationId: list-versions
      tags:
        - version
      parameters:
        - in: path
          name: table_id
          required: true
          schema:
            type: string
            example: 470
      responses:
        200:
          description: Successfully listed the versions of the table.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/VersionCollection"
        401:
          description: Unauthorized.
        400:
          description: table_id must be an integer.
        404:
          description: No table found for this table_id.

  /rules-api/tables/{table_id}/versions/{version}:
    get:
      summary: Get a version
      description: Get a table in a specific version.
      operationId: get-version
      tags:
        - version
      parameters:
        - in: path
          name: table_id
          required: true
          schema:
            type: string
            example: 470
        - in: path
          name: version
          required: true
          schema:
            type: string
            example: 470
      responses:
        200:
          description: Successfully got the table of this version.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/VersionTable"
        401:
          description: Unauthorized.
        400:
          description: table_id and version must be integers.
        404:
          description: No table found for this table_id or this version doesn't exists for this table.
    put:
      summary: Change current version
      description: Change the current version of a table.
      operationId: change-current-version
      tags:
        - version
      parameters:
        - in: path
          name: table_id
          required: true
          schema:
            type: string
            example: 470
        - in: path
          name: version
          required: true
          schema:
            type: string
            example: 5
      responses:
        204:
          description: Successfully changed the current version of the table.
        401:
          description: Unauthorized.
        400:
          description: table_id must be an integer.
        404:
          description: No table found for this table_id.

security:
  - ApiKeyAuth: []
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: query
      name: access_token
  schemas:
    Meta:
      type: object
      properties:
        limit:
          type: integer
          example: 100
        offset:
          type: integer
          example: 20
        total:
          type: integer
          example: 200

    ####### CONVERTED RULES #######
    ConvertedRules:
      type: object
      properties:
        rules:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/ConvertedRule"
    ConvertedRule:
      type: object
      properties:
        conditions:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/ConvertedRuleAttribute"
        consequences:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/ConvertedRuleAttribute"
        is_valid:
          $ref: "#/components/schemas/RuleValidity"
    ConvertedRuleAttribute:
      type: object
      properties:
        attribute_label:
          type: string
          example: "Brand"
        attribute_value:
          type: string
          example: "Norauto"
    RuleValidity:
      type: object
      properties:
        status:
          type: boolean
          example: false
        comment:
          type: string
          example: doublon

    ####### EVALUATION #######
    EvaluationResult:
      type: object
      properties:
        attribute_name:
          type: string
          example: ean
        value:
          type: string
          example: ABC13456
    Evaluation:
      type: array
      items:
        anyOf:
          - $ref: "#/components/schemas/EvaluationResult"
    Evaluations:
      type: object
      properties:
        results:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/Evaluation"

    ####### TABLE #######
    Table:
      type: object
      allOf:
        - $ref: "#/components/schemas/TableHeader"
        - properties:
            table_description:
              type: string
              example: "Description d'une table"
            table_comment:
              type: string
              example: "Commentaire d'une table"
            rule_type_label:
              type: string
              example: EQ->EQ
            family_name:
              type: string
              example: BEG
            rules:
              # $ref: '#/components/schemas/Rules'
              type: array
              items:
                type: object
            current_version:
              $ref: "#/components/schemas/CurrentVersion"
            links:
              $ref: "#/components/schemas/TableLinks"
    TableLinks:
      type: object
      properties:
        self:
          type: string
          example: https://api.autopass.pro/tables/4
        versions:
          type: string
          example: https://api.autopass.pro/tables/4/versions
    TableModification:
      type: object
      properties:
        name:
          type: string
          example: BEG Norauto/Wippers Norauto

        active:
          type: boolean
          example: true
    TableCreation:
      type: object
      properties:
        name:
          type: string
          example: BEG Norauto/Wippers Norauto
        description:
          type: string
          example: "Description d'une table"
        rule_type_id:
          type: integer
          example: 1
        family_id:
          type: integer
          example: 1
    TableCreationResponse:
      type: object
      properties:
        table_id:
          type: integer
          example: 4
        links:
          $ref: "#/components/schemas/TableCreationLinks"
    TableCreationLinks:
      type: object
      properties:
        self:
          type: string
          example: https://api.autopass.pro/tables/4

    ####### VERSION #######
    VersionCollection:
      type: object
      properties:
        meta:
          $ref: "#/components/schemas/Meta"
        versions:
          $ref: "#/components/schemas/Versions"
    Versions:
      type: array
      items:
        anyOf:
          - $ref: "#/components/schemas/Version"
    Version:
      allOf:
        - $ref: "#/components/schemas/CurrentVersion"
        - type: object
          properties:
            deleted_rules:
              type: integer
              example: 10
            added_rules:
              type: integer
              example: 5
            unchanged_rules:
              type: integer
              example: 50
            links:
              $ref: "#/components/schemas/VersionLinks"
    VersionLinks:
      type: object
      properties:
        self:
          type: string
          example: https://api.autopass.pro/table/470/versions/4
        table:
          type: string
          example: https://api.autopass.pro/table/470
    CurrentVersion:
      type: object
      properties:
        version_id:
          type: integer
          example: 2
        version_created_at:
          type: string
          example: "2015-12-31"
        version_comment:
          type: string
          example: "commentaire de la version"
    VersionTable:
      allOf:
        - $ref: "#/components/schemas/Table"
        - type: object
          properties:
            rules:
              $ref: "#/components/schemas/VersionRules"

    ####### RULE #######
    VersionRules:
      type: array
      items:
        anyOf:
          - $ref: "#/components/schemas/VersionRule"
    VersionRule:
      allOf:
        - $ref: "#/components/schemas/Rule"
        - type: object
          properties:
            status:
              type: string
              enum: [deleted, added, unchanged]
    Rules:
      type: array
      items:
        anyOf:
          - $ref: "#/components/schemas/Rule"
    Rule:
      type: object
      properties:
        rule_id:
          type: string
          example: rule_9288H2D
        annotation:
          type: string
          example: "annotation de règle"
        conditions:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/RuleAttribute"
        consequences:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/RuleAttribute"
    RuleAttribute:
      allOf:
        - $ref: "#/components/schemas/Attribute"
      type: object
      properties:
        attribute_value:
          type: string
          example: "Norauto"
    RulesCreation:
      type: object
      properties:
        table_comment:
          type: string
          example: "comment for the table new version"
        rules:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/RuleCreation"
    RuleCreation:
      type: object
      properties:
        rule_annotation:
          type: string
          example: "new rule annotation"
        conditions:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/NewRule"
        consequences:
          type: array
          items:
            anyOf:
              - $ref: "#/components/schemas/NewRule"
    NewRule:
      type: object
      properties:
        attribute_id:
          type: integer
          example: 1
        attribute_value:
          type: string
          example: "Norauto"
    RuleModification:
      type: object
      properties:
        table_comment:
          type: string
          example: "comment for the table new version"
      allOf:
        - $ref: "#/components/schemas/RuleCreation"
    RuleDeletedResponse:
      type: object
      properties:
        rules:
          type: array
          items:
            type: integer
            example: 0

    ####### RULE TYPE #######
    RuleTypeCollection:
      type: object
      properties:
        meta:
          $ref: "#/components/schemas/Meta"
        rule_types:
          $ref: "#/components/schemas/RuleTypes"
    RuleTypes:
      type: array
      items:
        anyOf:
          - $ref: "#/components/schemas/RuleType"
    RuleType:
      properties:
        rule_type_id:
          type: number
          example: 1
        rule_type_name:
          type: string
          example: "Vehicle->EQ"
        condition_type:
          $ref: "#/components/schemas/Entity"
        consequence_type:
          $ref: "#/components/schemas/Entity"
        families:
          $ref: "#/components/schemas/Families"
        links:
          $ref: "#/components/schemas/RuleTypeLinks"
    RuleTypeLinks:
      type: object
      properties:
        self:
          type: string
          example: https://api.autopass.pro/rule-types/1

    ####### FAMILY #######
    Families:
      type: array
      items:
        anyOf:
          - $ref: "#/components/schemas/Family"
    Family:
      type: object
      properties:
        family_id:
          type: number
          example: 434
        family_label:
          type: string
          example: "balai d'essuie-glace"
        tables:
          allOf:
            - $ref: "#/components/schemas/TablesHeader"

    ####### TABLE HEADER #######
    TablesHeader:
      type: array
      items:
        allOf:
          - $ref: "#/components/schemas/TableHeader"
          - type: object
            properties:
              table_version:
                type: number
                example: 42

    TableHeader:
      type: object
      properties:
        table_id:
          type: number
          example: 3
        table_name:
          type: string
          example: "BEG_PREMIER_PRIXVALEO_"
        table_description:
          type: string
          example: "Description d'une table"
        rules_number:
          type: number
          example: 9
        table_status:
          type: boolean
          example: true

    ####### ENTITY #######
    Entity:
      type: object
      properties:
        entity_id:
          type: number
          example: 3
        entity_name:
          type: string
          example: "Vehicle"
        attributes:
          $ref: "#/components/schemas/Attributes"

    ####### ATTRIBUTE #######
    Attributes:
      type: array
      items:
        anyOf:
          - $ref: "#/components/schemas/Attribute"
    Attribute:
      type: object
      properties:
        attribute_id:
          type: number
          example: 3
        attribute_type:
          type: string
          example: "string"
        attribute_label:
          type: string
          example: "Brand"

    PostDecisionTable2:
      type: object
      required:
        - name
        - description
        - conditions
        - consequences
        - rules
      properties:
        name:
          type: string
          example: BEG_NORAUTO__WIPERS_NORAUTO
        description:
          type: string
          example: Données extraites de la feuille BEG Norauto  Wipers Norauto
        family_id:
          type: integer
          example: 414
        rule_type_id:
          type: integer
          example: 1
        conditions:
          type: array
          items:
            $ref: "#/components/schemas/Condition"
          example:
            - name: brand
              type_ref: string
            - name: brand code
              type_ref: string
            - name: ean
              type_ref: string
            - name: label
              type_ref: string
            - name: reference
              type_ref: string
        consequences:
          type: array
          items:
            $ref: "#/components/schemas/Consequence"
          example:
            - name: brand
              type_ref: string
            - name: brand code
              type_ref: string
            - name: ean
              type_ref: string
            - name: label
              type_ref: string
            - name: reference
              type_ref: string
        rules:
          type: array
          items:
            $ref: "#/components/schemas/Rule2"
        comment:
          type: string
          example: commentaire de la version
    Rule2:
      type: object
      required:
        - rule_id
        - inputs
        - outputs
      properties:
        rule_id:
          type: string
          example: ""
        inputs:
          type: array
          items:
            type: string
          example:
            - BOSCH
            - "-"
            - "3165141087529"
            - "-"
            - "3397118505"
        outputs:
          type: array
          items:
            type: string
          example:
            - NORAUTO
            - "2134882"
            - "3501361157947"
            - 35 KIT BEG NORAUTO SUP5-CLIO
            - BEG35NEW
        annotation:
          type: string
          example: commentaire de la règle
    Condition:
      type: object
      required:
        - name
        - type_ref
      properties:
        name:
          type: string
          example: brand
        type_ref:
          type: string
          example: string
    Consequence:
      type: object
      required:
        - name
        - type_ref
      properties:
        name:
          type: string
          example: brand
        type_ref:
          type: string
          example: string
