|
| 1 | +/** |
| 2 | + * Sanctions Screening Tables |
| 3 | + * |
| 4 | + * Stores the result of every OFAC / global-sanctions check we run, plus the |
| 5 | + * status of every screened wallet (so requests from BLOCKED accounts can be |
| 6 | + * rejected at the middleware layer without re-querying the upstream provider). |
| 7 | + * |
| 8 | + * - screened_users : the per-wallet status row (the "User" status the |
| 9 | + * issue refers to). account_status is the gate |
| 10 | + * checked by the request-time middleware. |
| 11 | + * - security_audit : append-only audit log of every screening event, |
| 12 | + * with provider identity, risk score, and reason |
| 13 | + * for the flag. Satisfies regulatory reporting. |
| 14 | + * - sanctions_review_queue : compliance-officer worklist for false-positive |
| 15 | + * review. cleared addresses are unblocked and |
| 16 | + * re-screening is suppressed via override_until. |
| 17 | + */ |
| 18 | +exports.up = async function up(knex) { |
| 19 | + const hasScreenedUsers = await knex.schema.hasTable('screened_users'); |
| 20 | + if (!hasScreenedUsers) { |
| 21 | + await knex.schema.createTable('screened_users', (table) => { |
| 22 | + table.string('wallet_address').primary(); |
| 23 | + table.string('account_status').notNullable().defaultTo('ACTIVE'); |
| 24 | + table.string('risk_level').nullable(); |
| 25 | + table.decimal('risk_score', 10, 4).nullable(); |
| 26 | + table.text('flagged_lists').nullable(); |
| 27 | + table.text('block_reason').nullable(); |
| 28 | + table.string('blocking_provider').nullable(); |
| 29 | + table.timestamp('last_screened_at').nullable(); |
| 30 | + table.string('last_audit_id').nullable(); |
| 31 | + table.timestamp('blocked_at').nullable(); |
| 32 | + table.timestamp('unblocked_at').nullable(); |
| 33 | + table.string('unblocked_by').nullable(); |
| 34 | + table.timestamp('override_until').nullable(); |
| 35 | + table.timestamp('created_at').notNullable().defaultTo(knex.fn.now()); |
| 36 | + table.timestamp('updated_at').notNullable().defaultTo(knex.fn.now()); |
| 37 | + |
| 38 | + table.index(['account_status']); |
| 39 | + table.index(['risk_level']); |
| 40 | + table.index(['last_screened_at']); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + const hasSecurityAudit = await knex.schema.hasTable('security_audit'); |
| 45 | + if (!hasSecurityAudit) { |
| 46 | + await knex.schema.createTable('security_audit', (table) => { |
| 47 | + table.string('id').primary(); |
| 48 | + table.string('wallet_address').notNullable().index(); |
| 49 | + table.string('event_type').notNullable(); |
| 50 | + table.string('provider').nullable(); |
| 51 | + table.string('risk_level').nullable(); |
| 52 | + table.decimal('risk_score', 10, 4).nullable(); |
| 53 | + table.text('flagged_lists').nullable(); |
| 54 | + table.text('reason').nullable(); |
| 55 | + table.text('provider_response').nullable(); |
| 56 | + table.string('triggering_action').nullable(); |
| 57 | + table.string('actor').nullable(); |
| 58 | + table.string('ip_address').nullable(); |
| 59 | + table.timestamp('created_at').notNullable().defaultTo(knex.fn.now()); |
| 60 | + |
| 61 | + table.index(['wallet_address', 'created_at']); |
| 62 | + table.index(['event_type', 'created_at']); |
| 63 | + table.index(['provider', 'created_at']); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + const hasReviewQueue = await knex.schema.hasTable('sanctions_review_queue'); |
| 68 | + if (!hasReviewQueue) { |
| 69 | + await knex.schema.createTable('sanctions_review_queue', (table) => { |
| 70 | + table.string('id').primary(); |
| 71 | + table.string('wallet_address').notNullable(); |
| 72 | + table.string('triggering_audit_id').nullable(); |
| 73 | + table.string('status').notNullable().defaultTo('open'); |
| 74 | + table.string('risk_level').nullable(); |
| 75 | + table.decimal('risk_score', 10, 4).nullable(); |
| 76 | + table.text('flagged_lists').nullable(); |
| 77 | + table.text('reason').nullable(); |
| 78 | + table.timestamp('submitted_at').notNullable().defaultTo(knex.fn.now()); |
| 79 | + table.timestamp('reviewed_at').nullable(); |
| 80 | + table.string('reviewed_by').nullable(); |
| 81 | + table.text('decision_notes').nullable(); |
| 82 | + |
| 83 | + table.index(['wallet_address', 'status']); |
| 84 | + table.index(['status', 'submitted_at']); |
| 85 | + }); |
| 86 | + |
| 87 | + await knex.raw( |
| 88 | + `CREATE UNIQUE INDEX IF NOT EXISTS idx_sanctions_review_queue_one_open_per_wallet |
| 89 | + ON sanctions_review_queue (wallet_address) WHERE status = 'open'` |
| 90 | + ); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +exports.down = async function down(knex) { |
| 95 | + await knex.schema.dropTableIfExists('sanctions_review_queue'); |
| 96 | + await knex.schema.dropTableIfExists('security_audit'); |
| 97 | + await knex.schema.dropTableIfExists('screened_users'); |
| 98 | +}; |
0 commit comments