---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Microsoft Dynamics 365 Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and Microsoft Dynamics 365 (Example)

metadata:
  name: Microsoft Dynamics 365 Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  dataverseApiHost:
    description: Host name of the Dataverse Web API of your environment, without the scheme
    type: string
    default: example.api.crm.dynamics.com

  environmentUrl:
    description: URL of your Dataverse environment, used as the token audience
    type: string
    default: https://example.crm.dynamics.com

  tenantId:
    description: Directory (tenant) ID of your Microsoft Entra ID tenant
    type: string
    default: 00000000-0000-0000-0000-000000000000

  clientId:
    description: Application (client) ID of the app registration
    type: string

  clientSecret:
    description: Client secret of the app registration
    type: string

  entitySetName:
    description: Entity set name of the Dataverse table
    type: string
    default: cr123_qualityevents

  pollInterval:
    description: Polling interval for the Dataverse table in milliseconds
    type: integer
    default: 30000

  topicRoot:
    description: Root of the MQTT topic hierarchy
    type: string
    default: enterprise

resources:
  # Connection to the Dataverse Web API using OAuth 2.0 client credentials
  dynamicsConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref dataverseApiHost
        port: 443
        # Applied to the paths of all endpoints of this connection
        prefix: /api/data/v9.2
        # Headers recommended by Microsoft for all Dataverse Web API requests
        headers:
          Accept: application/json
          OData-MaxVersion: '4.0'
          OData-Version: '4.0'
        oauthClientCredentials:
          # Microsoft Entra ID v1.0 token endpoint of your tenant
          auth_url: !sub 'https://login.microsoftonline.com/${tenantId}/oauth2/token'
          client_id: !ref clientId
          client_secret: !ref clientSecret
          # The environment URL identifies the target API of the token
          audience: !ref environmentUrl

  # Creates one row in the Dataverse table per MQTT message
  qualityEventWriteEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref dynamicsConnection
      write:
        path: !sub '/${entitySetName}'

  qualityEventWriteMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/quality-events'
          publish:
            endpoint: !ref qualityEventWriteEndpoint
          rules:
            - transform:
                expression: '{ "body": $ }'

  # Polls the Dataverse table with an OData query
  # Replace the cr123 prefix with the publisher prefix of your table
  qualityEventPollEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref dynamicsConnection
      subscribe:
        path: !sub '/${entitySetName}'
        interval: !ref pollInterval
        query:
          $select: 'cr123_name,cr123_line,cr123_defecttype,cr123_measuredvalue,modifiedon'
          $filter: 'statecode eq 0'
          $orderby: 'modifiedon desc'
          $top: '10'

  qualityEventPollMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref qualityEventPollEndpoint
          publish:
            topic: !sub '${topicRoot}/dynamics-365/quality-events'
