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
Binary file modified data/disciplr.db
Binary file not shown.
Binary file removed data/disciplr.db-shm
Binary file not shown.
Binary file removed data/disciplr.db-wal
Binary file not shown.
70 changes: 35 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions src/routes/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
} from '../services/vaultStore.js'
import { normalizeCreateVaultInput, validateCreateVaultInput } from '../services/vaultValidation.js'
import { queryParser } from '../middleware/queryParser.js'
import { applyFilters, applySort, paginateArray } from '../utils/pagination.js'
import { updateAnalyticsSummary } from '../db/database.js'
import { utcNow } from '../utils/timestamps.js'
import { prisma } from '../lib/prisma.js'

Expand All @@ -35,7 +33,7 @@ export interface Vault {
id: string
creator: string
amount: string
status: 'active' | 'completed' | 'failed' | 'cancelled'
status: 'pending' | 'active' | 'completed' | 'failed' | 'cancelled'
startTimestamp: string
endTimestamp: string
successDestination: string
Expand Down
36 changes: 34 additions & 2 deletions src/services/vaultTransitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { vaults, type Vault } from '../routes/vaults.js'
import { allMilestonesVerified } from './milestones.js'
import { VaultStatus } from '../types/vault.js'

type TerminalStatus = 'completed' | 'failed' | 'cancelled'

Expand All @@ -10,18 +11,34 @@ export interface TransitionResult {

const TERMINAL_STATUSES: ReadonlySet<string> = new Set(['completed', 'failed', 'cancelled'])

// Define valid transitions
const VALID_TRANSITIONS: Record<string, Set<string>> = {
'pending': new Set(['active']),
'active': new Set(['completed', 'failed', 'cancelled']),
'completed': new Set(),
'failed': new Set(),
'cancelled': new Set(),
}

const findVault = (vaultId: string): Vault | undefined =>
vaults.find((v) => v.id === vaultId)

export const getTransitionError = (
vault: Vault,
targetStatus: TerminalStatus,
targetStatus: string,
requesterId?: string,
): string | null => {
// Check if current status is terminal
if (TERMINAL_STATUSES.has(vault.status)) {
return `Vault is already '${vault.status}' and cannot transition`
}

// Check if transition is valid
const currentStatus = vault.status
if (!VALID_TRANSITIONS[currentStatus]?.has(targetStatus)) {
return `Invalid transition from '${currentStatus}' to '${targetStatus}'`
}

switch (targetStatus) {
case 'completed': {
if (!allMilestonesVerified(vault.id)) {
Expand All @@ -43,8 +60,12 @@ export const getTransitionError = (
}
return null
}
case 'active': {
// pending -> active is always allowed (no additional checks)
return null
}
default:
return `Unknown target status: ${targetStatus as string}`
return `Unknown target status: ${targetStatus}`
}
}

Expand Down Expand Up @@ -81,6 +102,17 @@ export const cancelVault = (vaultId: string, requesterId: string): TransitionRes
return { success: true }
}

export const activateVault = (vaultId: string): TransitionResult => {
const vault = findVault(vaultId)
if (!vault) return { success: false, error: 'Vault not found' }

const error = getTransitionError(vault, 'active')
if (error) return { success: false, error }

vault.status = 'active'
return { success: true }
}

export const checkExpiredVaults = (): string[] => {
const now = new Date()
const failed: string[] = []
Expand Down
Loading