Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+62
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { queueNames, type TypedQueue } from "@hivegrid/queue";
|
||||
|
||||
import { GDPR_USER_SELF_EXPORT_TYPE, enqueueGdprExport } from "./gdprExportQueue.server";
|
||||
|
||||
function createQueueMock(jobId = "queued-job"): TypedQueue<typeof queueNames.exports> & { add: ReturnType<typeof vi.fn>; close: ReturnType<typeof vi.fn> } {
|
||||
return {
|
||||
add: vi.fn(async () => ({ id: jobId })),
|
||||
close: vi.fn(async () => undefined),
|
||||
} as unknown as TypedQueue<typeof queueNames.exports> & { add: ReturnType<typeof vi.fn>; close: ReturnType<typeof vi.fn> };
|
||||
}
|
||||
|
||||
describe("enqueueGdprExport", () => {
|
||||
it("enqueues a GDPR self-export on the shared export queue", async () => {
|
||||
const queue = createQueueMock("bullmq_job_1");
|
||||
|
||||
await expect(
|
||||
enqueueGdprExport(
|
||||
{
|
||||
exportJobId: "gdpr_export_1",
|
||||
tenantId: "tenant_1",
|
||||
requestedByUserId: "user_1",
|
||||
scope: "user_self",
|
||||
targetUserId: "user_1",
|
||||
requestId: "req_1",
|
||||
},
|
||||
{ queue },
|
||||
),
|
||||
).resolves.toEqual({ enqueued: true, jobId: "bullmq_job_1" });
|
||||
|
||||
expect(queue.add).toHaveBeenCalledWith(
|
||||
"run-export",
|
||||
expect.objectContaining({
|
||||
kind: "export.run",
|
||||
exportJobId: "gdpr_export_1",
|
||||
tenantId: "tenant_1",
|
||||
requestedByUserId: "user_1",
|
||||
exportType: GDPR_USER_SELF_EXPORT_TYPE,
|
||||
format: "json",
|
||||
requestId: "req_1",
|
||||
metadata: { scope: "user_self", targetUserId: "user_1" },
|
||||
}),
|
||||
expect.objectContaining({ jobId: "gdpr_export_1" }),
|
||||
);
|
||||
expect(queue.close).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns queue-unavailable when no queue or Redis connection is configured", async () => {
|
||||
await expect(
|
||||
enqueueGdprExport(
|
||||
{
|
||||
exportJobId: "gdpr_export_1",
|
||||
tenantId: "tenant_1",
|
||||
requestedByUserId: "user_1",
|
||||
scope: "user_self",
|
||||
},
|
||||
{ env: {} },
|
||||
),
|
||||
).resolves.toEqual({ enqueued: false, reason: "queue-unavailable" });
|
||||
});
|
||||
});
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
buildExportJob,
|
||||
enqueueJob,
|
||||
queueNames,
|
||||
resolveRedisConnectionOptions,
|
||||
resolveRedisConnectionOptionsFromProcessEnv,
|
||||
type RedisConnectionEnv,
|
||||
type TypedQueue,
|
||||
} from "@hivegrid/queue";
|
||||
|
||||
export type GdprExportScope = "user_self" | "tenant_full";
|
||||
|
||||
export type EnqueueGdprExportInput = {
|
||||
exportJobId: string;
|
||||
tenantId: string;
|
||||
requestedByUserId: string;
|
||||
scope: GdprExportScope;
|
||||
targetUserId?: string | null;
|
||||
requestId?: string;
|
||||
};
|
||||
|
||||
export type EnqueueGdprExportResult = { enqueued: true; jobId?: string } | { enqueued: false; reason: "queue-unavailable" };
|
||||
|
||||
type Dependencies = {
|
||||
env?: RedisConnectionEnv;
|
||||
queue?: TypedQueue<typeof queueNames.exports>;
|
||||
};
|
||||
|
||||
export const GDPR_USER_SELF_EXPORT_TYPE = "gdpr_user_self";
|
||||
export const GDPR_TENANT_FULL_EXPORT_TYPE = "gdpr_tenant_full";
|
||||
|
||||
export async function enqueueGdprExport(input: EnqueueGdprExportInput, dependencies: Dependencies = {}): Promise<EnqueueGdprExportResult> {
|
||||
let queue = dependencies.queue;
|
||||
let shouldCloseQueue = false;
|
||||
const connection = queue
|
||||
? undefined
|
||||
: dependencies.env
|
||||
? resolveRedisConnectionOptions({ env: dependencies.env })
|
||||
: resolveRedisConnectionOptionsFromProcessEnv();
|
||||
|
||||
if (!queue && !connection) {
|
||||
return { enqueued: false, reason: "queue-unavailable" };
|
||||
}
|
||||
|
||||
if (!queue && connection) {
|
||||
const queueRuntimeModule = "@hivegrid/queue/runtime";
|
||||
const { createQueue } = (await import(/* @vite-ignore */ queueRuntimeModule)) as typeof import("@hivegrid/queue/runtime");
|
||||
queue = createQueue(queueNames.exports, { connection });
|
||||
shouldCloseQueue = true;
|
||||
}
|
||||
|
||||
if (!queue) {
|
||||
return { enqueued: false, reason: "queue-unavailable" };
|
||||
}
|
||||
|
||||
try {
|
||||
const descriptor = buildExportJob({
|
||||
exportJobId: input.exportJobId,
|
||||
tenantId: input.tenantId,
|
||||
requestedByUserId: input.requestedByUserId,
|
||||
exportType: input.scope === "tenant_full" ? GDPR_TENANT_FULL_EXPORT_TYPE : GDPR_USER_SELF_EXPORT_TYPE,
|
||||
format: "json",
|
||||
requestId: input.requestId,
|
||||
metadata: {
|
||||
scope: input.scope,
|
||||
...(input.targetUserId ? { targetUserId: input.targetUserId } : {}),
|
||||
},
|
||||
});
|
||||
const job = await enqueueJob(queue, descriptor, { tenantId: input.tenantId });
|
||||
return { enqueued: true, jobId: typeof job.id === "string" ? job.id : undefined };
|
||||
} finally {
|
||||
if (shouldCloseQueue && queue) {
|
||||
await queue.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user