Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,152 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import {
|
||||
adminTelephonyActionSchema,
|
||||
buildAdminTelephonyWorkspaceVM,
|
||||
} from "@hivegrid/view-models";
|
||||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
useActionData,
|
||||
useLoaderData,
|
||||
useNavigate,
|
||||
useNavigation,
|
||||
useSubmit,
|
||||
} from "react-router";
|
||||
|
||||
import { AdminTelephonyWorkspacePage } from "~/components/screens/admin/sales/telephony/AdminTelephonyWorkspacePage";
|
||||
import { AdminSalesLoadingPage } from "~/components/screens/admin/sales/AdminSalesLoadingPage";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { runAsAdminTelephonyOperator } from "~/services/admin/admin-telephony-connections.server";
|
||||
import {
|
||||
adminTelephonyWorkspaceHrefs,
|
||||
applyAdminTelephonyWorkspaceAction,
|
||||
readAdminTelephonyWorkspace,
|
||||
} from "~/services/admin/admin-telephony-workspace.server";
|
||||
import { appendCSRFToken } from "~/utils/csrf";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
import { useSaveToast } from "~/hooks/use-save-toast";
|
||||
|
||||
type ActionData = { intent?: string; ok?: true; messageKey?: string; error?: string };
|
||||
|
||||
const TELEPHONY_SUCCESS_KEYS: Readonly<Record<string, string>> = {
|
||||
"save-outcome": "admin.sales.telephony.toast.outcomeSaved",
|
||||
"confirm-match": "admin.sales.telephony.toast.matchConfirmed",
|
||||
"queue-call": "admin.sales.telephony.toast.callQueued",
|
||||
"create-dialer": "admin.sales.telephony.toast.dialerCreated",
|
||||
"next-dialer": "admin.sales.telephony.toast.nextDialerActivated",
|
||||
"finish-dialer": "admin.sales.telephony.toast.dialerEntryFinished",
|
||||
"update-policy": "admin.sales.telephony.toast.policySaved",
|
||||
"update-recording-policy": "admin.sales.telephony.toast.recordingPolicySaved",
|
||||
"confirm-recording-consent": "admin.sales.telephony.toast.recordingConsentConfirmed",
|
||||
};
|
||||
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
const selectedCallId = new URL(request.url).searchParams.get("call");
|
||||
return runAsAdminTelephonyOperator(
|
||||
request,
|
||||
[
|
||||
"admin_sales.phone.call",
|
||||
"admin_sales.communication.read_all",
|
||||
],
|
||||
async (context) => ({
|
||||
vm: buildAdminTelephonyWorkspaceVM(
|
||||
await readAdminTelephonyWorkspace(context, selectedCallId)
|
||||
),
|
||||
hrefs: adminTelephonyWorkspaceHrefs(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
const { t } = await getTranslations(request);
|
||||
await validateCSRFToken(request);
|
||||
const form = await request.formData();
|
||||
const raw = parsePayload(form);
|
||||
if (
|
||||
raw &&
|
||||
typeof raw === "object" &&
|
||||
"intent" in raw &&
|
||||
raw.intent === "queue-call" &&
|
||||
!("idempotencyKey" in raw)
|
||||
) {
|
||||
Object.assign(raw, { idempotencyKey: randomUUID() });
|
||||
}
|
||||
const parsed = adminTelephonyActionSchema.safeParse(raw);
|
||||
if (!parsed.success) {
|
||||
return Response.json(
|
||||
{ error: t("admin.sales.telephony.errors.invalidAction") },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
try {
|
||||
await runAsAdminTelephonyOperator(
|
||||
request,
|
||||
parsed.data.intent === "update-policy"
|
||||
? "admin_sales.integrations.manage"
|
||||
: parsed.data.intent === "update-recording-policy" ||
|
||||
parsed.data.intent === "confirm-recording-consent"
|
||||
? "admin_sales.phone.recording"
|
||||
: "admin_sales.phone.call",
|
||||
(context) =>
|
||||
applyAdminTelephonyWorkspaceAction(context, parsed.data)
|
||||
);
|
||||
return { intent: parsed.data.intent, ok: true, messageKey: TELEPHONY_SUCCESS_KEYS[parsed.data.intent] } satisfies ActionData;
|
||||
} catch {
|
||||
return Response.json(
|
||||
{ error: t("admin.sales.telephony.errors.actionFailed") },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function HydrateFallback(): React.JSX.Element {
|
||||
return <AdminSalesLoadingPage />;
|
||||
}
|
||||
|
||||
export default function AdminTelephonyWorkspaceRoute(): React.JSX.Element {
|
||||
const { t } = useTranslation();
|
||||
const { vm, hrefs } = useLoaderData<typeof loader>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const navigation = useNavigation();
|
||||
const navigate = useNavigate();
|
||||
const submit = useSubmit();
|
||||
|
||||
useSaveToast(actionData?.ok ? actionData : undefined);
|
||||
|
||||
return (
|
||||
<AdminTelephonyWorkspacePage
|
||||
vm={vm}
|
||||
hrefs={hrefs}
|
||||
submitting={navigation.state === "submitting"}
|
||||
errorMessage={actionData?.error ?? null}
|
||||
actions={{
|
||||
onOpenCall: (callId) =>
|
||||
navigate(`?call=${encodeURIComponent(callId)}`),
|
||||
onSubmit: (payload) => {
|
||||
const form = appendCSRFToken(new FormData());
|
||||
form.set(
|
||||
"payload",
|
||||
JSON.stringify({
|
||||
...payload,
|
||||
reason: t(
|
||||
`admin.sales.telephony.audit.${payload.intent.replaceAll("-", "_")}`
|
||||
),
|
||||
})
|
||||
);
|
||||
submit(form, { method: "post" });
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function parsePayload(form: FormData): Record<string, unknown> | null {
|
||||
try {
|
||||
const parsed = JSON.parse(String(form.get("payload") ?? "{}")) as unknown;
|
||||
return parsed && typeof parsed === "object"
|
||||
? (parsed as Record<string, unknown>)
|
||||
: null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user