Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { GTM_EVENTS_API_SECRET_KEY, GTM_URL } from "@carbon/env";
import { getLogger } from "@carbon/logger";

const log = getLogger("stripe", "gtm-events");

// The forward runs as background work that keeps a serverless instance up, so
// a GTM that never answers must not hold it until the function times out.
const TIMEOUT_MS = 10_000;

export async function forwardToGtm(
  type: string,
  metadata: Record<string, unknown>
): Promise<void> {
  if (!GTM_URL || !GTM_EVENTS_API_SECRET_KEY) {
    log.error("missing GTM_URL or GTM_EVENTS_API_SECRET_KEY");
    return;
  }

  try {
    const res = await fetch(`${GTM_URL}/api/events`, {
      method: "POST",
      headers: {
        "content-type": "application/json",
        "x-gtm-events-key": GTM_EVENTS_API_SECRET_KEY
      },
      body: JSON.stringify({ type, metadata }),
      signal: AbortSignal.timeout(TIMEOUT_MS)
    });

    if (!res.ok) {
      const body = await res.text().catch(() => "");
      log.error("request failed", { status: res.status, body });
    }
  } catch (err) {
    log.error("request failed", { error: err });
  }
}