@kne/fastify-account 1.0.0-alpha.2 → 1.0.0-alpha.4

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 (42) hide show
  1. package/README.md +47 -2
  2. package/index.js +10 -4
  3. package/libs/controllers/account.js +7 -6
  4. package/libs/controllers/admin.js +16 -15
  5. package/libs/controllers/adminPermission.js +42 -35
  6. package/libs/controllers/adminRole.js +13 -12
  7. package/libs/controllers/adminTenant.js +39 -36
  8. package/libs/controllers/tenant.js +7 -6
  9. package/libs/controllers/user.js +4 -3
  10. package/libs/models/admin-role.js +4 -8
  11. package/libs/models/application.js +16 -10
  12. package/libs/models/login-log.js +4 -8
  13. package/libs/models/permission.js +7 -9
  14. package/libs/models/tenant-application.js +8 -10
  15. package/libs/models/tenant-org.js +5 -9
  16. package/libs/models/tenant-permission.js +7 -9
  17. package/libs/models/tenant-role-application.js +13 -13
  18. package/libs/models/tenant-role-permission.js +9 -14
  19. package/libs/models/tenant-role.js +5 -9
  20. package/libs/models/tenant-share-group-permission.js +5 -9
  21. package/libs/models/tenant-share-group.js +5 -9
  22. package/libs/models/tenant-source-user-share-group.js +5 -9
  23. package/libs/models/tenant-token.js +7 -9
  24. package/libs/models/tenant-user-org.js +11 -10
  25. package/libs/models/tenant-user-role.js +11 -10
  26. package/libs/models/tenant-user-share-group.js +6 -10
  27. package/libs/models/tenant-user.js +35 -16
  28. package/libs/models/tenant.js +17 -9
  29. package/libs/models/user-account.js +17 -9
  30. package/libs/models/user.js +27 -17
  31. package/libs/models/verification-code.js +4 -8
  32. package/libs/services/account.js +26 -16
  33. package/libs/services/admin.js +14 -116
  34. package/libs/services/application.js +151 -0
  35. package/libs/services/permission.js +47 -145
  36. package/libs/services/tenant-invite.js +62 -0
  37. package/libs/services/tenant-org.js +84 -0
  38. package/libs/services/tenant-role.js +108 -0
  39. package/libs/services/tenant-user.js +486 -0
  40. package/libs/services/tenant.js +68 -669
  41. package/libs/services/user.js +63 -33
  42. package/package.json +3 -3
@@ -3,15 +3,37 @@ const { Unauthorized } = require('http-errors');
3
3
  const get = require('lodash/get');
4
4
  const pick = require('lodash/pick');
