import { readFile } from "node:fs/promises"; type TokenResponse = { jwtToken: string }; function required(name: string): string { const value = process.env[name]; if (!value) throw new Error(`Missing required environment variable ${name}.`); return value; } const identityUrl = process.env.DECISIOQ_IDENTITY_URL ?? "https://identity.vinquery.com/connect/token"; const catalogUrl = process.env.DECISIOQ_DKS_URL ?? "https://dks.vinquery.com"; const decisionServiceUrl = process.env.DECISIOQ_DDE_URL ?? "https://dde.vinquery.com"; const correlationId = `auto-auct-044-ts-${Date.now()}`; const tokenResponse = await fetch(identityUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ clientId: required("DECISIOQ_CLIENT_ID"), clientSecret: required("DECISIOQ_CLIENT_SECRET"), audience: process.env.DECISIOQ_AUDIENCE ?? "vinquery:api:decisioq" }) }); if (!tokenResponse.ok) throw new Error(`Token request failed: ${tokenResponse.status}`); const token = (await tokenResponse.json()) as TokenResponse; const detailResponse = await fetch(`${catalogUrl.replace(/\/$/, "")}/decisioncatalog/decisions/AUTO-AUCT-044`, { headers: { Authorization: `Bearer ${token.jwtToken}`, "X-Correlation-Id": correlationId } }); if (!detailResponse.ok) throw new Error(`Decision detail failed: ${detailResponse.status}`); const payload = await readFile("auto-auct-044-execute.json", "utf8"); const executionResponse = await fetch(`${decisionServiceUrl.replace(/\/$/, "")}/api/v1/decide`, { method: "POST", headers: { Authorization: `Bearer ${token.jwtToken}`, "Content-Type": "application/json", "X-Correlation-Id": correlationId }, body: payload }); console.log(await executionResponse.text()); if (!executionResponse.ok) throw new Error(`Decision execution failed: ${executionResponse.status}`);