PSA/shared/workflow/persistence/workflowRuntimeEventModelV2.ts
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

100 lines
3.2 KiB
TypeScript

import { Knex } from 'knex';
export type WorkflowRuntimeEventRecord = {
event_id: string;
// uuid Citus distribution column. The legacy `tenant_id` column is being phased
// out (dropped in the cleanup migration) and is not referenced here.
tenant?: string | null;
event_name: string;
correlation_key?: string | null;
payload?: Record<string, unknown> | null;
payload_schema_ref?: string | null;
schema_ref_conflict?: { submission: string; catalog: string } | null;
created_at: string;
processed_at?: string | null;
matched_run_id?: string | null;
matched_wait_id?: string | null;
matched_step_path?: string | null;
error_message?: string | null;
};
const WorkflowRuntimeEventModelV2 = {
create: async (knex: Knex, data: Partial<WorkflowRuntimeEventRecord>): Promise<WorkflowRuntimeEventRecord> => {
const [record] = await knex<WorkflowRuntimeEventRecord>('workflow_runtime_events')
.insert({
...data,
created_at: data.created_at ?? new Date().toISOString()
})
.returning('*');
return record;
},
update: async (knex: Knex, eventId: string, data: Partial<WorkflowRuntimeEventRecord>, tenant?: string | null): Promise<WorkflowRuntimeEventRecord> => {
const query = knex<WorkflowRuntimeEventRecord>('workflow_runtime_events').where({ event_id: eventId });
if (tenant) query.andWhere({ tenant });
const [record] = await query
.update({
...data
})
.returning('*');
return record;
},
getById: async (knex: Knex, eventId: string, tenant?: string | null): Promise<WorkflowRuntimeEventRecord | null> => {
const query = knex<WorkflowRuntimeEventRecord>('workflow_runtime_events').where({ event_id: eventId });
if (tenant) query.andWhere({ tenant });
const record = await query.first();
return record || null;
},
list: async (
knex: Knex,
options?: {
tenantId?: string | null;
eventName?: string;
correlationKey?: string;
from?: string;
to?: string;
status?: 'matched' | 'unmatched' | 'error';
limit?: number;
cursor?: number;
}
): Promise<WorkflowRuntimeEventRecord[]> => {
const query = knex<WorkflowRuntimeEventRecord>('workflow_runtime_events');
if (options?.tenantId) {
query.where('tenant', options.tenantId);
}
if (options?.eventName) {
query.where('event_name', options.eventName);
}
if (options?.correlationKey) {
query.where('correlation_key', options.correlationKey);
}
if (options?.from) {
query.where('created_at', '>=', options.from);
}
if (options?.to) {
query.where('created_at', '<=', options.to);
}
if (options?.status === 'matched') {
query.whereNotNull('matched_run_id').whereNull('error_message');
}
if (options?.status === 'unmatched') {
query.whereNull('matched_run_id').whereNull('error_message');
}
if (options?.status === 'error') {
query.whereNotNull('error_message');
}
const limit = options?.limit ?? 100;
const cursor = options?.cursor ?? 0;
return query
.orderBy('created_at', 'desc')
.orderBy('event_id', 'desc')
.limit(limit + 1)
.offset(cursor);
}
};
export default WorkflowRuntimeEventModelV2;