5
5
  module.exports = fp(async (fastify, options) => {
6
- const getUserInfo = async authenticatePayload => {
6
+ const { models, services } = fastify.account;
7
+
8
+ const getUserInstance = async ({ id }) => {
9
+ const user = await models.user.findOne({
10
+ where: {
11
+ uuid: id
12
+ }
13
+ });
14
+
15
+ if (!user) {
16
+ throw new Error('用户不存在');
17
+ }
18
+
19
+ return user;
20
+ };
21
+
22
+ const getUser = async authenticatePayload => {
7
23
  if (!(authenticatePayload && authenticatePayload.id)) {
8
24
  throw new Unauthorized();
9
25
  }
10
- const user = await fastify.account.models.user.findByPk(authenticatePayload.id);
26
+ const user = await models.user.findOne({
27
+ where: {
28
+ uuid: authenticatePayload.id
29
+ }
30
+ });
11
31
  if (!user) {
12
32
  throw new Unauthorized();
13
33
  }
14
- return pick(user, ['id', 'avatar', 'nickname', 'phone', 'email', 'gender', 'status', 'birthday', 'description', 'currentTenantId']);
34
+ return Object.assign({}, pick(user, ['avatar', 'nickname', 'phone', 'email', 'gender', 'status', 'birthday', 'description', 'currentTenantId']), {
35
+ id: user.uuid
36
+ });
15
37
  };
16
38
 
17
39
  const accountIsExists = async ({ email, phone }, currentUser) => {
@@ -24,7 +46,7 @@ module.exports = fp(async (fastify, options) => {
24
46
  }
25
47
 
26
48
  return (
27
- (await fastify.account.models.user.count({
49
+ (await models.user.count({
28
50
  where: {
29
51
  [fastify.sequelize.Sequelize.Op.or]: query
30
52
  }
@@ -39,8 +61,8 @@ module.exports = fp(async (fastify, options) => {
39
61
  if (!password) {
40
62
  throw new Error('密码不能为空');
41
63
  }
42
- const account = await fastify.account.models.userAccount.create(await fastify.account.services.account.passwordEncryption(password));
43
- const user = await fastify.account.models.user.create({
64
+ const account = await models.userAccount.create(await services.account.passwordEncryption(password));
65
+ const user = await models.user.create({
44
66
  avatar,
45
67
  nickname,
46
68
  gender,
@@ -49,18 +71,15 @@ module.exports = fp(async (fastify, options) => {
49
71
  phone,
50
72
  email,
51
73
  status,
52
- userAccountId: account.id
74
+ userAccountId: account.uuid
53
75
  });
54
- await account.update({ belongToUserId: user.id });
55
- return user;
76
+ await account.update({ belongToUserId: user.uuid });
77
+
78
+ return Object.assign({}, user.get({ pain: true }), { id: user.uuid });
56
79
  };
57
80
 
58
81
  const saveUser = async ({ id, ...otherInfo }) => {
59
- const user = await fastify.account.models.user.findByPk(id);
60
-
61
- if (!user) {
62
- throw new Error('用户不存在');
63
- }
82
+ const user = await getUserInstance({ id });
64
83
 
65
84
  if ((await accountIsExists({ phone: otherInfo.phone, email: otherInfo.email }, user)) > 0) {
66
85
  throw new Error('手机号或者邮箱都不能重复');
@@ -76,42 +95,53 @@ module.exports = fp(async (fastify, options) => {
76
95
  };
77
96
 
78
97
  const closeUser = async ({ id }) => {
79
- const user = await fastify.account.models.user.findByPk(id);
80
-
81
- if (!user) {
82
- throw new Error('用户不存在');
83
- }
98
+ const user = await getUserInstance({ id });
84
99
  user.status = 12;
85
100
  await user.save();
86
101
  };
87
102
 
88
103
  const openUser = async ({ id }) => {
89
- const user = await fastify.account.models.user.findByPk(id);
90
-
91
- if (!user) {
92
- throw new Error('用户不存在');
93
- }
104
+ const user = await getUserInstance({ id });
94
105
  user.status = 0;
95
106
  await user.save();
96
107
  };
97
108
 
98
109
  const setCurrentTenantId = async ({ id, tenantId }) => {
99
- await fastify.account.services.tenant.getTenantInfo({ id: tenantId });
100
- const user = await fastify.account.models.user.findByPk(id);
101
-
102
- if (!user) {
103
- throw new Error('用户不存在');
104
- }
110
+ await services.tenant.getTenant({ id: tenantId });
111
+ const user = await getUserInstance({ id });
105
112
  user.currentTenantId = tenantId;
106
113
  await user.save();
107
114
  };
108
- fastify.account.services.user = {
109
- getUserInfo,
115
+
116
+ const getAllUserList = async ({ filter, perPage, currentPage }) => {
117
+ const { count, rows } = await models.user.findAndCountAll({
118
+ include: [
119
+ {
120
+ attributes: ['role'],
121
+ model: models.adminRole
122
+ },
123
+ {
124
+ model: models.tenant
125
+ }
126
+ ]
127
+ });
128
+ return {
129
+ pageData: rows.map(item => {
130
+ return Object.assign({}, item.get({ pain: true }), { id: item.uuid });
131
+ }),
132
+ totalCount: count
133
+ };
134
+ };
135
+
136
+ services.user = {
137
+ getUser,
138
+ getUserInstance,
110
139
  saveUser,
111
140
  accountIsExists,
112
141
  addUser,
113
142
  closeUser,
114
143
  openUser,
115
- setCurrentTenantId
144
+ setCurrentTenantId,
145
+ getAllUserList
116
146
  };
117
147
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "fastify的用户管理账号等实现",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -38,9 +38,9 @@
38
38
  "homepage": "https://github.com/kne-union/fastify-account#readme",
39
39
  "devDependencies": {
40
40
  "@fastify/swagger": "^8.14.0",
41
- "@kne/fastify-file-manager": "^1.0.0",
41
+ "@kne/fastify-file-manager": "^1.1.2",
42
42
  "@kne/fastify-response-data-format": "^0.1.0",
43
- "@kne/fastify-sequelize": "^1.0.0",
43
+ "@kne/fastify-sequelize": "^2.0.2",
44
44
  "fastify": "^4.27.0",
45
45
  "husky": "^9.0.11",
46
46
  "nodemon": "^3.1.1",