Support interactive customer transfers for enrolled devices
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
import { useState } from 'react';
|
||||
import { ApiError, record, request, session, tenantPath, type Session, type Tenant } from './api';
|
||||
import { ErrorBox, errorText, useResource } from './resource';
|
||||
import { allowed } from './safety';
|
||||
|
||||
type Transfer = {
|
||||
id: string;
|
||||
state: 'pending' | 'enrolled' | 'ready' | 'completed';
|
||||
tenant: string;
|
||||
asset: string | null;
|
||||
agent: string | null;
|
||||
hostname: string | null;
|
||||
};
|
||||
|
||||
function parseTransfer(value: unknown): Transfer | null {
|
||||
if (value === null) return null;
|
||||
const t = record(value);
|
||||
const { transfer_id: id, target_tenant_id: tenant, state } = t;
|
||||
if (typeof id !== 'string' || typeof tenant !== 'string' ||
|
||||
(state !== 'pending' && state !== 'enrolled' && state !== 'ready' && state !== 'completed')) {
|
||||
throw new Error('Der Server lieferte keinen gültigen Kundenwechsel.');
|
||||
}
|
||||
return {
|
||||
id, tenant, state,
|
||||
asset: typeof t.target_asset_id === 'string' ? t.target_asset_id : null,
|
||||
agent: typeof t.target_agent_id === 'string' ? t.target_agent_id : null,
|
||||
hostname: typeof t.target_hostname === 'string' ? t.target_hostname : null
|
||||
};
|
||||
}
|
||||
|
||||
function transferError(error: unknown) {
|
||||
if (error instanceof ApiError) {
|
||||
const messages: Record<string, string> = {
|
||||
DEVICE_HAS_OPEN_WORK: 'Schließen Sie offene Aufträge und Genehmigungen für dieses Gerät ab.',
|
||||
TRANSFER_ALREADY_PENDING: 'Für dieses Gerät ist bereits ein Kundenwechsel vorgemerkt.',
|
||||
TRANSFER_ALREADY_STARTED: 'Der Agent übernimmt bereits die neue Kopplung.',
|
||||
TRANSFER_AGENT_UPDATE_REQUIRED: 'Dieses Gerät benötigt eine Agentversion mit Kundenwechsel.',
|
||||
TRANSFER_TARGET_NOT_READY: 'Warten Sie auf die erste Inventarmeldung beim Zielkunden.',
|
||||
TRANSFER_SOURCE_INACTIVE: 'Diese Kopplung ist im bisherigen Kunden nicht mehr aktiv.',
|
||||
FORBIDDEN: 'Für den Kundenwechsel fehlen Rechte beim bisherigen Kunden oder beim Zielkunden.',
|
||||
NOT_FOUND: 'Das Gerät oder der Zielkunde ist für diese Sitzung nicht verfügbar.'
|
||||
};
|
||||
return messages[error.code] ?? errorText(error);
|
||||
}
|
||||
return errorText(error);
|
||||
}
|
||||
|
||||
export function DeviceCustomerTransfer({ auth, customer, asset, agent, customers, active, supported }: {
|
||||
auth: Session;
|
||||
customer: string;
|
||||
asset: string;
|
||||
agent: string;
|
||||
customers: Tenant[];
|
||||
active: boolean;
|
||||
supported: boolean;
|
||||
}) {
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
const [target, setTarget] = useState('');
|
||||
const [confirmed, setConfirmed] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const base = tenantPath(customer, '/devices/' + encodeURIComponent(asset) + '/customer-transfer');
|
||||
const query = '?agent_id=' + encodeURIComponent(agent);
|
||||
const status = useResource(async signal => {
|
||||
if (!expanded) return null;
|
||||
const response = record(await request(base + query, { signal, tenantId: customer }));
|
||||
return { transfer: parseTransfer(response.transfer) };
|
||||
}, [base, query, expanded, auth.principal.id], expanded);
|
||||
const destination = useResource(async signal => {
|
||||
if (!target || !expanded || status.data?.transfer) return null;
|
||||
return session(await request('/auth/session', { signal, tenantId: target }));
|
||||
}, [target, expanded, status.data?.transfer?.id, auth.principal.id]);
|
||||
const canEnroll = !!destination.data && allowed(destination.data, 'devices:enroll')
|
||||
&& allowed(destination.data, 'inventory:read');
|
||||
const choices = customers.filter(entry => entry.tenant_id !== customer);
|
||||
const current = status.data?.transfer ?? null;
|
||||
const targetName = choices.find(entry => entry.tenant_id === current?.tenant)?.name;
|
||||
async function submit(suffix: string, body: Record<string, unknown>) {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await request(base + suffix + query, { csrf: auth.csrf_token, tenantId: customer, body });
|
||||
setConfirmed(false);
|
||||
status.reload();
|
||||
} catch (e) {
|
||||
setError(transferError(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return <details onToggle={event => setExpanded(event.currentTarget.open)}>
|
||||
<summary>Kunden wechseln</summary>
|
||||
{expanded && <>
|
||||
<ErrorBox error={error ?? status.error} />
|
||||
{current ? <>
|
||||
{targetName && <p>Zielkunde <strong>{targetName}</strong></p>}
|
||||
{current.state === 'pending' && <>
|
||||
<p role="status">Der Wechsel ist vorgemerkt. Der Agent übernimmt ihn beim nächsten verfügbaren Kontakt.</p>
|
||||
<button disabled={busy} className="quiet"
|
||||
onClick={() => void submit('/' + encodeURIComponent(current.id) + '/cancel', {})}>Vormerkung abbrechen</button>
|
||||
</>}
|
||||
{current.state === 'enrolled' && <p role="status">Die neue Kopplung wartet auf ihre erste Inventarmeldung.</p>}
|
||||
{current.state === 'ready' && <>
|
||||
<p role="status"><strong>{current.hostname}</strong> hat sich beim Zielkunden gemeldet.</p>
|
||||
<label className="check"><input type="checkbox" checked={confirmed} onChange={e => setConfirmed(e.target.checked)} />
|
||||
Ich bestätige den Kundenwechsel für dieses Gerät.
|
||||
</label>
|
||||
<button className="primary" disabled={busy || !confirmed}
|
||||
onClick={() => void submit('/' + encodeURIComponent(current.id) + '/complete', {})}>Kundenwechsel abschließen</button>
|
||||
</>}
|
||||
{current.state === 'completed' && <p role="status">Kundenwechsel abgeschlossen. Die frühere Kopplung steht in der Historie.</p>}
|
||||
{current.asset && current.agent && ['ready', 'completed'].includes(current.state) &&
|
||||
<a href={'#geraete/' + [current.tenant, current.asset, current.agent].map(encodeURIComponent).join('/')}>
|
||||
Neue Kopplung öffnen
|
||||
</a>}
|
||||
</> : status.data && (active ? supported ? choices.length ? <>
|
||||
<p>Der Agent registriert sich beim Zielkunden. Inventar und Aufträge der bisherigen Kopplung bleiben in dessen Historie.
|
||||
Gerätegebundene Administratorrechte brauchen eine neue Bindung.</p>
|
||||
<form onSubmit={e => {
|
||||
e.preventDefault();
|
||||
if (!busy && canEnroll && target) void submit('', { target_tenant_id: target });
|
||||
}}>
|
||||
<label>Zielkunde<select aria-label="Zielkunde" value={target} onChange={e => setTarget(e.target.value)} required disabled={busy}>
|
||||
<option value="">Kunden auswählen</option>
|
||||
{choices.map(entry => <option key={entry.tenant_id} value={entry.tenant_id}>{entry.name}</option>)}
|
||||
</select></label>
|
||||
{target && (destination.error || destination.data && !canEnroll) &&
|
||||
<p>Für diesen Zielkunden fehlt das Recht zur Geräteaufnahme oder zum Lesen des Inventars.</p>}
|
||||
<button className="primary" disabled={busy || !canEnroll}>Kundenwechsel vormerken</button>
|
||||
</form>
|
||||
</> : <p>Für diese Sitzung ist kein weiterer Kunde verfügbar.</p>
|
||||
: <p>Der Agent benötigt ein Update für den Kundenwechsel.</p>
|
||||
: <p>Für einen Kundenwechsel ist eine aktive Kopplung erforderlich.</p>)}
|
||||
</>}
|
||||
</details>;
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import { allowed } from './safety';
|
||||
import { CatalogCheckNotice } from './catalog-check';
|
||||
import { devicePlatform } from './script-catalog';
|
||||
import { DeviceInventoryPanels, systemDetails } from './device-inventory';
|
||||
import { DeviceCustomerTransfer } from './device-customer-transfer';
|
||||
import { ErrorBox, errorText, useResource } from './resource';
|
||||
import { NavIcon, orderDetailRoute } from './navigation';
|
||||
import { orderTitle, Status } from './order-presentation';
|
||||
@@ -146,6 +147,7 @@ function DeviceDetail({
|
||||
asset,
|
||||
agent,
|
||||
auth,
|
||||
customers,
|
||||
back,
|
||||
openOperation,
|
||||
changed
|
||||
@@ -154,6 +156,7 @@ function DeviceDetail({
|
||||
asset: string;
|
||||
agent: string;
|
||||
auth: Session;
|
||||
customers: Tenant[];
|
||||
back: () => void;
|
||||
openOperation: (id: string, jobId: string) => void;
|
||||
changed: (notice?: string) => void;
|
||||
@@ -277,6 +280,9 @@ function DeviceDetail({
|
||||
<details open={managing} onToggle={e => setManaging(e.currentTarget.open)}>
|
||||
<summary>Gerät verwalten</summary>
|
||||
<ErrorBox error={error} />
|
||||
<DeviceCustomerTransfer auth={scoped} customer={customer} asset={asset} agent={agent}
|
||||
customers={customers} active={d.lifecycle === 'current'}
|
||||
supported={Array.isArray(d.capabilities) && d.capabilities.includes('device.customer_transfer/1')} />
|
||||
{['archived', 'current'].includes(String(d.lifecycle)) && (
|
||||
<button
|
||||
disabled={busy}
|
||||
@@ -437,6 +443,7 @@ export function Devices({
|
||||
key={detailPath}
|
||||
{...target}
|
||||
auth={auth}
|
||||
customers={customers}
|
||||
back={back}
|
||||
openOperation={(id, jobId) => onOperation(target.customer, id, jobId)}
|
||||
changed={(notice) => {
|
||||
|
||||
Reference in New Issue
Block a user