@nocobase/plugin-departments 1.6.20

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 (84) hide show
  1. package/LICENSE.txt +161 -0
  2. package/README.md +1 -0
  3. package/client.d.ts +2 -0
  4. package/client.js +1 -0
  5. package/dist/client/ResourcesProvider.d.ts +20 -0
  6. package/dist/client/collections/departments.d.ts +144 -0
  7. package/dist/client/collections/users.d.ts +205 -0
  8. package/dist/client/components/DepartmentOwnersField.d.ts +18 -0
  9. package/dist/client/components/ReadOnlyAssociationField.d.ts +18 -0
  10. package/dist/client/components/UserDepartmentsField.d.ts +18 -0
  11. package/dist/client/components/UserMainDepartmentField.d.ts +18 -0
  12. package/dist/client/components/fieldSettings.d.ts +22 -0
  13. package/dist/client/components/index.d.ts +20 -0
  14. package/dist/client/departments/AggregateSearch.d.ts +18 -0
  15. package/dist/client/departments/Department.d.ts +45 -0
  16. package/dist/client/departments/DepartmentBlock.d.ts +18 -0
  17. package/dist/client/departments/DepartmentField.d.ts +18 -0
  18. package/dist/client/departments/DepartmentManagement.d.ts +18 -0
  19. package/dist/client/departments/DepartmentOwnersField.d.ts +10 -0
  20. package/dist/client/departments/DepartmentTable.d.ts +18 -0
  21. package/dist/client/departments/DepartmentTree.d.ts +30 -0
  22. package/dist/client/departments/DepartmentTreeSelect.d.ts +24 -0
  23. package/dist/client/departments/IsOwnerField.d.ts +18 -0
  24. package/dist/client/departments/Member.d.ts +19 -0
  25. package/dist/client/departments/NewDepartment.d.ts +10 -0
  26. package/dist/client/departments/UserDepartmentsField.d.ts +10 -0
  27. package/dist/client/departments/schemas/departments.d.ts +257 -0
  28. package/dist/client/departments/schemas/users.d.ts +443 -0
  29. package/dist/client/hooks/departments-manager.d.ts +37 -0
  30. package/dist/client/hooks/index.d.ts +18 -0
  31. package/dist/client/hooks/tree-manager.d.ts +38 -0
  32. package/dist/client/hooks/useTableBlockProps.d.ts +28 -0
  33. package/dist/client/index.d.ts +23 -0
  34. package/dist/client/index.js +10 -0
  35. package/dist/client/locale.d.ts +9 -0
  36. package/dist/client/roles/RoleDepartmentsManager.d.ts +18 -0
  37. package/dist/client/roles/schemas/departments.d.ts +235 -0
  38. package/dist/client/utils.d.ts +17 -0
  39. package/dist/externalVersion.js +29 -0
  40. package/dist/index.d.ts +18 -0
  41. package/dist/index.js +48 -0
  42. package/dist/locale/en-US.json +35 -0
  43. package/dist/locale/zh-CN.json +35 -0
  44. package/dist/server/actions/departments.d.ts +21 -0
  45. package/dist/server/actions/departments.js +114 -0
  46. package/dist/server/actions/users.d.ts +20 -0
  47. package/dist/server/actions/users.js +151 -0
  48. package/dist/server/collections/departmentRoles.d.ts +10 -0
  49. package/dist/server/collections/departmentRoles.js +37 -0
  50. package/dist/server/collections/departments.d.ts +37 -0
  51. package/dist/server/collections/departments.js +172 -0
  52. package/dist/server/collections/departmentsUsers.d.ts +10 -0
  53. package/dist/server/collections/departmentsUsers.js +53 -0
  54. package/dist/server/collections/roles.d.ts +14 -0
  55. package/dist/server/collections/roles.js +48 -0
  56. package/dist/server/collections/users.d.ts +67 -0
  57. package/dist/server/collections/users.js +96 -0
  58. package/dist/server/department-data-sync-resource.d.ts +27 -0
  59. package/dist/server/department-data-sync-resource.js +339 -0
  60. package/dist/server/index.d.ts +17 -0
  61. package/dist/server/index.js +42 -0
  62. package/dist/server/middlewares/destroy-department-check.d.ts +18 -0
  63. package/dist/server/middlewares/destroy-department-check.js +62 -0
  64. package/dist/server/middlewares/index.d.ts +22 -0
  65. package/dist/server/middlewares/index.js +40 -0
  66. package/dist/server/middlewares/reset-user-departments-cache.d.ts +18 -0
  67. package/dist/server/middlewares/reset-user-departments-cache.js +48 -0
  68. package/dist/server/middlewares/set-department-owners.d.ts +18 -0
  69. package/dist/server/middlewares/set-department-owners.js +83 -0
  70. package/dist/server/middlewares/set-departments-roles.d.ts +18 -0
  71. package/dist/server/middlewares/set-departments-roles.js +71 -0
  72. package/dist/server/middlewares/set-main-department.d.ts +18 -0
  73. package/dist/server/middlewares/set-main-department.js +114 -0
  74. package/dist/server/middlewares/update-department-isleaf.d.ts +18 -0
  75. package/dist/server/middlewares/update-department-isleaf.js +93 -0
  76. package/dist/server/migrations/update-field-uischemas-20240307124823.d.ts +20 -0
  77. package/dist/server/migrations/update-field-uischemas-20240307124823.js +106 -0
  78. package/dist/server/models/department.d.ts +20 -0
  79. package/dist/server/models/department.js +47 -0
  80. package/dist/server/plugin.d.ts +19 -0
  81. package/dist/server/plugin.js +154 -0
  82. package/package.json +22 -0
  83. package/server.d.ts +2 -0
  84. package/server.js +1 -0
