Skip to content
Open
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
Empty file added docs/CLAIM_STATE_MACHINE.md
Empty file.
11 changes: 7 additions & 4 deletions src/claims/claim-resolution.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Claim } from './entities/claim.entity';
import { Claim, ClaimState } from './entities/claim.entity';
import { ClaimsCache } from '../cache/claims.cache';

interface VoteWeightSummary {
Expand Down Expand Up @@ -46,9 +46,12 @@ export class ClaimResolutionService {
const verdict = votes.trueWeight > votes.falseWeight;
const confidence = this.computeConfidenceScore(votes);

claim.resolvedVerdict = verdict;
claim.confidenceScore = confidence;
claim.finalized = true;
// Use transitionTo helper for validated state transition
// This transitions directly to FINALIZED with verdict and confidence
claim.transitionTo(ClaimState.FINALIZED, {
verdict,
confidence,
});

const savedClaim = await this.claimRepo.save(claim);
// Invalidate both the claim-specific cache and the latest claims list cache
Expand Down
15 changes: 11 additions & 4 deletions src/claims/claims.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Claim } from './entities/claim.entity';
import { Claim, ClaimState } from './entities/claim.entity';
import { CreateClaimDto } from './dto/create-claim.dto';
import { ClaimsCache } from '../cache/claims.cache';
import { RedisService } from '../redis/redis.service';
Expand Down Expand Up @@ -112,6 +112,7 @@ export class ClaimsService {

/**
* Resolve a claim (update verdict and confidence)
* Uses state machine validation to ensure valid transitions
*/
async resolveClaim(
claimId: string,
Expand All @@ -124,8 +125,11 @@ export class ClaimsService {

const beforeState = { ...claim };

claim.resolvedVerdict = verdict;
claim.confidenceScore = confidenceScore;
// Use transitionTo helper for validated state transition
claim.transitionTo(ClaimState.RESOLVED, {
verdict,
confidence: confidenceScore,
});

const updatedClaim = await this.claimRepo.save(claim);
// Invalidate both the claim-specific cache and the latest claims list cache
Expand All @@ -147,14 +151,17 @@ export class ClaimsService {

/**
* Finalize a claim
* Uses state machine validation to ensure valid transitions
*/
async finalizeClaim(claimId: string, userId?: string): Promise<Claim> {
const claim = await this.findOne(claimId);
if (!claim) throw new Error(`Claim ${claimId} not found`);

const beforeState = { ...claim };

claim.finalized = true;
// Use transitionTo helper for validated state transition
claim.transitionTo(ClaimState.FINALIZED);

const updatedClaim = await this.claimRepo.save(claim);
// Invalidate both the claim-specific cache and the latest claims list cache
await this.claimsCache.invalidateClaim(claimId);
Expand Down
Loading
Loading