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.5 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 type { Database } from "@carbon/database";
import type { SupabaseClient } from "@supabase/supabase-js";
import type { OwnerPermissions } from "../engine/owner";

export interface DispatchContext {
  client: SupabaseClient<Database>;
  companyId: string;
  companyGroupId: string;
  userId: string;
  /** The owner's permissions, read for this run. The API gate checks the
   *  operation's own permission against them. */
  permissions: OwnerPermissions;
}

// Deliberately loose: the dispatcher widens `success` to boolean, so a narrower
// union here would force a cast at the one registration site.
export interface DispatchResult {
  success: boolean;
  data?: unknown;
  error?: unknown;
}

// Structurally satisfied by the ERP app's `executeFunction`, so the dependency
// points from the app into this package and never the other way.
export type WorkflowDispatch = (
  functionName: string,
  context: DispatchContext,
  args?: Record<string, unknown> | string
) => Promise<DispatchResult>;

let dispatch: WorkflowDispatch | undefined;

/** The ERP app supplies this at boot; packages/jobs cannot import ~/modules itself. */
export function setWorkflowDispatch(fn: WorkflowDispatch): void {
  dispatch = fn;
}

export function getWorkflowDispatch(): WorkflowDispatch | undefined {
  return dispatch;
}