create-windy 0.2.34 → 0.3.0

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.
Files changed (237) hide show
  1. package/dist/cli.js +167 -17
  2. package/package.json +1 -1
  3. package/template/.windy-template.json +2 -2
  4. package/template/AGENTS.md +5 -0
  5. package/template/README.md +3 -1
  6. package/template/apps/agent-server/Dockerfile +1 -0
  7. package/template/apps/agent-server/README.md +18 -5
  8. package/template/apps/agent-server/adk_web_agents/__init__.py +1 -0
  9. package/template/apps/agent-server/adk_web_agents/safe_debug/__init__.py +3 -0
  10. package/template/apps/agent-server/adk_web_agents/safe_debug/agent.py +40 -0
  11. package/template/apps/agent-server/pyproject.toml +1 -1
  12. package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +191 -16
  13. package/template/apps/agent-server/src/southwind_agent_server/adk_failure.py +13 -0
  14. package/template/apps/agent-server/src/southwind_agent_server/adk_model.py +225 -0
  15. package/template/apps/agent-server/src/southwind_agent_server/adk_tools.py +76 -0
  16. package/template/apps/agent-server/src/southwind_agent_server/app.py +1 -2
  17. package/template/apps/agent-server/tests/test_adk_adapter.py +305 -0
  18. package/template/apps/agent-server/tests/test_adk_web_agent.py +12 -0
  19. package/template/apps/server/package.json +4 -1
  20. package/template/apps/server/src/agent/run-facade.ts +2 -7
  21. package/template/apps/server/src/agent/tool-registry.ts +2 -6
  22. package/template/apps/server/src/application-services.ts +50 -0
  23. package/template/apps/server/src/audit/drizzle-log-store.ts +25 -0
  24. package/template/apps/server/src/audit/search-summary.ts +2 -6
  25. package/template/apps/server/src/auth/credential-restriction.ts +1 -0
  26. package/template/apps/server/src/auth/password.ts +30 -21
  27. package/template/apps/server/src/auth/routes.test.ts +44 -3
  28. package/template/apps/server/src/auth/routes.ts +6 -1
  29. package/template/apps/server/src/auth/runtime.ts +4 -1
  30. package/template/apps/server/src/auth/service.ts +25 -7
  31. package/template/apps/server/src/configuration/bootstrap.ts +20 -1
  32. package/template/apps/server/src/configuration/definition.test.ts +71 -0
  33. package/template/apps/server/src/configuration/definition.ts +242 -12
  34. package/template/apps/server/src/configuration/drizzle-repository.ts +36 -2
  35. package/template/apps/server/src/configuration/repository.ts +27 -2
  36. package/template/apps/server/src/configuration/routes.ts +4 -1
  37. package/template/apps/server/src/configuration/service.test.ts +100 -0
  38. package/template/apps/server/src/configuration/service.ts +50 -10
  39. package/template/apps/server/src/data-access/scoped-repository.integration.test.ts +3 -2
  40. package/template/apps/server/src/data-governance/export/service.ts +2 -6
  41. package/template/apps/server/src/example-modules.ts +4 -1
  42. package/template/apps/server/src/guards.ts +18 -5
  43. package/template/apps/server/src/http/request-source.test.ts +85 -0
  44. package/template/apps/server/src/http/request-source.ts +90 -0
  45. package/template/apps/server/src/index.ts +23 -6
  46. package/template/apps/server/src/license/routes.ts +2 -0
  47. package/template/apps/server/src/license/runtime-service.ts +10 -11
  48. package/template/apps/server/src/module-bootstrap.ts +2 -2
  49. package/template/apps/server/src/module-host/request-context.ts +6 -6
  50. package/template/apps/server/src/module-host/storage-scope.ts +2 -9
  51. package/template/apps/server/src/module-host/user-directory-port.ts +2 -6
  52. package/template/apps/server/src/notification/channel-adapter.ts +46 -0
  53. package/template/apps/server/src/notification/delivery-policy.test.ts +59 -0
  54. package/template/apps/server/src/notification/delivery-policy.ts +37 -0
  55. package/template/apps/server/src/notification/delivery-repository.ts +198 -0
  56. package/template/apps/server/src/notification/delivery-worker.test.ts +87 -0
  57. package/template/apps/server/src/notification/delivery-worker.ts +88 -0
  58. package/template/apps/server/src/notification/drizzle-management.ts +180 -0
  59. package/template/apps/server/src/notification/drizzle-query.ts +160 -0
  60. package/template/apps/server/src/notification/drizzle-repository.integration.test.ts +301 -0
  61. package/template/apps/server/src/notification/drizzle-repository.ts +156 -90
  62. package/template/apps/server/src/notification/extension-routes.test.ts +119 -0
  63. package/template/apps/server/src/notification/extension-routes.ts +111 -0
  64. package/template/apps/server/src/notification/memory-delivery-repository.ts +137 -0
  65. package/template/apps/server/src/notification/memory-support.ts +72 -0
  66. package/template/apps/server/src/notification/preference-repository.ts +95 -0
  67. package/template/apps/server/src/notification/recipient-directory.ts +104 -0
  68. package/template/apps/server/src/notification/repository.test.ts +171 -0
  69. package/template/apps/server/src/notification/repository.ts +179 -32
  70. package/template/apps/server/src/notification/revision-routes.test.ts +186 -0
  71. package/template/apps/server/src/notification/route-support.ts +225 -0
  72. package/template/apps/server/src/notification/routes.test.ts +182 -0
  73. package/template/apps/server/src/notification/routes.ts +225 -132
  74. package/template/apps/server/src/notification/service.test.ts +104 -0
  75. package/template/apps/server/src/notification/service.ts +140 -0
  76. package/template/apps/server/src/notification/smtp-adapter.test.ts +65 -0
  77. package/template/apps/server/src/notification/smtp-adapter.ts +68 -0
  78. package/template/apps/server/src/persistence.integration.test.ts +41 -2
  79. package/template/apps/server/src/route-guards.ts +1 -0
  80. package/template/apps/server/src/runtime-audit-list.test.ts +39 -0
  81. package/template/apps/server/src/runtime-development.ts +1 -0
  82. package/template/apps/server/src/runtime.test.ts +23 -9
  83. package/template/apps/server/src/runtime.ts +39 -2
  84. package/template/apps/server/src/scheduler/audit.ts +2 -11
  85. package/template/apps/server/src/scheduler/definitions.test.ts +6 -0
  86. package/template/apps/server/src/scheduler/definitions.ts +7 -0
  87. package/template/apps/server/src/scheduler/recovery-service.ts +2 -11
  88. package/template/apps/server/src/scheduler/routes.ts +2 -7
  89. package/template/apps/server/src/search/opensearch-client.test.ts +45 -0
  90. package/template/apps/server/src/search/opensearch-client.ts +145 -0
  91. package/template/apps/server/src/search/service.ts +2 -6
  92. package/template/apps/server/src/settings/routes.ts +15 -3
  93. package/template/apps/server/src/settings/runtime.ts +10 -1
  94. package/template/apps/server/src/storage/runtime.ts +13 -0
  95. package/template/apps/server/src/system/audit-query.ts +33 -4
  96. package/template/apps/server/src/system/audit-routes.test.ts +58 -4
  97. package/template/apps/server/src/system/audit-routes.ts +6 -5
  98. package/template/apps/server/src/system/department-organization.ts +320 -0
  99. package/template/apps/server/src/system/department-routes.test.ts +207 -48
  100. package/template/apps/server/src/system/department-routes.ts +114 -33
  101. package/template/apps/server/src/system/drizzle-repository.ts +15 -7
  102. package/template/apps/server/src/system/drizzle-scoped-repository.ts +5 -3
  103. package/template/apps/server/src/system/drizzle-system.ts +37 -9
  104. package/template/apps/server/src/system/drizzle-transaction-context.ts +14 -0
  105. package/template/apps/server/src/system/identity-route-config.ts +7 -2
  106. package/template/apps/server/src/system/mutation-runner.ts +23 -0
  107. package/template/apps/server/src/system/organization-postgres.integration.test.ts +176 -0
  108. package/template/apps/server/src/system/resource-access-response.ts +12 -6
  109. package/template/apps/server/src/system/role-department-scope-policy.ts +55 -0
  110. package/template/apps/server/src/system/route-helpers.ts +2 -5
  111. package/template/apps/server/src/system/route-types.ts +12 -3
  112. package/template/apps/server/src/system/routes-validation.test.ts +26 -0
  113. package/template/apps/server/src/system/routes.test.ts +10 -2
  114. package/template/apps/server/src/system/routes.ts +70 -36
  115. package/template/apps/server/src/system/seed.ts +7 -0
  116. package/template/apps/server/src/work-order/data-scope.integration.test.ts +9 -2
  117. package/template/apps/server/src/work-order/opensearch-index.test.ts +97 -0
  118. package/template/apps/server/src/work-order/opensearch-index.ts +313 -0
  119. package/template/apps/server/src/work-order/search-provider.ts +60 -1
  120. package/template/apps/server/src/work-order/service.ts +42 -10
  121. package/template/apps/web/src/app/error-pages.ts +9 -0
  122. package/template/apps/web/src/components/auth/PasswordChangeForm.vue +16 -11
  123. package/template/apps/web/src/components/auth/PasswordChangeForm.webtest.ts +14 -0
  124. package/template/apps/web/src/components/common/FormDialog.vue +16 -2
  125. package/template/apps/web/src/components/common/ModalLayout.webtest.ts +20 -1
  126. package/template/apps/web/src/composables/useGlobalSearch.ts +68 -0
  127. package/template/apps/web/src/composables/usePlatformSettings.ts +8 -7
  128. package/template/apps/web/src/layout/GlobalSearchResults.vue +58 -0
  129. package/template/apps/web/src/layout/NavigationSearchDialog.vue +42 -6
  130. package/template/apps/web/src/layout/NavigationSearchDialog.webtest.ts +71 -0
  131. package/template/apps/web/src/layout/NotificationMenu.vue +73 -31
  132. package/template/apps/web/src/layout/NotificationMenu.webtest.ts +44 -1
  133. package/template/apps/web/src/layout/NotificationMenuItem.vue +61 -0
  134. package/template/apps/web/src/layout/NotificationPreferencesDialog.vue +181 -0
  135. package/template/apps/web/src/layout/NotificationPreferencesDialog.webtest.ts +80 -0
  136. package/template/apps/web/src/lib/date-time.ts +22 -10
  137. package/template/apps/web/src/lib/date-time.webtest.ts +9 -1
  138. package/template/apps/web/src/main.ts +14 -1
  139. package/template/apps/web/src/pages/auth/ChangePasswordPage.vue +16 -1
  140. package/template/apps/web/src/pages/configuration/components/ModuleConfigVersionList.vue +1 -0
  141. package/template/apps/web/src/pages/configuration/components/ModuleConfigurationEditor.vue +10 -0
  142. package/template/apps/web/src/pages/configuration/components/ModuleConfigurationWorkbench.vue +37 -6
  143. package/template/apps/web/src/pages/errors/PlatformErrorPage.vue +115 -0
  144. package/template/apps/web/src/pages/errors/PlatformErrorPage.webtest.ts +44 -0
  145. package/template/apps/web/src/pages/errors/error-page-extension.ts +26 -0
  146. package/template/apps/web/src/pages/system/SystemNotificationsPage.vue +136 -68
  147. package/template/apps/web/src/pages/system/SystemNotificationsPage.webtest.ts +224 -12
  148. package/template/apps/web/src/pages/system/audit-log-presenter.ts +86 -0
  149. package/template/apps/web/src/pages/system/audit-time-range.ts +28 -0
  150. package/template/apps/web/src/pages/system/components/AuditActionCell.vue +10 -69
  151. package/template/apps/web/src/pages/system/components/AuditActionCell.webtest.ts +20 -27
  152. package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.vue +134 -0
  153. package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.webtest.ts +76 -0
  154. package/template/apps/web/src/pages/system/components/AuditLogFilters.vue +116 -52
  155. package/template/apps/web/src/pages/system/components/AuditLogFilters.webtest.ts +52 -15
  156. package/template/apps/web/src/pages/system/components/NotificationHistoryDialog.vue +129 -0
  157. package/template/apps/web/src/pages/system/components/NotificationManagementCard.vue +91 -0
  158. package/template/apps/web/src/pages/system/components/NotificationPublishDialog.vue +121 -0
  159. package/template/apps/web/src/pages/system/components/NotificationPublishForm.vue +162 -1
  160. package/template/apps/web/src/pages/system/components/SystemResourceActions.vue +68 -0
  161. package/template/apps/web/src/pages/system/components/SystemResourceEditor.vue +74 -0
  162. package/template/apps/web/src/pages/system/components/SystemResourceFilters.vue +3 -4
  163. package/template/apps/web/src/pages/system/components/SystemResourcePage.vue +91 -98
  164. package/template/apps/web/src/pages/system/components/SystemResourcePage.webtest.ts +68 -0
  165. package/template/apps/web/src/pages/system/components/SystemResourceTable.vue +20 -1
  166. package/template/apps/web/src/pages/system/components/SystemResourceTable.webtest.ts +28 -7
  167. package/template/apps/web/src/pages/system/components/department/DepartmentOrganizationForm.vue +210 -0
  168. package/template/apps/web/src/pages/system/components/department/DepartmentTransitionDialog.vue +112 -0
  169. package/template/apps/web/src/pages/system/components/user-account/UserAccountForm.vue +17 -4
  170. package/template/apps/web/src/pages/system/composables/useDepartmentTransitions.ts +79 -0
  171. package/template/apps/web/src/pages/system/composables/useNotificationPermissions.ts +14 -0
  172. package/template/apps/web/src/pages/system/composables/useSystemResource.ts +2 -0
  173. package/template/apps/web/src/pages/system/composables/useSystemResource.webtest.ts +15 -2
  174. package/template/apps/web/src/pages/system/resource-config.ts +12 -4
  175. package/template/apps/web/src/router/error-navigation.ts +50 -0
  176. package/template/apps/web/src/router/error-navigation.webtest.ts +106 -0
  177. package/template/apps/web/src/router/index.ts +71 -0
  178. package/template/apps/web/src/router/index.webtest.ts +29 -0
  179. package/template/apps/web/src/router/manifest-routes.webtest.ts +4 -0
  180. package/template/apps/web/src/router/shared-scaffold-neutrality.webtest.ts +12 -0
  181. package/template/apps/web/src/services/auth-api.ts +5 -0
  182. package/template/apps/web/src/services/http.ts +43 -6
  183. package/template/apps/web/src/services/http.webtest.ts +70 -0
  184. package/template/apps/web/src/services/notification-api.ts +85 -2
  185. package/template/apps/web/src/services/notification-api.webtest.ts +110 -0
  186. package/template/apps/web/src/services/search-api.ts +39 -0
  187. package/template/apps/web/src/services/system-api.ts +18 -0
  188. package/template/apps/web/vitest.config.ts +1 -0
  189. package/template/docker-compose.yml +66 -0
  190. package/template/docs/architecture/ai-runtime.md +14 -5
  191. package/template/docs/architecture/search-provider-protocol.md +38 -2
  192. package/template/docs/development/custom-error-pages.md +78 -0
  193. package/template/docs/development/custom-login-page.md +3 -3
  194. package/template/docs/platform/agent-runtime.md +32 -6
  195. package/template/docs/platform/audit-reliability.md +46 -2
  196. package/template/docs/platform/module-configuration.md +13 -4
  197. package/template/docs/platform/notifications.md +67 -11
  198. package/template/docs/platform/organization-membership.md +17 -3
  199. package/template/docs/platform/system-crud-api.md +16 -0
  200. package/template/docs/platform/web-system-crud.md +6 -2
  201. package/template/package.json +4 -0
  202. package/template/packages/config/index.test.ts +45 -0
  203. package/template/packages/config/package.json +1 -1
  204. package/template/packages/config/src/platform.ts +169 -48
  205. package/template/packages/database/drizzle/0035_material_nightshade.sql +10 -0
  206. package/template/packages/database/drizzle/0036_hesitant_speed.sql +35 -0
  207. package/template/packages/database/drizzle/0037_absent_nick_fury.sql +2 -0
  208. package/template/packages/database/drizzle/0038_uneven_wasp.sql +38 -0
  209. package/template/packages/database/drizzle/meta/0035_snapshot.json +7331 -0
  210. package/template/packages/database/drizzle/meta/0036_snapshot.json +7436 -0
  211. package/template/packages/database/drizzle/meta/0037_snapshot.json +7457 -0
  212. package/template/packages/database/drizzle/meta/0038_snapshot.json +7749 -0
  213. package/template/packages/database/drizzle/meta/_journal.json +28 -0
  214. package/template/packages/database/package.json +1 -1
  215. package/template/packages/database/src/schema/identity.ts +7 -0
  216. package/template/packages/database/src/schema/notifications.ts +135 -0
  217. package/template/packages/database/src/schema-notification-delivery.test.ts +54 -0
  218. package/template/packages/database/src/schema-workflow-notification.test.ts +40 -1
  219. package/template/packages/modules/package.json +1 -1
  220. package/template/packages/modules/src/system-api-permissions.ts +54 -0
  221. package/template/packages/modules/src/system-audit-actions.ts +10 -0
  222. package/template/packages/modules/src/system-features.ts +1 -1
  223. package/template/packages/modules/src/system-module-catalog.ts +2 -1
  224. package/template/packages/modules/src/system-modules.test.ts +13 -1
  225. package/template/packages/modules/src/system-modules.ts +12 -0
  226. package/template/packages/modules/src/system-permissions.ts +9 -5
  227. package/template/packages/server-sdk/package.json +1 -1
  228. package/template/packages/shared/package.json +1 -1
  229. package/template/packages/shared/src/audit.ts +25 -0
  230. package/template/packages/shared/src/module-configuration.ts +5 -0
  231. package/template/packages/shared/src/notification.ts +92 -1
  232. package/template/packages/shared/src/password.ts +7 -2
  233. package/template/packages/shared/src/platform-settings.ts +2 -0
  234. package/template/packages/storage/package.json +1 -1
  235. package/template/packages/storage/src/upload-service.test.ts +16 -0
  236. package/template/packages/storage/src/upload-service.ts +5 -1
  237. package/template/scripts/notification-postgres-regression.ts +27 -0
