@kne/fastify-account 1.0.0-alpha.5 → 1.0.0-alpha.7

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.6"
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.6</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,
@@ -46,16 +46,12 @@ module.exports = fp(async (fastify, options) => {
46
46
  const output = [];
47
47
  const core = (list, node) => {
48
48
  const { children, other } = groupBy(list, item => (item.pid === node.id ? 'children' : 'other'));
49
- if (!(other && other.length > 0)) {
50
- return;
51
- }
52
49
  if (!(children && children.length > 0)) {
53
50
  node.id !== 0 && output.push(node);
54
51
  return;
55
52
  }
56
-
57
53
  children.forEach(node => {
58
- core(other, node);
54
+ core(other || [], node);
59
55
  });
60
56
  return output;
61
57
  };
@@ -261,7 +257,7 @@ module.exports = fp(async (fastify, options) => {
261
257
  }
262
258
  };
263
259
 
264
- const addTenantUser = async ({ tenantId, roleIds, orgIds, userId, ...tenantUser }) => {
260
+ const addTenantUser = async ({ tenantId, roleIds, orgIds, userId, ...tenantUser }, transaction) => {
265
261
  const tenant = await services.tenant.getTenant({ id: tenantId });
266
262
 
267
263
  const currentAccountNumber = await models.tenantUser.count({
@@ -274,7 +270,7 @@ module.exports = fp(async (fastify, options) => {
274
270
 
275
271
  await checkTenantUserInfoValidate({ tenantId, roleIds, orgIds, userId });
276
272
 
277
- const t = await fastify.sequelize.instance.transaction();
273
+ const t = transaction || (await fastify.sequelize.instance.transaction());
278
274
 
279
275
  if (
280
276
  (await models.tenantUser.count({
@@ -323,9 +319,9 @@ module.exports = fp(async (fastify, options) => {
323
319
  { transaction: t }
324
320
  );
325
321
 
326
- await t.commit();
322
+ !transaction && (await t.commit());
327
323
  } catch (e) {
328
- await t.rollback();
324
+ !transaction && (await t.rollback());
329
325
  throw e;
330
326
  }
331
327
  };
@@ -470,6 +466,69 @@ module.exports = fp(async (fastify, options) => {
470
466
  };
471
467
  };
472
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
+ const t = await fastify.sequelize.instance.transaction();
497
+ try {
498
+ if (await services.user.accountIsExists(current, {})) {
499
+ errors.push({ item: current, msg: '用户已经存在,已发送加入租户邀请等待对方同意' });
500
+ continue;
501
+ }
502
+
503
+ const user = await services.user.addUser(
504
+ {
505
+ nickname: current.name,
506
+ phone: current.phone,
507
+ email: current.email,
508
+ password: services.account.md5(current.password || options.defaultPassword),
509
+ status: 1
510
+ },
511
+ { transaction: t }
512
+ );
513
+ await services.tenantUser.addTenantUser(
514
+ {
515
+ tenantId,
516
+ userId: user.id,
517
+ ...current
518
+ },
519
+ { transaction: t }
520
+ );
521
+ successes.push({ item: current });
522
+ await t.commit();
523
+ } catch (e) {
524
+ await t.rollback();
525
+ errors.push({ item: current, msg: e.message });
526
+ }
527
+ }
528
+
529
+ return { errors, successes };
530
+ };
531
+
473
532
  services.tenantUser = {
474
533
  getUserTenant,
475
534
  getTenantUserPermissionList,
@@ -481,6 +540,7 @@ module.exports = fp(async (fastify, options) => {
481
540
  deleteTenantUser,
482
541
  closeTenantUser,
483
542
  openTenantUser,
484
- getTenantUserList
543
+ getTenantUserList,
544
+ includeTenantUserBatch
485
545
  };
486
546
  });
@@ -54,7 +54,7 @@ module.exports = fp(async (fastify, options) => {
54
54
  );
55
55
  };
56
56
 
57
- const addUser = async ({ avatar, nickname, gender, birthday, description, phone, email, password, status }) => {
57
+ const addUser = async ({ avatar, nickname, gender, birthday, description, phone, email, password, status }, transaction) => {
58
58
  if ((await accountIsExists({ phone, email })) > 0) {
59
59
  throw new Error('手机号或者邮箱都不能重复');
60
60
  }
@@ -62,18 +62,21 @@ module.exports = fp(async (fastify, options) => {
62
62
  throw new Error('密码不能为空');
63
63
  }
64
64
  const account = await models.userAccount.create(await services.account.passwordEncryption(password));
65
- const user = await models.user.create({
66
- avatar,
67
- nickname,
68
- gender,
69
- birthday,
70
- description,
71
- phone,
72
- email,
73
- status,
74
- userAccountId: account.uuid
75
- });
76
- await account.update({ belongToUserId: user.uuid });
65
+ const user = await models.user.create(
66
+ {
67
+ avatar,
68
+ nickname,
69
+ gender,
70
+ birthday,
71
+ description,
72
+ phone,
73
+ email,
74
+ status,
75
+ userAccountId: account.uuid
76
+ },
77
+ { transaction }
78
+ );
79
+ await account.update({ belongToUserId: user.uuid }, { transaction });
77
80
 
78
81
  return Object.assign({}, user.get({ pain: true }), { id: user.uuid });
79
82
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.5",
3
+ "version": "1.0.0-alpha.7",
4
4
  "description": "fastify的用户管理账号等实现",
5
5
  "main": "index.js",
6
6
  "scripts": {