{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Connection",
  "oneOf": [
    {
      "title": "Full Connection",
      "description": "The connection your application has with your customer's TSP.",
      "allOf": [
        {
          "title": "Connection Base",
          "type": "object",
          "description": "Fields shared by all connection records.",
          "properties": {
            "id": {
              "title": "ConnectionId",
              "type": "string",
              "format": "ulid"
            },
            "company": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string",
                  "description": "Optional name of the connection."
                },
                "dotNumbers": {
                  "type": "array",
                  "description": "Optional DOT numbers associated with this connection.",
                  "items": {
                    "type": "string"
                  }
                }
              }
            },
            "account": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string",
                  "description": "The name of the account according to the provider."
                },
                "dotNumbers": {
                  "type": "array",
                  "description": "DOT numbers associated with the account according to the provider.",
                  "items": {
                    "type": "string"
                  }
                },
                "user": {
                  "type": "object",
                  "description": "The user of the provider account that created the connection.",
                  "properties": {
                    "sourceId": {
                      "description": "The ID of the user in the provider's system.",
                      "type": "string"
                    },
                    "firstName": {
                      "type": "string"
                    },
                    "lastName": {
                      "type": "string"
                    },
                    "email": {
                      "type": "string"
                    }
                  }
                }
              }
            },
            "provider": {
              "type": "object",
              "required": ["code", "name"],
              "properties": {
                "code": {
                  "type": "string",
                  "description": "Every provider has a unique code to identify it across Terminal's system. You can find each provider's code under [provider details](/providers)."
                },
                "name": {
                  "type": "string",
                  "description": "The name of the Telematics Service Provider."
                }
              }
            },
            "externalId": {
              "type": "string",
              "description": "An optional ID from your system that can be used to reference connections."
            },
            "sourceId": {
              "description": "The ID used in the source system to represent the account this connection has or had access to.\n\nThis may be an `organizationId` or `accountId`.\n\nNote: not all systems expose this information, in which case it may be undefined.",
              "title": "SourceId",
              "type": "string"
            },
            "syncMode": {
              "title": "SyncMode",
              "type": "string",
              "enum": ["automatic", "manual"],
              "description": "Enum values:\n- `automatic`: Terminal will keep this connections data up to date\n- `manual`: Terminal will only sync data upon request",
              "default": "automatic"
            },
            "token": {
              "type": "string",
              "pattern": "^con_tkn_\\S+$",
              "description": "This token is used when interacting with a connections' data."
            },
            "tags": {
              "type": "array",
              "description": "An optional list of tags from your system that can be used to reference connections.",
              "items": {
                "type": "string"
              }
            },
            "createdAt": {
              "title": "ISODateTime",
              "type": "string",
              "format": "date-time",
              "description": "[ISO 8601](https://www.w3.org/TR/NOTE-datetime) date"
            },
            "updatedAt": {
              "title": "ISODateTime",
              "type": "string",
              "format": "date-time",
              "description": "[ISO 8601](https://www.w3.org/TR/NOTE-datetime) date"
            }
          },
          "required": [
            "id",
            "company",
            "account",
            "provider",
            "syncMode",
            "token",
            "createdAt",
            "updatedAt"
          ]
        },
        {
          "type": "object",
          "properties": {
            "status": {
              "type": "string",
              "enum": [
                "connected",
                "disconnected",
                "archived",
                "pending_deletion"
              ],
              "description": "The current status of the connection."
            },
            "options": {
              "type": "object",
              "additionalProperties": true
            },
            "filters": {
              "type": "object",
              "properties": {
                "vehicles": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": ["active", "inactive"],
                      "description": "Filter connection to only include data related to vehicles with a specified status"
                    },
                    "excludeIds": {
                      "type": "array",
                      "items": {
                        "title": "VehicleId",
                        "description": "Unique identifier for the vehicle in Terminal.",
                        "type": "string",
                        "format": "ulid",
                        "pattern": "^vcl_[0-9A-HJKMNP-TV-Z]{26}$"
                      },
                      "description": "IDs of vehicles to exclude from data ingestion"
                    },
                    "includeIds": {
                      "type": "array",
                      "items": {
                        "title": "VehicleId",
                        "description": "Unique identifier for the vehicle in Terminal.",
                        "type": "string",
                        "format": "ulid",
                        "pattern": "^vcl_[0-9A-HJKMNP-TV-Z]{26}$"
                      },
                      "description": "IDs of vehicles to include in data ingestion (takes priority over other filters)"
                    }
                  }
                },
                "drivers": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": ["active", "inactive"],
                      "description": "Filter connection to only include data related to drivers with a specified status"
                    },
                    "excludeIds": {
                      "type": "array",
                      "items": {
                        "title": "DriverId",
                        "description": "Unique identifier for the driver in Terminal.",
                        "type": "string",
                        "format": "ulid",
                        "pattern": "^drv_[0-9A-HJKMNP-TV-Z]{26}$"
                      },
                      "description": "IDs of drivers to exclude from data ingestion"
                    },
                    "includeIds": {
                      "type": "array",
                      "items": {
                        "title": "DriverId",
                        "description": "Unique identifier for the driver in Terminal.",
                        "type": "string",
                        "format": "ulid",
                        "pattern": "^drv_[0-9A-HJKMNP-TV-Z]{26}$"
                      },
                      "description": "IDs of drivers to include in data ingestion (takes priority over other filters)"
                    }
                  }
                }
              },
              "description": "Filters applied to connection data"
            },
            "linkUrl": {
              "type": "string",
              "format": "uri",
              "description": "The URL to send your user to in order to have them re-authenticate the connection."
            }
          },
          "required": ["status", "options", "linkUrl"]
        }
      ]
    },
    {
      "title": "Deleted Connection",
      "description": "A retained connection record after connection-scoped data has been deleted.",
      "allOf": [
        {
          "title": "Connection Base",
          "type": "object",
          "description": "Fields shared by all connection records.",
          "properties": {
            "id": {
              "title": "ConnectionId",
              "type": "string",
              "format": "ulid"
            },
            "company": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string",
                  "description": "Optional name of the connection."
                },
                "dotNumbers": {
                  "type": "array",
                  "description": "Optional DOT numbers associated with this connection.",
                  "items": {
                    "type": "string"
                  }
                }
              }
            },
            "account": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string",
                  "description": "The name of the account according to the provider."
                },
                "dotNumbers": {
                  "type": "array",
                  "description": "DOT numbers associated with the account according to the provider.",
                  "items": {
                    "type": "string"
                  }
                },
                "user": {
                  "type": "object",
                  "description": "The user of the provider account that created the connection.",
                  "properties": {
                    "sourceId": {
                      "description": "The ID of the user in the provider's system.",
                      "type": "string"
                    },
                    "firstName": {
                      "type": "string"
                    },
                    "lastName": {
                      "type": "string"
                    },
                    "email": {
                      "type": "string"
                    }
                  }
                }
              }
            },
            "provider": {
              "type": "object",
              "required": ["code", "name"],
              "properties": {
                "code": {
                  "type": "string",
                  "description": "Every provider has a unique code to identify it across Terminal's system. You can find each provider's code under [provider details](/providers)."
                },
                "name": {
                  "type": "string",
                  "description": "The name of the Telematics Service Provider."
                }
              }
            },
            "externalId": {
              "type": "string",
              "description": "An optional ID from your system that can be used to reference connections."
            },
            "sourceId": {
              "description": "The ID used in the source system to represent the account this connection has or had access to.\n\nThis may be an `organizationId` or `accountId`.\n\nNote: not all systems expose this information, in which case it may be undefined.",
              "title": "SourceId",
              "type": "string"
            },
            "syncMode": {
              "title": "SyncMode",
              "type": "string",
              "enum": ["automatic", "manual"],
              "description": "Enum values:\n- `automatic`: Terminal will keep this connections data up to date\n- `manual`: Terminal will only sync data upon request",
              "default": "automatic"
            },
            "token": {
              "type": "string",
              "pattern": "^con_tkn_\\S+$",
              "description": "This token is used when interacting with a connections' data."
            },
            "tags": {
              "type": "array",
              "description": "An optional list of tags from your system that can be used to reference connections.",
              "items": {
                "type": "string"
              }
            },
            "createdAt": {
              "title": "ISODateTime",
              "type": "string",
              "format": "date-time",
              "description": "[ISO 8601](https://www.w3.org/TR/NOTE-datetime) date"
            },
            "updatedAt": {
              "title": "ISODateTime",
              "type": "string",
              "format": "date-time",
              "description": "[ISO 8601](https://www.w3.org/TR/NOTE-datetime) date"
            }
          },
          "required": [
            "id",
            "company",
            "account",
            "provider",
            "syncMode",
            "token",
            "createdAt",
            "updatedAt"
          ]
        },
        {
          "type": "object",
          "properties": {
            "status": {
              "type": "string",
              "enum": ["deleting", "deleted"]
            },
            "deletedAt": {
              "description": "When Terminal finished deleting connection-scoped data. Present once status is `deleted`.",
              "title": "ISODateTime",
              "type": "string",
              "format": "date-time"
            }
          },
          "required": ["status"]
        }
      ]
    }
  ]
}
