@nocobase/plugin-departments 2.0.0-alpha.9 → 2.0.0-beta.2

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 (35) hide show
  1. package/dist/client/collections/users.d.ts +32 -11
  2. package/dist/client/departments/DepartmentOwnersField.d.ts +3 -1
  3. package/dist/client/departments/DepartmentsField.d.ts +20 -0
  4. package/dist/client/index.js +1 -1
  5. package/dist/externalVersion.js +10 -9
  6. package/dist/locale/de-DE.json +37 -0
  7. package/dist/locale/en-US.json +25 -23
  8. package/dist/locale/es-ES.json +37 -0
  9. package/dist/locale/fr-FR.json +37 -0
  10. package/dist/locale/hu-HU.json +37 -0
  11. package/dist/locale/id-ID.json +37 -0
  12. package/dist/locale/it-IT.json +37 -0
  13. package/dist/locale/ja-JP.json +37 -0
  14. package/dist/locale/ko-KR.json +37 -0
  15. package/dist/locale/nl-NL.json +37 -0
  16. package/dist/locale/pt-BR.json +37 -0
  17. package/dist/locale/ru-RU.json +37 -0
  18. package/dist/locale/tr-TR.json +37 -0
  19. package/dist/locale/uk-UA.json +37 -0
  20. package/dist/locale/vi-VN.json +37 -0
  21. package/dist/locale/zh-CN.json +25 -23
  22. package/dist/locale/zh-TW.json +37 -0
  23. package/dist/server/actions/users.js +24 -38
  24. package/dist/server/collections/departments.js +1 -1
  25. package/dist/server/collections/departmentsUsers.js +4 -2
  26. package/dist/server/collections/users.d.ts +12 -5
  27. package/dist/server/collections/users.js +25 -16
  28. package/dist/server/department-data-sync-resource.js +21 -24
  29. package/dist/server/middlewares/set-departments-roles.js +1 -1
  30. package/dist/server/middlewares/set-main-department.js +48 -58
  31. package/dist/server/migrations/migrate-main-department-id-20250828100101.d.ts +14 -0
  32. package/dist/server/migrations/migrate-main-department-id-20250828100101.js +133 -0
  33. package/dist/server/plugin.d.ts +1 -0
  34. package/dist/server/plugin.js +49 -3
  35. package/package.json +4 -2
