acc_invoices: роль admin и переключатель роли CFO/accountant/employee; Keycloak: фирменный стиль KH Build
All checks were successful
ci / test (push) Successful in 55s
ci / deploy (push) Successful in 3m13s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-04 14:36:45 +03:00
parent d79ebfda10
commit b878aa72bb
9 changed files with 126 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
import * as Ariakit from "@ariakit/react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { ApiError, api, getToken, type Detail, type Line, type Lookups, type Me, type Row } from "./api.js";
import { ApiError, api, getActing, getToken, setActing, type Detail, type Line, type Lookups, type Me, type Row } from "./api.js";
import { FormDialog } from "./FormDialog.js";
import { Login } from "./Login.js";
import { finishLogin, logout, relogin } from "./oidc.js";
@@ -105,7 +105,15 @@ function Main({ me, onLogout }: { me: Me; onLogout: () => void }) {
</Ariakit.TabList>
<input className="search" type="search" placeholder="Поиск: вендор, номер, проект, сумма" value={q} onChange={(e) => setQ(e.target.value)} aria-label="Поиск" />
<span className="spacer" />
<span className="who" title={me.roles.join(", ") || "сотрудник"}>{me.name}</span>
<span className="who" title={"роли: " + (me.real_roles.join(", ") || "сотрудник")}>{me.name}</span>
{me.can_switch && (
<select className="role-switch" aria-label="Роль для отладки и тестирования" title="Роль (отладка и тестирование)" value={me.acting_role}
onChange={(e) => { setActing(e.target.value); location.reload(); }}>
<option value="cfo">CFO</option>
<option value="accountant">accountant</option>
<option value="employee">employee</option>
</select>
)}
<button className="btn" onClick={onLogout}>Выйти</button>
<button className="btn primary" disabled={!lookups} onClick={() => openForm(null, [])}>Create</button>
</Ariakit.Toolbar>

View File

@@ -1,4 +1,4 @@
export interface Me { person_id: string; name: string; roles: string[]; is_staff: boolean; filters: { key: string; label: string }[] }
export interface Me { person_id: string; name: string; roles: string[]; real_roles: string[]; can_switch: boolean; acting_role: string; is_staff: boolean; filters: { key: string; label: string }[] }
export interface Row {
id: string; invoice_number: string | null; vendor_name: string; vendor_qbo_id: string | null; invoice_date: string | null; amount: number;
project_customer_id: string | null; project_name: string | null; contract_id: string | null; pay_method: string | null;
@@ -20,6 +20,9 @@ export interface Lookups { projects: Named[]; vendors: Named[]; people: Named[];
import { freshToken, hasSession } from "./oidc.js";
export const getToken = (): string => (hasSession() ? "1" : "");
const AK = "acc_invoices_acting_role";
export const getActing = (): string => { try { return sessionStorage.getItem(AK) ?? ""; } catch { return ""; } };
export const setActing = (r: string): void => { try { sessionStorage.setItem(AK, r); } catch { /* ignore */ } };
export class ApiError extends Error {
constructor(message: string, readonly status: number, readonly problems?: string[]) { super(message); }
@@ -29,7 +32,7 @@ export class ApiError extends Error {
export async function api<T = unknown>(path: string, init: { method?: string; body?: unknown } = {}): Promise<T> {
const r = await fetch(`api/${path}`, {
method: init.method ?? "GET",
headers: { Authorization: `Bearer ${await freshToken()}`, ...(init.body !== undefined ? { "content-type": "application/json" } : {}) },
headers: { Authorization: `Bearer ${await freshToken()}`, ...(getActing() ? { "x-acting-role": getActing() } : {}), ...(init.body !== undefined ? { "content-type": "application/json" } : {}) },
body: init.body !== undefined ? JSON.stringify(init.body) : undefined,
});
const text = await r.text();

View File

@@ -23,6 +23,7 @@ body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 syst
.search { min-width: 240px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--text); }
.spacer { flex: 1; }
.who { color: var(--muted); }
.role-switch { padding: 5px 8px; border: 1px solid var(--accent); border-radius: 8px; background: var(--panel); color: var(--text); font: inherit; }
.btn { border: 1px solid var(--line); background: var(--panel); color: var(--text); padding: 6px 14px; border-radius: 8px; cursor: pointer; font: inherit; text-decoration: none; display: inline-block; }
.btn:hover:not(:disabled) { border-color: var(--accent); }