fix(web): crypto.randomUUID indisponivel fora de contexto seguro quebrava a transmissao

Acesso pelo IP da rede (http://192.168...) nao e contexto seguro e o
navegador nao expoe crypto.randomUUID, derrubando a conclusao do pedido.
Helper randomUUID() cai para UUID v4 via crypto.getRandomValues.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-22 18:19:41 +00:00
parent 36430fde63
commit d5279576d7
3 changed files with 16 additions and 2 deletions

View File

@@ -49,6 +49,7 @@ import { useCondicoesComerciais } from '../../lib/condicoes-comerciais';
import { CondicaoTag } from '../../components/CondicaoTag'; import { CondicaoTag } from '../../components/CondicaoTag';
import { apiFetch } from '../../lib/api-client'; import { apiFetch } from '../../lib/api-client';
import { enqueueOrder } from '../../lib/offline/order-queue'; import { enqueueOrder } from '../../lib/offline/order-queue';
import { randomUUID } from '../../lib/uuid';
const { Title, Text } = Typography; const { Title, Text } = Typography;
const { useBreakpoint } = Grid; const { useBreakpoint } = Grid;
@@ -1418,7 +1419,7 @@ export function NewOrderPage() {
descontoPerc: 0, descontoPerc: 0,
obs: obsCompleta || undefined, obs: obsCompleta || undefined,
endEntrega: endEntregaValue, endEntrega: endEntregaValue,
idempotencyKey: crypto.randomUUID(), idempotencyKey: randomUUID(),
itens: cart.map((it, idx) => ({ itens: cart.map((it, idx) => ({
idProduto: it.idProduto, idProduto: it.idProduto,
codProduto: it.codProduto, codProduto: it.codProduto,

View File

@@ -5,6 +5,7 @@
import type { CreatePedido } from '@sar/api-interface'; import type { CreatePedido } from '@sar/api-interface';
import { idbGetAll, idbPut, idbDelete, STORE_PENDING_ORDERS } from './idb'; import { idbGetAll, idbPut, idbDelete, STORE_PENDING_ORDERS } from './idb';
import { randomUUID } from '../uuid';
export interface PendingOrder { export interface PendingOrder {
idempotencyKey: string; // keyPath do IndexedDB idempotencyKey: string; // keyPath do IndexedDB
@@ -23,7 +24,7 @@ export async function enqueueOrder(
payload: CreatePedido, payload: CreatePedido,
clienteNome: string, clienteNome: string,
): Promise<PendingOrder> { ): Promise<PendingOrder> {
const key = payload.idempotencyKey ?? crypto.randomUUID(); const key = payload.idempotencyKey ?? randomUUID();
const order: PendingOrder = { const order: PendingOrder = {
idempotencyKey: key, idempotencyKey: key,
payload: { ...payload, idempotencyKey: key }, payload: { ...payload, idempotencyKey: key },

12
apps/web/src/lib/uuid.ts Normal file
View File

@@ -0,0 +1,12 @@
// crypto.randomUUID só existe em contexto seguro (HTTPS/localhost). Acessando o
// dev server pelo IP da rede (http://192.168...) ela é undefined — fallback gera
// UUID v4 com crypto.getRandomValues, disponível em qualquer contexto.
export function randomUUID(): string {
if (typeof crypto.randomUUID === 'function') return crypto.randomUUID();
const bytes = crypto.getRandomValues(new Uint8Array(16));
bytes[6] = (bytes[6] & 0x0f) | 0x40; // versão 4
bytes[8] = (bytes[8] & 0x3f) | 0x80; // variante RFC 4122
const hex = [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}