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
+72
@@ -0,0 +1,72 @@
|
||||
// @vitest-environment node
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
buildNotificationJob: vi.fn((input) => ({ queueName: "notifications", jobName: "send-notification", payload: input })),
|
||||
enqueueJob: vi.fn(async () => ({ id: "notification-job" })),
|
||||
createQueue: vi.fn(() => ({ close: vi.fn(async () => undefined) })),
|
||||
getRolesByName: vi.fn(),
|
||||
getAdminUsersInRoles: vi.fn(),
|
||||
getUsersRolesInTenant: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@hivegrid/queue", () => ({
|
||||
buildNotificationJob: mocks.buildNotificationJob,
|
||||
enqueueJob: mocks.enqueueJob,
|
||||
queueNames: { notifications: "notifications" },
|
||||
resolveRedisConnectionOptions: vi.fn(() => ({ host: "redis.test", port: 6379 })),
|
||||
}));
|
||||
vi.mock("@hivegrid/queue/runtime", () => ({ createQueue: mocks.createQueue }));
|
||||
vi.mock("@hivegrid/db/notifications.server", () => ({
|
||||
createNotificationDelivery: vi.fn(),
|
||||
redactNotificationDiagnosticText: (message: string) => message,
|
||||
}));
|
||||
vi.mock("~/utils/db/permissions/roles.db.server", () => ({ getRolesByName: mocks.getRolesByName }));
|
||||
vi.mock("~/utils/db/permissions/userRoles.db.server", () => ({ getUsersRolesInTenant: mocks.getUsersRolesInTenant }));
|
||||
vi.mock("~/utils/db/users.db.server", () => ({ getAdminUsersInRoles: mocks.getAdminUsersInRoles }));
|
||||
|
||||
import NotificationService from "./NotificationService";
|
||||
|
||||
describe("NotificationService source-key enqueue idempotency", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.getRolesByName.mockResolvedValue([{ id: "role_admin", type: "admin" }]);
|
||||
mocks.getAdminUsersInRoles.mockResolvedValue([
|
||||
{
|
||||
id: "admin_1",
|
||||
email: "admin@example.test",
|
||||
firstName: "Admin",
|
||||
lastName: "User",
|
||||
phone: null,
|
||||
defaultTenantId: "tenant_1",
|
||||
},
|
||||
]);
|
||||
mocks.getUsersRolesInTenant.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
it("derives the same PII-safe BullMQ idempotency key for a replayed event notification", async () => {
|
||||
const input = {
|
||||
channel: "admin-accounts" as const,
|
||||
tenantId: "tenant_1",
|
||||
notification: {
|
||||
message: "Account created",
|
||||
data: { sourceKey: "application-event:evt_0123456789abcdef0123456789abcdef01234567" },
|
||||
},
|
||||
};
|
||||
|
||||
await NotificationService.sendToRoles(input);
|
||||
await NotificationService.sendToRoles(input);
|
||||
|
||||
expect(mocks.enqueueJob).toHaveBeenCalledTimes(2);
|
||||
const options = mocks.enqueueJob.mock.calls.map(([, , enqueueOptions]) => enqueueOptions);
|
||||
expect(options[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
tenantId: "tenant_1",
|
||||
idempotencyKey: expect.stringMatching(/^notification-[a-f0-9]{64}$/u),
|
||||
})
|
||||
);
|
||||
expect(options[0]?.idempotencyKey).toBe(options[1]?.idempotencyKey);
|
||||
expect(options[0]?.idempotencyKey).not.toContain("tenant_1");
|
||||
expect(options[0]?.idempotencyKey).not.toContain("admin_1");
|
||||
});
|
||||
});
|
||||
+44
-2
@@ -8,6 +8,7 @@ import { NotificationMessageDto } from "../../dtos/NotificationMessageDto";
|
||||
import { NotificationSubscriberDto } from "../../dtos/NotificationSubscriberDto";
|
||||
import { NotificationTemplateDto } from "../../dtos/NotificationTemplateDto";
|
||||
import { NotificationChannel, NotificationChannels } from "../NotificationChannels";
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
type NotificationQueue = TypedQueue<typeof queueNames.notifications>;
|
||||
|
||||
@@ -181,18 +182,43 @@ function getNotificationSourceKey(channel: string, notification: INotification):
|
||||
return requestId ? `${channel}:request:${requestId}` : undefined;
|
||||
}
|
||||
|
||||
function buildNotificationQueueIdempotencyKey({
|
||||
tenantId,
|
||||
userId,
|
||||
channel,
|
||||
sourceKey,
|
||||
}: {
|
||||
tenantId: string;
|
||||
userId: string;
|
||||
channel: string;
|
||||
sourceKey: string;
|
||||
}): string {
|
||||
const digest = createHash("sha256")
|
||||
.update(tenantId)
|
||||
.update("\0")
|
||||
.update(userId)
|
||||
.update("\0")
|
||||
.update(channel)
|
||||
.update("\0")
|
||||
.update(sourceKey)
|
||||
.digest("hex");
|
||||
return `notification-${digest}`;
|
||||
}
|
||||
|
||||
function createSkippedNotificationDelivery({
|
||||
targetTenantId,
|
||||
to,
|
||||
channel,
|
||||
reason,
|
||||
lastError,
|
||||
sourceKey,
|
||||
}: {
|
||||
targetTenantId: string;
|
||||
to: INotificationUser;
|
||||
channel: string;
|
||||
reason: string;
|
||||
lastError: string;
|
||||
sourceKey?: string;
|
||||
}) {
|
||||
return createNotificationDelivery({
|
||||
tenantId: targetTenantId,
|
||||
@@ -200,6 +226,7 @@ function createSkippedNotificationDelivery({
|
||||
channel: "in-app",
|
||||
category: mapNotificationChannelToCategory(channel),
|
||||
messageKey: channel,
|
||||
sourceKey,
|
||||
status: "SKIPPED",
|
||||
lastError,
|
||||
payloadMetadata: {
|
||||
@@ -256,6 +283,7 @@ async function send({
|
||||
return;
|
||||
}
|
||||
|
||||
const sourceKey = getNotificationSourceKey(channel, notification);
|
||||
try {
|
||||
const queued = await withNotificationQueue(process.env as RedisConnectionEnv, async (queue) => {
|
||||
const descriptor = buildNotificationJob({
|
||||
@@ -265,7 +293,7 @@ async function send({
|
||||
channel: "in-app",
|
||||
category: mapNotificationChannelToCategory(channel),
|
||||
messageKey: channel,
|
||||
sourceKey: getNotificationSourceKey(channel, notification),
|
||||
sourceKey,
|
||||
metadata: {
|
||||
source: "legacy-notification-service",
|
||||
...(notification.action?.url ? { actionUrl: notification.action.url } : {}),
|
||||
@@ -279,7 +307,19 @@ async function send({
|
||||
attachments: notification.attachments,
|
||||
},
|
||||
});
|
||||
return enqueueJob(queue, descriptor, { tenantId: targetTenantId });
|
||||
return enqueueJob(queue, descriptor, {
|
||||
tenantId: targetTenantId,
|
||||
...(sourceKey
|
||||
? {
|
||||
idempotencyKey: buildNotificationQueueIdempotencyKey({
|
||||
tenantId: targetTenantId,
|
||||
userId: to.id,
|
||||
channel,
|
||||
sourceKey,
|
||||
}),
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
});
|
||||
if (!queued && required) {
|
||||
throw new Error("Notification queue is not configured.");
|
||||
@@ -291,6 +331,7 @@ async function send({
|
||||
channel,
|
||||
reason: "queue-not-configured",
|
||||
lastError: "Notification queue is not configured.",
|
||||
sourceKey,
|
||||
});
|
||||
}
|
||||
return queued;
|
||||
@@ -308,6 +349,7 @@ async function send({
|
||||
channel,
|
||||
reason: "queue-enqueue-failed",
|
||||
lastError: redactedMessage,
|
||||
sourceKey,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
import { DefaultAdminRoles } from "~/application/dtos/shared/DefaultAdminRoles";
|
||||
import { DefaultAppRoles } from "~/application/dtos/shared/DefaultAppRoles";
|
||||
|
||||
const NotificationChannelTypes = [
|
||||
export const NotificationChannelTypes = [
|
||||
"admin-accounts",
|
||||
"admin-users",
|
||||
"admin-subscriptions",
|
||||
@@ -15,7 +15,7 @@ const NotificationChannelTypes = [
|
||||
"roles",
|
||||
"my-rows",
|
||||
] as const;
|
||||
export type NotificationChannel = typeof NotificationChannelTypes[number];
|
||||
export type NotificationChannel = (typeof NotificationChannelTypes)[number];
|
||||
|
||||
export interface NotificationChannelDto {
|
||||
name: NotificationChannel;
|
||||
|
||||
Reference in new issue
Block a user