{
 "site": "Identity Desk",
 "domain": "iddesk",
 "description": "KYC and identity verification review: AI document checks and risk scores, analyst decisions, sanctions hits blocked, compliance escalations, and declines that always state their reason.",
 "roles": [
  "analyst",
  "compliance",
  "admin"
 ],
 "machines": [
  {
   "name": "onboarding",
   "description": "One application for one product, from submission to decision.",
   "field": "status",
   "machine": {
    "initial": "submitted",
    "states": {
     "submitted": {
      "description": "",
      "transitions": {
       "assign": {
        "to": "in_review",
        "guards": [
         "analyst",
         "admin"
        ],
        "requiredFields": [
         "analyst"
        ],
        "sets": {},
        "rules": []
       }
      }
     },
     "in_review": {
      "description": "",
      "transitions": {
       "approve": {
        "to": "approved",
        "guards": [
         "analyst",
         "compliance",
         "admin"
        ],
        "requiredFields": [],
        "sets": {
         "decidedAt": "now()"
        },
        "rules": [
         {
          "name": "approveNeedsCoreChecks",
          "description": "Approval needs a passing document check, selfie match and sanctions screening.",
          "action": "reject",
          "message": "Document, selfie match and sanctions screening must be on file and passing."
         },
         {
          "name": "noApprovalOverFailedChecks",
          "description": "A failed check blocks approval.",
          "action": "reject",
          "message": "A verification failed."
         },
         {
          "name": "hitsNeedClearance",
          "description": "A sanctions or PEP hit blocks approval until compliance clears it as a false positive.",
          "action": "reject",
          "message": "A watchlist hit must be cleared by compliance first."
         },
         {
          "name": "confirmedBlocks",
          "description": "A confirmed escalation can never be approved.",
          "action": "reject",
          "message": "Compliance confirmed a concern. Decline instead."
         },
         {
          "name": "decideOnlyOnAReviewedScore",
          "description": "No approval while the AI risk score is unreviewed, or without one.",
          "action": "reject",
          "message": "Review the AI risk assessment first."
         },
         {
          "name": "highRiskNeedsCompliance",
          "description": "A high final risk tier needs a compliance clearance before approval.",
          "action": "reject",
          "message": "High-risk applicants need compliance clearance."
         }
        ]
       },
       "decline": {
        "to": "declined",
        "guards": [
         "analyst",
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "declineReason"
        ],
        "sets": {
         "decidedAt": "now()"
        },
        "rules": []
       },
       "requestInfo": {
        "to": "info_requested",
        "guards": [
         "analyst",
         "admin"
        ],
        "requiredFields": [
         "infoRequested"
        ],
        "sets": {},
        "rules": []
       },
       "escalate": {
        "to": "escalated",
        "guards": [
         "analyst",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "info_requested": {
      "description": "",
      "transitions": {
       "infoReceived": {
        "to": "in_review",
        "guards": [
         "analyst",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       },
       "expire": {
        "to": "expired",
        "guards": [
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "escalated": {
      "description": "",
      "transitions": {
       "approve": {
        "to": "approved",
        "guards": [
         "compliance",
         "admin"
        ],
        "requiredFields": [],
        "sets": {
         "decidedAt": "now()"
        },
        "rules": [
         {
          "name": "approveNeedsCoreChecks",
          "description": "Approval needs a passing document check, selfie match and sanctions screening.",
          "action": "reject",
          "message": "Document, selfie match and sanctions screening must be on file and passing."
         },
         {
          "name": "noApprovalOverFailedChecks",
          "description": "A failed check blocks approval.",
          "action": "reject",
          "message": "A verification failed."
         },
         {
          "name": "hitsNeedClearance",
          "description": "A sanctions or PEP hit blocks approval until compliance clears it as a false positive.",
          "action": "reject",
          "message": "A watchlist hit must be cleared by compliance first."
         },
         {
          "name": "confirmedBlocks",
          "description": "A confirmed escalation can never be approved.",
          "action": "reject",
          "message": "Compliance confirmed a concern. Decline instead."
         },
         {
          "name": "decideOnlyOnAReviewedScore",
          "description": "No approval while the AI risk score is unreviewed, or without one.",
          "action": "reject",
          "message": "Review the AI risk assessment first."
         },
         {
          "name": "highRiskNeedsCompliance",
          "description": "A high final risk tier needs a compliance clearance before approval.",
          "action": "reject",
          "message": "High-risk applicants need compliance clearance."
         }
        ]
       },
       "decline": {
        "to": "declined",
        "guards": [
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "declineReason"
        ],
        "sets": {
         "decidedAt": "now()"
        },
        "rules": []
       }
      }
     },
     "approved": {
      "description": "",
      "transitions": {}
     },
     "declined": {
      "description": "",
      "transitions": {}
     },
     "expired": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "assessment",
   "description": "The AI's risk score for an applicant. An analyst accepts it or overrides the tier.",
   "field": "status",
   "machine": {
    "initial": "draft",
    "states": {
     "draft": {
      "description": "",
      "transitions": {
       "accept": {
        "to": "accepted",
        "guards": [
         "analyst",
         "compliance"
        ],
        "requiredFields": [
         "finalTier"
        ],
        "sets": {
         "reviewedBy": "$user.id"
        },
        "rules": []
       },
       "override": {
        "to": "overridden",
        "guards": [
         "analyst",
         "compliance"
        ],
        "requiredFields": [
         "finalTier",
         "reviewNote"
        ],
        "sets": {
         "reviewedBy": "$user.id"
        },
        "rules": []
       }
      }
     },
     "accepted": {
      "description": "",
      "transitions": {}
     },
     "overridden": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "escalation",
   "description": "Enhanced due diligence by compliance, assigned to someone other than the escalating analyst.",
   "field": "status",
   "machine": {
    "initial": "open",
    "states": {
     "open": {
      "description": "",
      "transitions": {
       "clear": {
        "to": "cleared",
        "guards": [
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "finding"
        ],
        "sets": {},
        "rules": []
       },
       "confirm": {
        "to": "confirmed",
        "guards": [
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "finding"
        ],
        "sets": {},
        "rules": []
       }
      }
     },
     "cleared": {
      "description": "",
      "transitions": {}
     },
     "confirmed": {
      "description": "",
      "transitions": {}
     }
    }
   }
  }
 ],
 "resources": {
  "applicant": {
   "description": "A person applying for an account.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "fullName": {
     "type": "string",
     "required": true,
     "maxLength": 120
    },
    "birthDate": {
     "type": "date",
     "required": true
    },
    "country": {
     "type": "string",
     "required": true,
     "pattern": "^[A-Z]{2}$"
    },
    "email": {
     "type": "email",
     "required": true
    },
    "phone": {
     "type": "phone",
     "required": true
    }
   },
   "permissions": {
    "list": [
     "analyst",
     "compliance",
     "admin"
    ],
    "get": [
     "analyst",
     "compliance",
     "admin"
    ],
    "create": [
     "admin"
    ],
    "update": [
     "analyst",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   }
  },
  "onboarding": {
   "description": "One application for one product, from submission to decision.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "number": {
     "type": "string",
     "required": true,
     "unique": true,
     "maxLength": 20
    },
    "applicant": {
     "type": "reference",
     "references": "adl.iddesk.iddesk.applicant",
     "required": true
    },
    "product": {
     "type": "string",
     "required": true,
     "enum": [
      "checking",
      "card",
      "crypto_wallet",
      "business_account",
      "brokerage"
     ]
    },
    "submittedAt": {
     "type": "datetime",
     "required": true
    },
    "analyst": {
     "type": "string",
     "maxLength": 100
    },
    "infoRequested": {
     "type": "text"
    },
    "declineReason": {
     "type": "text"
    },
    "decidedAt": {
     "type": "datetime"
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "submitted"
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "submitted",
    "states": {
     "submitted": {
      "transitions": {
       "assign": {
        "to": "in_review",
        "guards": [
         "analyst",
         "admin"
        ],
        "requiredFields": [
         "analyst"
        ]
       }
      }
     },
     "in_review": {
      "transitions": {
       "approve": {
        "to": "approved",
        "guards": [
         "analyst",
         "compliance",
         "admin"
        ],
        "sets": {
         "decidedAt": "now()"
        }
       },
       "decline": {
        "to": "declined",
        "guards": [
         "analyst",
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "declineReason"
        ],
        "sets": {
         "decidedAt": "now()"
        }
       },
       "requestInfo": {
        "to": "info_requested",
        "guards": [
         "analyst",
         "admin"
        ],
        "requiredFields": [
         "infoRequested"
        ]
       },
       "escalate": {
        "to": "escalated",
        "guards": [
         "analyst",
         "admin"
        ]
       }
      }
     },
     "info_requested": {
      "transitions": {
       "infoReceived": {
        "to": "in_review",
        "guards": [
         "analyst",
         "admin"
        ]
       },
       "expire": {
        "to": "expired",
        "guards": [
         "admin"
        ]
       }
      }
     },
     "escalated": {
      "transitions": {
       "approve": {
        "to": "approved",
        "guards": [
         "compliance",
         "admin"
        ],
        "sets": {
         "decidedAt": "now()"
        }
       },
       "decline": {
        "to": "declined",
        "guards": [
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "declineReason"
        ],
        "sets": {
         "decidedAt": "now()"
        }
       }
      }
     },
     "approved": {},
     "declined": {},
     "expired": {}
    }
   },
   "permissions": {
    "list": [
     "analyst",
     "compliance",
     "admin"
    ],
    "get": [
     "analyst",
     "compliance",
     "admin"
    ],
    "create": [
     "admin"
    ],
    "update": [
     "analyst",
     "compliance",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "history": {
    "events": {
     "approved": {
      "trigger": "transition",
      "transition": "approve",
      "severity": "success"
     },
     "declined": {
      "trigger": "transition",
      "transition": "decline",
      "capture": [
       "declineReason"
      ],
      "label": "Declined: {declineReason}",
      "severity": "warning"
     },
     "escalated": {
      "trigger": "transition",
      "transition": "escalate",
      "label": "Escalated to compliance",
      "severity": "warning"
     },
     "infoAsked": {
      "trigger": "transition",
      "transition": "requestInfo",
      "capture": [
       "infoRequested"
      ],
      "label": "Asked for: {infoRequested}",
      "severity": "info"
     }
    }
   },
   "indexes": [
    {
     "fields": [
      "applicant"
     ]
    },
    {
     "fields": [
      "status"
     ]
    }
   ]
  },
  "verification": {
   "description": "One identity check on an applicant.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "applicant": {
     "type": "reference",
     "references": "adl.iddesk.iddesk.applicant",
     "required": true
    },
    "kind": {
     "type": "string",
     "required": true,
     "enum": [
      "document",
      "selfie_match",
      "database",
      "address",
      "device",
      "sanctions",
      "pep"
     ]
    },
    "source": {
     "type": "string",
     "required": true,
     "enum": [
      "ai",
      "vendor",
      "analyst"
     ]
    },
    "result": {
     "type": "string",
     "required": true,
     "enum": [
      "pass",
      "fail",
      "review",
      "hit"
     ]
    },
    "score": {
     "type": "percentage"
    },
    "detail": {
     "type": "text",
     "required": true
    },
    "checkedAt": {
     "type": "datetime",
     "required": true
    }
   },
   "validation": {
    "hitsAreScreenings": {
     "rule": "result != 'hit' || kind == 'sanctions' || kind == 'pep'",
     "message": "Only sanctions and PEP screenings produce hits."
    }
   },
   "permissions": {
    "list": [
     "analyst",
     "compliance",
     "admin"
    ],
    "get": [
     "analyst",
     "compliance",
     "admin"
    ],
    "create": [
     "analyst",
     "admin"
    ],
    "update": [
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "applicant"
     ]
    }
   ]
  },
  "assessment": {
   "description": "The AI's risk score for an applicant. An analyst accepts it or overrides the tier.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "applicant": {
     "type": "reference",
     "references": "adl.iddesk.iddesk.applicant",
     "required": true
    },
    "model": {
     "type": "string",
     "required": true,
     "maxLength": 60
    },
    "score": {
     "type": "integer",
     "required": true,
     "min": 0,
     "max": 100
    },
    "aiTier": {
     "type": "string",
     "required": true,
     "enum": [
      "low",
      "medium",
      "high"
     ]
    },
    "factors": {
     "type": "text",
     "required": true
    },
    "finalTier": {
     "type": "string",
     "enum": [
      "low",
      "medium",
      "high"
     ]
    },
    "reviewNote": {
     "type": "text"
    },
    "reviewedBy": {
     "type": "string",
     "maxLength": 100
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "draft"
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "draft",
    "states": {
     "draft": {
      "transitions": {
       "accept": {
        "to": "accepted",
        "guards": [
         "analyst",
         "compliance"
        ],
        "requiredFields": [
         "finalTier"
        ],
        "sets": {
         "reviewedBy": "$user.id"
        }
       },
       "override": {
        "to": "overridden",
        "guards": [
         "analyst",
         "compliance"
        ],
        "requiredFields": [
         "finalTier",
         "reviewNote"
        ],
        "sets": {
         "reviewedBy": "$user.id"
        }
       }
      }
     },
     "accepted": {},
     "overridden": {}
    }
   },
   "permissions": {
    "list": [
     "analyst",
     "compliance",
     "admin"
    ],
    "get": [
     "analyst",
     "compliance",
     "admin"
    ],
    "create": [
     "analyst",
     "admin"
    ],
    "update": [
     "analyst",
     "compliance",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "applicant"
     ]
    }
   ]
  },
  "escalation": {
   "description": "Enhanced due diligence by compliance, assigned to someone other than the escalating analyst.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "onboarding": {
     "type": "reference",
     "references": "adl.iddesk.iddesk.onboarding",
     "required": true
    },
    "reason": {
     "type": "string",
     "required": true,
     "enum": [
      "watchlist_match",
      "high_risk_tier",
      "document_concern",
      "source_of_funds"
     ]
    },
    "escalatedBy": {
     "type": "string",
     "required": true,
     "maxLength": 100
    },
    "assignee": {
     "type": "string",
     "required": true,
     "maxLength": 100
    },
    "finding": {
     "type": "text"
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "open"
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "open",
    "states": {
     "open": {
      "transitions": {
       "clear": {
        "to": "cleared",
        "guards": [
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "finding"
        ]
       },
       "confirm": {
        "to": "confirmed",
        "guards": [
         "compliance",
         "admin"
        ],
        "requiredFields": [
         "finding"
        ]
       }
      }
     },
     "cleared": {},
     "confirmed": {}
    }
   },
   "permissions": {
    "list": [
     "analyst",
     "compliance",
     "admin"
    ],
    "get": [
     "analyst",
     "compliance",
     "admin"
    ],
    "create": [
     "analyst",
     "admin"
    ],
    "update": [
     "compliance",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "history": {
    "events": {
     "cleared": {
      "trigger": "transition",
      "transition": "clear",
      "capture": [
       "finding"
      ],
      "label": "Cleared: {finding}",
      "severity": "success"
     },
     "confirmed": {
      "trigger": "transition",
      "transition": "confirm",
      "capture": [
       "finding",
       "onboarding"
      ],
      "label": "Confirmed: {finding}",
      "severity": "error"
     }
    }
   },
   "indexes": [
    {
     "fields": [
      "onboarding"
     ]
    }
   ]
  }
 },
 "rules": {
  "approveNeedsCoreChecks": {
   "description": "Approval needs a passing document check, selfie match and sanctions screening.",
   "trigger": {
    "resource": "onboarding",
    "events": [
     "transition"
    ],
    "transition": "approve"
   },
   "condition": "len(query('verification', { applicant: $record.applicant, kind: 'document', result: 'pass' })) == 0 || len(query('verification', { applicant: $record.applicant, kind: 'selfie_match', result: 'pass' })) == 0 || len(query('verification', { applicant: $record.applicant, kind: 'sanctions', result: 'pass' })) + len(query('verification', { applicant: $record.applicant, kind: 'sanctions', result: 'hit' })) == 0",
   "action": "reject",
   "severity": "error",
   "message": "Document, selfie match and sanctions screening must be on file and passing."
  },
  "noApprovalOverFailedChecks": {
   "description": "A failed check blocks approval.",
   "trigger": {
    "resource": "onboarding",
    "events": [
     "transition"
    ],
    "transition": "approve"
   },
   "condition": "len(query('verification', { applicant: $record.applicant, result: 'fail' })) > 0",
   "action": "reject",
   "severity": "error",
   "message": "A verification failed."
  },
  "hitsNeedClearance": {
   "description": "A sanctions or PEP hit blocks approval until compliance clears it as a false positive.",
   "trigger": {
    "resource": "onboarding",
    "events": [
     "transition"
    ],
    "transition": "approve"
   },
   "condition": "len(query('verification', { applicant: $record.applicant, result: 'hit' })) > 0 && len(query('escalation', { onboarding: $record.id, status: 'cleared' })) == 0",
   "action": "reject",
   "severity": "error",
   "message": "A watchlist hit must be cleared by compliance first."
  },
  "confirmedBlocks": {
   "description": "A confirmed escalation can never be approved.",
   "trigger": {
    "resource": "onboarding",
    "events": [
     "transition"
    ],
    "transition": "approve"
   },
   "condition": "len(query('escalation', { onboarding: $record.id, status: 'confirmed' })) > 0",
   "action": "reject",
   "severity": "error",
   "message": "Compliance confirmed a concern. Decline instead."
  },
  "decideOnlyOnAReviewedScore": {
   "description": "No approval while the AI risk score is unreviewed, or without one.",
   "trigger": {
    "resource": "onboarding",
    "events": [
     "transition"
    ],
    "transition": "approve"
   },
   "condition": "len(query('assessment', { applicant: $record.applicant, status: 'draft' })) > 0 || (len(query('assessment', { applicant: $record.applicant, status: 'accepted' })) + len(query('assessment', { applicant: $record.applicant, status: 'overridden' }))) == 0",
   "action": "reject",
   "severity": "error",
   "message": "Review the AI risk assessment first."
  },
  "highRiskNeedsCompliance": {
   "description": "A high final risk tier needs a compliance clearance before approval.",
   "trigger": {
    "resource": "onboarding",
    "events": [
     "transition"
    ],
    "transition": "approve"
   },
   "condition": "some(query('assessment', { applicant: $record.applicant }), a => a.finalTier == 'high') && len(query('escalation', { onboarding: $record.id, status: 'cleared' })) == 0",
   "action": "reject",
   "severity": "error",
   "message": "High-risk applicants need compliance clearance."
  },
  "escalateToSomeoneElse": {
   "description": "Enhanced due diligence is done by someone other than the analyst who escalated.",
   "trigger": {
    "resource": "escalation",
    "events": [
     "create"
    ]
   },
   "condition": "$record.assignee == $record.escalatedBy",
   "action": "reject",
   "severity": "error",
   "message": "Assign the escalation to a different person."
  }
 },
 "workflows": {
  "confirmedEscalationDeclines": {
   "description": "A confirmed escalation declines its onboarding with the neutral notice.",
   "trigger": {
    "resource": "escalation",
    "event": "transition",
    "transition": "confirm"
   },
   "steps": [
    {
     "name": "decline",
     "action": "transition",
     "resource": "onboarding",
     "transition": "decline",
     "target": "$trigger.record.onboarding",
     "input": {
      "declineReason": "We are unable to open this account. You may contact us for more information about this decision."
     }
    }
   ]
  }
 },
 "schedules": {
  "expireUnansweredRequests": {
   "description": "Onboardings waiting on information for 30 days expire.",
   "recurrence": "daily at 03:30",
   "source": {
    "resource": "onboarding",
    "filter": "status == 'info_requested' && submittedAt < dateSub(now(), 30, 'day')"
   },
   "action": "transition",
   "transition": "expire"
  }
 }
}
