PSA/ee/server/migrations/20260524090100_create_teams_audit_events.cjs
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

106 lines
3.0 KiB
JavaScript

const AUDIT_ACTION_IDS = [
'assign_ticket',
'add_note',
'reply_to_contact',
'log_time',
'approval_response',
'create_ticket_from_message',
'update_from_message',
];
async function citusFunctionAvailable(knex) {
const result = await knex.raw(`
SELECT EXISTS (
SELECT 1
FROM pg_proc
WHERE proname = 'create_distributed_table'
) AS exists;
`);
return Boolean(result.rows?.[0]?.exists);
}
async function isDistributed(knex, tableName) {
const result = await knex.raw(`
SELECT EXISTS (
SELECT 1
FROM pg_dist_partition
WHERE logicalrelid = ?::regclass
) AS distributed;
`, [tableName]);
return Boolean(result.rows?.[0]?.distributed);
}
exports.up = async function up(knex) {
await knex.raw(`
CREATE TABLE IF NOT EXISTS teams_audit_events (
tenant uuid NOT NULL,
event_id uuid NOT NULL DEFAULT gen_random_uuid(),
actor_user_id uuid,
microsoft_user_id text,
surface text NOT NULL,
action_id text NOT NULL,
target_type text,
target_id text,
idempotency_key text,
payload_hash text,
result_status text NOT NULL,
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT teams_audit_events_pk PRIMARY KEY (tenant, event_id),
CONSTRAINT teams_audit_events_surface_check
CHECK (surface IN ('bot', 'message_extension', 'quick_action', 'tab')),
CONSTRAINT teams_audit_events_result_status_check
CHECK (result_status IN ('success', 'failure')),
CONSTRAINT teams_audit_events_action_id_check
CHECK (action_id IN (${AUDIT_ACTION_IDS.map((actionId) => `'${actionId}'`).join(', ')}))
);
`);
await knex.raw(`
CREATE INDEX IF NOT EXISTS teams_audit_events_actor_created_idx
ON teams_audit_events (tenant, actor_user_id, created_at DESC);
`);
await knex.raw(`
CREATE INDEX IF NOT EXISTS teams_audit_events_target_idx
ON teams_audit_events (tenant, target_type, target_id);
`);
await knex.raw(`
CREATE OR REPLACE FUNCTION cleanup_teams_audit_events(retention_interval interval DEFAULT interval '365 days')
RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
deleted_count integer;
BEGIN
DELETE FROM teams_audit_events
WHERE created_at < now() - retention_interval;
GET DIAGNOSTICS deleted_count = ROW_COUNT;
RETURN deleted_count;
END;
$$;
`);
if (await citusFunctionAvailable(knex)) {
if (!(await isDistributed(knex, 'teams_audit_events'))) {
await knex.raw(
`SELECT create_distributed_table(?, 'tenant', colocate_with => 'teams_integrations')`,
['teams_audit_events']
);
}
} else {
console.warn('[teams_audit_events] Skipping create_distributed_table (function unavailable)');
}
};
exports.down = async function down(knex) {
await knex.raw(`DROP FUNCTION IF EXISTS cleanup_teams_audit_events(interval);`);
await knex.raw(`DROP TABLE IF EXISTS teams_audit_events;`);
};
exports.config = { transaction: false };