Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+12
-7
@@ -83,6 +83,7 @@ vi.mock("~/utils/services/apiService", () => ({
|
||||
usage: apiAccessValidation.tenantApiKey?.usage,
|
||||
};
|
||||
},
|
||||
runWithWorkflowApiRlsContext: async (_apiAccessValidation: unknown, callback: () => unknown) => callback(),
|
||||
setWorkflowApiKeyLogStatus: async (
|
||||
apiAccessValidation: { apiKeyLog?: { id: string; tenantId?: string }; tenant?: { id: string } | null } | undefined,
|
||||
data: Record<string, unknown>
|
||||
@@ -132,6 +133,9 @@ const translated = {
|
||||
|
||||
function resetIdempotencyStore() {
|
||||
type StoredIdempotencyRow = {
|
||||
tenantId: string | null;
|
||||
partitionKey: string;
|
||||
systemOwned: boolean;
|
||||
scope: string;
|
||||
key: string;
|
||||
requestHash: string;
|
||||
@@ -142,14 +146,14 @@ function resetIdempotencyStore() {
|
||||
expiresAt: Date | null;
|
||||
};
|
||||
const rows = new Map<string, StoredIdempotencyRow>();
|
||||
const rowKey = (scope: string, key: string) => `${scope}:${key}`;
|
||||
const rowKey = (partitionKey: string, scope: string, key: string) => `${partitionKey}:${scope}:${key}`;
|
||||
|
||||
mocks.idempotencyKey.findUnique.mockImplementation(async ({ where }) => {
|
||||
const { scope, key } = where.idempotency_key_scope_key;
|
||||
return rows.get(rowKey(scope, key)) ?? null;
|
||||
const { partitionKey, scope, key } = where.idempotency_key_partition_scope_key;
|
||||
return rows.get(rowKey(partitionKey, scope, key)) ?? null;
|
||||
});
|
||||
mocks.idempotencyKey.create.mockImplementation(async ({ data }) => {
|
||||
const key = rowKey(data.scope, data.key);
|
||||
const key = rowKey(data.partitionKey, data.scope, data.key);
|
||||
if (rows.has(key)) {
|
||||
throw Object.assign(new Error("Unique constraint failed"), { code: "P2002" });
|
||||
}
|
||||
@@ -158,17 +162,18 @@ function resetIdempotencyStore() {
|
||||
return row;
|
||||
});
|
||||
mocks.idempotencyKey.update.mockImplementation(async ({ where, data }) => {
|
||||
const { scope, key } = where.idempotency_key_scope_key;
|
||||
const existing = rows.get(rowKey(scope, key));
|
||||
const { partitionKey, scope, key } = where.idempotency_key_partition_scope_key;
|
||||
const existing = rows.get(rowKey(partitionKey, scope, key));
|
||||
if (!existing) {
|
||||
throw new Error("Idempotency row not found");
|
||||
}
|
||||
const row = { ...existing, ...data };
|
||||
rows.set(rowKey(scope, key), row);
|
||||
rows.set(rowKey(partitionKey, scope, key), row);
|
||||
return row;
|
||||
});
|
||||
mocks.idempotencyKey.deleteMany.mockImplementation(async ({ where } = {}) => {
|
||||
for (const [key, row] of rows.entries()) {
|
||||
if (where?.partitionKey && row.partitionKey !== where.partitionKey) continue;
|
||||
if (where?.scope && row.scope !== where.scope) continue;
|
||||
if (where?.key && row.key !== where.key) continue;
|
||||
if (where?.expiresAt?.lte && (!row.expiresAt || row.expiresAt > where.expiresAt.lte)) continue;
|
||||
|
||||
+34
-36
@@ -1,4 +1,5 @@
|
||||
import { CatalogError, foundationErrors, type IdempotencyClient, type JsonValue, withIdempotency } from "@hivegrid/foundation";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { ActionFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import WorkflowsExecutionsService from "~/modules/workflowEngine/services/WorkflowsExecutionsService";
|
||||
@@ -51,49 +52,46 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const idempotencyKey = getIdempotencyKey(request, t);
|
||||
const input = body.input && typeof body.input === "object" && !Array.isArray(body.input) ? body.input : null;
|
||||
const userId = typeof body.userId === "string" && body.userId.length > 0 ? body.userId : null;
|
||||
const idempotency = await withIdempotency(
|
||||
{
|
||||
scope: `workflow-continuation:tenant:${tenantId}`,
|
||||
key: idempotencyKey,
|
||||
fingerprint: {
|
||||
method: request.method,
|
||||
workflowId,
|
||||
executionId: params.id!,
|
||||
return await runWithRlsContext({ tenantId, userId: userId ?? undefined }, async () => {
|
||||
const idempotency = await withIdempotency(
|
||||
{
|
||||
tenantId,
|
||||
body: toJsonValue(body),
|
||||
},
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
},
|
||||
db as unknown as IdempotencyClient,
|
||||
async () => {
|
||||
const execution = await WorkflowsExecutionsService.continueExecution(params.id!, {
|
||||
type: body.type,
|
||||
input,
|
||||
session: {
|
||||
scope: `workflow-continuation:tenant:${tenantId}`,
|
||||
key: idempotencyKey,
|
||||
fingerprint: {
|
||||
method: request.method,
|
||||
workflowId,
|
||||
executionId: params.id!,
|
||||
tenantId,
|
||||
userId,
|
||||
body: toJsonValue(body),
|
||||
},
|
||||
});
|
||||
return {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: { execution: toJsonValue(execution) },
|
||||
};
|
||||
}
|
||||
);
|
||||
return Response.json(idempotency.body ?? {}, { status: idempotency.status, headers: idempotency.headers });
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
},
|
||||
db as unknown as IdempotencyClient,
|
||||
async () => {
|
||||
const execution = await WorkflowsExecutionsService.continueExecution(params.id!, {
|
||||
type: body.type,
|
||||
input,
|
||||
session: {
|
||||
tenantId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
return {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: { execution: toJsonValue(execution) },
|
||||
};
|
||||
}
|
||||
);
|
||||
return Response.json(idempotency.body ?? {}, { status: idempotency.status, headers: idempotency.headers });
|
||||
});
|
||||
} catch (e: any) {
|
||||
if (e instanceof CatalogError && e.code === foundationErrors.idempotencyConflict.code) {
|
||||
return Response.json(
|
||||
{ error: workflowApiText(t, "workflows.errors.idempotencyKeyConflict") },
|
||||
{ status: 409 }
|
||||
);
|
||||
return Response.json({ error: workflowApiText(t, "workflows.errors.idempotencyKeyConflict") }, { status: 409 });
|
||||
}
|
||||
if (e instanceof CatalogError && e.code === foundationErrors.idempotencyInProgress.code) {
|
||||
return Response.json(
|
||||
{ error: workflowApiText(t, "workflows.errors.idempotencyKeyInProgress") },
|
||||
{ status: 409 }
|
||||
);
|
||||
return Response.json({ error: workflowApiText(t, "workflows.errors.idempotencyKeyInProgress") }, { status: 409 });
|
||||
}
|
||||
return Response.json({ error: redactWorkflowApiErrorMessage(request, e) }, { status: e.status ?? 500 });
|
||||
}
|
||||
|
||||
+28
-21
@@ -2,7 +2,12 @@ import { ActionFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import WorkflowsExecutionsService from "~/modules/workflowEngine/services/WorkflowsExecutionsService";
|
||||
import { requireInternalWebApiProxyRequest } from "~/utils/api/internalWebApiProxy.server";
|
||||
import { getWorkflowApiKeyFromRequest, setWorkflowApiKeyLogStatus, type WorkflowApiAccessValidation } from "~/utils/services/apiService";
|
||||
import {
|
||||
getWorkflowApiKeyFromRequest,
|
||||
runWithWorkflowApiRlsContext,
|
||||
setWorkflowApiKeyLogStatus,
|
||||
type WorkflowApiAccessValidation,
|
||||
} from "~/utils/services/apiService";
|
||||
import { getWorkflowErrorAccess, redactWorkflowApiErrorMessage, requireWorkflowTenant, workflowApiText } from "./workflowApiErrors.server";
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
@@ -12,27 +17,29 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
let apiAccessValidation: WorkflowApiAccessValidation | undefined = undefined;
|
||||
try {
|
||||
apiAccessValidation = await getWorkflowApiKeyFromRequest(request, params);
|
||||
if (request.method !== "POST") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const { userId } = apiAccessValidation;
|
||||
let body: { [key: string]: any } = {};
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.invalidJsonBody")), { status: 400 });
|
||||
}
|
||||
const execution = await WorkflowsExecutionsService.queueContinuationExecution(params.id!, {
|
||||
type: "api",
|
||||
input: body,
|
||||
session: {
|
||||
tenantId: tenant.id,
|
||||
userId: userId ?? null,
|
||||
},
|
||||
return await runWithWorkflowApiRlsContext(apiAccessValidation, async () => {
|
||||
if (request.method !== "POST") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const { userId } = apiAccessValidation;
|
||||
let body: { [key: string]: any } = {};
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.invalidJsonBody")), { status: 400 });
|
||||
}
|
||||
const execution = await WorkflowsExecutionsService.queueContinuationExecution(params.id!, {
|
||||
type: "api",
|
||||
input: body,
|
||||
session: {
|
||||
tenantId: tenant.id,
|
||||
userId: userId ?? null,
|
||||
},
|
||||
});
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 202, startTime });
|
||||
return Response.json({ execution, queued: true }, { status: 202 });
|
||||
});
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 202, startTime });
|
||||
return Response.json({ execution, queued: true }, { status: 202 });
|
||||
} catch (e: any) {
|
||||
const status = e.status ?? 500;
|
||||
const error = redactWorkflowApiErrorMessage(request, e);
|
||||
|
||||
+35
-37
@@ -1,4 +1,5 @@
|
||||
import { CatalogError, foundationErrors, type IdempotencyClient, type JsonValue, withIdempotency } from "@hivegrid/foundation";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { ActionFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import WorkflowsExecutionsService from "~/modules/workflowEngine/services/WorkflowsExecutionsService";
|
||||
@@ -51,50 +52,47 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const idempotencyKey = getIdempotencyKey(request, t);
|
||||
const input = body.input && typeof body.input === "object" && !Array.isArray(body.input) ? body.input : null;
|
||||
const userId = typeof body.userId === "string" && body.userId.length > 0 ? body.userId : null;
|
||||
const idempotency = await withIdempotency(
|
||||
{
|
||||
scope: `workflow-execution:tenant:${tenantId}`,
|
||||
key: idempotencyKey,
|
||||
fingerprint: {
|
||||
method: request.method,
|
||||
workflowId,
|
||||
executionId: params.id!,
|
||||
return await runWithRlsContext({ tenantId, userId: userId ?? undefined }, async () => {
|
||||
const idempotency = await withIdempotency(
|
||||
{
|
||||
tenantId,
|
||||
body: toJsonValue(body),
|
||||
},
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
},
|
||||
db as unknown as IdempotencyClient,
|
||||
async () => {
|
||||
const execution = await WorkflowsExecutionsService.executeExisting(params.id!, {
|
||||
workflowId,
|
||||
type: body.type,
|
||||
input,
|
||||
session: {
|
||||
scope: `workflow-execution:tenant:${tenantId}`,
|
||||
key: idempotencyKey,
|
||||
fingerprint: {
|
||||
method: request.method,
|
||||
workflowId,
|
||||
executionId: params.id!,
|
||||
tenantId,
|
||||
userId,
|
||||
body: toJsonValue(body),
|
||||
},
|
||||
});
|
||||
return {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: { execution: toJsonValue(execution) },
|
||||
};
|
||||
}
|
||||
);
|
||||
return Response.json(idempotency.body ?? {}, { status: idempotency.status, headers: idempotency.headers });
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
},
|
||||
db as unknown as IdempotencyClient,
|
||||
async () => {
|
||||
const execution = await WorkflowsExecutionsService.executeExisting(params.id!, {
|
||||
workflowId,
|
||||
type: body.type,
|
||||
input,
|
||||
session: {
|
||||
tenantId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
return {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: { execution: toJsonValue(execution) },
|
||||
};
|
||||
}
|
||||
);
|
||||
return Response.json(idempotency.body ?? {}, { status: idempotency.status, headers: idempotency.headers });
|
||||
});
|
||||
} catch (e: any) {
|
||||
if (e instanceof CatalogError && e.code === foundationErrors.idempotencyConflict.code) {
|
||||
return Response.json(
|
||||
{ error: workflowApiText(t, "workflows.errors.idempotencyKeyConflict") },
|
||||
{ status: 409 }
|
||||
);
|
||||
return Response.json({ error: workflowApiText(t, "workflows.errors.idempotencyKeyConflict") }, { status: 409 });
|
||||
}
|
||||
if (e instanceof CatalogError && e.code === foundationErrors.idempotencyInProgress.code) {
|
||||
return Response.json(
|
||||
{ error: workflowApiText(t, "workflows.errors.idempotencyKeyInProgress") },
|
||||
{ status: 409 }
|
||||
);
|
||||
return Response.json({ error: workflowApiText(t, "workflows.errors.idempotencyKeyInProgress") }, { status: 409 });
|
||||
}
|
||||
return Response.json({ error: redactWorkflowApiErrorMessage(request, e) }, { status: e.status ?? 500 });
|
||||
}
|
||||
|
||||
+19
-12
@@ -2,7 +2,12 @@ import { LoaderFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import WorkflowsService from "~/modules/workflowEngine/services/WorkflowsService";
|
||||
import { requireInternalWebApiProxyRequest } from "~/utils/api/internalWebApiProxy.server";
|
||||
import { getWorkflowApiKeyFromRequest, setWorkflowApiKeyLogStatus, type WorkflowApiAccessValidation } from "~/utils/services/apiService";
|
||||
import {
|
||||
getWorkflowApiKeyFromRequest,
|
||||
runWithWorkflowApiRlsContext,
|
||||
setWorkflowApiKeyLogStatus,
|
||||
type WorkflowApiAccessValidation,
|
||||
} from "~/utils/services/apiService";
|
||||
import { getWorkflowErrorAccess, redactWorkflowApiErrorMessage, requireWorkflowTenant, workflowApiText } from "./workflowApiErrors.server";
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
@@ -12,18 +17,20 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
let apiAccessValidation: WorkflowApiAccessValidation | undefined = undefined;
|
||||
try {
|
||||
apiAccessValidation = await getWorkflowApiKeyFromRequest(request, params);
|
||||
if (request.method !== "GET") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const execution = await WorkflowsService.getExecution(params.id!, {
|
||||
tenantId: tenant.id,
|
||||
return await runWithWorkflowApiRlsContext(apiAccessValidation, async () => {
|
||||
if (request.method !== "GET") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const execution = await WorkflowsService.getExecution(params.id!, {
|
||||
tenantId: tenant.id,
|
||||
});
|
||||
if (!execution) {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.executionNotFound")), { status: 404 });
|
||||
}
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 200, startTime });
|
||||
return Response.json({ execution }, { status: 200 });
|
||||
});
|
||||
if (!execution) {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.executionNotFound")), { status: 404 });
|
||||
}
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 200, startTime });
|
||||
return Response.json({ execution }, { status: 200 });
|
||||
} catch (e: any) {
|
||||
const status = e.status ?? 400;
|
||||
const error = redactWorkflowApiErrorMessage(request, e);
|
||||
|
||||
+27
-20
@@ -2,7 +2,12 @@ import { ActionFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import WorkflowsExecutionsService from "~/modules/workflowEngine/services/WorkflowsExecutionsService";
|
||||
import { requireInternalWebApiProxyRequest } from "~/utils/api/internalWebApiProxy.server";
|
||||
import { getWorkflowApiKeyFromRequest, setWorkflowApiKeyLogStatus, type WorkflowApiAccessValidation } from "~/utils/services/apiService";
|
||||
import {
|
||||
getWorkflowApiKeyFromRequest,
|
||||
runWithWorkflowApiRlsContext,
|
||||
setWorkflowApiKeyLogStatus,
|
||||
type WorkflowApiAccessValidation,
|
||||
} from "~/utils/services/apiService";
|
||||
import { getWorkflowErrorAccess, redactWorkflowApiErrorMessage, requireWorkflowTenant, workflowApiText } from "./workflowApiErrors.server";
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
@@ -12,26 +17,28 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
let apiAccessValidation: WorkflowApiAccessValidation | undefined = undefined;
|
||||
try {
|
||||
apiAccessValidation = await getWorkflowApiKeyFromRequest(request, params);
|
||||
if (request.method !== "POST") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const { userId } = apiAccessValidation;
|
||||
let body: { [key: string]: any } = {};
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.invalidJsonBody")), { status: 400 });
|
||||
}
|
||||
const execution = await WorkflowsExecutionsService.queueApiExecution(params.id!, {
|
||||
input: body,
|
||||
session: {
|
||||
tenantId: tenant.id,
|
||||
userId: userId ?? null,
|
||||
},
|
||||
return await runWithWorkflowApiRlsContext(apiAccessValidation, async () => {
|
||||
if (request.method !== "POST") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const { userId } = apiAccessValidation;
|
||||
let body: { [key: string]: any } = {};
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.invalidJsonBody")), { status: 400 });
|
||||
}
|
||||
const execution = await WorkflowsExecutionsService.queueApiExecution(params.id!, {
|
||||
input: body,
|
||||
session: {
|
||||
tenantId: tenant.id,
|
||||
userId: userId ?? null,
|
||||
},
|
||||
});
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 202, startTime });
|
||||
return Response.json({ execution }, { status: 202 });
|
||||
});
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 202, startTime });
|
||||
return Response.json({ execution }, { status: 202 });
|
||||
} catch (e: any) {
|
||||
const status = e.status ?? 500;
|
||||
const error = redactWorkflowApiErrorMessage(request, e);
|
||||
|
||||
+32
-25
@@ -4,7 +4,12 @@ import WorkflowsService from "~/modules/workflowEngine/services/WorkflowsService
|
||||
import { emitter } from "~/utils/stream/emitter.server";
|
||||
import { eventStream } from "~/utils/stream/event-stream";
|
||||
import { requireInternalWebApiProxyRequest } from "~/utils/api/internalWebApiProxy.server";
|
||||
import { getWorkflowApiKeyFromRequest, setWorkflowApiKeyLogStatus, WorkflowApiAccessValidation } from "~/utils/services/apiService";
|
||||
import {
|
||||
getWorkflowApiKeyFromRequest,
|
||||
runWithWorkflowApiRlsContext,
|
||||
setWorkflowApiKeyLogStatus,
|
||||
WorkflowApiAccessValidation,
|
||||
} from "~/utils/services/apiService";
|
||||
import { redactWorkflowApiErrorMessage, requireWorkflowTenant, workflowApiText } from "./workflowApiErrors.server";
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
@@ -17,32 +22,34 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.executionIdRequired")), { status: 400 });
|
||||
}
|
||||
apiAccessValidation = await getWorkflowApiKeyFromRequest(request, params);
|
||||
if (request.method !== "GET") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const execution = await WorkflowsService.getExecution(params.executionId, {
|
||||
tenantId: tenant.id,
|
||||
});
|
||||
if (!execution) {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.executionNotFound")), { status: 404 });
|
||||
}
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 200, startTime });
|
||||
const eventName = `workflowExecutionUpdate:${params.executionId}`;
|
||||
return eventStream(request.signal, (send) => {
|
||||
// listener handler
|
||||
const listener = (data: string) => {
|
||||
// data should be serialized
|
||||
send({ data });
|
||||
};
|
||||
return await runWithWorkflowApiRlsContext(apiAccessValidation, async () => {
|
||||
if (request.method !== "GET") {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.methodNotAllowed")), { status: 405 });
|
||||
}
|
||||
const tenant = requireWorkflowTenant(apiAccessValidation, t);
|
||||
const execution = await WorkflowsService.getExecution(params.executionId, {
|
||||
tenantId: tenant.id,
|
||||
});
|
||||
if (!execution) {
|
||||
throw Object.assign(new Error(workflowApiText(t, "workflows.errors.executionNotFound")), { status: 404 });
|
||||
}
|
||||
await setWorkflowApiKeyLogStatus(apiAccessValidation, { status: 200, startTime });
|
||||
const eventName = `workflowExecutionUpdate:${params.executionId}`;
|
||||
return eventStream(request.signal, (send) => {
|
||||
// listener handler
|
||||
const listener = (data: string) => {
|
||||
// data should be serialized
|
||||
send({ data });
|
||||
};
|
||||
|
||||
// event listener itself
|
||||
emitter.on(eventName, listener);
|
||||
// event listener itself
|
||||
emitter.on(eventName, listener);
|
||||
|
||||
// cleanup
|
||||
return () => {
|
||||
emitter.off(eventName, listener);
|
||||
};
|
||||
// cleanup
|
||||
return () => {
|
||||
emitter.off(eventName, listener);
|
||||
};
|
||||
});
|
||||
});
|
||||
} catch (e: any) {
|
||||
const status = e.status ?? 400;
|
||||
|
||||
+36
-28
@@ -1,4 +1,5 @@
|
||||
import { CatalogError, foundationErrors, type IdempotencyClient, type JsonValue, withIdempotency } from "@hivegrid/foundation";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { ActionFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import WorkflowsExecutionsService from "~/modules/workflowEngine/services/WorkflowsExecutionsService";
|
||||
@@ -56,37 +57,44 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const tenantId = getTenantId(request, t);
|
||||
const triggerBlockId = getTriggerBlockId(request, body);
|
||||
const idempotencyKey = getIdempotencyKey(request, t);
|
||||
const idempotency = await withIdempotency({
|
||||
scope: `workflow-trigger:tenant:${tenantId}`,
|
||||
key: idempotencyKey,
|
||||
fingerprint: {
|
||||
method: request.method,
|
||||
workflowId: params.id!,
|
||||
tenantId,
|
||||
triggerBlockId,
|
||||
body: toJsonValue(body),
|
||||
},
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
}, db as unknown as IdempotencyClient, async () => {
|
||||
const execution = await WorkflowsExecutionsService.execute(params.id!, {
|
||||
type: "trigger",
|
||||
input: body,
|
||||
session: {
|
||||
return await runWithRlsContext({ tenantId, userId: getUserId(body) ?? undefined }, async () => {
|
||||
const idempotency = await withIdempotency(
|
||||
{
|
||||
tenantId,
|
||||
userId: getUserId(body),
|
||||
scope: `workflow-trigger:tenant:${tenantId}`,
|
||||
key: idempotencyKey,
|
||||
fingerprint: {
|
||||
method: request.method,
|
||||
workflowId: params.id!,
|
||||
tenantId,
|
||||
triggerBlockId,
|
||||
body: toJsonValue(body),
|
||||
},
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
},
|
||||
...(triggerBlockId ? { fromBlockId: triggerBlockId } : {}),
|
||||
});
|
||||
if (execution.error) {
|
||||
throw Object.assign(new Error(execution.error), { status: 400 });
|
||||
}
|
||||
return {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: { execution: toJsonValue(execution) },
|
||||
};
|
||||
db as unknown as IdempotencyClient,
|
||||
async () => {
|
||||
const execution = await WorkflowsExecutionsService.execute(params.id!, {
|
||||
type: "trigger",
|
||||
input: body,
|
||||
session: {
|
||||
tenantId,
|
||||
userId: getUserId(body),
|
||||
},
|
||||
...(triggerBlockId ? { fromBlockId: triggerBlockId } : {}),
|
||||
});
|
||||
if (execution.error) {
|
||||
throw Object.assign(new Error(execution.error), { status: 400 });
|
||||
}
|
||||
return {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: { execution: toJsonValue(execution) },
|
||||
};
|
||||
}
|
||||
);
|
||||
return Response.json(idempotency.body ?? {}, { status: idempotency.status, headers: idempotency.headers });
|
||||
});
|
||||
return Response.json(idempotency.body ?? {}, { status: idempotency.status, headers: idempotency.headers });
|
||||
} catch (e: any) {
|
||||
if (e instanceof CatalogError && e.code === foundationErrors.idempotencyConflict.code) {
|
||||
return Response.json({ error: workflowApiText(t, "workflows.errors.idempotencyKeyConflict") }, { status: 409 });
|
||||
|
||||
Reference in new issue
Block a user