@kne/fastify-account 1.0.0-alpha.1 → 1.0.0-alpha.3

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 +101 -5
  2. package/index.js +10 -4
  3. package/libs/controllers/account.js +8 -7
  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 +16 -4
  9. package/libs/controllers/user.js +23 -1
  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 +14 -10
  18. package/libs/models/tenant-role-permission.js +10 -9
  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 -512
  41. package/libs/services/user.js +69 -30
  42. package/package.json +3 -3
@@ -1,19 +1,39 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const { Unauthorized } = require('http-errors');
3
3
  const get = require('lodash/get');
4
+ const pick = require('lodash/pick');
4
5
  module.exports = fp(async (fastify, options) => {
5
- 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 => {
6
23
  if (!(authenticatePayload && authenticatePayload.id)) {
7
24
  throw new Unauthorized();
8
25
  }
9
- 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
+ });
10
31
  if (!user) {
11
32
  throw new Unauthorized();
12
33
  }
13
- const userInfo = user.get({ plain: true });
14
- delete userInfo['userAccountId'];
15
-
16
- return userInfo;
34
+ return Object.assign({}, pick(user, ['avatar', 'nickname', 'phone', 'email', 'gender', 'status', 'birthday', 'description', 'currentTenantId']), {
35
+ id: user.uuid
36
+ });
17
37
  };
18
38
 
19
39
  const accountIsExists = async ({ email, phone }, currentUser) => {
@@ -26,7 +46,7 @@ module.exports = fp(async (fastify, options) => {
26
46
  }
27
47
 
28
48
  return (
29
- (await fastify.account.models.user.count({
49
+ (await models.user.count({
30
50
  where: {
31
51
  [fastify.sequelize.Sequelize.Op.or]: query
32
52
  }
@@ -41,8 +61,8 @@ module.exports = fp(async (fastify, options) => {
41
61
  if (!password) {
42
62
  throw new Error('密码不能为空');
43
63
  }
44
- const account = await fastify.account.models.userAccount.create(await fastify.account.services.account.passwordEncryption(password));
45
- 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({
46
66
  avatar,
47
67
  nickname,
48
68
  gender,
@@ -51,18 +71,15 @@ module.exports = fp(async (fastify, options) => {
51
71
  phone,
52
72
  email,
53
73
  status,
54
- userAccountId: account.id
74
+ userAccountId: account.uuid
55
75
  });
56
- await account.update({ belongToUserId: user.id });
57
- return user;
76
+ await account.update({ belongToUserId: user.uuid });
77
+
78
+ return Object.assign({}, user.get({ pain: true }), { id: user.uuid });
58
79
  };
59
80
 
60
81
  const saveUser = async ({ id, ...otherInfo }) => {
61
- const user = await fastify.account.models.user.findByPk(id);
62
-
63
- if (!user) {
64
- throw new Error('用户不存在');
65
- }
82
+ const user = await getUserInstance({ id });
66
83
 
67
84
  if ((await accountIsExists({ phone: otherInfo.phone, email: otherInfo.email }, user)) > 0) {
68
85
  throw new Error('手机号或者邮箱都不能重复');
@@ -78,31 +95,53 @@ module.exports = fp(async (fastify, options) => {
78
95
  };
79
96
 
80
97
  const closeUser = async ({ id }) => {
81
- const user = await fastify.account.models.user.findByPk(id);
82
-
83
- if (!user) {
84
- throw new Error('用户不存在');
85
- }
98
+ const user = await getUserInstance({ id });
86
99
  user.status = 12;
87
100
  await user.save();
88
101
  };
89
102
 
90
103
  const openUser = async ({ id }) => {
91
- const user = await fastify.account.models.user.findByPk(id);
92
-
93
- if (!user) {
94
- throw new Error('用户不存在');
95
- }
104
+ const user = await getUserInstance({ id });
96
105
  user.status = 0;
97
106
  await user.save();
98
107
  };
99
108
 
100
- fastify.account.services.user = {
101
- getUserInfo,
109
+ const setCurrentTenantId = async ({ id, tenantId }) => {
110
+ await services.tenant.getTenant({ id: tenantId });
111
+ const user = await getUserInstance({ id });
112
+ user.currentTenantId = tenantId;
113
+ await user.save();
114
+ };
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,
102
139
  saveUser,
103
140
  accountIsExists,
104
141
  addUser,
105
142
  closeUser,
106
- openUser
143
+ openUser,
144
+ setCurrentTenantId,
145
+ getAllUserList
107
146
  };
108
147
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.3",
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",