@@ -0,0 +1,133 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var migrate_main_department_id_20250828100101_exports = {};
28
+ __export(migrate_main_department_id_20250828100101_exports, {
29
+ default: () => migrate_main_department_id_20250828100101_default
30
+ });
31
+ module.exports = __toCommonJS(migrate_main_department_id_20250828100101_exports);
32
+ var import_server = require("@nocobase/server");
33
+ class migrate_main_department_id_20250828100101_default extends import_server.Migration {
34
+ on = "afterLoad";
35
+ appVersion = "<2.0.0";
36
+ async up() {
37
+ await this.db.sequelize.transaction(async (transaction) => {
38
+ var _a, _b;
39
+ const users = this.db.getCollection("users");
40
+ const departments = this.db.getCollection("departments");
41
+ if (!users || !departments) {
42
+ return;
43
+ }
44
+ const fieldRepo = this.db.getRepository("fields");
45
+ if (!fieldRepo) {
46
+ return;
47
+ }
48
+ const currentMainField = await fieldRepo.findOne({
49
+ filter: {
50
+ collectionName: "users",
51
+ name: "mainDepartment"
52
+ },
53
+ transaction
54
+ });
55
+ const iface = (_a = currentMainField == null ? void 0 : currentMainField.get) == null ? void 0 : _a.call(currentMainField, "interface");
56
+ const type = (_b = currentMainField == null ? void 0 : currentMainField.get) == null ? void 0 : _b.call(currentMainField, "type");
57
+ const shouldMigrate = !currentMainField || type === "belongsToMany" || iface === "m2m";
58
+ if (!shouldMigrate) {
59
+ return;
60
+ }
61
+ const rows = await this.db.getRepository("departmentsUsers").find({
62
+ filter: {
63
+ isMain: true
64
+ },
65
+ fields: ["userId", "departmentId"],
66
+ transaction
67
+ });
68
+ for (const row of rows) {
69
+ await this.db.getRepository("users").update({
70
+ filter: {
71
+ id: row.userId
72
+ },
73
+ values: {
74
+ mainDepartmentId: row.departmentId
75
+ },
76
+ transaction,
77
+ hooks: false
78
+ });
79
+ }
80
+ await fieldRepo.destroy({
81
+ filter: {
82
+ collectionName: "departmentsUsers",
83
+ name: "isMain"
84
+ },
85
+ transaction
86
+ });
87
+ const mainDepartmentField = await fieldRepo.findOne({
88
+ filter: {
89
+ collectionName: "users",
90
+ name: "mainDepartment"
91
+ },
92
+ transaction
93
+ });
94
+ if (mainDepartmentField) {
95
+ const nextOptions = {
96
+ ...mainDepartmentField.options || {},
97
+ target: "departments",
98
+ foreignKey: "mainDepartmentId",
99
+ onDelete: "SET NULL",
100
+ sourceKey: "id",
101
+ targetKey: "id",
102
+ uiSchema: {
103
+ type: "string",
104
+ title: '{{t("Main department")}}',
105
+ "x-component": "AssociationField",
106
+ "x-component-props": {
107
+ multiple: false,
108
+ fieldNames: {
109
+ label: "title",
110
+ value: "id"
111
+ }
112
+ }
113
+ }
114
+ };
115
+ delete nextOptions.through;
116
+ delete nextOptions.otherKey;
117
+ delete nextOptions.throughScope;
118
+ await fieldRepo.update({
119
+ filter: {
120
+ collectionName: "users",
121
+ name: "mainDepartment"
122
+ },
123
+ values: {
124
+ type: "belongsTo",
125
+ interface: "m2o",
126
+ options: nextOptions
127
+ },
128
+ transaction
129
+ });
130
+ }
131
+ });
132
+ }
133
+ }
@@ -15,5 +15,6 @@ export declare class PluginDepartmentsServer extends Plugin {
15
15
  afterEnable(): Promise<void>;
16
16
  afterDisable(): Promise<void>;
17
17
  remove(): Promise<void>;
18
+ registerErrorHandler(): void;
18
19
  }
19
20
  export default PluginDepartmentsServer;
@@ -35,7 +35,6 @@ var import_departments = require("./actions/departments");
35
35
  var import_users = require("./actions/users");
36
36
  var import_users2 = require("./collections/users");
37
37
  var import_middlewares = require("./middlewares");
38
- var import_set_departments_roles = require("./middlewares/set-departments-roles");
39
38
  var import_department = require("./models/department");
40
39
  var import_department_data_sync_resource = require("./department-data-sync-resource");
41
40
  class PluginDepartmentsServer extends import_server.Plugin {
@@ -52,6 +51,7 @@ class PluginDepartmentsServer extends import_server.Plugin {
52
51
  });
53
52
  }
