Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/events/risk-evaluated.event.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { RiskEvaluatedEvent } from './risk-evaluated.event';
import { DomainEventName } from './event-names';

describe('RiskEvaluatedEvent (#414)', () => {
it('constructs an immutable risk evaluation event with score, band, and factors', () => {
const evt = new RiskEvaluatedEvent(
'org_123',
{
transactionId: 'tx_999',
score: 82,
band: 'HIGH',
factors: [
{
factor: 'amount',
weight: 30,
contribution: 25,
detail: 'Exceeds standard agent transfer limit',
},
],
canAutoExecute: false,
},
'agent_42',
);

expect(RiskEvaluatedEvent.eventName).toBe(DomainEventName.RiskEvaluated);
expect(evt.organizationId).toBe('org_123');
expect(evt.payload.score).toBe(82);
expect(evt.payload.band).toBe('HIGH');
expect(evt.payload.canAutoExecute).toBe(false);
expect(evt.actorId).toBe('agent_42');
expect(evt.occurredAt).toBeInstanceOf(Date);
});
});
26 changes: 26 additions & 0 deletions src/events/risk-evaluated.event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RiskBand } from '@prisma/client';
import { DomainEventName } from './event-names';
import { RiskFactorScore } from '../modules/risk/risk.types';

export interface RiskEvaluatedEventPayload {
transactionId?: string;
score: number;
band: RiskBand;
factors: RiskFactorScore[];
canAutoExecute: boolean;
}

/**
* Strongly typed domain event emitted whenever an agent transaction undergoes
* risk scoring so downstream audit listeners persist immutable risk metadata.
*/
export class RiskEvaluatedEvent {
static readonly eventName = DomainEventName.RiskEvaluated;

constructor(
public readonly organizationId: string,
public readonly payload: RiskEvaluatedEventPayload,
public readonly actorId?: string,
public readonly occurredAt: Date = new Date(),
) {}
}
Loading