{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/api-evangelist/akahu/main/json-schema/akahu-referral-schema.json",
  "title": "Referral",
  "description": "An application snapshot that can be redeemed by another organisation to create a new application with copied resources.",
  "x-generated": "2026-10-26",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/akahu-referrals-api-openapi.yml#/components/schemas/Referral",
  "type": "object",
  "properties": {
    "_id": {
      "type": "string",
      "pattern": "^referral_[a-z0-9]{24,27}$",
      "description": "The unique ID of the referral."
    },
    "code": {
      "$ref": "#/$defs/ReferralCode"
    },
    "status": {
      "$ref": "#/$defs/ReferralStatus"
    },
    "reference": {
      "type": "string",
      "description": "The default reference that applications created from this referral will use."
    },
    "expires_at": {
      "type": "string",
      "format": "date-time",
      "description": "The time after which the referral code can no longer be redeemed."
    },
    "revoked_at": {
      "type": "string",
      "format": "date-time",
      "description": "The time at which the referral was revoked. Only present when `status` is `revoked`."
    },
    "created_at": {
      "type": "string",
      "format": "date-time"
    },
    "updated_at": {
      "type": "string",
      "format": "date-time"
    },
    "redeemed_by": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/ReferralOrgDetails"
      },
      "description": "Organisations that have successfully redeemed this referral."
    }
  },
  "required": [
    "_id",
    "code",
    "status",
    "reference",
    "expires_at",
    "created_at",
    "updated_at",
    "redeemed_by"
  ],
  "$defs": {
    "ReferralCode": {
      "type": "string",
      "pattern": "^AK_[A-Z0-9]{10}$",
      "description": "A referral code using the format `AK_` followed by 10 uppercase alphanumeric characters."
    },
    "ReferralOrgDetails": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "logo": {
          "type": "string",
          "description": "A URL referencing the org's logo in PNG format."
        }
      },
      "required": [
        "name"
      ],
      "description": "Details for an organisation involved in a referral."
    },
    "ReferralStatus": {
      "type": "string",
      "enum": [
        "active",
        "expired",
        "revoked"
      ],
      "description": "The status of a referral:\n\n - `active` The referral code can be redeemed.\n\n - `expired` The referral has passed its expiry and can no longer be redeemed.\n\n - `revoked` The referral has been revoked and can no longer be redeemed."
    }
  }
}