{
 "$schema": "https://json-schema.org/draft/2020-12/schema",
 "$id": "urn:luminetric:canonical:1:retrieval.completed",
 "$comment": "Generated by build_schemas.py. Open by design: accept fields and enum values you do not recognise (COMPATIBILITY.md).",
 "title": "retrieval.completed",
 "description": "A retrieval that reached the portal and listed its accounts. Statements follow as statement.available.",
 "type": "object",
 "properties": {
  "schema_version": {
   "type": "string",
   "pattern": "^1\\.[0-9]+\\.[0-9]+$",
   "description": "Semantic version of this contract. This schema covers major version 1."
  },
  "event": {
   "type": "string",
   "const": "retrieval.completed"
  },
  "event_id": {
   "type": "string",
   "pattern": "^rtv_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
   "description": "The delivery id."
  },
  "request_id": {
   "type": "string",
   "minLength": 1,
   "description": "The run: run_ plus a UUID for a run you triggered (POST /v1/connections/{connection_id}/runs), dsc_ plus a UUID for a discovery run you triggered, sch_ plus a UUID for a scheduled one. Statements the run downloads arrive under request ids of their own."
  },
  "connection_id": {
   "type": "string",
   "pattern": "^con_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
   "description": "The connection it ran for, when it ran for one."
  },
  "provider": {
   "type": [
    "string",
    "null"
   ]
  },
  "dry_run": {
   "type": "boolean",
   "description": "True for a discovery run: accounts listed, nothing downloaded."
  },
  "invoices_found": {
   "type": "integer",
   "minimum": 0
  },
  "invoices_downloaded": {
   "type": "integer",
   "minimum": 0
  },
  "accounts": {
   "type": "array",
   "items": {
    "type": "object",
    "properties": {
     "account_number": {
      "type": [
       "string",
       "null"
      ],
      "description": "Letters and digits only, upper case: the form to match on, as in the accounts API (sample 04) and statements' normalized."
     },
     "account_number_as_printed": {
      "type": [
       "string",
       "null"
      ],
      "description": "As the portal showed it."
     },
     "account_id": {
      "type": "string",
      "pattern": "^acc_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
      "description": "Our id for the account. Present when the request ran for a connection."
     },
     "collection": {
      "type": "string",
      "description": "Yours: whether we retrieve it. Never the same field as status. Known values: never_collected, active, paused. Values may be added in a minor version.",
      "x-known-values": [
       "never_collected",
       "active",
       "paused"
      ]
     },
     "status": {
      "type": "string",
      "description": "Ours: what the portal did. Known values: connection_success, connection_error. Values may be added in a minor version.",
      "x-known-values": [
       "connection_success",
       "connection_error"
      ]
     },
     "status_detail": {
      "type": "string",
      "description": "Ours: why. Known values: data_accessible, not_listed_by_portal. Values may be added in a minor version.",
      "x-known-values": [
       "data_accessible",
       "not_listed_by_portal"
      ]
     },
     "service_address": {
      "type": [
       "string",
       "null"
      ]
     },
     "most_recent_statement_date": {
      "type": [
       "string",
       "null"
      ],
      "description": "As the portal showed it, ISO YYYY-MM-DD when the session could read it."
     },
     "most_recent_total_due": {
      "type": [
       "string",
       "null"
      ],
      "description": "The portal's own text; not parsed."
     }
    },
    "required": [
     "account_number"
    ]
   }
  },
  "new_accounts": {
   "type": "array",
   "items": {
    "type": "object",
    "properties": {
     "account_number": {
      "type": [
       "string",
       "null"
      ],
      "description": "Letters and digits only, upper case: the form to match on, as in the accounts API (sample 04) and statements' normalized."
     },
     "account_number_as_printed": {
      "type": [
       "string",
       "null"
      ],
      "description": "As the portal showed it."
     },
     "account_id": {
      "type": "string",
      "pattern": "^acc_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
      "description": "Our id for the account. Present when the request ran for a connection."
     },
     "collection": {
      "type": "string",
      "description": "Yours: whether we retrieve it. Never the same field as status. Known values: never_collected, active, paused. Values may be added in a minor version.",
      "x-known-values": [
       "never_collected",
       "active",
       "paused"
      ]
     },
     "status": {
      "type": "string",
      "description": "Ours: what the portal did. Known values: connection_success, connection_error. Values may be added in a minor version.",
      "x-known-values": [
       "connection_success",
       "connection_error"
      ]
     },
     "status_detail": {
      "type": "string",
      "description": "Ours: why. Known values: data_accessible, not_listed_by_portal. Values may be added in a minor version.",
      "x-known-values": [
       "data_accessible",
       "not_listed_by_portal"
      ]
     }
    },
    "required": [
     "account_number"
    ]
   }
  },
  "missing_accounts": {
   "type": "array",
   "items": {
    "type": "object",
    "properties": {
     "account_number": {
      "type": [
       "string",
       "null"
      ],
      "description": "Letters and digits only, upper case: the form to match on, as in the accounts API (sample 04) and statements' normalized."
     },
     "account_number_as_printed": {
      "type": [
       "string",
       "null"
      ],
      "description": "As the portal showed it."
     },
     "account_id": {
      "type": "string",
      "pattern": "^acc_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
      "description": "Our id for the account. Present when the request ran for a connection."
     },
     "collection": {
      "type": "string",
      "description": "Yours: whether we retrieve it. Never the same field as status. Known values: never_collected, active, paused. Values may be added in a minor version.",
      "x-known-values": [
       "never_collected",
       "active",
       "paused"
      ]
     },
     "status": {
      "type": "string",
      "description": "Ours: what the portal did. Known values: connection_success, connection_error. Values may be added in a minor version.",
      "x-known-values": [
       "connection_success",
       "connection_error"
      ]
     },
     "status_detail": {
      "type": "string",
      "description": "Ours: why. Known values: data_accessible, not_listed_by_portal. Values may be added in a minor version.",
      "x-known-values": [
       "data_accessible",
       "not_listed_by_portal"
      ]
     }
    },
    "required": [
     "account_number"
    ]
   }
  },
  "enumeration": {
   "type": "object",
   "description": "How many of the accounts present before this run it listed. A shortfall marks nothing missing.",
   "properties": {
    "expected_account_count": {
     "type": "integer",
     "minimum": 0
    },
    "enumerated_account_count": {
     "type": "integer",
     "minimum": 0
    },
    "shortfall": {
     "type": "boolean"
    }
   },
   "required": [
    "expected_account_count",
    "enumerated_account_count",
    "shortfall"
   ]
  },
  "completed_at": {
   "type": "string",
   "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?Z$",
   "description": "When the retrieval completed. ISO 8601, UTC."
  }
 },
 "required": [
  "schema_version",
  "event",
  "event_id",
  "request_id",
  "dry_run",
  "invoices_found",
  "invoices_downloaded",
  "accounts",
  "completed_at"
 ]
}