@@ -0,0 +1,10 @@
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
+ declare const _default: import("@nocobase/database").CollectionOptions;
10
+ export default _default;
@@ -0,0 +1,53 @@
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 departmentsUsers_exports = {};
28
+ __export(departmentsUsers_exports, {
29
+ default: () => departmentsUsers_default
30
+ });
31
+ module.exports = __toCommonJS(departmentsUsers_exports);
32
+ var import_database = require("@nocobase/database");
33
+ var departmentsUsers_default = (0, import_database.defineCollection)({
34
+ name: "departmentsUsers",
35
+ dumpRules: "required",
36
+ migrationRules: ["schema-only"],
37
+ fields: [
38
+ {
39
+ type: "boolean",
40
+ name: "isOwner",
41
+ // Weather the user is the owner of the department
42
+ allowNull: false,
43
+ defaultValue: false
44
+ },
45
+ {
46
+ type: "boolean",
47
+ name: "isMain",
48
+ // Weather this is the main department of the user
49
+ allowNull: false,
50
+ defaultValue: false
51
+ }
52
+ ]
53
+ });
@@ -0,0 +1,14 @@
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
+ declare const _default: {
10
+ collectionOptions: import("@nocobase/database").CollectionOptions;
11
+ mergeOptions: import("deepmerge").Options;
12
+ extend: boolean;
13
+ };
14
+ export default _default;
@@ -0,0 +1,48 @@
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 roles_exports = {};
28
+ __export(roles_exports, {
29
+ default: () => roles_default
30
+ });
31
+ module.exports = __toCommonJS(roles_exports);
32
+ var import_database = require("@nocobase/database");
33
+ var roles_default = (0, import_database.extendCollection)({
34
+ name: "roles",
35
+ fields: [
36
+ {
37
+ type: "belongsToMany",
38
+ name: "departments",
39
+ target: "departments",
40
+ foreignKey: "roleName",
41
+ otherKey: "departmentId",
42
+ onDelete: "CASCADE",
43
+ sourceKey: "name",
44
+ targetKey: "id",
45
+ through: "departmentsRoles"
46
+ }
47
+ ]
48
+ });
@@ -0,0 +1,67 @@
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
+ export declare const departmentsField: {
10
+ collectionName: string;
11
+ interface: string;
12
+ type: string;
13
+ name: string;
14
+ target: string;
15
+ foreignKey: string;
16
+ otherKey: string;
17
+ onDelete: string;
18
+ sourceKey: string;
19
+ targetKey: string;
20
+ through: string;
21
+ uiSchema: {
22
+ type: string;
23
+ title: string;
24
+ 'x-component': string;
25
+ 'x-component-props': {
26
+ multiple: boolean;
27
+ fieldNames: {
28
+ label: string;
29
+ value: string;
30
+ };
31
+ };
32
+ };
33
+ };
34
+ export declare const mainDepartmentField: {
35
+ collectionName: string;
36
+ interface: string;
37
+ type: string;
38
+ name: string;
39
+ target: string;
40
+ foreignKey: string;
41
+ otherKey: string;
42
+ onDelete: string;
43
+ sourceKey: string;
44
+ targetKey: string;
45
+ through: string;
46
+ throughScope: {
47
+ isMain: boolean;
48
+ };
49
+ uiSchema: {
50
+ type: string;
51
+ title: string;
52
+ 'x-component': string;
53
+ 'x-component-props': {
54
+ multiple: boolean;
55
+ fieldNames: {
56
+ label: string;
57
+ value: string;
58
+ };
59
+ };
60
+ };
61
+ };
62
+ declare const _default: {
63
+ collectionOptions: import("@nocobase/database").CollectionOptions;
64
+ mergeOptions: import("deepmerge").Options;
65
+ extend: boolean;
66
+ };
67
+ export default _default;
@@ -0,0 +1,96 @@
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 users_exports = {};
28
+ __export(users_exports, {
29
+ default: () => users_default,
30
+ departmentsField: () => departmentsField,
31
+ mainDepartmentField: () => mainDepartmentField
32
+ });
33
+ module.exports = __toCommonJS(users_exports);
34
+ var import_database = require("@nocobase/database");
35
+ const departmentsField = {
36
+ collectionName: "users",
37
+ interface: "m2m",
38
+ type: "belongsToMany",
39
+ name: "departments",
40
+ target: "departments",
41
+ foreignKey: "userId",
42
+ otherKey: "departmentId",
43
+ onDelete: "CASCADE",
44
+ sourceKey: "id",
45
+ targetKey: "id",
46
+ through: "departmentsUsers",
47
+ uiSchema: {
48
+ type: "m2m",
49
+ title: '{{t("Departments")}}',
50
+ "x-component": "UserDepartmentsField",
51
+ "x-component-props": {
52
+ multiple: true,
53
+ fieldNames: {
54
+ label: "title",
55
+ value: "name"
56
+ }
57
+ }
58
+ }
59
+ };
60
+ const mainDepartmentField = {
61
+ collectionName: "users",
62
+ interface: "m2m",
63
+ type: "belongsToMany",
64
+ name: "mainDepartment",
65
+ target: "departments",
66
+ foreignKey: "userId",
67
+ otherKey: "departmentId",
68
+ onDelete: "CASCADE",
69
+ sourceKey: "id",
70
+ targetKey: "id",
71
+ through: "departmentsUsers",
72
+ throughScope: {
73
+ isMain: true
74
+ },
75
+ uiSchema: {
76
+ type: "m2m",
77
+ title: '{{t("Main department")}}',
78
+ "x-component": "UserMainDepartmentField",
79
+ "x-component-props": {
80
+ multiple: false,
81
+ fieldNames: {
82
+ label: "title",
83
+ value: "name"
84
+ }
85
+ }
86
+ }
87
+ };
88
+ var users_default = (0, import_database.extendCollection)({
89
+ name: "users",
90
+ fields: [departmentsField, mainDepartmentField]
91
+ });
92
+ // Annotate the CommonJS export names for ESM import in node:
93
+ 0 && (module.exports = {
94
+ departmentsField,
95
+ mainDepartmentField
96
+ });
@@ -0,0 +1,27 @@
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
+ import { Model } from '@nocobase/database';
10
+ import lodash from 'lodash';
11
+ import { FormatDepartment, FormatUserDepartment, OriginRecord, PrimaryKey, RecordResourceChanged, SyncAccept, UserDataResource } from '@nocobase/plugin-user-data-sync';
12
+ export declare class DepartmentDataSyncResource extends UserDataResource {
13
+ name: string;
14
+ accepts: SyncAccept[];
15
+ get userRepo(): import("@nocobase/database").Repository<any, any>;
16
+ get deptRepo(): import("@nocobase/database").Repository<any, any>;
17
+ get deptUserRepo(): import("@nocobase/database").Repository<any, any>;
18
+ getFlteredSourceDepartment(sourceDepartment: FormatDepartment): lodash.Omit<FormatDepartment, string>;
19
+ update(record: OriginRecord, resourcePks: PrimaryKey[]): Promise<RecordResourceChanged[]>;
20
+ create(record: OriginRecord): Promise<RecordResourceChanged[]>;
21
+ getDepartmentIdsBySourceUks(sourceUks: PrimaryKey[], sourceName: string): Promise<any>;
22
+ getDepartmentIdBySourceUk(sourceUk: PrimaryKey, sourceName: string): Promise<any>;
23
+ updateUserDepartments(user: any, currentDepartmentIds: PrimaryKey[], sourceDepartments: (PrimaryKey | FormatUserDepartment)[], sourceName: string): Promise<RecordResourceChanged[]>;
24
+ updateDepartment(department: Model, sourceDepartment: FormatDepartment, sourceName: string): Promise<void>;
25
+ createDepartment(sourceDepartment: FormatDepartment, sourceName: string): Promise<string>;
26
+ updateParentDepartment(department: Model, parentUid: string, sourceName: string): Promise<void>;
27
+ }
@@ -0,0 +1,339 @@
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 __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var department_data_sync_resource_exports = {};
38
+ __export(department_data_sync_resource_exports, {
39
+ DepartmentDataSyncResource: () => DepartmentDataSyncResource
40
+ });
41
+ module.exports = __toCommonJS(department_data_sync_resource_exports);
42
+ var import_lodash = __toESM(require("lodash"));
43
+ var import_plugin_user_data_sync = require("@nocobase/plugin-user-data-sync");
44
+ class DepartmentDataSyncResource extends import_plugin_user_data_sync.UserDataResource {
45
+ name = "departments";
46
+ accepts = ["user", "department"];
47
+ get userRepo() {
48
+ return this.db.getRepository("users");
49
+ }
50
+ get deptRepo() {
51
+ return this.db.getRepository("departments");
52
+ }
53
+ get deptUserRepo() {
54
+ return this.db.getRepository("departmentsUsers");
55
+ }
56
+ getFlteredSourceDepartment(sourceDepartment) {
57
+ const deleteProps = [
58
+ "id",
59
+ "uid",
60
+ "createdAt",
61
+ "updatedAt",
62
+ "sort",
63
+ "createdById",
64
+ "updatedById",
65
+ "isDeleted",
66
+ "parentId",
67
+ "parentUid"
68
+ ];
69
+ return import_lodash.default.omit(sourceDepartment, deleteProps);
70
+ }
71
+ async update(record, resourcePks) {
72
+ const { dataType, metaData, sourceName } = record;
73
+ if (dataType === "user") {
74
+ const sourceUser = metaData;
75
+ if (sourceUser.isDeleted) {
76
+ if (!resourcePks || !resourcePks.length) {
77
+ return [];
78
+ } else {
79
+ return resourcePks.map((id) => ({ resourcesPk: id, isDeleted: true }));
80
+ }
81
+ }
82
+ const resources = record.resources.filter((r) => r.resource === "users");
83
+ if (!resources.length) {
84
+ return [];
85
+ }
86
+ const user = await this.userRepo.findOne({
87
+ filterByTk: resources[0].resourcePk
88
+ });
89
+ if (!user) {
90
+ if (!resourcePks || !resourcePks.length) {
91
+ return [];
92
+ } else {
93
+ return resourcePks.map((id) => ({ resourcesPk: id, isDeleted: true }));
94
+ }
95
+ } else {
96
+ return await this.updateUserDepartments(user, resourcePks, sourceUser.departments, sourceName);
97
+ }
98
+ } else if (dataType === "department") {
99
+ const sourceDepartment = metaData;
100
+ const department = await this.deptRepo.findOne({
101
+ filterByTk: resourcePks[0]
102
+ });
103
+ if (!department) {
104
+ if (sourceDepartment.isDeleted) {
105
+ return [{ resourcesPk: resourcePks[0], isDeleted: true }];
106
+ }
107
+ const result = await this.create(record);
108
+ return [...result, { resourcesPk: resourcePks[0], isDeleted: true }];
109
+ }
110
+ await this.updateDepartment(department, sourceDepartment, sourceName);
111
+ } else {
112
+ this.logger.warn(`update department: unsupported data type: ${dataType}`);
113
+ }
114
+ return [];
115
+ }
116
+ async create(record) {
117
+ const { dataType, metaData, sourceName } = record;
118
+ if (dataType === "user") {
119
+ const sourceUser = metaData;
120
+ if (sourceUser.isDeleted) {
121
+ return [];
122
+ }
123
+ const resources = record.resources.filter((r) => r.resource === "users");
124
+ if (!resources.length) {
125
+ return [];
126
+ }
127
+ const user = await this.userRepo.findOne({
128
+ filterByTk: resources[0].resourcePk
129
+ });
130
+ return await this.updateUserDepartments(user, [], sourceUser.departments, sourceName);
131
+ } else if (dataType === "department") {
132
+ const sourceDepartment = metaData;
133
+ const newDepartmentId = await this.createDepartment(sourceDepartment, sourceName);
134
+ return [{ resourcesPk: newDepartmentId, isDeleted: false }];
135
+ } else {
136
+ this.logger.warn(`create department: unsupported data type: ${dataType}`);
137
+ }
138
+ return [];
139
+ }
140
+ async getDepartmentIdsBySourceUks(sourceUks, sourceName) {
141
+ const syncDepartmentRecords = await this.syncRecordRepo.find({
142
+ filter: {
143
+ sourceName,
144
+ dataType: "department",
145
+ sourceUk: { $in: sourceUks },
146
+ "resources.resource": this.name
147
+ },
148
+ appends: ["resources"]
149
+ });
150
+ const departmentIds = syncDepartmentRecords.filter((record) => {
151
+ var _a;
152
+ return (_a = record.resources) == null ? void 0 : _a.length;
153
+ }).map((record) => record.resources[0].resourcePk);
154
+ return departmentIds;
155
+ }
156
+ async getDepartmentIdBySourceUk(sourceUk, sourceName) {
157
+ var _a;
158
+ const syncDepartmentRecord = await this.syncRecordRepo.findOne({
159
+ filter: {
160
+ sourceName,
161
+ dataType: "department",
162
+ sourceUk,
163
+ "resources.resource": this.name
164
+ },
165
+ appends: ["resources"]
166
+ });
167
+ if (syncDepartmentRecord && ((_a = syncDepartmentRecord.resources) == null ? void 0 : _a.length)) {
168
+ return syncDepartmentRecord.resources[0].resourcePk;
169
+ }
170
+ }
171
+ async updateUserDepartments(user, currentDepartmentIds, sourceDepartments, sourceName) {
172
+ if (!this.deptRepo) {
173
+ return [];
174
+ }
175
+ if (!sourceDepartments || !sourceDepartments.length) {
176
+ const userDepartments = await user.getDepartments();
177
+ if (userDepartments.length) {
178
+ await user.removeDepartments(userDepartments);
179
+ }
180
+ if (currentDepartmentIds && currentDepartmentIds.length) {
181
+ return currentDepartmentIds.map((id) => ({ resourcesPk: id, isDeleted: true }));
182
+ } else {
183
+ return [];
184
+ }
185
+ } else {
186
+ const sourceDepartmentIds = sourceDepartments.map((sourceDepartment) => {
187
+ if (typeof sourceDepartment === "string" || typeof sourceDepartment === "number") {
188
+ return sourceDepartment;
189
+ }
190
+ return sourceDepartment.uid;
191
+ });
192
+ const newDepartmentIds = await this.getDepartmentIdsBySourceUks(sourceDepartmentIds, sourceName);
193
+ const newDepartments = await this.deptRepo.find({
194
+ filter: { id: { $in: newDepartmentIds } }
195
+ });
196
+ const realCurrentDepartments = await user.getDepartments();
197
+ const toRealRemoveDepartments = realCurrentDepartments.filter((currnetDepartment) => {
198
+ return !newDepartments.find((newDepartment) => newDepartment.id === currnetDepartment.id);
199
+ });
200
+ if (toRealRemoveDepartments.length) {
201
+ await user.removeDepartments(toRealRemoveDepartments);
202
+ }
203
+ const toRealAddDepartments = newDepartments.filter((newDepartment) => {
204
+ if (realCurrentDepartments.length === 0) {
205
+ return true;
206
+ }
207
+ return !realCurrentDepartments.find((currentDepartment) => currentDepartment.id === newDepartment.id);
208
+ });
209
+ if (toRealAddDepartments.length) {
210
+ await user.addDepartments(toRealAddDepartments);
211
+ }
212
+ for (const sourceDepartment of sourceDepartments) {
213
+ this.logger.debug("update dept owner: " + JSON.stringify(sourceDepartment));
214
+ let isOwner = false;
215
+ let isMain = false;
216
+ let uid;
217
+ if (typeof sourceDepartment !== "string" && typeof sourceDepartment !== "number") {
218
+ isOwner = sourceDepartment.isOwner || false;
219
+ isMain = sourceDepartment.isMain || false;
220
+ uid = sourceDepartment.uid;
221
+ } else {
222
+ uid = sourceDepartment;
223
+ }
224
+ const deptId = await this.getDepartmentIdBySourceUk(uid, sourceName);
225
+ this.logger.debug("update dept owner: " + JSON.stringify({ deptId, isOwner, isMain, userId: user.id }));
226
+ if (!deptId) {
227
+ continue;
228
+ }
229
+ await this.deptUserRepo.update({
230
+ filter: {
231
+ userId: user.id,
232
+ departmentId: deptId
233
+ },
234
+ values: {
235
+ isOwner,
236
+ isMain
237
+ }
238
+ });
239
+ }
240
+ const recordResourceChangeds = [];
241
+ if (currentDepartmentIds !== void 0 && currentDepartmentIds.length > 0) {
242
+ const toRemoveDepartmentIds = currentDepartmentIds.filter(
243
+ (currentDepartmentId) => !newDepartmentIds.includes(currentDepartmentId)
244
+ );
245
+ recordResourceChangeds.push(
246
+ ...toRemoveDepartmentIds.map((departmentId) => {
247
+ return { resourcesPk: departmentId, isDeleted: true };
248
+ })
249
+ );
250
+ const toAddDepartmentIds = newDepartmentIds.filter(
251
+ (newDepartmentId) => !currentDepartmentIds.includes(newDepartmentId)
252
+ );
253
+ recordResourceChangeds.push(
254
+ ...toAddDepartmentIds.map((departmentId) => {
255
+ return { resourcesPk: departmentId, isDeleted: false };
256
+ })
257
+ );
258
+ } else {
259
+ recordResourceChangeds.push(
260
+ ...toRealAddDepartments.map((department) => {
261
+ return {
262
+ resourcesPk: department.id,
263
+ isDeleted: false
264
+ };
265
+ })
266
+ );
267
+ }
268
+ return recordResourceChangeds;
269
+ }
270
+ }
271
+ async updateDepartment(department, sourceDepartment, sourceName) {
272
+ if (sourceDepartment.isDeleted) {
273
+ await department.destroy();
274
+ return;
275
+ }
276
+ let dataChanged = false;
277
+ const filteredSourceDepartment = this.getFlteredSourceDepartment(sourceDepartment);
278
+ import_lodash.default.forOwn(filteredSourceDepartment, (value, key) => {
279
+ if (department[key] !== value) {
280
+ department[key] = value;
281
+ dataChanged = true;
282
+ }
283
+ });
284
+ if (dataChanged) {
285
+ await department.save();
286
+ }
287
+ await this.updateParentDepartment(department, sourceDepartment.parentUid, sourceName);
288
+ }
289
+ async createDepartment(sourceDepartment, sourceName) {
290
+ const filteredSourceDepartment = this.getFlteredSourceDepartment(sourceDepartment);
291
+ const department = await this.deptRepo.create({
292
+ values: filteredSourceDepartment
293
+ });
294
+ await this.updateParentDepartment(department, sourceDepartment.parentUid, sourceName);
295
+ return department.id;
296
+ }
297
+ async updateParentDepartment(department, parentUid, sourceName) {
298
+ var _a;
299
+ if (!parentUid) {
300
+ const parentDepartment = await department.getParent();
301
+ if (parentDepartment) {
302
+ await department.setParent(null);
303
+ }
304
+ } else {
305
+ const syncDepartmentRecord = await this.syncRecordRepo.findOne({
306
+ filter: {
307
+ sourceName,
308
+ dataType: "department",
309
+ sourceUk: parentUid,
310
+ "resources.resource": this.name
311
+ },
312
+ appends: ["resources"]
313
+ });
314
+ if (syncDepartmentRecord && ((_a = syncDepartmentRecord.resources) == null ? void 0 : _a.length)) {
315
+ const parentDepartment = await this.deptRepo.findOne({
316
+ filterByTk: syncDepartmentRecord.resources[0].resourcePk
317
+ });
318
+ if (!parentDepartment) {
319
+ await department.setParent(null);
320
+ return;
321
+ }
322
+ const parent = await department.getParent();
323
+ if (parent) {
324
+ if (parentDepartment.id !== parent.id) {
325
+ await department.setParent(parentDepartment);
326
+ }
327
+ } else {
328
+ await department.setParent(parentDepartment);
329
+ }
330
+ } else {
331
+ await department.setParent(null);
332
+ }
333
+ }
334
+ }
335
+ }
336
+ // Annotate the CommonJS export names for ESM import in node:
337
+ 0 && (module.exports = {
338
+ DepartmentDataSyncResource
339
+ });
@@ -0,0 +1,17 @@
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
+ * This file is part of the NocoBase (R) project.
11
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
12
+ * Authors: NocoBase Team.
13
+ *
14
+ * This program is offered under a commercial license.
15
+ * For more information, see <https://www.nocobase.com/agreement>
16
+ */
17
+ export { default } from './plugin';