@kne/fastify-account 1.0.0-alpha.7 → 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.6"
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.6</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
 
@@ -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 }, transaction) => {
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({
@@ -270,7 +270,7 @@ module.exports = fp(async (fastify, options) => {
270
270
 
271
271
  await checkTenantUserInfoValidate({ tenantId, roleIds, orgIds, userId });
272
272
 
273
- const t = transaction || (await fastify.sequelize.instance.transaction());
273
+ const t = await fastify.sequelize.instance.transaction();
274
274
 
275
275
  if (
276
276
  (await models.tenantUser.count({
@@ -319,9 +319,9 @@ module.exports = fp(async (fastify, options) => {
319
319
  { transaction: t }
320
320
  );
321
321
 
322
- !transaction && (await t.commit());
322
+ await t.commit();
323
323
  } catch (e) {
324
- !transaction && (await t.rollback());
324
+ await t.rollback();
325
325
  throw e;
326
326
  }
327
327
  };
@@ -493,36 +493,39 @@ module.exports = fp(async (fastify, options) => {
493
493
  errors.push({ item: current, msg: '租户用户已经存在,或手机邮箱和已有租户用户重复' });
494
494
  continue;
495
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
496
 
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
- );
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 });
513
511
  await services.tenantUser.addTenantUser(
514
- {
515
- tenantId,
516
- userId: user.id,
517
- ...current
518
- },
519
- { transaction: t }
512
+ Object.assign(
513
+ {},
514
+ {
515
+ orgIds: [rootOrg.id],
516
+ roleIds: []
517
+ },
518
+ {
519
+ tenantId,
520
+ userId: user.id,
521
+ ...current
522
+ }
523
+ )
520
524
  );
521
525
  successes.push({ item: current });
522
- await t.commit();
523
526
  } catch (e) {
524
- await t.rollback();
525
527
  errors.push({ item: current, msg: e.message });
528
+ throw e;
526
529
  }
527
530
  }
528
531
 
@@ -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 }, transaction) => {
57
+ const addUser = async ({ avatar, nickname, gender, birthday, description, phone, email, password, status }) => {
58
58
  if ((await accountIsExists({ phone, email })) > 0) {
59
59
  throw new Error('手机号或者邮箱都不能重复');
60
60
  }
@@ -62,21 +62,18 @@ 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
- {
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 });
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 });
80
77
 
81
78
  return Object.assign({}, user.get({ pain: true }), { id: user.uuid });
82
79
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.8",
4
4
  "description": "fastify的用户管理账号等实现",
5
5
  "main": "index.js",
6
6
  "scripts": {