54
53
  async load() {
54
+ this.registerErrorHandler();
55
55
  this.app.resourceManager.registerActionHandlers({
56
56
  "users:listExcludeDept": import_users.listExcludeDept,
57
57
  "users:setMainDepartment": import_users.setMainDepartment,
@@ -73,13 +73,13 @@ class PluginDepartmentsServer extends import_server.Plugin {
73
73
  "departments.members:*"
74
74
  ]
75
75
  });
76
- this.app.resourceManager.use(import_set_departments_roles.setDepartmentsInfo, {
76
+ this.app.resourceManager.use(import_middlewares.setDepartmentsInfo, {
77
77
  tag: "setDepartmentsInfo",
78
78
  before: "setCurrentRole",
79
79
  after: "auth"
80
80
  });
81
81
  this.app.dataSourceManager.afterAddDataSource((dataSource) => {
82
- dataSource.resourceManager.use(import_set_departments_roles.setDepartmentsInfo, {
82
+ dataSource.resourceManager.use(import_middlewares.setDepartmentsInfo, {
83
83
  tag: "setDepartmentsInfo",
84
84
  before: "setCurrentRole",
85
85
  after: "auth"
@@ -98,6 +98,38 @@ class PluginDepartmentsServer extends import_server.Plugin {
98
98
  const cache = this.app.cache;
99
99
  await cache.del(`departments:${model.get("userId")}`);
100
100
  });
101
+ this.app.db.on("users.beforeSave", async (model, options) => {
102
+ var _a;
103
+ const mainDepartmentId = model.get("mainDepartmentId");
104
+ if (!mainDepartmentId) {
105
+ return;
106
+ }
107
+ const userId = model.get("id");
108
+ const transaction = options == null ? void 0 : options.transaction;
109
+ if (userId) {
110
+ const userDepartment = await this.app.db.getRepository("departmentsUsers").findOne({
111
+ filter: {
112
+ userId,
113
+ departmentId: mainDepartmentId
114
+ },
115
+ transaction
116
+ });
117
+ if (userDepartment) {
118
+ return;
119
+ }
120
+ }
121
+ const submittedDepartments = (_a = options == null ? void 0 : options.values) == null ? void 0 : _a.departments;
122
+ if (Array.isArray(submittedDepartments)) {
123
+ const included = submittedDepartments.some((d) => {
124
+ const id = typeof d === "object" ? d && (d.id ?? d) : d;
125
+ return `${id}` === `${mainDepartmentId}`;
126
+ });
127
+ if (included) {
128
+ return;
129
+ }
130
+ }
131
+ throw new Error(`Invalid main department, it must be one of the user's departments`);
132
+ });
101
133
  this.app.on("beforeSignOut", ({ userId }) => {
102
134
  this.app.cache.del(`departments:${userId}`);
103
135
  });
@@ -146,6 +178,20 @@ class PluginDepartmentsServer extends import_server.Plugin {
146
178
  }
147
179
  async remove() {
148
180
  }
181
+ registerErrorHandler() {
182
+ const errorHandlerPlugin = this.app.pm.get("error-handler");
183
+ errorHandlerPlugin.errorHandler.register(
184
+ (err) => {
185
+ return err.message === "Invalid main department, it must be one of the user's departments";
186
+ },
187
+ (err, ctx) => {
188
+ return ctx.throw(
189
+ 400,
190
+ ctx.i18n.t("Invalid main department, it must be one of the user's departments", { ns: "departments" })
191
+ );
192
+ }
193
+ );
194
+ }
149
195
  }
150
196
  var plugin_default = PluginDepartmentsServer;
151
197
  // Annotate the CommonJS export names for ESM import in node:
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-departments",
3
3
  "displayName": "Departments",
4
+ "displayName.ru-RU": "Подразделения",
4
5
  "displayName.zh-CN": "部门",
5
6
  "description": "Organize users by departments, set hierarchical relationships, link roles to control permissions, and use departments as variables in workflows and expressions.",
7
+ "description.ru-RU": "Организация пользователей по подразделениям, установление иерархических связей, привязка ролей для управления правами и использование подразделений в качестве переменных в рабочих процессах и выражениях.",
6
8
  "description.zh-CN": "以部门来组织用户,设定上下级关系,绑定角色控制权限,并支持作为变量用于工作流和表达式。",
7
- "version": "2.0.0-alpha.9",
9
+ "version": "2.0.0-beta.2",
8
10
  "main": "dist/server/index.js",
9
11
  "peerDependencies": {
10
12
  "@nocobase/actions": "2.x",
@@ -16,5 +18,5 @@
16
18
  "keywords": [
17
19
  "Users & permissions"
18
20
  ],
19
- "gitHead": "4a9acf96f21a3aa35bccbd188b942595b09da0a9"
21
+ "gitHead": "b77a33ee933ae6e09d2d5dce017ca15d8552d57b"
20
22
  }