58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { getDbClient } from "@hivegrid/db/client.server";
|
|
import { publishOutboxEvent, type OutboxPublisherTransaction } from "@hivegrid/db/outbox";
|
|
import { emitPluginHook, PluginHookValidationError, type PluginHookDefinition } from "@hivegrid/plugin-runtime";
|
|
import { apiLogger } from "./logger.js";
|
|
|
|
const pluginHookLogger = apiLogger.child?.({ component: "plugin-hook-publisher" }) ?? apiLogger;
|
|
|
|
export async function emitPluginHookFireAndForget<TPayload>(
|
|
def: PluginHookDefinition<TPayload>,
|
|
payload: TPayload,
|
|
options: {
|
|
tenantId?: string | null;
|
|
dispatchKey?: string | null;
|
|
} = {},
|
|
): Promise<void> {
|
|
try {
|
|
const db = getDbClient() as unknown as {
|
|
$transaction: <T>(runner: (tx: OutboxPublisherTransaction) => Promise<T>) => Promise<T>;
|
|
};
|
|
await db.$transaction(async (tx) => {
|
|
await emitPluginHook(
|
|
tx,
|
|
def,
|
|
payload,
|
|
async (innerTx, args) => {
|
|
await publishOutboxEvent(innerTx as OutboxPublisherTransaction, {
|
|
tenantId: args.tenantId,
|
|
type: args.type,
|
|
payload: args.payload as unknown as Parameters<typeof publishOutboxEvent>[1]["payload"],
|
|
dispatchKey: args.dispatchKey,
|
|
});
|
|
},
|
|
options,
|
|
);
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof PluginHookValidationError) {
|
|
pluginHookLogger.warn(
|
|
{
|
|
hookName: def.name,
|
|
tenantId: options.tenantId ?? null,
|
|
issues: error.issues,
|
|
},
|
|
"plugin.hook.payload_invalid",
|
|
);
|
|
return;
|
|
}
|
|
pluginHookLogger.error(
|
|
{
|
|
hookName: def.name,
|
|
tenantId: options.tenantId ?? null,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
},
|
|
"plugin.hook.emit_failed",
|
|
);
|
|
}
|
|
}
|