79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
// Per-workflow ZinoClient singletons. app 434 has three workflows, each with
|
|
// its own workflow_uuid, so each needs its own client (ZinoClient binds one
|
|
// workflow uuid at construction). Import the one you need, or use clientFor().
|
|
import { ZinoClient } from './client';
|
|
import { BASE_URL, WORKFLOWS } from './config';
|
|
|
|
export type WorkflowSlug = keyof typeof WORKFLOWS;
|
|
|
|
const cache = new Map<WorkflowSlug, ZinoClient>();
|
|
|
|
/** Shared client for a workflow (memoized so the JWT/session is reused). */
|
|
export function clientFor(slug: WorkflowSlug): ZinoClient {
|
|
let c = cache.get(slug);
|
|
if (!c) {
|
|
c = new ZinoClient(BASE_URL, WORKFLOWS[slug].workflowUuid);
|
|
cache.set(slug, c);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
export const orderBookingClient = clientFor('orderBooking');
|
|
export const storeClient = clientFor('store');
|
|
export const dailyReportsClient = clientFor('dailyReports');
|
|
export const userClient = clientFor('user');
|
|
|
|
// The session (access token + refresh cookie) is shared: every client reads the
|
|
// token from the one session client, so a refresh reaches all of them at once.
|
|
const ALL = [orderBookingClient, storeClient, dailyReportsClient, userClient];
|
|
|
|
import { changeExpiredPassword } from './auth';
|
|
import { sessionClient } from './session';
|
|
|
|
/** Log in once; the session is shared by every workflow client. */
|
|
export async function loginAll(email: string, password: string, orgId?: string) {
|
|
const res = await orderBookingClient.login(email, password, orgId);
|
|
try {
|
|
const me = await orderBookingClient.getMe();
|
|
res.user = { ...res.user, ...me };
|
|
} catch (e) {
|
|
console.error('Failed to fetch user profile:', e);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/** Complete a forced password change during login and seed all clients. */
|
|
export async function completePasswordChangeAll(
|
|
email: string,
|
|
currentPassword: string,
|
|
newPassword: string,
|
|
orgId?: string
|
|
) {
|
|
const res = await changeExpiredPassword(email, currentPassword, newPassword, orgId);
|
|
sessionClient.adopt(res);
|
|
ALL.forEach((c) => c.setUser(res.user));
|
|
try {
|
|
const me = await orderBookingClient.getMe();
|
|
res.user = { ...res.user, ...me };
|
|
} catch (e) {
|
|
console.error('Failed to fetch user profile:', e);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/** End the session: cleared locally at once, then revoked on the server. */
|
|
export function logoutAll(): Promise<void> {
|
|
ALL.forEach((c) => c.setUser(null));
|
|
return orderBookingClient.logout();
|
|
}
|
|
|
|
/** Current shared token (null when logged out). */
|
|
export function currentToken(): string | null {
|
|
return sessionClient.getAccessToken();
|
|
}
|
|
|
|
/** Register a 401 handler on every client (e.g. to bounce back to login). */
|
|
export function onAuthErrorAll(fn: () => void): void {
|
|
ALL.forEach((c) => c.setAuthErrorHandler(fn));
|
|
}
|