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.
- package/dist/cli.js +167 -17
- package/package.json +1 -1
- package/template/.windy-template.json +2 -2
- package/template/AGENTS.md +5 -0
- package/template/README.md +3 -1
- package/template/apps/agent-server/Dockerfile +1 -0
- package/template/apps/agent-server/README.md +18 -5
- package/template/apps/agent-server/adk_web_agents/__init__.py +1 -0
- package/template/apps/agent-server/adk_web_agents/safe_debug/__init__.py +3 -0
- package/template/apps/agent-server/adk_web_agents/safe_debug/agent.py +40 -0
- package/template/apps/agent-server/pyproject.toml +1 -1
- package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +191 -16
- package/template/apps/agent-server/src/southwind_agent_server/adk_failure.py +13 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_model.py +225 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_tools.py +76 -0
- package/template/apps/agent-server/src/southwind_agent_server/app.py +1 -2
- package/template/apps/agent-server/tests/test_adk_adapter.py +305 -0
- package/template/apps/agent-server/tests/test_adk_web_agent.py +12 -0
- package/template/apps/server/package.json +4 -1
- package/template/apps/server/src/agent/run-facade.ts +2 -7
- package/template/apps/server/src/agent/tool-registry.ts +2 -6
- package/template/apps/server/src/application-services.ts +50 -0
- package/template/apps/server/src/audit/drizzle-log-store.ts +25 -0
- package/template/apps/server/src/audit/search-summary.ts +2 -6
- package/template/apps/server/src/auth/credential-restriction.ts +1 -0
- package/template/apps/server/src/auth/password.ts +30 -21
- package/template/apps/server/src/auth/routes.test.ts +44 -3
- package/template/apps/server/src/auth/routes.ts +6 -1
- package/template/apps/server/src/auth/runtime.ts +4 -1
- package/template/apps/server/src/auth/service.ts +25 -7
- package/template/apps/server/src/configuration/bootstrap.ts +20 -1
- package/template/apps/server/src/configuration/definition.test.ts +71 -0
- package/template/apps/server/src/configuration/definition.ts +242 -12
- package/template/apps/server/src/configuration/drizzle-repository.ts +36 -2
- package/template/apps/server/src/configuration/repository.ts +27 -2
- package/template/apps/server/src/configuration/routes.ts +4 -1
- package/template/apps/server/src/configuration/service.test.ts +100 -0
- package/template/apps/server/src/configuration/service.ts +50 -10
- package/template/apps/server/src/data-access/scoped-repository.integration.test.ts +3 -2
- package/template/apps/server/src/data-governance/export/service.ts +2 -6
- package/template/apps/server/src/example-modules.ts +4 -1
- package/template/apps/server/src/guards.ts +18 -5
- package/template/apps/server/src/http/request-source.test.ts +85 -0
- package/template/apps/server/src/http/request-source.ts +90 -0
- package/template/apps/server/src/index.ts +23 -6
- package/template/apps/server/src/license/routes.ts +2 -0
- package/template/apps/server/src/license/runtime-service.ts +10 -11
- package/template/apps/server/src/module-bootstrap.ts +2 -2
- package/template/apps/server/src/module-host/request-context.ts +6 -6
- package/template/apps/server/src/module-host/storage-scope.ts +2 -9
- package/template/apps/server/src/module-host/user-directory-port.ts +2 -6
- package/template/apps/server/src/notification/channel-adapter.ts +46 -0
- package/template/apps/server/src/notification/delivery-policy.test.ts +59 -0
- package/template/apps/server/src/notification/delivery-policy.ts +37 -0
- package/template/apps/server/src/notification/delivery-repository.ts +198 -0
- package/template/apps/server/src/notification/delivery-worker.test.ts +87 -0
- package/template/apps/server/src/notification/delivery-worker.ts +88 -0
- package/template/apps/server/src/notification/drizzle-management.ts +180 -0
- package/template/apps/server/src/notification/drizzle-query.ts +160 -0
- package/template/apps/server/src/notification/drizzle-repository.integration.test.ts +301 -0
- package/template/apps/server/src/notification/drizzle-repository.ts +156 -90
- package/template/apps/server/src/notification/extension-routes.test.ts +119 -0
- package/template/apps/server/src/notification/extension-routes.ts +111 -0
- package/template/apps/server/src/notification/memory-delivery-repository.ts +137 -0
- package/template/apps/server/src/notification/memory-support.ts +72 -0
- package/template/apps/server/src/notification/preference-repository.ts +95 -0
- package/template/apps/server/src/notification/recipient-directory.ts +104 -0
- package/template/apps/server/src/notification/repository.test.ts +171 -0
- package/template/apps/server/src/notification/repository.ts +179 -32
- package/template/apps/server/src/notification/revision-routes.test.ts +186 -0
- package/template/apps/server/src/notification/route-support.ts +225 -0
- package/template/apps/server/src/notification/routes.test.ts +182 -0
- package/template/apps/server/src/notification/routes.ts +225 -132
- package/template/apps/server/src/notification/service.test.ts +104 -0
- package/template/apps/server/src/notification/service.ts +140 -0
- package/template/apps/server/src/notification/smtp-adapter.test.ts +65 -0
- package/template/apps/server/src/notification/smtp-adapter.ts +68 -0
- package/template/apps/server/src/persistence.integration.test.ts +41 -2
- package/template/apps/server/src/route-guards.ts +1 -0
- package/template/apps/server/src/runtime-audit-list.test.ts +39 -0
- package/template/apps/server/src/runtime-development.ts +1 -0
- package/template/apps/server/src/runtime.test.ts +23 -9
- package/template/apps/server/src/runtime.ts +39 -2
- package/template/apps/server/src/scheduler/audit.ts +2 -11
- package/template/apps/server/src/scheduler/definitions.test.ts +6 -0
- package/template/apps/server/src/scheduler/definitions.ts +7 -0
- package/template/apps/server/src/scheduler/recovery-service.ts +2 -11
- package/template/apps/server/src/scheduler/routes.ts +2 -7
- package/template/apps/server/src/search/opensearch-client.test.ts +45 -0
- package/template/apps/server/src/search/opensearch-client.ts +145 -0
- package/template/apps/server/src/search/service.ts +2 -6
- package/template/apps/server/src/settings/routes.ts +15 -3
- package/template/apps/server/src/settings/runtime.ts +10 -1
- package/template/apps/server/src/storage/runtime.ts +13 -0
- package/template/apps/server/src/system/audit-query.ts +33 -4
- package/template/apps/server/src/system/audit-routes.test.ts +58 -4
- package/template/apps/server/src/system/audit-routes.ts +6 -5
- package/template/apps/server/src/system/department-organization.ts +320 -0
- package/template/apps/server/src/system/department-routes.test.ts +207 -48
- package/template/apps/server/src/system/department-routes.ts +114 -33
- package/template/apps/server/src/system/drizzle-repository.ts +15 -7
- package/template/apps/server/src/system/drizzle-scoped-repository.ts +5 -3
- package/template/apps/server/src/system/drizzle-system.ts +37 -9
- package/template/apps/server/src/system/drizzle-transaction-context.ts +14 -0
- package/template/apps/server/src/system/identity-route-config.ts +7 -2
- package/template/apps/server/src/system/mutation-runner.ts +23 -0
- package/template/apps/server/src/system/organization-postgres.integration.test.ts +176 -0
- package/template/apps/server/src/system/resource-access-response.ts +12 -6
- package/template/apps/server/src/system/role-department-scope-policy.ts +55 -0
- package/template/apps/server/src/system/route-helpers.ts +2 -5
- package/template/apps/server/src/system/route-types.ts +12 -3
- package/template/apps/server/src/system/routes-validation.test.ts +26 -0
- package/template/apps/server/src/system/routes.test.ts +10 -2
- package/template/apps/server/src/system/routes.ts +70 -36
- package/template/apps/server/src/system/seed.ts +7 -0
- package/template/apps/server/src/work-order/data-scope.integration.test.ts +9 -2
- package/template/apps/server/src/work-order/opensearch-index.test.ts +97 -0
- package/template/apps/server/src/work-order/opensearch-index.ts +313 -0
- package/template/apps/server/src/work-order/search-provider.ts +60 -1
- package/template/apps/server/src/work-order/service.ts +42 -10
- package/template/apps/web/src/app/error-pages.ts +9 -0
- package/template/apps/web/src/components/auth/PasswordChangeForm.vue +16 -11
- package/template/apps/web/src/components/auth/PasswordChangeForm.webtest.ts +14 -0
- package/template/apps/web/src/components/common/FormDialog.vue +16 -2
- package/template/apps/web/src/components/common/ModalLayout.webtest.ts +20 -1
- package/template/apps/web/src/composables/useGlobalSearch.ts +68 -0
- package/template/apps/web/src/composables/usePlatformSettings.ts +8 -7
- package/template/apps/web/src/layout/GlobalSearchResults.vue +58 -0
- package/template/apps/web/src/layout/NavigationSearchDialog.vue +42 -6
- package/template/apps/web/src/layout/NavigationSearchDialog.webtest.ts +71 -0
- package/template/apps/web/src/layout/NotificationMenu.vue +73 -31
- package/template/apps/web/src/layout/NotificationMenu.webtest.ts +44 -1
- package/template/apps/web/src/layout/NotificationMenuItem.vue +61 -0
- package/template/apps/web/src/layout/NotificationPreferencesDialog.vue +181 -0
- package/template/apps/web/src/layout/NotificationPreferencesDialog.webtest.ts +80 -0
- package/template/apps/web/src/lib/date-time.ts +22 -10
- package/template/apps/web/src/lib/date-time.webtest.ts +9 -1
- package/template/apps/web/src/main.ts +14 -1
- package/template/apps/web/src/pages/auth/ChangePasswordPage.vue +16 -1
- package/template/apps/web/src/pages/configuration/components/ModuleConfigVersionList.vue +1 -0
- package/template/apps/web/src/pages/configuration/components/ModuleConfigurationEditor.vue +10 -0
- package/template/apps/web/src/pages/configuration/components/ModuleConfigurationWorkbench.vue +37 -6
- package/template/apps/web/src/pages/errors/PlatformErrorPage.vue +115 -0
- package/template/apps/web/src/pages/errors/PlatformErrorPage.webtest.ts +44 -0
- package/template/apps/web/src/pages/errors/error-page-extension.ts +26 -0
- package/template/apps/web/src/pages/system/SystemNotificationsPage.vue +136 -68
- package/template/apps/web/src/pages/system/SystemNotificationsPage.webtest.ts +224 -12
- package/template/apps/web/src/pages/system/audit-log-presenter.ts +86 -0
- package/template/apps/web/src/pages/system/audit-time-range.ts +28 -0
- package/template/apps/web/src/pages/system/components/AuditActionCell.vue +10 -69
- package/template/apps/web/src/pages/system/components/AuditActionCell.webtest.ts +20 -27
- package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.vue +134 -0
- package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.webtest.ts +76 -0
- package/template/apps/web/src/pages/system/components/AuditLogFilters.vue +116 -52
- package/template/apps/web/src/pages/system/components/AuditLogFilters.webtest.ts +52 -15
- package/template/apps/web/src/pages/system/components/NotificationHistoryDialog.vue +129 -0
- package/template/apps/web/src/pages/system/components/NotificationManagementCard.vue +91 -0
- package/template/apps/web/src/pages/system/components/NotificationPublishDialog.vue +121 -0
- package/template/apps/web/src/pages/system/components/NotificationPublishForm.vue +162 -1
- package/template/apps/web/src/pages/system/components/SystemResourceActions.vue +68 -0
- package/template/apps/web/src/pages/system/components/SystemResourceEditor.vue +74 -0
- package/template/apps/web/src/pages/system/components/SystemResourceFilters.vue +3 -4
- package/template/apps/web/src/pages/system/components/SystemResourcePage.vue +91 -98
- package/template/apps/web/src/pages/system/components/SystemResourcePage.webtest.ts +68 -0
- package/template/apps/web/src/pages/system/components/SystemResourceTable.vue +20 -1
- package/template/apps/web/src/pages/system/components/SystemResourceTable.webtest.ts +28 -7
- package/template/apps/web/src/pages/system/components/department/DepartmentOrganizationForm.vue +210 -0
- package/template/apps/web/src/pages/system/components/department/DepartmentTransitionDialog.vue +112 -0
- package/template/apps/web/src/pages/system/components/user-account/UserAccountForm.vue +17 -4
- package/template/apps/web/src/pages/system/composables/useDepartmentTransitions.ts +79 -0
- package/template/apps/web/src/pages/system/composables/useNotificationPermissions.ts +14 -0
- package/template/apps/web/src/pages/system/composables/useSystemResource.ts +2 -0
- package/template/apps/web/src/pages/system/composables/useSystemResource.webtest.ts +15 -2
- package/template/apps/web/src/pages/system/resource-config.ts +12 -4
- package/template/apps/web/src/router/error-navigation.ts +50 -0
- package/template/apps/web/src/router/error-navigation.webtest.ts +106 -0
- package/template/apps/web/src/router/index.ts +71 -0
- package/template/apps/web/src/router/index.webtest.ts +29 -0
- package/template/apps/web/src/router/manifest-routes.webtest.ts +4 -0
- package/template/apps/web/src/router/shared-scaffold-neutrality.webtest.ts +12 -0
- package/template/apps/web/src/services/auth-api.ts +5 -0
- package/template/apps/web/src/services/http.ts +43 -6
- package/template/apps/web/src/services/http.webtest.ts +70 -0
- package/template/apps/web/src/services/notification-api.ts +85 -2
- package/template/apps/web/src/services/notification-api.webtest.ts +110 -0
- package/template/apps/web/src/services/search-api.ts +39 -0
- package/template/apps/web/src/services/system-api.ts +18 -0
- package/template/apps/web/vitest.config.ts +1 -0
- package/template/docker-compose.yml +66 -0
- package/template/docs/architecture/ai-runtime.md +14 -5
- package/template/docs/architecture/search-provider-protocol.md +38 -2
- package/template/docs/development/custom-error-pages.md +78 -0
- package/template/docs/development/custom-login-page.md +3 -3
- package/template/docs/platform/agent-runtime.md +32 -6
- package/template/docs/platform/audit-reliability.md +46 -2
- package/template/docs/platform/module-configuration.md +13 -4
- package/template/docs/platform/notifications.md +67 -11
- package/template/docs/platform/organization-membership.md +17 -3
- package/template/docs/platform/system-crud-api.md +16 -0
- package/template/docs/platform/web-system-crud.md +6 -2
- package/template/package.json +4 -0
- package/template/packages/config/index.test.ts +45 -0
- package/template/packages/config/package.json +1 -1
- package/template/packages/config/src/platform.ts +169 -48
- package/template/packages/database/drizzle/0035_material_nightshade.sql +10 -0
- package/template/packages/database/drizzle/0036_hesitant_speed.sql +35 -0
- package/template/packages/database/drizzle/0037_absent_nick_fury.sql +2 -0
- package/template/packages/database/drizzle/0038_uneven_wasp.sql +38 -0
- package/template/packages/database/drizzle/meta/0035_snapshot.json +7331 -0
- package/template/packages/database/drizzle/meta/0036_snapshot.json +7436 -0
- package/template/packages/database/drizzle/meta/0037_snapshot.json +7457 -0
- package/template/packages/database/drizzle/meta/0038_snapshot.json +7749 -0
- package/template/packages/database/drizzle/meta/_journal.json +28 -0
- package/template/packages/database/package.json +1 -1
- package/template/packages/database/src/schema/identity.ts +7 -0
- package/template/packages/database/src/schema/notifications.ts +135 -0
- package/template/packages/database/src/schema-notification-delivery.test.ts +54 -0
- package/template/packages/database/src/schema-workflow-notification.test.ts +40 -1
- package/template/packages/modules/package.json +1 -1
- package/template/packages/modules/src/system-api-permissions.ts +54 -0
- package/template/packages/modules/src/system-audit-actions.ts +10 -0
- package/template/packages/modules/src/system-features.ts +1 -1
- package/template/packages/modules/src/system-module-catalog.ts +2 -1
- package/template/packages/modules/src/system-modules.test.ts +13 -1
- package/template/packages/modules/src/system-modules.ts +12 -0
- package/template/packages/modules/src/system-permissions.ts +9 -5
- package/template/packages/server-sdk/package.json +1 -1
- package/template/packages/shared/package.json +1 -1
- package/template/packages/shared/src/audit.ts +25 -0
- package/template/packages/shared/src/module-configuration.ts +5 -0
- package/template/packages/shared/src/notification.ts +92 -1
- package/template/packages/shared/src/password.ts +7 -2
- package/template/packages/shared/src/platform-settings.ts +2 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/upload-service.test.ts +16 -0
- package/template/packages/storage/src/upload-service.ts +5 -1
- package/template/scripts/notification-postgres-regression.ts +27 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import type { LifecycleStatus } from "@southwind-ai/shared";
|
|
2
|
+
import type { RequestActor } from "../guards.js";
|
|
3
|
+
import type {
|
|
4
|
+
ManagedDepartment,
|
|
5
|
+
ManagedRole,
|
|
6
|
+
ManagedUser,
|
|
7
|
+
} from "./entities.js";
|
|
8
|
+
import type { EntityRepository } from "./repository.js";
|
|
9
|
+
import type {
|
|
10
|
+
ResourcePolicyError,
|
|
11
|
+
ResourcePolicyResult,
|
|
12
|
+
} from "./route-types.js";
|
|
13
|
+
|
|
14
|
+
export type DepartmentTransitionAction = "disable" | "delete";
|
|
15
|
+
|
|
16
|
+
export interface DepartmentTransitionResult {
|
|
17
|
+
department: ManagedDepartment;
|
|
18
|
+
movedChildren: number;
|
|
19
|
+
movedUsers: number;
|
|
20
|
+
updatedRoles: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface OrganizationRepositories {
|
|
24
|
+
departments: EntityRepository<ManagedDepartment>;
|
|
25
|
+
users: EntityRepository<ManagedUser>;
|
|
26
|
+
roles: EntityRepository<ManagedRole>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class DepartmentOrganizationPolicy {
|
|
30
|
+
constructor(private readonly repositories: OrganizationRepositories) {}
|
|
31
|
+
|
|
32
|
+
async validateInput(
|
|
33
|
+
input: Partial<ManagedDepartment>,
|
|
34
|
+
current?: ManagedDepartment,
|
|
35
|
+
): Promise<ResourcePolicyResult> {
|
|
36
|
+
if (
|
|
37
|
+
input.code !== undefined &&
|
|
38
|
+
(typeof input.code !== "string" ||
|
|
39
|
+
!/^[a-zA-Z][a-zA-Z0-9_-]{1,79}$/.test(input.code.trim()))
|
|
40
|
+
) {
|
|
41
|
+
return invalid(
|
|
42
|
+
"INVALID_DEPARTMENT_CODE",
|
|
43
|
+
"部门编码需以字母开头且至少 2 位",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
if (
|
|
47
|
+
input.name !== undefined &&
|
|
48
|
+
(typeof input.name !== "string" || !input.name.trim())
|
|
49
|
+
) {
|
|
50
|
+
return invalid("INVALID_DEPARTMENT_NAME", "部门名称不能为空");
|
|
51
|
+
}
|
|
52
|
+
if (
|
|
53
|
+
input.order !== undefined &&
|
|
54
|
+
(!Number.isSafeInteger(input.order) || input.order < 0)
|
|
55
|
+
) {
|
|
56
|
+
return invalid("INVALID_DEPARTMENT_ORDER", "部门排序必须是非负整数");
|
|
57
|
+
}
|
|
58
|
+
if (input.status !== undefined && !isLifecycleStatus(input.status)) {
|
|
59
|
+
return invalid("INVALID_DEPARTMENT_STATUS", "部门状态不合法");
|
|
60
|
+
}
|
|
61
|
+
if (
|
|
62
|
+
!current &&
|
|
63
|
+
(typeof input.code !== "string" ||
|
|
64
|
+
typeof input.name !== "string" ||
|
|
65
|
+
typeof input.parentId !== "string" ||
|
|
66
|
+
!input.parentId)
|
|
67
|
+
) {
|
|
68
|
+
return invalid(
|
|
69
|
+
"DEPARTMENT_PARENT_REQUIRED",
|
|
70
|
+
"平台只允许一个根组织,新部门必须选择上级部门",
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async beforeCreate(
|
|
77
|
+
input: Partial<ManagedDepartment>,
|
|
78
|
+
): Promise<ResourcePolicyResult> {
|
|
79
|
+
const duplicate = (await this.repositories.departments.all()).find(
|
|
80
|
+
(department) => department.code === input.code?.trim(),
|
|
81
|
+
);
|
|
82
|
+
if (duplicate) {
|
|
83
|
+
return conflict("DUPLICATE_DEPARTMENT_CODE", "部门编码已存在");
|
|
84
|
+
}
|
|
85
|
+
return this.validateParent(undefined, input.parentId);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async beforeUpdate(
|
|
89
|
+
current: ManagedDepartment,
|
|
90
|
+
input: Partial<ManagedDepartment>,
|
|
91
|
+
): Promise<ResourcePolicyResult> {
|
|
92
|
+
if (input.code && input.code !== current.code) {
|
|
93
|
+
const duplicate = (await this.repositories.departments.all()).find(
|
|
94
|
+
(department) =>
|
|
95
|
+
department.id !== current.id &&
|
|
96
|
+
department.code === input.code?.trim(),
|
|
97
|
+
);
|
|
98
|
+
if (duplicate) {
|
|
99
|
+
return conflict("DUPLICATE_DEPARTMENT_CODE", "部门编码已存在");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (input.parentId !== undefined) {
|
|
103
|
+
const parentError = await this.validateParent(current, input.parentId);
|
|
104
|
+
if (parentError) return parentError;
|
|
105
|
+
}
|
|
106
|
+
if (
|
|
107
|
+
input.status !== undefined &&
|
|
108
|
+
input.status !== "active" &&
|
|
109
|
+
current.status === "active"
|
|
110
|
+
) {
|
|
111
|
+
return this.validateCanLeaveActive(current);
|
|
112
|
+
}
|
|
113
|
+
if (input.status === "active") {
|
|
114
|
+
return this.validateParent(current, current.parentId);
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
beforeDelete(current: ManagedDepartment): Promise<ResourcePolicyResult> {
|
|
120
|
+
return this.validateCanLeaveActive(current);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async transition(
|
|
124
|
+
id: string,
|
|
125
|
+
action: DepartmentTransitionAction,
|
|
126
|
+
targetDepartmentId: string | undefined,
|
|
127
|
+
actor: RequestActor,
|
|
128
|
+
): Promise<DepartmentTransitionResult | ResourcePolicyError> {
|
|
129
|
+
const source = await this.repositories.departments.get(id);
|
|
130
|
+
if (!source) {
|
|
131
|
+
return conflict("DEPARTMENT_NOT_FOUND", "部门不存在或已删除");
|
|
132
|
+
}
|
|
133
|
+
if (!source.parentId) {
|
|
134
|
+
return conflict("ROOT_DEPARTMENT_IMMUTABLE", "根组织不能停用或删除");
|
|
135
|
+
}
|
|
136
|
+
const references = await this.references(source.id);
|
|
137
|
+
const referenceCount =
|
|
138
|
+
references.children.length +
|
|
139
|
+
references.users.length +
|
|
140
|
+
references.roles.length;
|
|
141
|
+
let target: ManagedDepartment | undefined;
|
|
142
|
+
if (referenceCount > 0) {
|
|
143
|
+
if (!targetDepartmentId) {
|
|
144
|
+
return conflict(
|
|
145
|
+
"DEPARTMENT_MIGRATION_REQUIRED",
|
|
146
|
+
"部门仍有关联数据,请选择迁移目标后重试",
|
|
147
|
+
referenceDetails(references),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
target = await this.repositories.departments.get(targetDepartmentId);
|
|
151
|
+
const targetError = await this.validateMigrationTarget(source, target);
|
|
152
|
+
if (targetError) return targetError;
|
|
153
|
+
} else if (targetDepartmentId) {
|
|
154
|
+
target = await this.repositories.departments.get(targetDepartmentId);
|
|
155
|
+
const targetError = await this.validateMigrationTarget(source, target);
|
|
156
|
+
if (targetError) return targetError;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (target) {
|
|
160
|
+
await this.repositories.departments.updateMany(
|
|
161
|
+
references.children.map((child) => ({
|
|
162
|
+
id: child.id,
|
|
163
|
+
input: { parentId: target.id },
|
|
164
|
+
})),
|
|
165
|
+
actor,
|
|
166
|
+
);
|
|
167
|
+
await this.repositories.users.updateMany(
|
|
168
|
+
references.users.map((user) => ({
|
|
169
|
+
id: user.id,
|
|
170
|
+
input: { departmentId: target.id },
|
|
171
|
+
})),
|
|
172
|
+
actor,
|
|
173
|
+
);
|
|
174
|
+
await this.repositories.roles.updateMany(
|
|
175
|
+
references.roles.map((role) => ({
|
|
176
|
+
id: role.id,
|
|
177
|
+
input: {
|
|
178
|
+
departmentIds: unique(
|
|
179
|
+
(role.departmentIds || []).map((departmentId) =>
|
|
180
|
+
departmentId === source.id ? target.id : departmentId,
|
|
181
|
+
),
|
|
182
|
+
),
|
|
183
|
+
},
|
|
184
|
+
})),
|
|
185
|
+
actor,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
const department =
|
|
189
|
+
action === "delete"
|
|
190
|
+
? await this.repositories.departments.softDelete(source.id, actor)
|
|
191
|
+
: await this.repositories.departments.update(
|
|
192
|
+
source.id,
|
|
193
|
+
{ status: "disabled" },
|
|
194
|
+
actor,
|
|
195
|
+
);
|
|
196
|
+
if (!department) {
|
|
197
|
+
return conflict("DEPARTMENT_NOT_FOUND", "部门不存在或已删除");
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
department,
|
|
201
|
+
movedChildren: references.children.length,
|
|
202
|
+
movedUsers: references.users.length,
|
|
203
|
+
updatedRoles: references.roles.length,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private async validateParent(
|
|
208
|
+
current: ManagedDepartment | undefined,
|
|
209
|
+
parentId: string | undefined,
|
|
210
|
+
): Promise<ResourcePolicyResult> {
|
|
211
|
+
if (current && !current.parentId) {
|
|
212
|
+
return parentId === undefined
|
|
213
|
+
? undefined
|
|
214
|
+
: conflict("ROOT_DEPARTMENT_IMMUTABLE", "根组织不能迁移到其它部门");
|
|
215
|
+
}
|
|
216
|
+
if (!parentId) {
|
|
217
|
+
return conflict(
|
|
218
|
+
"DEPARTMENT_PARENT_REQUIRED",
|
|
219
|
+
"平台只允许一个根组织,部门必须保留上级部门",
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const parent = await this.repositories.departments.get(parentId);
|
|
223
|
+
if (!parent || parent.status !== "active") {
|
|
224
|
+
return conflict("INVALID_DEPARTMENT_PARENT", "上级部门不存在或未启用");
|
|
225
|
+
}
|
|
226
|
+
if (!current) return undefined;
|
|
227
|
+
return (await this.isDescendant(parent.id, current.id))
|
|
228
|
+
? conflict("DEPARTMENT_CYCLE", "不能把部门移动到自身或其下级部门")
|
|
229
|
+
: undefined;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private async validateCanLeaveActive(
|
|
233
|
+
current: ManagedDepartment,
|
|
234
|
+
): Promise<ResourcePolicyResult> {
|
|
235
|
+
if (!current.parentId) {
|
|
236
|
+
return conflict("ROOT_DEPARTMENT_IMMUTABLE", "根组织不能停用或删除");
|
|
237
|
+
}
|
|
238
|
+
const references = await this.references(current.id);
|
|
239
|
+
return references.children.length ||
|
|
240
|
+
references.users.length ||
|
|
241
|
+
references.roles.length
|
|
242
|
+
? conflict(
|
|
243
|
+
"DEPARTMENT_MIGRATION_REQUIRED",
|
|
244
|
+
"部门仍有关联数据,请通过组织迁移完成停用或删除",
|
|
245
|
+
referenceDetails(references),
|
|
246
|
+
)
|
|
247
|
+
: undefined;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private async validateMigrationTarget(
|
|
251
|
+
source: ManagedDepartment,
|
|
252
|
+
target: ManagedDepartment | undefined,
|
|
253
|
+
): Promise<ResourcePolicyError | undefined> {
|
|
254
|
+
if (!target || target.status !== "active") {
|
|
255
|
+
return conflict("INVALID_MIGRATION_TARGET", "迁移目标不存在或未启用");
|
|
256
|
+
}
|
|
257
|
+
if (await this.isDescendant(target.id, source.id)) {
|
|
258
|
+
return conflict("DEPARTMENT_CYCLE", "迁移目标不能是当前部门或其下级部门");
|
|
259
|
+
}
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private async isDescendant(
|
|
264
|
+
candidateId: string,
|
|
265
|
+
ancestorId: string,
|
|
266
|
+
): Promise<boolean> {
|
|
267
|
+
if (candidateId === ancestorId) return true;
|
|
268
|
+
const departments = await this.repositories.departments.all();
|
|
269
|
+
const byId = new Map(departments.map((item) => [item.id, item]));
|
|
270
|
+
const visited = new Set<string>();
|
|
271
|
+
let current = byId.get(candidateId);
|
|
272
|
+
while (current?.parentId && !visited.has(current.id)) {
|
|
273
|
+
visited.add(current.id);
|
|
274
|
+
if (current.parentId === ancestorId) return true;
|
|
275
|
+
current = byId.get(current.parentId);
|
|
276
|
+
}
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private async references(departmentId: string) {
|
|
281
|
+
const departments = await this.repositories.departments.all();
|
|
282
|
+
const users = await this.repositories.users.all();
|
|
283
|
+
const roles = await this.repositories.roles.all();
|
|
284
|
+
return {
|
|
285
|
+
children: departments.filter((item) => item.parentId === departmentId),
|
|
286
|
+
users: users.filter((item) => item.departmentId === departmentId),
|
|
287
|
+
roles: roles.filter((item) => item.departmentIds?.includes(departmentId)),
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function referenceDetails(
|
|
293
|
+
references: Awaited<ReturnType<DepartmentOrganizationPolicy["references"]>>,
|
|
294
|
+
): Record<string, unknown> {
|
|
295
|
+
return {
|
|
296
|
+
childDepartmentCount: references.children.length,
|
|
297
|
+
userCount: references.users.length,
|
|
298
|
+
roleCount: references.roles.length,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function invalid(error: string, message: string): ResourcePolicyError {
|
|
303
|
+
return { error, message };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function conflict(
|
|
307
|
+
error: string,
|
|
308
|
+
message: string,
|
|
309
|
+
details?: Record<string, unknown>,
|
|
310
|
+
): ResourcePolicyError {
|
|
311
|
+
return { error, message, ...(details ? { details } : {}) };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function unique(values: string[]): string[] {
|
|
315
|
+
return [...new Set(values)].sort();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function isLifecycleStatus(value: unknown): value is LifecycleStatus {
|
|
319
|
+
return ["draft", "active", "disabled", "archived"].includes(String(value));
|
|
320
|
+
}
|
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
import { createFoundationSnapshot } from "../foundation.js";
|
|
3
3
|
import { createServerRuntime } from "../runtime.js";
|
|
4
|
-
import {
|
|
4
|
+
import { FakeRouteApp } from "./route-test-app.js";
|
|
5
|
+
import { registerSystemRoutes } from "./routes.js";
|
|
5
6
|
import { createSystemRepositories } from "./seed.js";
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (!handler) throw new Error(`未注册路由 ${method} ${path}`);
|
|
25
|
-
return handler(context);
|
|
26
|
-
}
|
|
8
|
+
async function fixture() {
|
|
9
|
+
const snapshot = createFoundationSnapshot();
|
|
10
|
+
const repositories = createSystemRepositories(snapshot);
|
|
11
|
+
const runtime = createServerRuntime({ WINDY_DEV_ADMIN_TOKEN: "token_1" });
|
|
12
|
+
const app = new FakeRouteApp();
|
|
13
|
+
registerSystemRoutes(app, repositories, runtime, snapshot.features);
|
|
14
|
+
const guardContext = await runtime.createGuardContext(
|
|
15
|
+
new Request("http://localhost/api/system/departments", {
|
|
16
|
+
headers: { authorization: "Bearer token_1" },
|
|
17
|
+
}),
|
|
18
|
+
);
|
|
19
|
+
return {
|
|
20
|
+
app,
|
|
21
|
+
repositories,
|
|
22
|
+
runtime,
|
|
23
|
+
context: { guardContext, params: {}, query: {} },
|
|
24
|
+
};
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
describe("
|
|
30
|
-
test("
|
|
27
|
+
describe("部门组织路由", () => {
|
|
28
|
+
test("部门处置缺少权限时经过统一 Guard 拒绝", async () => {
|
|
31
29
|
const snapshot = createFoundationSnapshot();
|
|
32
|
-
const runtime = createServerRuntime(
|
|
30
|
+
const runtime = createServerRuntime();
|
|
33
31
|
const app = new FakeRouteApp();
|
|
34
32
|
registerSystemRoutes(
|
|
35
33
|
app,
|
|
@@ -38,43 +36,204 @@ describe("部门归属路由", () => {
|
|
|
38
36
|
snapshot.features,
|
|
39
37
|
);
|
|
40
38
|
const guardContext = await runtime.createGuardContext(
|
|
41
|
-
new Request(
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
new Request(
|
|
40
|
+
"http://localhost/api/system/departments/dept_platform/transition",
|
|
41
|
+
),
|
|
44
42
|
);
|
|
45
|
-
const
|
|
43
|
+
const response = (await app.invoke(
|
|
44
|
+
"POST",
|
|
45
|
+
"/system/departments/:id/transition",
|
|
46
|
+
{
|
|
47
|
+
guardContext,
|
|
48
|
+
params: { id: "dept_platform" },
|
|
49
|
+
query: {},
|
|
50
|
+
body: { action: "delete" },
|
|
51
|
+
},
|
|
52
|
+
)) as Response;
|
|
46
53
|
|
|
54
|
+
expect(response.status).toBe(403);
|
|
55
|
+
await expect(response.json()).resolves.toMatchObject({
|
|
56
|
+
error: "FORBIDDEN",
|
|
57
|
+
});
|
|
58
|
+
expect(runtime.auditSink.list()[0]?.action).toBe("security.denied");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("快捷创建必须挂到唯一根组织并拒绝第二个根", async () => {
|
|
62
|
+
const { app, context } = await fixture();
|
|
63
|
+
const missingParent = (await app.invoke(
|
|
64
|
+
"POST",
|
|
65
|
+
"/system/departments/quick",
|
|
66
|
+
{
|
|
67
|
+
...context,
|
|
68
|
+
body: { code: "sales", name: "销售部" },
|
|
69
|
+
},
|
|
70
|
+
)) as Response;
|
|
47
71
|
const department = (await app.invoke("POST", "/system/departments/quick", {
|
|
48
72
|
...context,
|
|
49
|
-
body: {
|
|
50
|
-
|
|
51
|
-
|
|
73
|
+
body: {
|
|
74
|
+
code: "sales",
|
|
75
|
+
name: "销售部",
|
|
76
|
+
parentId: "dept_platform",
|
|
77
|
+
},
|
|
78
|
+
})) as { id: string; parentId: string };
|
|
79
|
+
const secondRoot = (await app.invoke("POST", "/system/departments", {
|
|
80
|
+
...context,
|
|
81
|
+
body: { code: "root2", name: "第二根组织", order: 20 },
|
|
82
|
+
})) as Response;
|
|
83
|
+
|
|
84
|
+
expect(missingParent.status).toBe(400);
|
|
85
|
+
await expect(missingParent.json()).resolves.toMatchObject({
|
|
86
|
+
error: "DEPARTMENT_PARENT_REQUIRED",
|
|
87
|
+
});
|
|
88
|
+
expect(department.parentId).toBe("dept_platform");
|
|
89
|
+
expect(secondRoot.status).toBe(400);
|
|
90
|
+
await expect(secondRoot.json()).resolves.toMatchObject({
|
|
91
|
+
error: "DEPARTMENT_PARENT_REQUIRED",
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("移动部门会拒绝自身、下级和根组织变更", async () => {
|
|
96
|
+
const { app, context } = await fixture();
|
|
97
|
+
await createDepartment(app, context, "dept_a", "部门 A", "dept_platform");
|
|
98
|
+
await createDepartment(app, context, "dept_b", "部门 B", "dept_a");
|
|
99
|
+
|
|
100
|
+
const cycle = (await app.invoke("PUT", "/system/departments/:id", {
|
|
101
|
+
...context,
|
|
102
|
+
params: { id: "dept_a" },
|
|
103
|
+
body: { parentId: "dept_b" },
|
|
104
|
+
})) as Response;
|
|
105
|
+
const rootMove = (await app.invoke("PUT", "/system/departments/:id", {
|
|
106
|
+
...context,
|
|
107
|
+
params: { id: "dept_platform" },
|
|
108
|
+
body: { parentId: "dept_a" },
|
|
109
|
+
})) as Response;
|
|
110
|
+
|
|
111
|
+
expect(cycle.status).toBe(409);
|
|
112
|
+
await expect(cycle.json()).resolves.toMatchObject({
|
|
113
|
+
error: "DEPARTMENT_CYCLE",
|
|
114
|
+
});
|
|
115
|
+
expect(rootMove.status).toBe(409);
|
|
116
|
+
await expect(rootMove.json()).resolves.toMatchObject({
|
|
117
|
+
error: "ROOT_DEPARTMENT_IMMUTABLE",
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("有关联数据时要求显式迁移,并原子更新子级、用户和角色范围", async () => {
|
|
122
|
+
const { app, context, repositories, runtime } = await fixture();
|
|
123
|
+
await createDepartment(
|
|
124
|
+
app,
|
|
125
|
+
context,
|
|
126
|
+
"dept_source",
|
|
127
|
+
"待处置部门",
|
|
128
|
+
"dept_platform",
|
|
129
|
+
);
|
|
130
|
+
await createDepartment(
|
|
131
|
+
app,
|
|
132
|
+
context,
|
|
133
|
+
"dept_child",
|
|
134
|
+
"下级部门",
|
|
135
|
+
"dept_source",
|
|
136
|
+
);
|
|
137
|
+
await app.invoke("POST", "/system/roles", {
|
|
52
138
|
...context,
|
|
53
139
|
body: {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
140
|
+
id: "role_custom",
|
|
141
|
+
code: "custom-scope",
|
|
142
|
+
name: "自定义范围",
|
|
143
|
+
dataScope: "custom",
|
|
144
|
+
departmentIds: ["dept_source"],
|
|
145
|
+
permissionKeys: [],
|
|
146
|
+
menuKeys: [],
|
|
58
147
|
},
|
|
59
|
-
})
|
|
60
|
-
|
|
148
|
+
});
|
|
149
|
+
await app.invoke("POST", "/system/users", {
|
|
61
150
|
...context,
|
|
62
151
|
body: {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
152
|
+
id: "user_source",
|
|
153
|
+
username: "source-user",
|
|
154
|
+
displayName: "来源用户",
|
|
155
|
+
roleIds: ["role_custom"],
|
|
156
|
+
departmentId: "dept_source",
|
|
67
157
|
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const blocked = (await app.invoke("DELETE", "/system/departments/:id", {
|
|
161
|
+
...context,
|
|
162
|
+
params: { id: "dept_source" },
|
|
68
163
|
})) as Response;
|
|
164
|
+
expect(blocked.status).toBe(409);
|
|
165
|
+
await expect(blocked.json()).resolves.toMatchObject({
|
|
166
|
+
error: "DEPARTMENT_MIGRATION_REQUIRED",
|
|
167
|
+
details: {
|
|
168
|
+
childDepartmentCount: 1,
|
|
169
|
+
userCount: 1,
|
|
170
|
+
roleCount: 1,
|
|
171
|
+
},
|
|
172
|
+
});
|
|
69
173
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
174
|
+
const transitioned = (await app.invoke(
|
|
175
|
+
"POST",
|
|
176
|
+
"/system/departments/:id/transition",
|
|
177
|
+
{
|
|
178
|
+
...context,
|
|
179
|
+
params: { id: "dept_source" },
|
|
180
|
+
body: {
|
|
181
|
+
action: "delete",
|
|
182
|
+
targetDepartmentId: "dept_platform",
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
)) as {
|
|
186
|
+
movedChildren: number;
|
|
187
|
+
movedUsers: number;
|
|
188
|
+
updatedRoles: number;
|
|
189
|
+
};
|
|
190
|
+
expect(transitioned).toMatchObject({
|
|
191
|
+
movedChildren: 1,
|
|
192
|
+
movedUsers: 1,
|
|
193
|
+
updatedRoles: 1,
|
|
194
|
+
});
|
|
195
|
+
expect(await repositories.departments.get("dept_source")).toBeUndefined();
|
|
196
|
+
expect(await repositories.departments.get("dept_child")).toMatchObject({
|
|
197
|
+
parentId: "dept_platform",
|
|
75
198
|
});
|
|
76
|
-
expect(
|
|
77
|
-
"
|
|
199
|
+
expect(await repositories.users.get("user_source")).toMatchObject({
|
|
200
|
+
departmentId: "dept_platform",
|
|
201
|
+
});
|
|
202
|
+
expect(await repositories.roles.get("role_custom")).toMatchObject({
|
|
203
|
+
departmentIds: ["dept_platform"],
|
|
204
|
+
});
|
|
205
|
+
expect(runtime.auditSink.list().at(-1)?.action).toBe(
|
|
206
|
+
"organization.department.transition",
|
|
78
207
|
);
|
|
79
208
|
});
|
|
209
|
+
|
|
210
|
+
test("角色自定义数据权限只接受启用部门", async () => {
|
|
211
|
+
const { app, context } = await fixture();
|
|
212
|
+
const response = (await app.invoke("POST", "/system/roles", {
|
|
213
|
+
...context,
|
|
214
|
+
body: {
|
|
215
|
+
code: "invalid-custom",
|
|
216
|
+
name: "无效范围",
|
|
217
|
+
dataScope: "custom",
|
|
218
|
+
departmentIds: ["dept_missing"],
|
|
219
|
+
},
|
|
220
|
+
})) as Response;
|
|
221
|
+
expect(response.status).toBe(400);
|
|
222
|
+
await expect(response.json()).resolves.toMatchObject({
|
|
223
|
+
error: "INVALID_ROLE_DEPARTMENT_SCOPE",
|
|
224
|
+
});
|
|
225
|
+
});
|
|
80
226
|
});
|
|
227
|
+
|
|
228
|
+
function createDepartment(
|
|
229
|
+
app: FakeRouteApp,
|
|
230
|
+
context: Record<string, unknown>,
|
|
231
|
+
id: string,
|
|
232
|
+
name: string,
|
|
233
|
+
parentId: string,
|
|
234
|
+
) {
|
|
235
|
+
return app.invoke("POST", "/system/departments", {
|
|
236
|
+
...context,
|
|
237
|
+
body: { id, code: id, name, parentId, order: 10 },
|
|
238
|
+
});
|
|
239
|
+
}
|