Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 16:45 UTC
dependabot/pip/backend/setuptools-gte-75-and-lt-85
add-index-html-10623938925177053894
arena/094f314c-aram
arena/2d4eda07-aram
chatgpt-phase1b2-autopilot
codex/adr-future-connectors-pi-v1
codex/aram-account-note-source-trace-20261031
codex/aram-actions-node24-maintenance-20261031
codex/aram-analyst-review-workbook-20261031
codex/aram-bs-candidate-statement-20261031
codex/aram-cash-flow-direct-candidate-20261031
codex/aram-clean-reference-year-integrity-20261030
codex/aram-cross-statement-controls-20261031
codex/aram-equity-changes-candidate-20261031
codex/aram-five-statement-cli-20261031
codex/aram-five-statement-integration-20261031
codex/aram-five-statement-orchestrator-20261031
codex/aram-gate3-coverage-target-guard-20261030
codex/aram-gate3-hidden-xlsx-guard-20261030
codex/aram-gate3-integration-validation-20261030
codex/aram-gate3-mapping-change-preview-20261030
codex/aram-gate3-stale-note-guard-20261030
codex/aram-income-statement-candidate-20261031
codex/aram-manifest-referential-integrity-20261031
codex/aram-mapping-source-parser-20261101
codex/aram-multidimensional-mapping-20261101
codex/aram-multistatement-mapping-20261031
codex/aram-pipeline-manifest-schema-20261031
codex/aram-readonly-mapping-coverage-20261031
codex/aram-reconciliation-review-gate-20261031
codex/aram-same-flush-fiscal-lock-20261030
codex/aram-synthetic-windows-demo-20261031
codex/aram-trial-balance-diagnostic-20261031
codex/aram-windows-background-detail-search-20261031
codex/aram-windows-detail-review-20261031
codex/aram-windows-five-statement-pilot-20261031
codex/aram-windows-statement-figures-20261031
codex/gate-2a-pilot-workbook
codex/gate-2a-pilot-workbook-6864754244772568979
codex/gate3-authenticated-principal-boundary-20261030
codex/gate3-authored-draft-staging-20261030
codex/gate3-carry-forward-preview-20261023
codex/gate3-draft-staging-audit-20261030
codex/gate3-mapping-batch-freshness-20261030
codex/gate3-mapping-batch-review-20261030
codex/gate3-mapping-core
codex/gate3-mapping-coverage-20261023
codex/gate3-mapping-excel-20261023
codex/gate3-note-mapping-review-20261023
codex/gate3-signed-year-scope-20261030
codex/gate3-unapproved-proposal-storage-20261030
codex/payvast-mapping-discovery-20261023
codex/phase-2a-backend-foundation
codex/phase-2b-post-merge-record
codex/phase-2b-source-registration
codex/phase-2b-source-registration-12955190277775369594
codex/post-gate2a-control-reconciliation
codex/post-pr18-acceptance-record
codex/project-control-foundation
codex/t1-repo-hygiene-20261023
codex/t2-ci-hardening-20261023
codex/t3-explicit-migrations-20261023
codex/t6a-readonly-diagnostics-20261023
codex/v1-01-safety-gate-hardening
codex/v1-year-isolation-followup-20261023
codex/windows-native-pilot-shell-20261030
dependabot/github_actions/actions/checkout-7
dependabot/github_actions/actions/setup-node-6
dependabot/github_actions/actions/setup-python-7
dependabot/github_actions/actions/upload-artifact-7
dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1
dependabot/npm_and_yarn/frontend/vite-8.4.0
dependabot/npm_and_yarn/frontend/vitest-5.1.2
dependabot/pip/backend/mypy-gte-1.13-and-lt-3
dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8
dependabot/pip/backend/setuptools-gte-75-and-lt-85
docs/aram-master-control-plan-20261111
docs/aram-roadmap-dashboard-20261030
docs/financial-statement-reconciliation-1402-1404-20261110
feat/gate3-governed-mapping-20261101
feat/gate3-package9-1-security-20261103
feat/gate3-package9-isolated-20261103
fix/password-unicode-validation-20261111
governance/aram-5-1-3-controls-20261030
governance/role-based-operating-model
jules-1168663913887856558-73b8a885
jules-gate-2b-mapping-workflow-foundation-4137724387867065330
phase-1b1-audit-report-7365389436990903239
phase-2a-backend-foundation-10237597358138521261
phase1b2-postmerge-metadata-fix
refactor/drg-parent-year-id-15088443704684163304
source-data-cleanup
| 1 | import type { |
| 2 | ApproveResult, |
| 3 | BuildResult, |
| 4 | CompanyResult, |
| 5 | DrillDownResult, |
| 6 | LedgerImportResult, |
| 7 | MappingImportResult, |
| 8 | MappingResult, |
| 9 | NoteResult, |
| 10 | NoteRole, |
| 11 | NoteSummary, |
| 12 | PreflightResult, |
| 13 | StatementsResult, |
| 14 | UnknownAccountsResult, |
| 15 | } from './types' |
| 16 | import { authHeaders, clearSession, setSession, type Identity } from './session' |
| 17 | |
| 18 | const BASE = '/api/v1' |
| 19 | |
| 20 | export class ApiError extends Error { |
| 21 | status: number |
| 22 | constructor(status: number, message: string) { |
| 23 | super(message) |
| 24 | this.status = status |
| 25 | this.name = 'ApiError' |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | async function request<T>(path: string, init?: RequestInit): Promise<T> { |
| 30 | const res = await fetch(`${BASE}${path}`, { |
| 31 | ...init, |
| 32 | headers: { ...authHeaders(), ...(init?.headers as Record<string, string> | undefined) }, |
| 33 | }) |
| 34 | if (res.status === 401) { |
| 35 | // The session is gone (expired, revoked, or never existed): drop it so the |
| 36 | // UI returns to the login screen instead of retrying with a dead token. |
| 37 | clearSession() |
| 38 | } |
| 39 | if (!res.ok) { |
| 40 | let detail = `HTTP ${res.status}` |
| 41 | try { |
| 42 | const body = (await res.json()) as { detail?: unknown } |
| 43 | if (body.detail !== undefined) { |
| 44 | detail = typeof body.detail === 'string' ? body.detail : JSON.stringify(body.detail) |
| 45 | } |
| 46 | } catch { |
| 47 | /* keep default detail */ |
| 48 | } |
| 49 | throw new ApiError(res.status, detail) |
| 50 | } |
| 51 | return (await res.json()) as T |
| 52 | } |
| 53 | |
| 54 | /** Download a file with the session attached (a plain <a href> cannot send it). */ |
| 55 | async function download(path: string, filename: string): Promise<void> { |
| 56 | const res = await fetch(`${BASE}${path}`, { headers: authHeaders() }) |
| 57 | if (res.status === 401) clearSession() |
| 58 | if (!res.ok) throw new ApiError(res.status, `HTTP ${res.status}`) |
| 59 | const url = URL.createObjectURL(await res.blob()) |
| 60 | try { |
| 61 | const link = document.createElement('a') |
| 62 | link.href = url |
| 63 | link.download = filename |
| 64 | document.body.appendChild(link) |
| 65 | link.click() |
| 66 | link.remove() |
| 67 | } finally { |
| 68 | URL.revokeObjectURL(url) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | function json(body: unknown): RequestInit { |
| 73 | return { |
| 74 | method: 'POST', |
| 75 | headers: { 'Content-Type': 'application/json' }, |
| 76 | body: JSON.stringify(body), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | function multipart(file: File): RequestInit { |
| 81 | const form = new FormData() |
| 82 | form.append('file', file) |
| 83 | return { method: 'POST', body: form } |
| 84 | } |
| 85 | |
| 86 | export const api = { |
| 87 | createCompany: (code: string, name: string, year: number) => |
| 88 | request<CompanyResult>('/companies', json({ code, name, year })), |
| 89 | |
| 90 | uploadLedger: (yearId: number, file: File) => |
| 91 | request<LedgerImportResult>(`/years/${yearId}/ledger`, multipart(file)), |
| 92 | |
| 93 | createNote: ( |
| 94 | yearId: number, |
| 95 | noteNumber: string, |
| 96 | title: string, |
| 97 | statement: string, |
| 98 | section: string, |
| 99 | role?: NoteRole, |
| 100 | ) => |
| 101 | request<NoteResult>( |
| 102 | `/years/${yearId}/notes`, |
| 103 | json({ note_number: noteNumber, title, statement, section, ...(role ? { role } : {}) }), |
| 104 | ), |
| 105 | |
| 106 | createMapping: (yearId: number, accountCode: string, targetNoteId: number) => |
| 107 | request<MappingResult>(`/years/${yearId}/mappings`, json({ account_code: accountCode, target_note_id: targetNoteId })), |
| 108 | |
| 109 | buildStatements: (yearId: number) => request<BuildResult>(`/years/${yearId}/statements/build`, { method: 'POST' }), |
| 110 | |
| 111 | listStatements: (yearId: number) => request<StatementsResult>(`/years/${yearId}/statements`), |
| 112 | |
| 113 | drillDown: (lineId: number) => request<DrillDownResult>(`/statement-lines/${lineId}/drilldown`), |
| 114 | |
| 115 | preflight: (yearId: number) => request<PreflightResult>(`/years/${yearId}/preflight`), |
| 116 | |
| 117 | approve: (yearId: number) => request<ApproveResult>(`/years/${yearId}/approve`, { method: 'POST' }), |
| 118 | |
| 119 | unknownAccounts: (yearId: number) => request<UnknownAccountsResult>(`/years/${yearId}/unknown-accounts`), |
| 120 | |
| 121 | importMappings: (yearId: number, file: File) => |
| 122 | request<MappingImportResult>(`/years/${yearId}/mappings/import`, multipart(file)), |
| 123 | |
| 124 | downloadMappings: (yearId: number) => |
| 125 | download(`/years/${yearId}/mappings/export`, `mappings-${yearId}.xlsx`), |
| 126 | |
| 127 | downloadStatements: (yearId: number, format: 'xlsx' | 'json') => |
| 128 | download(`/years/${yearId}/statements/export?format=${format}`, `statements-${yearId}.${format}`), |
| 129 | |
| 130 | listNotes: (yearId: number) => |
| 131 | request<{ notes: (NoteSummary & { role: NoteRole | null })[] }>(`/years/${yearId}/notes`), |
| 132 | |
| 133 | listMappings: (yearId: number) => |
| 134 | request<{ mappings: { mapping_id: number; account_code: string; target_note_id: number; mapping_type: string }[] }>( |
| 135 | `/years/${yearId}/mappings`, |
| 136 | ), |
| 137 | |
| 138 | updateNote: ( |
| 139 | yearId: number, |
| 140 | noteId: number, |
| 141 | patch: { title?: string; note_number?: string; statement?: string; section?: string; role?: NoteRole }, |
| 142 | ) => |
| 143 | request<{ note_id: number; note_number: string; title: string }>(`/years/${yearId}/notes/${noteId}`, { |
| 144 | method: 'PUT', |
| 145 | headers: { 'Content-Type': 'application/json' }, |
| 146 | body: JSON.stringify(patch), |
| 147 | }), |
| 148 | |
| 149 | deleteNote: (yearId: number, noteId: number) => |
| 150 | request<{ deleted: number }>(`/years/${yearId}/notes/${noteId}`, { method: 'DELETE' }), |
| 151 | |
| 152 | deleteMapping: (yearId: number, mappingId: number) => |
| 153 | request<{ deleted: number }>(`/years/${yearId}/mappings/${mappingId}`, { method: 'DELETE' }), |
| 154 | |
| 155 | login: async (username: string, password: string): Promise<Identity> => { |
| 156 | const res = await request<Identity & { token: string }>('/auth/login', json({ username, password })) |
| 157 | const who: Identity = { username: res.username, display_name: res.display_name, role: res.role } |
| 158 | setSession(res.token, who) |
| 159 | return who |
| 160 | }, |
| 161 | |
| 162 | logout: async (): Promise<void> => { |
| 163 | try { |
| 164 | await request<{ logged_out: boolean }>('/auth/logout', { method: 'POST' }) |
| 165 | } finally { |
| 166 | clearSession() |
| 167 | } |
| 168 | }, |
| 169 | |
| 170 | me: () => request<Identity>('/auth/me'), |
| 171 | |
| 172 | bootstrapAdmin: (username: string, password: string) => |
| 173 | request<{ username: string; role: string }>('/users', json({ username, password })), |
| 174 | |
| 175 | createUser: (username: string, password: string, role: string) => |
| 176 | request<{ username: string; role: string }>('/users', json({ username, password, role })), |
| 177 | |
| 178 | auditVerify: () => request<{ chain_valid: boolean }>('/audit/verify'), |
| 179 | |
| 180 | lockYear: (yearId: number) => |
| 181 | request<{ year_id: number; status: string }>(`/years/${yearId}/lock`, { method: 'POST' }), |
| 182 | |
| 183 | requestReopen: (yearId: number, reason: string) => |
| 184 | request<{ request_id: number; status: string }>(`/years/${yearId}/reopen/request`, json({ reason })), |
| 185 | |
| 186 | approveReopen: (requestId: number) => |
| 187 | request<{ request_id: number; status: string }>(`/reopen/${requestId}/approve`, { method: 'POST' }), |
| 188 | |
| 189 | reopenYear: (yearId: number) => |
| 190 | request<{ year_id: number; status: string }>(`/years/${yearId}/reopen`, { method: 'POST' }), |
| 191 | |
| 192 | backup: (dest: string) => |
| 193 | request<{ dest: string; manifest: { audit_chain_valid: boolean } }>('/backup', json({ dest })), |
| 194 | |
| 195 | verifyBackup: (dest: string) => |
| 196 | request<{ valid: boolean; source_files: number; audit_entries: number }>('/backup/verify', json({ dest })), |
| 197 | } |