@open-mercato/core 0.4.6-develop-37588eaea9 → 0.4.6-develop-5cacfcc21a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/modules/notifications/events.js +30 -0
- package/dist/modules/notifications/events.js.map +7 -0
- package/dist/modules/notifications/frontend/NotificationInboxPageClient.js +2 -3
- package/dist/modules/notifications/frontend/NotificationInboxPageClient.js.map +2 -2
- package/dist/modules/notifications/lib/events.js +6 -1
- package/dist/modules/notifications/lib/events.js.map +2 -2
- package/dist/modules/notifications/lib/notificationMapper.js +10 -1
- package/dist/modules/notifications/lib/notificationMapper.js.map +2 -2
- package/dist/modules/notifications/lib/notificationService.js +26 -1
- package/dist/modules/notifications/lib/notificationService.js.map +2 -2
- package/dist/modules/progress/events.js +6 -6
- package/dist/modules/progress/events.js.map +2 -2
- package/dist/modules/progress/lib/events.js.map +1 -1
- package/dist/modules/progress/lib/progressServiceImpl.js +38 -29
- package/dist/modules/progress/lib/progressServiceImpl.js.map +2 -2
- package/dist/modules/query_index/api/reindex.js +3 -0
- package/dist/modules/query_index/api/reindex.js.map +2 -2
- package/dist/modules/query_index/components/QueryIndexesTable.js +8 -10
- package/dist/modules/query_index/components/QueryIndexesTable.js.map +2 -2
- package/dist/modules/query_index/subscribers/reindex.js +89 -1
- package/dist/modules/query_index/subscribers/reindex.js.map +2 -2
- package/package.json +2 -2
- package/src/modules/notifications/README.md +21 -0
- package/src/modules/notifications/events.ts +28 -0
- package/src/modules/notifications/frontend/NotificationInboxPageClient.tsx +2 -3
- package/src/modules/notifications/lib/events.ts +5 -0
- package/src/modules/notifications/lib/notificationMapper.ts +12 -1
- package/src/modules/notifications/lib/notificationService.ts +33 -1
- package/src/modules/progress/events.ts +6 -6
- package/src/modules/progress/lib/events.ts +60 -0
- package/src/modules/progress/lib/progressServiceImpl.ts +32 -22
- package/src/modules/query_index/api/reindex.ts +3 -0
- package/src/modules/query_index/components/QueryIndexesTable.tsx +8 -10
- package/src/modules/query_index/subscribers/reindex.ts +99 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createModuleEvents } from "@open-mercato/shared/modules/events";
|
|
2
|
+
import { NOTIFICATION_SSE_EVENTS } from "./lib/events.js";
|
|
3
|
+
const events = [
|
|
4
|
+
{
|
|
5
|
+
id: NOTIFICATION_SSE_EVENTS.CREATED,
|
|
6
|
+
label: "Notification Created",
|
|
7
|
+
entity: "notification",
|
|
8
|
+
category: "system",
|
|
9
|
+
clientBroadcast: true
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
id: NOTIFICATION_SSE_EVENTS.BATCH_CREATED,
|
|
13
|
+
label: "Notification Batch Created",
|
|
14
|
+
entity: "notification",
|
|
15
|
+
category: "system",
|
|
16
|
+
clientBroadcast: true
|
|
17
|
+
}
|
|
18
|
+
];
|
|
19
|
+
const eventsConfig = createModuleEvents({
|
|
20
|
+
moduleId: "notifications",
|
|
21
|
+
events
|
|
22
|
+
});
|
|
23
|
+
const emitNotificationEvent = eventsConfig.emit;
|
|
24
|
+
var events_default = eventsConfig;
|
|
25
|
+
export {
|
|
26
|
+
events_default as default,
|
|
27
|
+
emitNotificationEvent,
|
|
28
|
+
eventsConfig
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/modules/notifications/events.ts"],
|
|
4
|
+
"sourcesContent": ["import { createModuleEvents } from '@open-mercato/shared/modules/events'\nimport { NOTIFICATION_SSE_EVENTS } from './lib/events'\n\nconst events = [\n {\n id: NOTIFICATION_SSE_EVENTS.CREATED,\n label: 'Notification Created',\n entity: 'notification',\n category: 'system',\n clientBroadcast: true,\n },\n {\n id: NOTIFICATION_SSE_EVENTS.BATCH_CREATED,\n label: 'Notification Batch Created',\n entity: 'notification',\n category: 'system',\n clientBroadcast: true,\n },\n] as const\n\nexport const eventsConfig = createModuleEvents({\n moduleId: 'notifications',\n events,\n})\n\nexport const emitNotificationEvent = eventsConfig.emit\n\nexport default eventsConfig\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,0BAA0B;AACnC,SAAS,+BAA+B;AAExC,MAAM,SAAS;AAAA,EACb;AAAA,IACE,IAAI,wBAAwB;AAAA,IAC5B,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,iBAAiB;AAAA,EACnB;AAAA,EACA;AAAA,IACE,IAAI,wBAAwB;AAAA,IAC5B,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,iBAAiB;AAAA,EACnB;AACF;AAEO,MAAM,eAAe,mBAAmB;AAAA,EAC7C,UAAU;AAAA,EACV;AACF,CAAC;AAEM,MAAM,wBAAwB,aAAa;AAElD,IAAO,iBAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
3
|
import { useRouter } from "next/navigation";
|
|
4
4
|
import { useT } from "@open-mercato/shared/lib/i18n/context";
|
|
5
|
-
import { NotificationPanel } from "@open-mercato/ui/backend/notifications";
|
|
6
|
-
import { useNotificationsPoll } from "@open-mercato/ui/backend/notifications";
|
|
5
|
+
import { NotificationPanel, useNotifications } from "@open-mercato/ui/backend/notifications";
|
|
7
6
|
function NotificationInboxPageClient() {
|
|
8
7
|
const t = useT();
|
|
9
8
|
const router = useRouter();
|
|
@@ -16,7 +15,7 @@ function NotificationInboxPageClient() {
|
|
|
16
15
|
dismissUndo,
|
|
17
16
|
undoDismiss,
|
|
18
17
|
markAllRead
|
|
19
|
-
} =
|
|
18
|
+
} = useNotifications();
|
|
20
19
|
return /* @__PURE__ */ jsx(
|
|
21
20
|
NotificationPanel,
|
|
22
21
|
{
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/notifications/frontend/NotificationInboxPageClient.tsx"],
|
|
4
|
-
"sourcesContent": ["'use client'\n\nimport * as React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { NotificationPanel
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["'use client'\n\nimport * as React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { NotificationPanel, useNotifications } from '@open-mercato/ui/backend/notifications'\n\nexport function NotificationInboxPageClient() {\n const t = useT()\n const router = useRouter()\n const {\n notifications,\n unreadCount,\n markAsRead,\n executeAction,\n dismiss,\n dismissUndo,\n undoDismiss,\n markAllRead,\n } = useNotifications()\n\n return (\n <NotificationPanel\n open\n onOpenChange={(open) => {\n if (!open) router.push('/backend')\n }}\n notifications={notifications}\n unreadCount={unreadCount}\n onMarkAsRead={markAsRead}\n onExecuteAction={executeAction}\n onDismiss={dismiss}\n dismissUndo={dismissUndo}\n onUndoDismiss={undoDismiss}\n onMarkAllRead={markAllRead}\n t={t}\n />\n )\n}\n\nexport default NotificationInboxPageClient\n"],
|
|
5
|
+
"mappings": ";AAsBI;AAnBJ,SAAS,iBAAiB;AAC1B,SAAS,YAAY;AACrB,SAAS,mBAAmB,wBAAwB;AAE7C,SAAS,8BAA8B;AAC5C,QAAM,IAAI,KAAK;AACf,QAAM,SAAS,UAAU;AACzB,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,iBAAiB;AAErB,SACE;AAAA,IAAC;AAAA;AAAA,MACC,MAAI;AAAA,MACJ,cAAc,CAAC,SAAS;AACtB,YAAI,CAAC,KAAM,QAAO,KAAK,UAAU;AAAA,MACnC;AAAA,MACA;AAAA,MACA;AAAA,MACA,cAAc;AAAA,MACd,iBAAiB;AAAA,MACjB,WAAW;AAAA,MACX;AAAA,MACA,eAAe;AAAA,MACf,eAAe;AAAA,MACf;AAAA;AAAA,EACF;AAEJ;AAEA,IAAO,sCAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -6,7 +6,12 @@ const NOTIFICATION_EVENTS = {
|
|
|
6
6
|
RESTORED: "notifications.restored",
|
|
7
7
|
EXPIRED: "notifications.expired"
|
|
8
8
|
};
|
|
9
|
+
const NOTIFICATION_SSE_EVENTS = {
|
|
10
|
+
CREATED: "notifications.notification.created",
|
|
11
|
+
BATCH_CREATED: "notifications.notification.batch_created"
|
|
12
|
+
};
|
|
9
13
|
export {
|
|
10
|
-
NOTIFICATION_EVENTS
|
|
14
|
+
NOTIFICATION_EVENTS,
|
|
15
|
+
NOTIFICATION_SSE_EVENTS
|
|
11
16
|
};
|
|
12
17
|
//# sourceMappingURL=events.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/notifications/lib/events.ts"],
|
|
4
|
-
"sourcesContent": ["export const NOTIFICATION_EVENTS = {\n CREATED: 'notifications.created',\n READ: 'notifications.read',\n ACTIONED: 'notifications.actioned',\n DISMISSED: 'notifications.dismissed',\n RESTORED: 'notifications.restored',\n EXPIRED: 'notifications.expired',\n} as const\n\nexport type NotificationCreatedPayload = {\n notificationId: string\n recipientUserId: string\n type: string\n title: string\n tenantId: string\n organizationId?: string | null\n}\n\nexport type NotificationReadPayload = {\n notificationId: string\n userId: string\n tenantId: string\n}\n\nexport type NotificationActionedPayload = {\n notificationId: string\n actionId: string\n userId: string\n tenantId: string\n}\n\nexport type NotificationDismissedPayload = {\n notificationId: string\n userId: string\n tenantId: string\n}\n\nexport type NotificationRestoredPayload = {\n notificationId: string\n userId: string\n tenantId: string\n status: 'read' | 'unread'\n}\n\nexport type NotificationExpiredPayload = {\n notificationIds: string[]\n tenantId: string\n}\n"],
|
|
5
|
-
"mappings": "AAAO,MAAM,sBAAsB;AAAA,EACjC,SAAS;AAAA,EACT,MAAM;AAAA,EACN,UAAU;AAAA,EACV,WAAW;AAAA,EACX,UAAU;AAAA,EACV,SAAS;AACX;",
|
|
4
|
+
"sourcesContent": ["export const NOTIFICATION_EVENTS = {\n CREATED: 'notifications.created',\n READ: 'notifications.read',\n ACTIONED: 'notifications.actioned',\n DISMISSED: 'notifications.dismissed',\n RESTORED: 'notifications.restored',\n EXPIRED: 'notifications.expired',\n} as const\n\nexport const NOTIFICATION_SSE_EVENTS = {\n CREATED: 'notifications.notification.created',\n BATCH_CREATED: 'notifications.notification.batch_created',\n} as const\n\nexport type NotificationCreatedPayload = {\n notificationId: string\n recipientUserId: string\n type: string\n title: string\n tenantId: string\n organizationId?: string | null\n}\n\nexport type NotificationReadPayload = {\n notificationId: string\n userId: string\n tenantId: string\n}\n\nexport type NotificationActionedPayload = {\n notificationId: string\n actionId: string\n userId: string\n tenantId: string\n}\n\nexport type NotificationDismissedPayload = {\n notificationId: string\n userId: string\n tenantId: string\n}\n\nexport type NotificationRestoredPayload = {\n notificationId: string\n userId: string\n tenantId: string\n status: 'read' | 'unread'\n}\n\nexport type NotificationExpiredPayload = {\n notificationIds: string[]\n tenantId: string\n}\n"],
|
|
5
|
+
"mappings": "AAAO,MAAM,sBAAsB;AAAA,EACjC,SAAS;AAAA,EACT,MAAM;AAAA,EACN,UAAU;AAAA,EACV,WAAW;AAAA,EACX,UAAU;AAAA,EACV,SAAS;AACX;AAEO,MAAM,0BAA0B;AAAA,EACrC,SAAS;AAAA,EACT,eAAe;AACjB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
function toNotificationDto(notification) {
|
|
2
|
+
const createdAt = notification.createdAt instanceof Date ? notification.createdAt : (() => {
|
|
3
|
+
if (process.env.NODE_ENV !== "test") {
|
|
4
|
+
console.warn(
|
|
5
|
+
"[notifications] Invalid createdAt on notification entity, falling back to current time",
|
|
6
|
+
{ id: notification.id, createdAt: notification.createdAt }
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
return /* @__PURE__ */ new Date();
|
|
10
|
+
})();
|
|
2
11
|
return {
|
|
3
12
|
id: notification.id,
|
|
4
13
|
type: notification.type,
|
|
@@ -23,7 +32,7 @@ function toNotificationDto(notification) {
|
|
|
23
32
|
sourceEntityType: notification.sourceEntityType,
|
|
24
33
|
sourceEntityId: notification.sourceEntityId,
|
|
25
34
|
linkHref: notification.linkHref,
|
|
26
|
-
createdAt:
|
|
35
|
+
createdAt: createdAt.toISOString(),
|
|
27
36
|
readAt: notification.readAt?.toISOString() ?? null,
|
|
28
37
|
actionTaken: notification.actionTaken
|
|
29
38
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/notifications/lib/notificationMapper.ts"],
|
|
4
|
-
"sourcesContent": ["import type { NotificationDto } from '@open-mercato/shared/modules/notifications/types'\nimport { Notification } from '../data/entities'\n\nexport function toNotificationDto(notification: Notification): NotificationDto {\n return {\n id: notification.id,\n type: notification.type,\n title: notification.title,\n body: notification.body,\n titleKey: notification.titleKey,\n bodyKey: notification.bodyKey,\n titleVariables: notification.titleVariables,\n bodyVariables: notification.bodyVariables,\n icon: notification.icon,\n severity: notification.severity,\n status: notification.status,\n actions: notification.actionData?.actions?.map((action) => ({\n id: action.id,\n label: action.label,\n labelKey: action.labelKey,\n variant: action.variant,\n icon: action.icon,\n })) ?? [],\n primaryActionId: notification.actionData?.primaryActionId,\n sourceModule: notification.sourceModule,\n sourceEntityType: notification.sourceEntityType,\n sourceEntityId: notification.sourceEntityId,\n linkHref: notification.linkHref,\n createdAt:
|
|
5
|
-
"mappings": "AAGO,SAAS,kBAAkB,cAA6C;AAC7E,SAAO;AAAA,IACL,IAAI,aAAa;AAAA,IACjB,MAAM,aAAa;AAAA,IACnB,OAAO,aAAa;AAAA,IACpB,MAAM,aAAa;AAAA,IACnB,UAAU,aAAa;AAAA,IACvB,SAAS,aAAa;AAAA,IACtB,gBAAgB,aAAa;AAAA,IAC7B,eAAe,aAAa;AAAA,IAC5B,MAAM,aAAa;AAAA,IACnB,UAAU,aAAa;AAAA,IACvB,QAAQ,aAAa;AAAA,IACrB,SAAS,aAAa,YAAY,SAAS,IAAI,CAAC,YAAY;AAAA,MAC1D,IAAI,OAAO;AAAA,MACX,OAAO,OAAO;AAAA,MACd,UAAU,OAAO;AAAA,MACjB,SAAS,OAAO;AAAA,MAChB,MAAM,OAAO;AAAA,IACf,EAAE,KAAK,CAAC;AAAA,IACR,iBAAiB,aAAa,YAAY;AAAA,IAC1C,cAAc,aAAa;AAAA,IAC3B,kBAAkB,aAAa;AAAA,IAC/B,gBAAgB,aAAa;AAAA,IAC7B,UAAU,aAAa;AAAA,IACvB,WAAW,
|
|
4
|
+
"sourcesContent": ["import type { NotificationDto } from '@open-mercato/shared/modules/notifications/types'\nimport { Notification } from '../data/entities'\n\nexport function toNotificationDto(notification: Notification): NotificationDto {\n const createdAt = notification.createdAt instanceof Date\n ? notification.createdAt\n : (() => {\n if (process.env.NODE_ENV !== 'test') {\n console.warn(\n '[notifications] Invalid createdAt on notification entity, falling back to current time',\n { id: notification.id, createdAt: notification.createdAt },\n )\n }\n return new Date()\n })()\n return {\n id: notification.id,\n type: notification.type,\n title: notification.title,\n body: notification.body,\n titleKey: notification.titleKey,\n bodyKey: notification.bodyKey,\n titleVariables: notification.titleVariables,\n bodyVariables: notification.bodyVariables,\n icon: notification.icon,\n severity: notification.severity,\n status: notification.status,\n actions: notification.actionData?.actions?.map((action) => ({\n id: action.id,\n label: action.label,\n labelKey: action.labelKey,\n variant: action.variant,\n icon: action.icon,\n })) ?? [],\n primaryActionId: notification.actionData?.primaryActionId,\n sourceModule: notification.sourceModule,\n sourceEntityType: notification.sourceEntityType,\n sourceEntityId: notification.sourceEntityId,\n linkHref: notification.linkHref,\n createdAt: createdAt.toISOString(),\n readAt: notification.readAt?.toISOString() ?? null,\n actionTaken: notification.actionTaken,\n }\n}\n"],
|
|
5
|
+
"mappings": "AAGO,SAAS,kBAAkB,cAA6C;AAC7E,QAAM,YAAY,aAAa,qBAAqB,OAChD,aAAa,aACZ,MAAM;AACP,QAAI,QAAQ,IAAI,aAAa,QAAQ;AACnC,cAAQ;AAAA,QACN;AAAA,QACA,EAAE,IAAI,aAAa,IAAI,WAAW,aAAa,UAAU;AAAA,MAC3D;AAAA,IACF;AACA,WAAO,oBAAI,KAAK;AAAA,EAClB,GAAG;AACL,SAAO;AAAA,IACL,IAAI,aAAa;AAAA,IACjB,MAAM,aAAa;AAAA,IACnB,OAAO,aAAa;AAAA,IACpB,MAAM,aAAa;AAAA,IACnB,UAAU,aAAa;AAAA,IACvB,SAAS,aAAa;AAAA,IACtB,gBAAgB,aAAa;AAAA,IAC7B,eAAe,aAAa;AAAA,IAC5B,MAAM,aAAa;AAAA,IACnB,UAAU,aAAa;AAAA,IACvB,QAAQ,aAAa;AAAA,IACrB,SAAS,aAAa,YAAY,SAAS,IAAI,CAAC,YAAY;AAAA,MAC1D,IAAI,OAAO;AAAA,MACX,OAAO,OAAO;AAAA,MACd,UAAU,OAAO;AAAA,MACjB,SAAS,OAAO;AAAA,MAChB,MAAM,OAAO;AAAA,IACf,EAAE,KAAK,CAAC;AAAA,IACR,iBAAiB,aAAa,YAAY;AAAA,IAC1C,cAAc,aAAa;AAAA,IAC3B,kBAAkB,aAAa;AAAA,IAC/B,gBAAgB,aAAa;AAAA,IAC7B,UAAU,aAAa;AAAA,IACvB,WAAW,UAAU,YAAY;AAAA,IACjC,QAAQ,aAAa,QAAQ,YAAY,KAAK;AAAA,IAC9C,aAAa,aAAa;AAAA,EAC5B;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Notification } from "../data/entities.js";
|
|
2
|
-
import { NOTIFICATION_EVENTS } from "./events.js";
|
|
2
|
+
import { NOTIFICATION_EVENTS, NOTIFICATION_SSE_EVENTS } from "./events.js";
|
|
3
3
|
import {
|
|
4
4
|
buildNotificationEntity,
|
|
5
5
|
emitNotificationCreated,
|
|
@@ -54,6 +54,22 @@ function applyNotificationContent(notification, input, recipientUserId, ctx) {
|
|
|
54
54
|
notification.actionResult = null;
|
|
55
55
|
notification.createdAt = /* @__PURE__ */ new Date();
|
|
56
56
|
}
|
|
57
|
+
async function emitNotificationSseEvents(eventBus, notifications, ctx, recipientUserIds) {
|
|
58
|
+
await eventBus.emit(NOTIFICATION_SSE_EVENTS.BATCH_CREATED, {
|
|
59
|
+
tenantId: ctx.tenantId,
|
|
60
|
+
organizationId: normalizeOrgScope(ctx.organizationId),
|
|
61
|
+
recipientUserIds,
|
|
62
|
+
count: notifications.length
|
|
63
|
+
});
|
|
64
|
+
for (const notification of notifications) {
|
|
65
|
+
await eventBus.emit(NOTIFICATION_SSE_EVENTS.CREATED, {
|
|
66
|
+
tenantId: notification.tenantId,
|
|
67
|
+
organizationId: notification.organizationId ?? null,
|
|
68
|
+
recipientUserId: notification.recipientUserId,
|
|
69
|
+
notification: toNotificationDto(notification)
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
57
73
|
async function createOrRefreshNotification(em, input, recipientUserId, ctx) {
|
|
58
74
|
if (input.groupKey && input.groupKey.trim().length > 0) {
|
|
59
75
|
const orgScope = normalizeOrgScope(ctx.organizationId) ?? "global";
|
|
@@ -92,6 +108,12 @@ function createNotificationService(deps) {
|
|
|
92
108
|
return entity;
|
|
93
109
|
});
|
|
94
110
|
await emitNotificationCreated(eventBus, notification, ctx);
|
|
111
|
+
await eventBus.emit(NOTIFICATION_SSE_EVENTS.CREATED, {
|
|
112
|
+
tenantId: notification.tenantId,
|
|
113
|
+
organizationId: notification.organizationId ?? null,
|
|
114
|
+
recipientUserId: notification.recipientUserId,
|
|
115
|
+
notification: toNotificationDto(notification)
|
|
116
|
+
});
|
|
95
117
|
return notification;
|
|
96
118
|
},
|
|
97
119
|
async createBatch(input, ctx) {
|
|
@@ -107,6 +129,7 @@ function createNotificationService(deps) {
|
|
|
107
129
|
await tx.flush();
|
|
108
130
|
});
|
|
109
131
|
await emitNotificationCreatedBatch(eventBus, notifications, ctx);
|
|
132
|
+
await emitNotificationSseEvents(eventBus, notifications, ctx, recipientUserIds);
|
|
110
133
|
return notifications;
|
|
111
134
|
},
|
|
112
135
|
async createForRole(input, ctx) {
|
|
@@ -128,6 +151,7 @@ function createNotificationService(deps) {
|
|
|
128
151
|
await tx.flush();
|
|
129
152
|
});
|
|
130
153
|
await emitNotificationCreatedBatch(eventBus, notifications, ctx);
|
|
154
|
+
await emitNotificationSseEvents(eventBus, notifications, ctx, uniqueRecipientUserIds);
|
|
131
155
|
return notifications;
|
|
132
156
|
},
|
|
133
157
|
async createForFeature(input, ctx) {
|
|
@@ -151,6 +175,7 @@ function createNotificationService(deps) {
|
|
|
151
175
|
await tx.flush();
|
|
152
176
|
});
|
|
153
177
|
await emitNotificationCreatedBatch(eventBus, notifications, ctx);
|
|
178
|
+
await emitNotificationSseEvents(eventBus, notifications, ctx, uniqueRecipientUserIds);
|
|
154
179
|
return notifications;
|
|
155
180
|
},
|
|
156
181
|
async markAsRead(notificationId, ctx) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/notifications/lib/notificationService.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/core'\nimport type { Knex } from 'knex'\nimport { Notification, type NotificationStatus } from '../data/entities'\nimport type { CreateNotificationInput, CreateBatchNotificationInput, CreateRoleNotificationInput, CreateFeatureNotificationInput, ExecuteActionInput } from '../data/validators'\nimport type { NotificationPollData } from '@open-mercato/shared/modules/notifications/types'\nimport { NOTIFICATION_EVENTS } from './events'\nimport {\n buildNotificationEntity,\n emitNotificationCreated,\n emitNotificationCreatedBatch,\n type NotificationContentInput,\n type NotificationTenantContext,\n} from './notificationFactory'\nimport { toNotificationDto } from './notificationMapper'\nimport { getRecipientUserIdsForFeature, getRecipientUserIdsForRole } from './notificationRecipients'\nimport { assertSafeNotificationHref, sanitizeNotificationActions } from './safeHref'\n\nconst DEBUG = process.env.NOTIFICATIONS_DEBUG === 'true'\n\nfunction debug(...args: unknown[]): void {\n if (DEBUG) {\n console.log('[notifications]', ...args)\n }\n}\n\nfunction getKnex(em: EntityManager): Knex {\n return (em.getConnection() as unknown as { getKnex: () => Knex }).getKnex()\n}\n\nconst UNIQUE_NOTIFICATION_ACTIVE_STATUSES: NotificationStatus[] = ['unread', 'read', 'actioned']\n\nfunction normalizeOrgScope(organizationId: string | null | undefined): string | null {\n return organizationId ?? null\n}\n\nfunction applyNotificationContent(\n notification: Notification,\n input: NotificationContentInput,\n recipientUserId: string,\n ctx: NotificationTenantContext,\n) {\n const actions = sanitizeNotificationActions(input.actions)\n const linkHref = assertSafeNotificationHref(input.linkHref)\n\n notification.recipientUserId = recipientUserId\n notification.type = input.type\n notification.titleKey = input.titleKey\n notification.bodyKey = input.bodyKey\n notification.titleVariables = input.titleVariables\n notification.bodyVariables = input.bodyVariables\n notification.title = input.title || input.titleKey || ''\n notification.body = input.body\n notification.icon = input.icon\n notification.severity = input.severity ?? 'info'\n notification.actionData = actions\n ? {\n actions,\n primaryActionId: input.primaryActionId,\n }\n : null\n notification.sourceModule = input.sourceModule\n notification.sourceEntityType = input.sourceEntityType\n notification.sourceEntityId = input.sourceEntityId\n notification.linkHref = linkHref\n notification.groupKey = input.groupKey\n notification.expiresAt = input.expiresAt ? new Date(input.expiresAt) : null\n notification.tenantId = ctx.tenantId\n notification.organizationId = normalizeOrgScope(ctx.organizationId)\n notification.status = 'unread'\n notification.readAt = null\n notification.actionedAt = null\n notification.dismissedAt = null\n notification.actionTaken = null\n notification.actionResult = null\n notification.createdAt = new Date()\n}\n\nasync function createOrRefreshNotification(\n em: EntityManager,\n input: NotificationContentInput,\n recipientUserId: string,\n ctx: NotificationTenantContext,\n): Promise<Notification> {\n if (input.groupKey && input.groupKey.trim().length > 0) {\n const orgScope = normalizeOrgScope(ctx.organizationId) ?? 'global'\n const lockKey = `notifications:${ctx.tenantId}:${orgScope}:${recipientUserId}:${input.type}:${input.groupKey}`\n try {\n const knex = getKnex(em)\n await knex.raw('select pg_advisory_xact_lock(hashtext(?))', [lockKey])\n } catch {\n // If advisory locks are unavailable, continue with best-effort dedupe.\n }\n\n const existing = await em.findOne(Notification, {\n recipientUserId,\n tenantId: ctx.tenantId,\n organizationId: normalizeOrgScope(ctx.organizationId),\n type: input.type,\n groupKey: input.groupKey,\n status: { $in: UNIQUE_NOTIFICATION_ACTIVE_STATUSES },\n }, {\n orderBy: { createdAt: 'desc' },\n })\n\n if (existing) {\n applyNotificationContent(existing, input, recipientUserId, ctx)\n return existing\n }\n }\n\n return buildNotificationEntity(em, input, recipientUserId, ctx)\n}\n\nexport interface NotificationServiceContext {\n tenantId: string\n organizationId?: string | null\n userId?: string | null\n}\n\nexport interface NotificationService {\n create(input: CreateNotificationInput, ctx: NotificationServiceContext): Promise<Notification>\n createBatch(input: CreateBatchNotificationInput, ctx: NotificationServiceContext): Promise<Notification[]>\n createForRole(input: CreateRoleNotificationInput, ctx: NotificationServiceContext): Promise<Notification[]>\n createForFeature(input: CreateFeatureNotificationInput, ctx: NotificationServiceContext): Promise<Notification[]>\n markAsRead(notificationId: string, ctx: NotificationServiceContext): Promise<Notification>\n markAllAsRead(ctx: NotificationServiceContext): Promise<number>\n dismiss(notificationId: string, ctx: NotificationServiceContext): Promise<Notification>\n restoreDismissed(\n notificationId: string,\n status: 'read' | 'unread' | undefined,\n ctx: NotificationServiceContext\n ): Promise<Notification>\n executeAction(\n notificationId: string,\n input: ExecuteActionInput,\n ctx: NotificationServiceContext\n ): Promise<{ notification: Notification; result: unknown }>\n getUnreadCount(ctx: NotificationServiceContext): Promise<number>\n getPollData(ctx: NotificationServiceContext, since?: string): Promise<NotificationPollData>\n cleanupExpired(): Promise<number>\n deleteBySource(\n sourceEntityType: string,\n sourceEntityId: string,\n ctx: NotificationServiceContext\n ): Promise<number>\n}\n\nexport interface NotificationServiceDeps {\n em: EntityManager\n eventBus: { emit: (event: string, payload: unknown) => Promise<void> }\n commandBus?: {\n execute: (\n commandId: string,\n options: { input: unknown; ctx: unknown; metadata?: unknown }\n ) => Promise<{ result: unknown }>\n }\n container?: { resolve: (name: string) => unknown }\n}\n\nexport function createNotificationService(deps: NotificationServiceDeps): NotificationService {\n const { em: rootEm, eventBus, commandBus, container } = deps\n\n return {\n async create(input, ctx) {\n const { recipientUserId, ...content } = input\n const writeEm = rootEm.fork()\n const notification = await writeEm.transactional(async (tx) => {\n const entity = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n await tx.flush()\n return entity\n })\n\n await emitNotificationCreated(eventBus, notification, ctx)\n\n return notification\n },\n\n async createBatch(input, ctx) {\n const recipientUserIds = Array.from(new Set(input.recipientUserIds))\n const { recipientUserIds: _recipientUserIds, ...content } = input\n const notifications: Notification[] = []\n const writeEm = rootEm.fork()\n\n await writeEm.transactional(async (tx) => {\n for (const recipientUserId of recipientUserIds) {\n const notification = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n notifications.push(notification)\n }\n await tx.flush()\n })\n\n await emitNotificationCreatedBatch(eventBus, notifications, ctx)\n\n return notifications\n },\n\n async createForRole(input, ctx) {\n const em = rootEm.fork()\n\n const knex = getKnex(em)\n const recipientUserIds = await getRecipientUserIdsForRole(knex, ctx.tenantId, input.roleId)\n if (recipientUserIds.length === 0) {\n return []\n }\n\n const { roleId: _roleId, ...content } = input\n const notifications: Notification[] = []\n const uniqueRecipientUserIds = Array.from(new Set(recipientUserIds))\n const writeEm = rootEm.fork()\n\n await writeEm.transactional(async (tx) => {\n for (const recipientUserId of uniqueRecipientUserIds) {\n const notification = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n notifications.push(notification)\n }\n await tx.flush()\n })\n\n await emitNotificationCreatedBatch(eventBus, notifications, ctx)\n\n return notifications\n },\n\n async createForFeature(input, ctx) {\n const em = rootEm.fork()\n const knex = getKnex(em)\n const recipientUserIds = await getRecipientUserIdsForFeature(knex, ctx.tenantId, input.requiredFeature)\n\n if (recipientUserIds.length === 0) {\n debug('No users found with feature:', input.requiredFeature, 'in tenant:', ctx.tenantId)\n return []\n }\n\n debug('Creating notifications for', recipientUserIds.length, 'user(s) with feature:', input.requiredFeature)\n\n const { requiredFeature: _requiredFeature, ...content } = input\n const notifications: Notification[] = []\n const uniqueRecipientUserIds = Array.from(new Set(recipientUserIds))\n const writeEm = rootEm.fork()\n\n await writeEm.transactional(async (tx) => {\n for (const recipientUserId of uniqueRecipientUserIds) {\n const notification = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n notifications.push(notification)\n }\n await tx.flush()\n })\n\n await emitNotificationCreatedBatch(eventBus, notifications, ctx)\n\n return notifications\n },\n\n async markAsRead(notificationId, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n if (notification.status === 'unread') {\n notification.status = 'read'\n notification.readAt = new Date()\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.READ, {\n notificationId: notification.id,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n }\n\n return notification\n },\n\n async markAllAsRead(ctx) {\n const em = rootEm.fork()\n const knex = getKnex(em)\n\n const result = await knex('notifications')\n .where({\n recipient_user_id: ctx.userId,\n tenant_id: ctx.tenantId,\n status: 'unread',\n })\n .update({\n status: 'read',\n read_at: knex.fn.now(),\n })\n\n return result\n },\n\n async dismiss(notificationId, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n notification.status = 'dismissed'\n notification.dismissedAt = new Date()\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.DISMISSED, {\n notificationId: notification.id,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n return notification\n },\n\n async restoreDismissed(notificationId, status, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n if (notification.status !== 'dismissed') {\n return notification\n }\n\n const targetStatus = status ?? 'read'\n notification.status = targetStatus\n notification.dismissedAt = null\n\n if (targetStatus === 'unread') {\n notification.readAt = null\n } else if (!notification.readAt) {\n notification.readAt = new Date()\n }\n\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.RESTORED, {\n notificationId: notification.id,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n status: targetStatus,\n })\n\n return notification\n },\n\n async executeAction(notificationId, input, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n const actionData = notification.actionData\n const action = actionData?.actions?.find((a) => a.id === input.actionId)\n\n if (!action) {\n throw new Error('Action not found')\n }\n\n let result: unknown = null\n\n if (action.commandId && commandBus && container) {\n const commandInput = {\n id: notification.sourceEntityId,\n ...input.payload,\n }\n\n // Build a CommandRuntimeContext from the notification service context\n const commandCtx = {\n container,\n auth: {\n sub: ctx.userId,\n tenantId: ctx.tenantId,\n orgId: ctx.organizationId,\n },\n organizationScope: null,\n selectedOrganizationId: ctx.organizationId ?? null,\n organizationIds: ctx.organizationId ? [ctx.organizationId] : null,\n }\n\n const commandResult = await commandBus.execute(action.commandId, {\n input: commandInput,\n ctx: commandCtx,\n metadata: {\n tenantId: ctx.tenantId,\n organizationId: ctx.organizationId,\n resourceKind: 'notifications',\n },\n })\n\n result = commandResult.result\n }\n\n notification.status = 'actioned'\n notification.actionedAt = new Date()\n notification.actionTaken = input.actionId\n notification.actionResult = result as Record<string, unknown>\n\n if (!notification.readAt) {\n notification.readAt = new Date()\n }\n\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.ACTIONED, {\n notificationId: notification.id,\n actionId: input.actionId,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n return { notification, result }\n },\n\n async getUnreadCount(ctx) {\n const em = rootEm.fork()\n return em.count(Notification, {\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n status: 'unread',\n })\n },\n\n async getPollData(ctx, since) {\n const em = rootEm.fork()\n const filters: Record<string, unknown> = {\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n }\n\n if (since) {\n filters.createdAt = { $gt: new Date(since) }\n }\n\n const [notifications, unreadCount] = await Promise.all([\n em.find(Notification, filters, {\n orderBy: { createdAt: 'desc' },\n limit: 50,\n }),\n em.count(Notification, {\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n status: 'unread',\n }),\n ])\n\n const recent = notifications.map(toNotificationDto)\n const hasNew = since ? recent.length > 0 : false\n\n return {\n unreadCount,\n recent,\n hasNew,\n lastId: recent[0]?.id,\n }\n },\n\n async cleanupExpired() {\n const em = rootEm.fork()\n const knex = getKnex(em)\n\n const result = await knex('notifications')\n .where('expires_at', '<', knex.fn.now())\n .whereNotIn('status', ['actioned', 'dismissed'])\n .update({\n status: 'dismissed',\n dismissed_at: knex.fn.now(),\n })\n\n return result\n },\n\n async deleteBySource(sourceEntityType, sourceEntityId, ctx) {\n const em = rootEm.fork()\n const knex = getKnex(em)\n\n const result = await knex('notifications')\n .where({\n source_entity_type: sourceEntityType,\n source_entity_id: sourceEntityId,\n tenant_id: ctx.tenantId,\n })\n .delete()\n\n return result\n },\n }\n}\n\n/**\n * Helper to create notification service from a DI container.\n * Use this in API routes and commands to avoid DI resolution issues.\n */\nexport function resolveNotificationService(container: {\n resolve: (name: string) => unknown\n}): NotificationService {\n const em = container.resolve('em') as EntityManager\n const eventBus = container.resolve('eventBus') as { emit: (event: string, payload: unknown) => Promise<void> }\n\n // commandBus may not be registered in all contexts, so resolve it safely\n let commandBus: NotificationServiceDeps['commandBus']\n try {\n commandBus = container.resolve('commandBus') as typeof commandBus\n } catch {\n // commandBus not available - actions with commandId won't work\n commandBus = undefined\n }\n\n return createNotificationService({ em, eventBus, commandBus, container })\n}\n"],
|
|
5
|
-
"mappings": "AAEA,SAAS,oBAA6C;AAGtD,SAAS,
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/core'\nimport type { Knex } from 'knex'\nimport { Notification, type NotificationStatus } from '../data/entities'\nimport type { CreateNotificationInput, CreateBatchNotificationInput, CreateRoleNotificationInput, CreateFeatureNotificationInput, ExecuteActionInput } from '../data/validators'\nimport type { NotificationPollData } from '@open-mercato/shared/modules/notifications/types'\nimport { NOTIFICATION_EVENTS, NOTIFICATION_SSE_EVENTS } from './events'\nimport {\n buildNotificationEntity,\n emitNotificationCreated,\n emitNotificationCreatedBatch,\n type NotificationContentInput,\n type NotificationTenantContext,\n} from './notificationFactory'\nimport { toNotificationDto } from './notificationMapper'\nimport { getRecipientUserIdsForFeature, getRecipientUserIdsForRole } from './notificationRecipients'\nimport { assertSafeNotificationHref, sanitizeNotificationActions } from './safeHref'\n\nconst DEBUG = process.env.NOTIFICATIONS_DEBUG === 'true'\n\nfunction debug(...args: unknown[]): void {\n if (DEBUG) {\n console.log('[notifications]', ...args)\n }\n}\n\nfunction getKnex(em: EntityManager): Knex {\n return (em.getConnection() as unknown as { getKnex: () => Knex }).getKnex()\n}\n\nconst UNIQUE_NOTIFICATION_ACTIVE_STATUSES: NotificationStatus[] = ['unread', 'read', 'actioned']\n\nfunction normalizeOrgScope(organizationId: string | null | undefined): string | null {\n return organizationId ?? null\n}\n\nfunction applyNotificationContent(\n notification: Notification,\n input: NotificationContentInput,\n recipientUserId: string,\n ctx: NotificationTenantContext,\n) {\n const actions = sanitizeNotificationActions(input.actions)\n const linkHref = assertSafeNotificationHref(input.linkHref)\n\n notification.recipientUserId = recipientUserId\n notification.type = input.type\n notification.titleKey = input.titleKey\n notification.bodyKey = input.bodyKey\n notification.titleVariables = input.titleVariables\n notification.bodyVariables = input.bodyVariables\n notification.title = input.title || input.titleKey || ''\n notification.body = input.body\n notification.icon = input.icon\n notification.severity = input.severity ?? 'info'\n notification.actionData = actions\n ? {\n actions,\n primaryActionId: input.primaryActionId,\n }\n : null\n notification.sourceModule = input.sourceModule\n notification.sourceEntityType = input.sourceEntityType\n notification.sourceEntityId = input.sourceEntityId\n notification.linkHref = linkHref\n notification.groupKey = input.groupKey\n notification.expiresAt = input.expiresAt ? new Date(input.expiresAt) : null\n notification.tenantId = ctx.tenantId\n notification.organizationId = normalizeOrgScope(ctx.organizationId)\n notification.status = 'unread'\n notification.readAt = null\n notification.actionedAt = null\n notification.dismissedAt = null\n notification.actionTaken = null\n notification.actionResult = null\n notification.createdAt = new Date()\n}\n\nasync function emitNotificationSseEvents(\n eventBus: { emit: (event: string, payload: unknown) => Promise<void> },\n notifications: Notification[],\n ctx: NotificationServiceContext,\n recipientUserIds: string[],\n): Promise<void> {\n await eventBus.emit(NOTIFICATION_SSE_EVENTS.BATCH_CREATED, {\n tenantId: ctx.tenantId,\n organizationId: normalizeOrgScope(ctx.organizationId),\n recipientUserIds,\n count: notifications.length,\n })\n\n for (const notification of notifications) {\n await eventBus.emit(NOTIFICATION_SSE_EVENTS.CREATED, {\n tenantId: notification.tenantId,\n organizationId: notification.organizationId ?? null,\n recipientUserId: notification.recipientUserId,\n notification: toNotificationDto(notification),\n })\n }\n}\n\nasync function createOrRefreshNotification(\n em: EntityManager,\n input: NotificationContentInput,\n recipientUserId: string,\n ctx: NotificationTenantContext,\n): Promise<Notification> {\n if (input.groupKey && input.groupKey.trim().length > 0) {\n const orgScope = normalizeOrgScope(ctx.organizationId) ?? 'global'\n const lockKey = `notifications:${ctx.tenantId}:${orgScope}:${recipientUserId}:${input.type}:${input.groupKey}`\n try {\n const knex = getKnex(em)\n await knex.raw('select pg_advisory_xact_lock(hashtext(?))', [lockKey])\n } catch {\n // If advisory locks are unavailable, continue with best-effort dedupe.\n }\n\n const existing = await em.findOne(Notification, {\n recipientUserId,\n tenantId: ctx.tenantId,\n organizationId: normalizeOrgScope(ctx.organizationId),\n type: input.type,\n groupKey: input.groupKey,\n status: { $in: UNIQUE_NOTIFICATION_ACTIVE_STATUSES },\n }, {\n orderBy: { createdAt: 'desc' },\n })\n\n if (existing) {\n applyNotificationContent(existing, input, recipientUserId, ctx)\n return existing\n }\n }\n\n return buildNotificationEntity(em, input, recipientUserId, ctx)\n}\n\nexport interface NotificationServiceContext {\n tenantId: string\n organizationId?: string | null\n userId?: string | null\n}\n\nexport interface NotificationService {\n create(input: CreateNotificationInput, ctx: NotificationServiceContext): Promise<Notification>\n createBatch(input: CreateBatchNotificationInput, ctx: NotificationServiceContext): Promise<Notification[]>\n createForRole(input: CreateRoleNotificationInput, ctx: NotificationServiceContext): Promise<Notification[]>\n createForFeature(input: CreateFeatureNotificationInput, ctx: NotificationServiceContext): Promise<Notification[]>\n markAsRead(notificationId: string, ctx: NotificationServiceContext): Promise<Notification>\n markAllAsRead(ctx: NotificationServiceContext): Promise<number>\n dismiss(notificationId: string, ctx: NotificationServiceContext): Promise<Notification>\n restoreDismissed(\n notificationId: string,\n status: 'read' | 'unread' | undefined,\n ctx: NotificationServiceContext\n ): Promise<Notification>\n executeAction(\n notificationId: string,\n input: ExecuteActionInput,\n ctx: NotificationServiceContext\n ): Promise<{ notification: Notification; result: unknown }>\n getUnreadCount(ctx: NotificationServiceContext): Promise<number>\n getPollData(ctx: NotificationServiceContext, since?: string): Promise<NotificationPollData>\n cleanupExpired(): Promise<number>\n deleteBySource(\n sourceEntityType: string,\n sourceEntityId: string,\n ctx: NotificationServiceContext\n ): Promise<number>\n}\n\nexport interface NotificationServiceDeps {\n em: EntityManager\n eventBus: { emit: (event: string, payload: unknown) => Promise<void> }\n commandBus?: {\n execute: (\n commandId: string,\n options: { input: unknown; ctx: unknown; metadata?: unknown }\n ) => Promise<{ result: unknown }>\n }\n container?: { resolve: (name: string) => unknown }\n}\n\nexport function createNotificationService(deps: NotificationServiceDeps): NotificationService {\n const { em: rootEm, eventBus, commandBus, container } = deps\n\n return {\n async create(input, ctx) {\n const { recipientUserId, ...content } = input\n const writeEm = rootEm.fork()\n const notification = await writeEm.transactional(async (tx) => {\n const entity = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n await tx.flush()\n return entity\n })\n\n await emitNotificationCreated(eventBus, notification, ctx)\n await eventBus.emit(NOTIFICATION_SSE_EVENTS.CREATED, {\n tenantId: notification.tenantId,\n organizationId: notification.organizationId ?? null,\n recipientUserId: notification.recipientUserId,\n notification: toNotificationDto(notification),\n })\n\n return notification\n },\n\n async createBatch(input, ctx) {\n const recipientUserIds = Array.from(new Set(input.recipientUserIds))\n const { recipientUserIds: _recipientUserIds, ...content } = input\n const notifications: Notification[] = []\n const writeEm = rootEm.fork()\n\n await writeEm.transactional(async (tx) => {\n for (const recipientUserId of recipientUserIds) {\n const notification = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n notifications.push(notification)\n }\n await tx.flush()\n })\n\n await emitNotificationCreatedBatch(eventBus, notifications, ctx)\n await emitNotificationSseEvents(eventBus, notifications, ctx, recipientUserIds)\n\n return notifications\n },\n\n async createForRole(input, ctx) {\n const em = rootEm.fork()\n\n const knex = getKnex(em)\n const recipientUserIds = await getRecipientUserIdsForRole(knex, ctx.tenantId, input.roleId)\n if (recipientUserIds.length === 0) {\n return []\n }\n\n const { roleId: _roleId, ...content } = input\n const notifications: Notification[] = []\n const uniqueRecipientUserIds = Array.from(new Set(recipientUserIds))\n const writeEm = rootEm.fork()\n\n await writeEm.transactional(async (tx) => {\n for (const recipientUserId of uniqueRecipientUserIds) {\n const notification = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n notifications.push(notification)\n }\n await tx.flush()\n })\n\n await emitNotificationCreatedBatch(eventBus, notifications, ctx)\n await emitNotificationSseEvents(eventBus, notifications, ctx, uniqueRecipientUserIds)\n\n return notifications\n },\n\n async createForFeature(input, ctx) {\n const em = rootEm.fork()\n const knex = getKnex(em)\n const recipientUserIds = await getRecipientUserIdsForFeature(knex, ctx.tenantId, input.requiredFeature)\n\n if (recipientUserIds.length === 0) {\n debug('No users found with feature:', input.requiredFeature, 'in tenant:', ctx.tenantId)\n return []\n }\n\n debug('Creating notifications for', recipientUserIds.length, 'user(s) with feature:', input.requiredFeature)\n\n const { requiredFeature: _requiredFeature, ...content } = input\n const notifications: Notification[] = []\n const uniqueRecipientUserIds = Array.from(new Set(recipientUserIds))\n const writeEm = rootEm.fork()\n\n await writeEm.transactional(async (tx) => {\n for (const recipientUserId of uniqueRecipientUserIds) {\n const notification = await createOrRefreshNotification(tx, content, recipientUserId, ctx)\n notifications.push(notification)\n }\n await tx.flush()\n })\n\n await emitNotificationCreatedBatch(eventBus, notifications, ctx)\n await emitNotificationSseEvents(eventBus, notifications, ctx, uniqueRecipientUserIds)\n\n return notifications\n },\n\n async markAsRead(notificationId, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n if (notification.status === 'unread') {\n notification.status = 'read'\n notification.readAt = new Date()\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.READ, {\n notificationId: notification.id,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n }\n\n return notification\n },\n\n async markAllAsRead(ctx) {\n const em = rootEm.fork()\n const knex = getKnex(em)\n\n const result = await knex('notifications')\n .where({\n recipient_user_id: ctx.userId,\n tenant_id: ctx.tenantId,\n status: 'unread',\n })\n .update({\n status: 'read',\n read_at: knex.fn.now(),\n })\n\n return result\n },\n\n async dismiss(notificationId, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n notification.status = 'dismissed'\n notification.dismissedAt = new Date()\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.DISMISSED, {\n notificationId: notification.id,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n return notification\n },\n\n async restoreDismissed(notificationId, status, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n if (notification.status !== 'dismissed') {\n return notification\n }\n\n const targetStatus = status ?? 'read'\n notification.status = targetStatus\n notification.dismissedAt = null\n\n if (targetStatus === 'unread') {\n notification.readAt = null\n } else if (!notification.readAt) {\n notification.readAt = new Date()\n }\n\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.RESTORED, {\n notificationId: notification.id,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n status: targetStatus,\n })\n\n return notification\n },\n\n async executeAction(notificationId, input, ctx) {\n const em = rootEm.fork()\n const notification = await em.findOneOrFail(Notification, {\n id: notificationId,\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n const actionData = notification.actionData\n const action = actionData?.actions?.find((a) => a.id === input.actionId)\n\n if (!action) {\n throw new Error('Action not found')\n }\n\n let result: unknown = null\n\n if (action.commandId && commandBus && container) {\n const commandInput = {\n id: notification.sourceEntityId,\n ...input.payload,\n }\n\n // Build a CommandRuntimeContext from the notification service context\n const commandCtx = {\n container,\n auth: {\n sub: ctx.userId,\n tenantId: ctx.tenantId,\n orgId: ctx.organizationId,\n },\n organizationScope: null,\n selectedOrganizationId: ctx.organizationId ?? null,\n organizationIds: ctx.organizationId ? [ctx.organizationId] : null,\n }\n\n const commandResult = await commandBus.execute(action.commandId, {\n input: commandInput,\n ctx: commandCtx,\n metadata: {\n tenantId: ctx.tenantId,\n organizationId: ctx.organizationId,\n resourceKind: 'notifications',\n },\n })\n\n result = commandResult.result\n }\n\n notification.status = 'actioned'\n notification.actionedAt = new Date()\n notification.actionTaken = input.actionId\n notification.actionResult = result as Record<string, unknown>\n\n if (!notification.readAt) {\n notification.readAt = new Date()\n }\n\n await em.flush()\n\n await eventBus.emit(NOTIFICATION_EVENTS.ACTIONED, {\n notificationId: notification.id,\n actionId: input.actionId,\n userId: ctx.userId,\n tenantId: ctx.tenantId,\n })\n\n return { notification, result }\n },\n\n async getUnreadCount(ctx) {\n const em = rootEm.fork()\n return em.count(Notification, {\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n status: 'unread',\n })\n },\n\n async getPollData(ctx, since) {\n const em = rootEm.fork()\n const filters: Record<string, unknown> = {\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n }\n\n if (since) {\n filters.createdAt = { $gt: new Date(since) }\n }\n\n const [notifications, unreadCount] = await Promise.all([\n em.find(Notification, filters, {\n orderBy: { createdAt: 'desc' },\n limit: 50,\n }),\n em.count(Notification, {\n recipientUserId: ctx.userId,\n tenantId: ctx.tenantId,\n status: 'unread',\n }),\n ])\n\n const recent = notifications.map(toNotificationDto)\n const hasNew = since ? recent.length > 0 : false\n\n return {\n unreadCount,\n recent,\n hasNew,\n lastId: recent[0]?.id,\n }\n },\n\n async cleanupExpired() {\n const em = rootEm.fork()\n const knex = getKnex(em)\n\n const result = await knex('notifications')\n .where('expires_at', '<', knex.fn.now())\n .whereNotIn('status', ['actioned', 'dismissed'])\n .update({\n status: 'dismissed',\n dismissed_at: knex.fn.now(),\n })\n\n return result\n },\n\n async deleteBySource(sourceEntityType, sourceEntityId, ctx) {\n const em = rootEm.fork()\n const knex = getKnex(em)\n\n const result = await knex('notifications')\n .where({\n source_entity_type: sourceEntityType,\n source_entity_id: sourceEntityId,\n tenant_id: ctx.tenantId,\n })\n .delete()\n\n return result\n },\n }\n}\n\n/**\n * Helper to create notification service from a DI container.\n * Use this in API routes and commands to avoid DI resolution issues.\n */\nexport function resolveNotificationService(container: {\n resolve: (name: string) => unknown\n}): NotificationService {\n const em = container.resolve('em') as EntityManager\n const eventBus = container.resolve('eventBus') as { emit: (event: string, payload: unknown) => Promise<void> }\n\n // commandBus may not be registered in all contexts, so resolve it safely\n let commandBus: NotificationServiceDeps['commandBus']\n try {\n commandBus = container.resolve('commandBus') as typeof commandBus\n } catch {\n // commandBus not available - actions with commandId won't work\n commandBus = undefined\n }\n\n return createNotificationService({ em, eventBus, commandBus, container })\n}\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,oBAA6C;AAGtD,SAAS,qBAAqB,+BAA+B;AAC7D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP,SAAS,yBAAyB;AAClC,SAAS,+BAA+B,kCAAkC;AAC1E,SAAS,4BAA4B,mCAAmC;AAExE,MAAM,QAAQ,QAAQ,IAAI,wBAAwB;AAElD,SAAS,SAAS,MAAuB;AACvC,MAAI,OAAO;AACT,YAAQ,IAAI,mBAAmB,GAAG,IAAI;AAAA,EACxC;AACF;AAEA,SAAS,QAAQ,IAAyB;AACxC,SAAQ,GAAG,cAAc,EAAyC,QAAQ;AAC5E;AAEA,MAAM,sCAA4D,CAAC,UAAU,QAAQ,UAAU;AAE/F,SAAS,kBAAkB,gBAA0D;AACnF,SAAO,kBAAkB;AAC3B;AAEA,SAAS,yBACP,cACA,OACA,iBACA,KACA;AACA,QAAM,UAAU,4BAA4B,MAAM,OAAO;AACzD,QAAM,WAAW,2BAA2B,MAAM,QAAQ;AAE1D,eAAa,kBAAkB;AAC/B,eAAa,OAAO,MAAM;AAC1B,eAAa,WAAW,MAAM;AAC9B,eAAa,UAAU,MAAM;AAC7B,eAAa,iBAAiB,MAAM;AACpC,eAAa,gBAAgB,MAAM;AACnC,eAAa,QAAQ,MAAM,SAAS,MAAM,YAAY;AACtD,eAAa,OAAO,MAAM;AAC1B,eAAa,OAAO,MAAM;AAC1B,eAAa,WAAW,MAAM,YAAY;AAC1C,eAAa,aAAa,UACtB;AAAA,IACE;AAAA,IACA,iBAAiB,MAAM;AAAA,EACzB,IACA;AACJ,eAAa,eAAe,MAAM;AAClC,eAAa,mBAAmB,MAAM;AACtC,eAAa,iBAAiB,MAAM;AACpC,eAAa,WAAW;AACxB,eAAa,WAAW,MAAM;AAC9B,eAAa,YAAY,MAAM,YAAY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;AAAA,IAEA,MAAM,eAAe,kBAAkB,gBAAgB,KAAK;AAC1D,YAAM,KAAK,OAAO,KAAK;AACvB,YAAM,OAAO,QAAQ,EAAE;AAEvB,YAAM,SAAS,MAAM,KAAK,eAAe,EACtC,MAAM;AAAA,QACL,oBAAoB;AAAA,QACpB,kBAAkB;AAAA,QAClB,WAAW,IAAI;AAAA,MACjB,CAAC,EACA,OAAO;AAEV,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAMO,SAAS,2BAA2B,WAEnB;AACtB,QAAM,KAAK,UAAU,QAAQ,IAAI;AACjC,QAAM,WAAW,UAAU,QAAQ,UAAU;AAG7C,MAAI;AACJ,MAAI;AACF,iBAAa,UAAU,QAAQ,YAAY;AAAA,EAC7C,QAAQ;AAEN,iBAAa;AAAA,EACf;AAEA,SAAO,0BAA0B,EAAE,IAAI,UAAU,YAAY,UAAU,CAAC;AAC1E;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { createModuleEvents } from "@open-mercato/shared/modules/events";
|
|
2
2
|
const events = [
|
|
3
|
-
{ id: "progress.job.created", label: "Job Created", entity: "job", category: "crud" },
|
|
4
|
-
{ id: "progress.job.started", label: "Job Started", entity: "job", category: "lifecycle" },
|
|
5
|
-
{ id: "progress.job.updated", label: "Job Updated", entity: "job", category: "lifecycle" },
|
|
6
|
-
{ id: "progress.job.completed", label: "Job Completed", entity: "job", category: "lifecycle" },
|
|
7
|
-
{ id: "progress.job.failed", label: "Job Failed", entity: "job", category: "lifecycle" },
|
|
8
|
-
{ id: "progress.job.cancelled", label: "Job Cancelled", entity: "job", category: "lifecycle" }
|
|
3
|
+
{ id: "progress.job.created", label: "Job Created", entity: "job", category: "crud", clientBroadcast: true },
|
|
4
|
+
{ id: "progress.job.started", label: "Job Started", entity: "job", category: "lifecycle", clientBroadcast: true },
|
|
5
|
+
{ id: "progress.job.updated", label: "Job Updated", entity: "job", category: "lifecycle", clientBroadcast: true },
|
|
6
|
+
{ id: "progress.job.completed", label: "Job Completed", entity: "job", category: "lifecycle", clientBroadcast: true },
|
|
7
|
+
{ id: "progress.job.failed", label: "Job Failed", entity: "job", category: "lifecycle", clientBroadcast: true },
|
|
8
|
+
{ id: "progress.job.cancelled", label: "Job Cancelled", entity: "job", category: "lifecycle", clientBroadcast: true }
|
|
9
9
|
];
|
|
10
10
|
const eventsConfig = createModuleEvents({
|
|
11
11
|
moduleId: "progress",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/modules/progress/events.ts"],
|
|
4
|
-
"sourcesContent": ["import { createModuleEvents } from '@open-mercato/shared/modules/events'\n\nexport const events = [\n { id: 'progress.job.created', label: 'Job Created', entity: 'job', category: 'crud' },\n { id: 'progress.job.started', label: 'Job Started', entity: 'job', category: 'lifecycle' },\n { id: 'progress.job.updated', label: 'Job Updated', entity: 'job', category: 'lifecycle' },\n { id: 'progress.job.completed', label: 'Job Completed', entity: 'job', category: 'lifecycle' },\n { id: 'progress.job.failed', label: 'Job Failed', entity: 'job', category: 'lifecycle' },\n { id: 'progress.job.cancelled', label: 'Job Cancelled', entity: 'job', category: 'lifecycle' },\n] as const\n\nexport const eventsConfig = createModuleEvents({\n moduleId: 'progress',\n events,\n})\n\nexport const emitProgressEvent = eventsConfig.emit\n\nexport type ProgressEventId = typeof events[number]['id']\n\nexport default eventsConfig\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,0BAA0B;AAE5B,MAAM,SAAS;AAAA,EACpB,EAAE,IAAI,wBAAwB,OAAO,eAAe,QAAQ,OAAO,UAAU,
|
|
4
|
+
"sourcesContent": ["import { createModuleEvents } from '@open-mercato/shared/modules/events'\n\nexport const events = [\n { id: 'progress.job.created', label: 'Job Created', entity: 'job', category: 'crud', clientBroadcast: true },\n { id: 'progress.job.started', label: 'Job Started', entity: 'job', category: 'lifecycle', clientBroadcast: true },\n { id: 'progress.job.updated', label: 'Job Updated', entity: 'job', category: 'lifecycle', clientBroadcast: true },\n { id: 'progress.job.completed', label: 'Job Completed', entity: 'job', category: 'lifecycle', clientBroadcast: true },\n { id: 'progress.job.failed', label: 'Job Failed', entity: 'job', category: 'lifecycle', clientBroadcast: true },\n { id: 'progress.job.cancelled', label: 'Job Cancelled', entity: 'job', category: 'lifecycle', clientBroadcast: true },\n] as const\n\nexport const eventsConfig = createModuleEvents({\n moduleId: 'progress',\n events,\n})\n\nexport const emitProgressEvent = eventsConfig.emit\n\nexport type ProgressEventId = typeof events[number]['id']\n\nexport default eventsConfig\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,0BAA0B;AAE5B,MAAM,SAAS;AAAA,EACpB,EAAE,IAAI,wBAAwB,OAAO,eAAe,QAAQ,OAAO,UAAU,QAAQ,iBAAiB,KAAK;AAAA,EAC3G,EAAE,IAAI,wBAAwB,OAAO,eAAe,QAAQ,OAAO,UAAU,aAAa,iBAAiB,KAAK;AAAA,EAChH,EAAE,IAAI,wBAAwB,OAAO,eAAe,QAAQ,OAAO,UAAU,aAAa,iBAAiB,KAAK;AAAA,EAChH,EAAE,IAAI,0BAA0B,OAAO,iBAAiB,QAAQ,OAAO,UAAU,aAAa,iBAAiB,KAAK;AAAA,EACpH,EAAE,IAAI,uBAAuB,OAAO,cAAc,QAAQ,OAAO,UAAU,aAAa,iBAAiB,KAAK;AAAA,EAC9G,EAAE,IAAI,0BAA0B,OAAO,iBAAiB,QAAQ,OAAO,UAAU,aAAa,iBAAiB,KAAK;AACtH;AAEO,MAAM,eAAe,mBAAmB;AAAA,EAC7C,UAAU;AAAA,EACV;AACF,CAAC;AAEM,MAAM,oBAAoB,aAAa;AAI9C,IAAO,iBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/progress/lib/events.ts"],
|
|
4
|
-
"sourcesContent": ["import { events } from '../events'\n\ntype EventEntry = typeof events[number]\ntype EventMap = { [K in EventEntry['id'] as Uppercase<K extends `progress.job.${infer A}` ? `JOB_${A}` : never>]: K }\n\nfunction buildEventMap<T extends readonly { id: string }[]>(defs: T) {\n const map: Record<string, string> = {}\n for (const def of defs) {\n const suffix = def.id.replace('progress.job.', '')\n map[`JOB_${suffix.toUpperCase()}`] = def.id\n }\n return map as EventMap\n}\n\nexport const PROGRESS_EVENTS = buildEventMap(events)\n\nexport type ProgressJobCreatedPayload = {\n jobId: string\n jobType: string\n name: string\n tenantId: string\n organizationId?: string | null\n}\n\nexport type ProgressJobStartedPayload = {\n jobId: string\n jobType: string\n tenantId: string\n}\n\nexport type ProgressJobUpdatedPayload = {\n jobId: string\n jobType?: string\n progressPercent: number\n processedCount: number\n totalCount?: number | null\n etaSeconds?: number | null\n tenantId: string\n}\n\nexport type ProgressJobCompletedPayload = {\n jobId: string\n jobType: string\n resultSummary?: Record<string, unknown> | null\n tenantId: string\n}\n\nexport type ProgressJobFailedPayload = {\n jobId: string\n jobType: string\n errorMessage: string\n tenantId: string\n stale?: boolean\n}\n\nexport type ProgressJobCancelledPayload = {\n jobId: string\n jobType: string\n tenantId: string\n}\n"],
|
|
4
|
+
"sourcesContent": ["import { events } from '../events'\n\ntype EventEntry = typeof events[number]\ntype EventMap = { [K in EventEntry['id'] as Uppercase<K extends `progress.job.${infer A}` ? `JOB_${A}` : never>]: K }\n\nfunction buildEventMap<T extends readonly { id: string }[]>(defs: T) {\n const map: Record<string, string> = {}\n for (const def of defs) {\n const suffix = def.id.replace('progress.job.', '')\n map[`JOB_${suffix.toUpperCase()}`] = def.id\n }\n return map as EventMap\n}\n\nexport const PROGRESS_EVENTS = buildEventMap(events)\n\nexport type ProgressJobCreatedPayload = {\n jobId: string\n jobType: string\n name: string\n description?: string | null\n status?: string\n progressPercent?: number\n processedCount?: number\n totalCount?: number | null\n etaSeconds?: number | null\n cancellable?: boolean\n startedAt?: string | null\n finishedAt?: string | null\n tenantId: string\n organizationId?: string | null\n}\n\nexport type ProgressJobStartedPayload = {\n jobId: string\n jobType: string\n name?: string\n description?: string | null\n status?: string\n progressPercent?: number\n processedCount?: number\n totalCount?: number | null\n etaSeconds?: number | null\n cancellable?: boolean\n startedAt?: string | null\n finishedAt?: string | null\n tenantId: string\n organizationId?: string | null\n}\n\nexport type ProgressJobUpdatedPayload = {\n jobId: string\n jobType?: string\n name?: string\n description?: string | null\n status?: string\n progressPercent: number\n processedCount: number\n totalCount?: number | null\n etaSeconds?: number | null\n tenantId: string\n organizationId?: string | null\n cancellable?: boolean\n startedAt?: string | null\n finishedAt?: string | null\n}\n\nexport type ProgressJobCompletedPayload = {\n jobId: string\n jobType: string\n name?: string\n description?: string | null\n status?: string\n progressPercent?: number\n processedCount?: number\n totalCount?: number | null\n etaSeconds?: number | null\n cancellable?: boolean\n startedAt?: string | null\n finishedAt?: string | null\n resultSummary?: Record<string, unknown> | null\n tenantId: string\n organizationId?: string | null\n}\n\nexport type ProgressJobFailedPayload = {\n jobId: string\n jobType: string\n name?: string\n description?: string | null\n status?: string\n progressPercent?: number\n processedCount?: number\n totalCount?: number | null\n etaSeconds?: number | null\n cancellable?: boolean\n startedAt?: string | null\n finishedAt?: string | null\n errorMessage: string\n tenantId: string\n organizationId?: string | null\n stale?: boolean\n}\n\nexport type ProgressJobCancelledPayload = {\n jobId: string\n jobType: string\n name?: string\n description?: string | null\n status?: string\n progressPercent?: number\n processedCount?: number\n totalCount?: number | null\n etaSeconds?: number | null\n cancellable?: boolean\n startedAt?: string | null\n finishedAt?: string | null\n tenantId: string\n organizationId?: string | null\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,cAAc;AAKvB,SAAS,cAAmD,MAAS;AACnE,QAAM,MAA8B,CAAC;AACrC,aAAW,OAAO,MAAM;AACtB,UAAM,SAAS,IAAI,GAAG,QAAQ,iBAAiB,EAAE;AACjD,QAAI,OAAO,OAAO,YAAY,CAAC,EAAE,IAAI,IAAI;AAAA,EAC3C;AACA,SAAO;AACT;AAEO,MAAM,kBAAkB,cAAc,MAAM;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { ProgressJob } from "../data/entities.js";
|
|
2
2
|
import { calculateEta, calculateProgressPercent, STALE_JOB_TIMEOUT_SECONDS } from "./progressService.js";
|
|
3
3
|
import { PROGRESS_EVENTS } from "./events.js";
|
|
4
|
+
function buildJobPayload(job) {
|
|
5
|
+
return {
|
|
6
|
+
jobId: job.id,
|
|
7
|
+
jobType: job.jobType,
|
|
8
|
+
name: job.name,
|
|
9
|
+
description: job.description ?? null,
|
|
10
|
+
status: job.status,
|
|
11
|
+
progressPercent: job.progressPercent,
|
|
12
|
+
processedCount: job.processedCount,
|
|
13
|
+
totalCount: job.totalCount ?? null,
|
|
14
|
+
etaSeconds: job.etaSeconds ?? null,
|
|
15
|
+
cancellable: job.cancellable,
|
|
16
|
+
startedAt: job.startedAt?.toISOString() ?? null,
|
|
17
|
+
finishedAt: job.finishedAt?.toISOString() ?? null
|
|
18
|
+
};
|
|
19
|
+
}
|
|
4
20
|
function createProgressService(em, eventBus) {
|
|
5
21
|
return {
|
|
6
22
|
async createJob(input, ctx) {
|
|
@@ -21,9 +37,7 @@ function createProgressService(em, eventBus) {
|
|
|
21
37
|
});
|
|
22
38
|
await em.persistAndFlush(job);
|
|
23
39
|
await eventBus.emit(PROGRESS_EVENTS.JOB_CREATED, {
|
|
24
|
-
|
|
25
|
-
jobType: job.jobType,
|
|
26
|
-
name: job.name,
|
|
40
|
+
...buildJobPayload(job),
|
|
27
41
|
tenantId: ctx.tenantId,
|
|
28
42
|
organizationId: ctx.organizationId
|
|
29
43
|
});
|
|
@@ -36,9 +50,9 @@ function createProgressService(em, eventBus) {
|
|
|
36
50
|
job.heartbeatAt = /* @__PURE__ */ new Date();
|
|
37
51
|
await em.flush();
|
|
38
52
|
await eventBus.emit(PROGRESS_EVENTS.JOB_STARTED, {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
53
|
+
...buildJobPayload(job),
|
|
54
|
+
tenantId: ctx.tenantId,
|
|
55
|
+
organizationId: job.organizationId ?? null
|
|
42
56
|
});
|
|
43
57
|
return job;
|
|
44
58
|
},
|
|
@@ -66,13 +80,9 @@ function createProgressService(em, eventBus) {
|
|
|
66
80
|
job.heartbeatAt = /* @__PURE__ */ new Date();
|
|
67
81
|
await em.flush();
|
|
68
82
|
await eventBus.emit(PROGRESS_EVENTS.JOB_UPDATED, {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
processedCount: job.processedCount,
|
|
73
|
-
totalCount: job.totalCount,
|
|
74
|
-
etaSeconds: job.etaSeconds,
|
|
75
|
-
tenantId: ctx.tenantId
|
|
83
|
+
...buildJobPayload(job),
|
|
84
|
+
tenantId: ctx.tenantId,
|
|
85
|
+
organizationId: job.organizationId ?? null
|
|
76
86
|
});
|
|
77
87
|
return job;
|
|
78
88
|
},
|
|
@@ -88,10 +98,9 @@ function createProgressService(em, eventBus) {
|
|
|
88
98
|
}
|
|
89
99
|
await em.flush();
|
|
90
100
|
await eventBus.emit(PROGRESS_EVENTS.JOB_UPDATED, {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
tenantId: ctx.tenantId
|
|
101
|
+
...buildJobPayload(job),
|
|
102
|
+
tenantId: ctx.tenantId,
|
|
103
|
+
organizationId: job.organizationId ?? null
|
|
95
104
|
});
|
|
96
105
|
return job;
|
|
97
106
|
},
|
|
@@ -107,10 +116,10 @@ function createProgressService(em, eventBus) {
|
|
|
107
116
|
}
|
|
108
117
|
await em.flush();
|
|
109
118
|
await eventBus.emit(PROGRESS_EVENTS.JOB_COMPLETED, {
|
|
110
|
-
|
|
111
|
-
jobType: job.jobType,
|
|
119
|
+
...buildJobPayload(job),
|
|
112
120
|
resultSummary: job.resultSummary,
|
|
113
|
-
tenantId: ctx.tenantId
|
|
121
|
+
tenantId: ctx.tenantId,
|
|
122
|
+
organizationId: job.organizationId ?? null
|
|
114
123
|
});
|
|
115
124
|
return job;
|
|
116
125
|
},
|
|
@@ -123,10 +132,10 @@ function createProgressService(em, eventBus) {
|
|
|
123
132
|
job.errorStack = input.errorStack;
|
|
124
133
|
await em.flush();
|
|
125
134
|
await eventBus.emit(PROGRESS_EVENTS.JOB_FAILED, {
|
|
126
|
-
|
|
127
|
-
jobType: job.jobType,
|
|
135
|
+
...buildJobPayload(job),
|
|
128
136
|
errorMessage: job.errorMessage,
|
|
129
|
-
tenantId: ctx.tenantId
|
|
137
|
+
tenantId: ctx.tenantId,
|
|
138
|
+
organizationId: job.organizationId ?? null
|
|
130
139
|
});
|
|
131
140
|
return job;
|
|
132
141
|
},
|
|
@@ -145,9 +154,9 @@ function createProgressService(em, eventBus) {
|
|
|
145
154
|
}
|
|
146
155
|
await em.flush();
|
|
147
156
|
await eventBus.emit(PROGRESS_EVENTS.JOB_CANCELLED, {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
157
|
+
...buildJobPayload(job),
|
|
158
|
+
tenantId: ctx.tenantId,
|
|
159
|
+
organizationId: job.organizationId ?? null
|
|
151
160
|
});
|
|
152
161
|
return job;
|
|
153
162
|
},
|
|
@@ -197,11 +206,11 @@ function createProgressService(em, eventBus) {
|
|
|
197
206
|
job.finishedAt = /* @__PURE__ */ new Date();
|
|
198
207
|
job.errorMessage = `Job stale: no heartbeat for ${timeoutSeconds} seconds`;
|
|
199
208
|
await eventBus.emit(PROGRESS_EVENTS.JOB_FAILED, {
|
|
200
|
-
|
|
201
|
-
jobType: job.jobType,
|
|
209
|
+
...buildJobPayload(job),
|
|
202
210
|
errorMessage: job.errorMessage,
|
|
203
211
|
tenantId: job.tenantId,
|
|
204
|
-
stale: true
|
|
212
|
+
stale: true,
|
|
213
|
+
organizationId: job.organizationId ?? null
|
|
205
214
|
});
|
|
206
215
|
}
|
|
207
216
|
await em.flush();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/progress/lib/progressServiceImpl.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/core'\nimport { ProgressJob } from '../data/entities'\nimport type { ProgressService } from './progressService'\nimport { calculateEta, calculateProgressPercent, STALE_JOB_TIMEOUT_SECONDS } from './progressService'\nimport { PROGRESS_EVENTS } from './events'\n\nexport function createProgressService(em: EntityManager, eventBus: { emit: (event: string, payload: Record<string, unknown>) => Promise<void> }): ProgressService {\n return {\n async createJob(input, ctx) {\n const job = em.create(ProgressJob, {\n jobType: input.jobType,\n name: input.name,\n description: input.description,\n totalCount: input.totalCount,\n cancellable: input.cancellable ?? false,\n meta: input.meta,\n parentJobId: input.parentJobId,\n partitionIndex: input.partitionIndex,\n partitionCount: input.partitionCount,\n startedByUserId: ctx.userId,\n tenantId: ctx.tenantId,\n organizationId: ctx.organizationId,\n status: 'pending',\n })\n\n await em.persistAndFlush(job)\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_CREATED, {\n
|
|
5
|
-
"mappings": "AACA,SAAS,mBAAmB;AAE5B,SAAS,cAAc,0BAA0B,iCAAiC;AAClF,SAAS,uBAAuB;
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/core'\nimport { ProgressJob } from '../data/entities'\nimport type { ProgressService } from './progressService'\nimport { calculateEta, calculateProgressPercent, STALE_JOB_TIMEOUT_SECONDS } from './progressService'\nimport { PROGRESS_EVENTS } from './events'\n\nfunction buildJobPayload(job: ProgressJob): Record<string, unknown> {\n return {\n jobId: job.id,\n jobType: job.jobType,\n name: job.name,\n description: job.description ?? null,\n status: job.status,\n progressPercent: job.progressPercent,\n processedCount: job.processedCount,\n totalCount: job.totalCount ?? null,\n etaSeconds: job.etaSeconds ?? null,\n cancellable: job.cancellable,\n startedAt: job.startedAt?.toISOString() ?? null,\n finishedAt: job.finishedAt?.toISOString() ?? null,\n }\n}\n\nexport function createProgressService(em: EntityManager, eventBus: { emit: (event: string, payload: Record<string, unknown>) => Promise<void> }): ProgressService {\n return {\n async createJob(input, ctx) {\n const job = em.create(ProgressJob, {\n jobType: input.jobType,\n name: input.name,\n description: input.description,\n totalCount: input.totalCount,\n cancellable: input.cancellable ?? false,\n meta: input.meta,\n parentJobId: input.parentJobId,\n partitionIndex: input.partitionIndex,\n partitionCount: input.partitionCount,\n startedByUserId: ctx.userId,\n tenantId: ctx.tenantId,\n organizationId: ctx.organizationId,\n status: 'pending',\n })\n\n await em.persistAndFlush(job)\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_CREATED, {\n ...buildJobPayload(job),\n tenantId: ctx.tenantId,\n organizationId: ctx.organizationId,\n })\n\n return job\n },\n\n async startJob(jobId, ctx) {\n const job = await em.findOneOrFail(ProgressJob, { id: jobId, tenantId: ctx.tenantId })\n\n job.status = 'running'\n job.startedAt = new Date()\n job.heartbeatAt = new Date()\n\n await em.flush()\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_STARTED, {\n ...buildJobPayload(job),\n tenantId: ctx.tenantId,\n organizationId: job.organizationId ?? null,\n })\n\n return job\n },\n\n async updateProgress(jobId, input, ctx) {\n const job = await em.findOneOrFail(ProgressJob, { id: jobId, tenantId: ctx.tenantId })\n\n if (input.processedCount !== undefined) {\n job.processedCount = input.processedCount\n }\n if (input.totalCount !== undefined) {\n job.totalCount = input.totalCount\n }\n if (input.meta !== undefined) {\n job.meta = { ...job.meta, ...input.meta }\n }\n\n if (input.progressPercent !== undefined) {\n job.progressPercent = input.progressPercent\n } else if (job.totalCount) {\n job.progressPercent = calculateProgressPercent(job.processedCount, job.totalCount)\n }\n\n if (input.etaSeconds !== undefined) {\n job.etaSeconds = input.etaSeconds\n } else if (job.startedAt && job.totalCount) {\n job.etaSeconds = calculateEta(job.processedCount, job.totalCount, job.startedAt)\n }\n\n job.heartbeatAt = new Date()\n\n await em.flush()\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_UPDATED, {\n ...buildJobPayload(job),\n tenantId: ctx.tenantId,\n organizationId: job.organizationId ?? null,\n })\n\n return job\n },\n\n async incrementProgress(jobId, delta, ctx) {\n const job = await em.findOneOrFail(ProgressJob, { id: jobId, tenantId: ctx.tenantId })\n\n job.processedCount += delta\n job.heartbeatAt = new Date()\n\n if (job.totalCount) {\n job.progressPercent = calculateProgressPercent(job.processedCount, job.totalCount)\n if (job.startedAt) {\n job.etaSeconds = calculateEta(job.processedCount, job.totalCount, job.startedAt)\n }\n }\n\n await em.flush()\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_UPDATED, {\n ...buildJobPayload(job),\n tenantId: ctx.tenantId,\n organizationId: job.organizationId ?? null,\n })\n\n return job\n },\n\n async completeJob(jobId, input, ctx) {\n const job = await em.findOne(ProgressJob, { id: jobId, tenantId: ctx.tenantId })\n if (!job) throw new Error(`Job ${jobId} not found`)\n\n job.status = 'completed'\n job.finishedAt = new Date()\n job.progressPercent = 100\n job.etaSeconds = 0\n if (input?.resultSummary) {\n job.resultSummary = input.resultSummary\n }\n\n await em.flush()\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_COMPLETED, {\n ...buildJobPayload(job),\n resultSummary: job.resultSummary,\n tenantId: ctx.tenantId,\n organizationId: job.organizationId ?? null,\n })\n\n return job\n },\n\n async failJob(jobId, input, ctx) {\n const job = await em.findOne(ProgressJob, { id: jobId, tenantId: ctx.tenantId })\n if (!job) throw new Error(`Job ${jobId} not found`)\n\n job.status = 'failed'\n job.finishedAt = new Date()\n job.errorMessage = input.errorMessage\n job.errorStack = input.errorStack\n\n await em.flush()\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_FAILED, {\n ...buildJobPayload(job),\n errorMessage: job.errorMessage,\n tenantId: ctx.tenantId,\n organizationId: job.organizationId ?? null,\n })\n\n return job\n },\n\n async cancelJob(jobId, ctx) {\n const job = await em.findOneOrFail(ProgressJob, {\n id: jobId,\n tenantId: ctx.tenantId,\n cancellable: true,\n status: { $in: ['pending', 'running'] },\n })\n\n job.cancelRequestedAt = new Date()\n job.cancelledByUserId = ctx.userId\n\n if (job.status === 'pending') {\n job.status = 'cancelled'\n job.finishedAt = new Date()\n }\n\n await em.flush()\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_CANCELLED, {\n ...buildJobPayload(job),\n tenantId: ctx.tenantId,\n organizationId: job.organizationId ?? null,\n })\n\n return job\n },\n\n async isCancellationRequested(jobId) {\n const job = await em.findOne(ProgressJob, { id: jobId })\n return job?.cancelRequestedAt != null\n },\n\n async getActiveJobs(ctx) {\n return em.find(ProgressJob, {\n tenantId: ctx.tenantId,\n ...(ctx.organizationId ? { organizationId: ctx.organizationId } : {}),\n status: { $in: ['pending', 'running'] },\n parentJobId: null,\n }, {\n orderBy: { createdAt: 'DESC' },\n limit: 50,\n })\n },\n\n async getRecentlyCompletedJobs(ctx, sinceSeconds = 30) {\n const cutoff = new Date(Date.now() - sinceSeconds * 1000)\n return em.find(ProgressJob, {\n tenantId: ctx.tenantId,\n ...(ctx.organizationId ? { organizationId: ctx.organizationId } : {}),\n status: { $in: ['completed', 'failed'] },\n finishedAt: { $gte: cutoff },\n parentJobId: null,\n }, {\n orderBy: { finishedAt: 'DESC' },\n limit: 10,\n })\n },\n\n async getJob(jobId, ctx) {\n return em.findOne(ProgressJob, {\n id: jobId,\n tenantId: ctx.tenantId,\n })\n },\n\n async markStaleJobsFailed(tenantId: string, timeoutSeconds = STALE_JOB_TIMEOUT_SECONDS) {\n const cutoff = new Date(Date.now() - timeoutSeconds * 1000)\n\n const staleJobs = await em.find(ProgressJob, {\n tenantId,\n status: 'running',\n heartbeatAt: { $lt: cutoff },\n })\n\n for (const job of staleJobs) {\n job.status = 'failed'\n job.finishedAt = new Date()\n job.errorMessage = `Job stale: no heartbeat for ${timeoutSeconds} seconds`\n\n await eventBus.emit(PROGRESS_EVENTS.JOB_FAILED, {\n ...buildJobPayload(job),\n errorMessage: job.errorMessage,\n tenantId: job.tenantId,\n stale: true,\n organizationId: job.organizationId ?? null,\n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