@kne/fastify-account 1.0.0-alpha.6 → 1.0.0-alpha.8

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/README.md CHANGED
@@ -22,7 +22,7 @@ npm i --save @kne/fastify-account
22
22
  ### API
23
23
 
24
24
  ---
25
- title: "@kne/fastify-account v1.0.0-alpha.4"
25
+ title: "@kne/fastify-account v1.0.0-alpha.7"
26
26
  language_tabs:
27
27
  - shell: Shell
28
28
  - http: HTTP
@@ -42,7 +42,7 @@ headingLevel: 2
42
42
 
43
43
  <!-- Generator: Widdershins v4.0.1 -->
44
44
 
45
- <h1 id="-kne-fastify-account">@kne/fastify-account v1.0.0-alpha.4</h1>
45
+ <h1 id="-kne-fastify-account">@kne/fastify-account v1.0.0-alpha.7</h1>
46
46
 
47
47
  > Scroll down for code samples, example requests and responses. Select a language for code samples from the tabs above or the mobile navigation menu.
48
48
 
@@ -34,7 +34,7 @@ module.exports = fp(async (fastify, options) => {
34
34
  },
35
35
  async request => {
36
36
  const userInfo = request.body;
37
- await services.admin.addUser(Object.assign({}, userInfo, { password: options.defaultPassword }));
37
+ await services.admin.addUser(Object.assign({}, userInfo, { password: services.account.md5(options.defaultPassword) }));
38
38
  return {};
39
39
  }
40
40
  );
@@ -1,6 +1,7 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const dayjs = require('dayjs');
3
3
  const bcrypt = require('bcryptjs');
4
+ const crypto = require('crypto');
4
5
 
5
6
  function generateRandom6DigitNumber() {
6
7
  const randomNumber = Math.random() * 1000000;
@@ -76,6 +77,12 @@ module.exports = fp(async (fastify, options) => {
76
77
  };
77
78
  };
78
79
 
80
+ const md5 = value => {
81
+ const hash = crypto.createHash('md5');
82
+ hash.update(value);
83
+ return hash.digest('hex');
84
+ };
85
+
79
86
  const resetPassword = async ({ userId, password }) => {
80
87
  const userInfo = await models.user.findOne({
81
88
  where: { uuid: userId }
@@ -198,6 +205,7 @@ module.exports = fp(async (fastify, options) => {
198
205
  };
199
206
 
200
207
  services.account = {
208
+ md5,
201
209
  login,
202
210
  register,
203
211
  sendEmailCode,
@@ -74,11 +74,24 @@ module.exports = fp(async (fastify, options) => {
74
74
  return data.map(item => item.get({ plain: true }));
75
75
  };
76
76
 
77
+ const getTenantOrgRoot = async ({ tenantId }) => {
78
+ const data = await models.tenantOrg.findOne({
79
+ where: { tenantId, pid: 0 }
80
+ });
81
+
82
+ if (!data) {
83
+ throw new Error('该租户不存在根节点');
84
+ }
85
+
86
+ return data.get({ plain: true });
87
+ };
88
+
77
89
  services.tenantOrg = {
78
90
  getTenantOrgInstance,
79
91
  addTenantOrg,
80
92
  saveTenantOrg,
81
93
  deleteTenantOrg,
82
- getTenantOrgList
94
+ getTenantOrgList,
95
+ getTenantOrgRoot
83
96
  };
84
97
  });
@@ -257,7 +257,7 @@ module.exports = fp(async (fastify, options) => {
257
257
  }
258
258
  };
259
259
 
260
- const addTenantUser = async ({ tenantId, roleIds, orgIds, userId, ...tenantUser }) => {
260
+ const addTenantUser = async ({ tenantId, roleIds = [], orgIds = [], userId, ...tenantUser }) => {
261
261
  const tenant = await services.tenant.getTenant({ id: tenantId });
262
262
 
263
263
  const currentAccountNumber = await models.tenantUser.count({
@@ -466,6 +466,72 @@ module.exports = fp(async (fastify, options) => {
466
466
  };
467
467
  };
468
468
 
469
+ const includeTenantUserBatch = async ({ tenantId, list }) => {
470
+ await services.tenant.getTenant({ id: tenantId });
471
+ const errors = [],
472
+ successes = [];
473
+ for (let current of list) {
474
+ if (!(current.phone || current.email)) {
475
+ errors.push({ item: current, msg: '电话和邮箱不能同时为空' });
476
+ continue;
477
+ }
478
+ const currentQuery = [];
479
+ if (current.phone) {
480
+ currentQuery.push({ phone: current.phone });
481
+ }
482
+ if (current.email) {
483
+ currentQuery.push({ email: current.email });
484
+ }
485
+
486
+ if (
487
+ (await models.tenantUser.count({
488
+ where: {
489
+ [Op.or]: currentQuery
490
+ }
491
+ })) > 0
492
+ ) {
493
+ errors.push({ item: current, msg: '租户用户已经存在,或手机邮箱和已有租户用户重复' });
494
+ continue;
495
+ }
496
+
497
+ if (await services.user.accountIsExists(current, {})) {
498
+ errors.push({ item: current, msg: '用户已经存在,已发送加入租户邀请等待对方同意' });
499
+ continue;
500
+ }
501
+
502
+ try {
503
+ const user = await services.user.addUser({
504
+ nickname: current.name,
505
+ phone: current.phone,
506
+ email: current.email,
507
+ password: services.account.md5(current.password || options.defaultPassword),
508
+ status: 1
509
+ });
510
+ const rootOrg = await services.tenantOrg.getTenantOrgRoot({ tenantId });
511
+ await services.tenantUser.addTenantUser(
512
+ Object.assign(
513
+ {},
514
+ {
515
+ orgIds: [rootOrg.id],
516
+ roleIds: []
517
+ },
518
+ {
519
+ tenantId,
520
+ userId: user.id,
521
+ ...current
522
+ }
523
+ )
524
+ );
525
+ successes.push({ item: current });
526
+ } catch (e) {
527
+ errors.push({ item: current, msg: e.message });
528
+ throw e;
529
+ }
530
+ }
531
+
532
+ return { errors, successes };
533
+ };
534
+
469
535
  services.tenantUser = {
470
536
  getUserTenant,
471
537
  getTenantUserPermissionList,
@@ -477,6 +543,7 @@ module.exports = fp(async (fastify, options) => {
477
543
  deleteTenantUser,
478
544
  closeTenantUser,
479
545
  openTenantUser,
480
- getTenantUserList
546
+ getTenantUserList,
547
+ includeTenantUserBatch
481
548
  };
482
549
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.6",
3
+ "version": "1.0.0-alpha.8",
4
4
  "description": "fastify的用户管理账号等实现",
5
5
  "main": "index.js",
6
6
  "scripts": {