@@ -0,0 +1,72 @@
1
+ import type {
2
+ NotificationRevision,
3
+ PageResult,
4
+ PlatformNotification,
5
+ UpdateNotificationInput,
6
+ } from "@southwind-ai/shared";
7
+ import type { RequestActor } from "../guards.js";
8
+
9
+ export function visibleTo(
10
+ item: PlatformNotification,
11
+ actor: Pick<RequestActor, "id" | "roleCodes" | "scopeContext">,
12
+ ) {
13
+ const roles = item.recipientRoleCodes || [];
14
+ const userIds = item.recipientUserIds || [];
15
+ const departmentIds = item.recipientDepartmentIds || [];
16
+ return (
17
+ (!item.recipientUserId &&
18
+ !userIds.length &&
19
+ !roles.length &&
20
+ !departmentIds.length) ||
21
+ item.recipientUserId === actor.id ||
22
+ userIds.includes(actor.id) ||
23
+ Boolean(
24
+ actor.scopeContext?.departmentId &&
25
+ departmentIds.includes(actor.scopeContext.departmentId),
26
+ ) ||
27
+ roles.some((role) => actor.roleCodes.includes(role))
28
+ );
29
+ }
30
+
31
+ export function sameContent(
32
+ notification: PlatformNotification,
33
+ input: UpdateNotificationInput,
34
+ ) {
35
+ return (
36
+ notification.title === input.title &&
37
+ notification.summary === input.summary &&
38
+ notification.content === input.content &&
39
+ notification.category === input.category
40
+ );
41
+ }
42
+
43
+ export function toRevision(
44
+ notification: PlatformNotification,
45
+ editedAt: string,
46
+ editedBy: string,
47
+ ): NotificationRevision {
48
+ return {
49
+ notificationId: notification.id,
50
+ revision: notification.revision,
51
+ title: notification.title,
52
+ summary: notification.summary,
53
+ content: notification.content,
54
+ category: notification.category,
55
+ editedAt,
56
+ editedBy,
57
+ };
58
+ }
59
+
60
+ export function pageResult<T>(
61
+ items: T[],
62
+ page: number,
63
+ pageSize: number,
64
+ ): PageResult<T> {
65
+ const start = (page - 1) * pageSize;
66
+ return {
67
+ items: items.slice(start, start + pageSize),
68
+ total: items.length,
69
+ page,
70
+ pageSize,
71
+ };
72
+ }
@@ -0,0 +1,95 @@
1
+ import { notificationPreferences } from "@southwind-ai/database";
2
+ import type {
3
+ NotificationPreference,
4
+ UpdateNotificationPreferenceInput,
5
+ } from "@southwind-ai/shared";
6
+ import { eq } from "drizzle-orm";
7
+ import type { NodePgDatabase } from "drizzle-orm/node-postgres";
8
+
9
+ export interface NotificationPreferenceRepository {
10
+ get(userId: string): Promise<NotificationPreference | undefined>;
11
+ save(
12
+ userId: string,
13
+ input: UpdateNotificationPreferenceInput,
14
+ actorId: string,
15
+ ): Promise<NotificationPreference>;
16
+ }
17
+
18
+ export class InMemoryNotificationPreferenceRepository implements NotificationPreferenceRepository {
19
+ private readonly preferences = new Map<string, NotificationPreference>();
20
+
21
+ async get(userId: string) {
22
+ return this.preferences.get(userId);
23
+ }
24
+
25
+ async save(
26
+ userId: string,
27
+ input: UpdateNotificationPreferenceInput,
28
+ actorId: string,
29
+ ) {
30
+ const preference = {
31
+ userId,
32
+ enabledChannels: unique(input.enabledChannels),
33
+ disabledCategories: unique(input.disabledCategories),
34
+ updatedAt: new Date().toISOString(),
35
+ };
36
+ this.preferences.set(userId, preference);
37
+ void actorId;
38
+ return preference;
39
+ }
40
+ }
41
+
42
+ export class DrizzleNotificationPreferenceRepository implements NotificationPreferenceRepository {
43
+ constructor(private readonly db: NodePgDatabase) {}
44
+
45
+ async get(userId: string) {
46
+ const [row] = await this.db
47
+ .select()
48
+ .from(notificationPreferences)
49
+ .where(eq(notificationPreferences.userId, userId))
50
+ .limit(1);
51
+ return row ? toPreference(row) : undefined;
52
+ }
53
+
54
+ async save(
55
+ userId: string,
56
+ input: UpdateNotificationPreferenceInput,
57
+ actorId: string,
58
+ ) {
59
+ const now = new Date();
60
+ const [row] = await this.db
61
+ .insert(notificationPreferences)
62
+ .values({
63
+ userId,
64
+ enabledChannels: unique(input.enabledChannels),
65
+ disabledCategories: unique(input.disabledCategories),
66
+ updatedAt: now,
67
+ updatedBy: actorId,
68
+ })
69
+ .onConflictDoUpdate({
70
+ target: notificationPreferences.userId,
71
+ set: {
72
+ enabledChannels: unique(input.enabledChannels),
73
+ disabledCategories: unique(input.disabledCategories),
74
+ updatedAt: now,
75
+ updatedBy: actorId,
76
+ },
77
+ })
78
+ .returning();
79
+ if (!row) throw new Error("通知偏好保存未返回记录");
80
+ return toPreference(row);
81
+ }
82
+ }
83
+
84
+ function toPreference(row: typeof notificationPreferences.$inferSelect) {
85
+ return {
86
+ userId: row.userId,
87
+ enabledChannels: row.enabledChannels,
88
+ disabledCategories: row.disabledCategories,
89
+ updatedAt: row.updatedAt.toISOString(),
90
+ };
91
+ }
92
+
93
+ function unique<T>(values: T[]) {
94
+ return [...new Set(values)];
95
+ }
@@ -0,0 +1,104 @@
1
+ import type {
2
+ ExternalNotificationChannel,
3
+ NotificationAudienceOptions,
4
+ PublishNotificationInput,
5
+ } from "@southwind-ai/shared";
6
+ import type { SystemRepositories } from "../system/seed.js";
7
+
8
+ export interface NotificationRecipient {
9
+ id: string;
10
+ email?: string;
11
+ phone?: string;
12
+ }
13
+
14
+ export interface NotificationRecipientDirectory {
15
+ options(): Promise<NotificationAudienceOptions>;
16
+ resolve(input: PublishNotificationInput): Promise<NotificationRecipient[]>;
17
+ address(
18
+ userId: string,
19
+ channel: ExternalNotificationChannel,
20
+ ): Promise<string | undefined>;
21
+ }
22
+
23
+ export class SystemNotificationRecipientDirectory implements NotificationRecipientDirectory {
24
+ constructor(private readonly repositories: SystemRepositories) {}
25
+
26
+ async options(): Promise<NotificationAudienceOptions> {
27
+ const [users, roles, departments] = await Promise.all([
28
+ this.repositories.users.all({ status: "active" }),
29
+ this.repositories.roles.all({ status: "active" }),
30
+ this.repositories.departments.all({ status: "active" }),
31
+ ]);
32
+ return {
33
+ users: users.map((user) => ({ id: user.id, label: user.displayName })),
34
+ roles: roles.map((role) => ({
35
+ id: role.id,
36
+ label: role.name,
37
+ code: role.code,
38
+ })),
39
+ departments: departments.map((department) => ({
40
+ id: department.id,
41
+ label: department.name,
42
+ code: department.code,
43
+ })),
44
+ };
45
+ }
46
+
47
+ async resolve(input: PublishNotificationInput) {
48
+ const [users, roles, departments] = await Promise.all([
49
+ this.repositories.users.all({ status: "active" }),
50
+ this.repositories.roles.all({ status: "active" }),
51
+ this.repositories.departments.all({ status: "active" }),
52
+ ]);
53
+ const userIds = new Set([
54
+ ...(input.recipientUserIds || []),
55
+ ...(input.recipientUserId ? [input.recipientUserId] : []),
56
+ ]);
57
+ const roleIds = new Set(
58
+ roles
59
+ .filter((role) => input.recipientRoleCodes?.includes(role.code))
60
+ .map((role) => role.id),
61
+ );
62
+ const departmentIds = new Set(input.recipientDepartmentIds || []);
63
+ if (input.includeChildDepartments) {
64
+ includeDescendants(departmentIds, departments);
65
+ }
66
+ const targeted =
67
+ userIds.size > 0 || roleIds.size > 0 || departmentIds.size > 0;
68
+ return users
69
+ .filter(
70
+ (user) =>
71
+ !targeted ||
72
+ userIds.has(user.id) ||
73
+ user.roleIds.some((roleId) => roleIds.has(roleId)) ||
74
+ Boolean(user.departmentId && departmentIds.has(user.departmentId)),
75
+ )
76
+ .map(({ id, email, phone }) => ({ id, email, phone }));
77
+ }
78
+
79
+ async address(userId: string, channel: ExternalNotificationChannel) {
80
+ const user = await this.repositories.users.get(userId);
81
+ if (!user || user.status !== "active") return undefined;
82
+ return channel === "email" ? user.email : user.phone;
83
+ }
84
+ }
85
+
86
+ function includeDescendants(
87
+ targetIds: Set<string>,
88
+ departments: Array<{ id: string; parentId?: string }>,
89
+ ) {
90
+ let changed = true;
91
+ while (changed) {
92
+ changed = false;
93
+ for (const department of departments) {
94
+ if (
95
+ department.parentId &&
96
+ targetIds.has(department.parentId) &&
97
+ !targetIds.has(department.id)
98
+ ) {
99
+ targetIds.add(department.id);
100
+ changed = true;
101
+ }
102
+ }
103
+ }
104
+ }
@@ -0,0 +1,171 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { PublishNotificationInput } from "@southwind-ai/shared";
3
+ import type { RequestActor } from "../guards.js";
4
+ import { InMemoryNotificationRepository } from "./repository.js";
5
+
6
+ describe("内存通知收件箱", () => {
7
+ test("关键词、分类、已读和收藏组合在分页前筛选并返回正确总数", async () => {
8
+ const repository = new InMemoryNotificationRepository();
9
+ await repository.publish(notice("季度安全提醒", "安全"), admin);
10
+ const first = await repository.publish(
11
+ notice("安全维护窗口", "安全"),
12
+ admin,
13
+ );
14
+ await repository.publish(notice("产品更新", "产品"), admin);
15
+ await repository.markRead(first.id, userA);
16
+ await repository.setFavorite(first.id, true, userA);
17
+
18
+ const result = await repository.listInbox(userA, 1, 1, {
19
+ q: "维护",
20
+ category: "安全",
21
+ read: "read",
22
+ favorite: true,
23
+ });
24
+
25
+ expect(result).toMatchObject({ total: 1, page: 1, pageSize: 1 });
26
+ expect(result.items[0]).toMatchObject({
27
+ id: first.id,
28
+ read: true,
29
+ favorite: true,
30
+ });
31
+ });
32
+
33
+ test("收藏按 Actor 隔离,且与已读状态相互独立", async () => {
34
+ const repository = new InMemoryNotificationRepository();
35
+ const published = await repository.publish(
36
+ notice("独立状态测试", "系统"),
37
+ admin,
38
+ );
39
+ await repository.setFavorite(published.id, true, userA);
40
+
41
+ expect((await repository.listInbox(userA, 1, 10)).items[0]).toMatchObject({
42
+ favorite: true,
43
+ read: false,
44
+ });
45
+ expect((await repository.listInbox(userB, 1, 10)).items[0]).toMatchObject({
46
+ favorite: false,
47
+ read: false,
48
+ });
49
+
50
+ await repository.markRead(published.id, userA);
51
+ await repository.setFavorite(published.id, false, userA);
52
+ expect((await repository.listInbox(userA, 1, 10)).items[0]).toMatchObject({
53
+ favorite: false,
54
+ read: true,
55
+ });
56
+ });
57
+
58
+ test("不可见通知不能通过猜测 ID 收藏或取消收藏", async () => {
59
+ const repository = new InMemoryNotificationRepository();
60
+ const targeted = await repository.publish(
61
+ {
62
+ ...notice("定向通知", "审批"),
63
+ recipientUserId: userA.id,
64
+ },
65
+ admin,
66
+ );
67
+
68
+ expect(
69
+ await repository.setFavorite(targeted.id, true, userB),
70
+ ).toBeUndefined();
71
+ expect(
72
+ await repository.setFavorite(targeted.id, false, userB),
73
+ ).toBeUndefined();
74
+ expect((await repository.listInbox(userB, 1, 10)).total).toBe(0);
75
+ });
76
+
77
+ test("快速重复收藏和取消收藏保持幂等", async () => {
78
+ const repository = new InMemoryNotificationRepository();
79
+ const published = await repository.publish(
80
+ notice("重复操作测试", "系统"),
81
+ admin,
82
+ );
83
+
84
+ const favorites = await Promise.all([
85
+ repository.setFavorite(published.id, true, userA),
86
+ repository.setFavorite(published.id, true, userA),
87
+ ]);
88
+ expect(favorites.filter((result) => result?.changed)).toHaveLength(1);
89
+ expect(
90
+ (await repository.listInbox(userA, 1, 10, { favorite: true })).total,
91
+ ).toBe(1);
92
+
93
+ const removals = await Promise.all([
94
+ repository.setFavorite(published.id, false, userA),
95
+ repository.setFavorite(published.id, false, userA),
96
+ ]);
97
+ expect(removals.filter((result) => result?.changed)).toHaveLength(1);
98
+ expect(
99
+ (await repository.listInbox(userA, 1, 10, { favorite: true })).total,
100
+ ).toBe(0);
101
+ });
102
+
103
+ test("发布形成初始版本,修改追加不可变历史和时间", async () => {
104
+ const repository = new InMemoryNotificationRepository();
105
+ const published = await repository.publish(
106
+ notice("初始公告", "系统"),
107
+ admin,
108
+ );
109
+ const updated = await repository.update(
110
+ published.id,
111
+ notice("修改公告", "安全"),
112
+ userA,
113
+ );
114
+ const repeated = await repository.update(
115
+ published.id,
116
+ notice("修改公告", "安全"),
117
+ userA,
118
+ );
119
+ const history = await repository.listRevisions(published.id, 1, 10);
120
+
121
+ expect(updated).toMatchObject({
122
+ changed: true,
123
+ notification: { revision: 2, title: "修改公告" },
124
+ });
125
+ expect(repeated).toMatchObject({ changed: false });
126
+ expect(history?.total).toBe(2);
127
+ expect(history?.items).toMatchObject([
128
+ { revision: 2, editedBy: userA.id, editedAt: expect.any(String) },
129
+ { revision: 1, editedBy: admin.id, editedAt: expect.any(String) },
130
+ ]);
131
+ });
132
+
133
+ test("归档公告保持历史可读但拒绝继续修改", async () => {
134
+ const repository = new InMemoryNotificationRepository();
135
+ const published = await repository.publish(
136
+ notice("待归档公告", "系统"),
137
+ admin,
138
+ );
139
+ await repository.archive(published.id, admin);
140
+
141
+ expect(
142
+ await repository.update(published.id, notice("越界修改", "系统"), admin),
143
+ ).toBeUndefined();
144
+ expect((await repository.listRevisions(published.id, 1, 10))?.total).toBe(
145
+ 1,
146
+ );
147
+ });
148
+ });
149
+
150
+ function notice(title: string, category: string): PublishNotificationInput {
151
+ return {
152
+ title,
153
+ summary: `${title}摘要`,
154
+ content: `${title}正文`,
155
+ category,
156
+ };
157
+ }
158
+
159
+ const admin = actor("admin", ["admin"]);
160
+ const userA = actor("user-a", ["member"]);
161
+ const userB = actor("user-b", ["member"]);
162
+
163
+ function actor(id: string, roleCodes: string[]): RequestActor {
164
+ return {
165
+ id,
166
+ name: id,
167
+ type: "user",
168
+ roleCodes,
169
+ permissionKeys: ["system.notification.read"],
170
+ };
171
+ }