@chevre/domain 21.2.0-alpha.145 → 21.2.0-alpha.146

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.
@@ -0,0 +1,54 @@
1
+ // tslint:disable:no-console
2
+ import * as moment from 'moment';
3
+ import * as mongoose from 'mongoose';
4
+
5
+ import { chevre } from '../../../lib/index';
6
+
7
+ const PROJECT_ID = String(process.env.PROJECT_ID);
8
+ const memberId = 'xxx';
9
+
10
+ async function main() {
11
+ await mongoose.connect(<string>process.env.MONGOLAB_URI);
12
+
13
+ const memberRepo = new chevre.repository.Member(mongoose.connection);
14
+ const roleRepo = new chevre.repository.Role(mongoose.connection);
15
+
16
+ let now: Date;
17
+
18
+ // let now = new Date();
19
+ // const aggregateRoleNamesResult = await memberRepo.aggregateRoleNames({
20
+ // project: { id: { $eq: PROJECT_ID } },
21
+ // member: { id: { $eq: memberId } }
22
+ // });
23
+
24
+ // const aggregatePermissionsResult = await roleRepo.aggregatePermissions({
25
+ // roleName: {
26
+ // $in: aggregateRoleNamesResult.map((r) => r.roleName)
27
+ // }
28
+ // });
29
+ // console.log('time cost:', moment()
30
+ // .diff(now));
31
+
32
+ now = new Date();
33
+ const searchPermissionsResult = await chevre.service.iam.searchPermissions({
34
+ project: { id: PROJECT_ID },
35
+ member: { id: memberId }
36
+ })({
37
+ member: memberRepo,
38
+ role: roleRepo
39
+ });
40
+ console.log('time cost:', moment()
41
+ .diff(now));
42
+
43
+ console.log(searchPermissionsResult);
44
+ console.log(searchPermissionsResult.permissions.length);
45
+
46
+ // console.log(aggregateRoleNamesResult);
47
+ // console.log(aggregateRoleNamesResult.length);
48
+ // console.log(aggregatePermissionsResult);
49
+ // console.log(aggregatePermissionsResult.length);
50
+ }
51
+
52
+ main()
53
+ .then(console.log)
54
+ .catch(console.error);
@@ -9,6 +9,20 @@ export declare class MongoRepository {
9
9
  static CREATE_MONGO_CONDITIONS(params: factory.iam.ISearchConditions): any[];
10
10
  count(params: factory.iam.ISearchConditions): Promise<number>;
11
11
  search(params: factory.iam.ISearchConditions): Promise<factory.iam.IMember[]>;
12
+ aggregateRoleNames(params: {
13
+ project: {
14
+ id: {
15
+ $eq: string;
16
+ };
17
+ };
18
+ member: {
19
+ id: {
20
+ $eq: string;
21
+ };
22
+ };
23
+ }): Promise<{
24
+ roleName: string;
25
+ }[]>;
12
26
  findById(params: {
13
27
  id: string;
14
28
  }): Promise<factory.iam.IMember>;
@@ -132,6 +132,33 @@ class MongoRepository {
132
132
  .then((docs) => docs.map((doc) => doc.toObject()));
133
133
  });
134
134
  }
135
+ aggregateRoleNames(params) {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ const matchStages = [
138
+ { $match: { 'project.id': { $eq: params.project.id.$eq } } },
139
+ { $match: { 'member.id': { $eq: params.member.id.$eq } } }
140
+ ];
141
+ const aggregate = this.memberModel.aggregate([
142
+ // ...(typeof params.sort?.productID === 'number')
143
+ // ? [{ $sort: { productID: params.sort.productID } }]
144
+ // : [],
145
+ ...matchStages,
146
+ {
147
+ $unwind: {
148
+ path: '$member.hasRole'
149
+ }
150
+ },
151
+ {
152
+ $project: {
153
+ _id: 0,
154
+ roleName: '$member.hasRole.roleName'
155
+ }
156
+ }
157
+ ]);
158
+ return aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
159
+ .exec();
160
+ });
161
+ }
135
162
  findById(params) {
136
163
  return __awaiter(this, void 0, void 0, function* () {
137
164
  const doc = yield this.memberModel.findOne({
@@ -671,7 +671,7 @@ class MongoRepository {
671
671
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
672
672
  return __awaiter(this, void 0, void 0, function* () {
673
673
  if (useScreeningRoomType === true) {
674
- const matchStages = [{ $match: { typeOf: { $eq: factory.placeType.ScreeningRoomSection } } }];
674
+ const matchStages = [{ $match: { typeOf: { $eq: factory.placeType.ScreeningRoom } } }];
675
675
  const projectIdEq = (_b = (_a = searchConditions.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
676
676
  if (typeof projectIdEq === 'string') {
677
677
  matchStages.push({
@@ -17,6 +17,13 @@ export declare class MongoRepository {
17
17
  static CREATE_MONGO_CONDITIONS(params: factory.iam.IRoleSearchConditions): any[];
18
18
  count(params: factory.iam.IRoleSearchConditions): Promise<number>;
19
19
  search(params: factory.iam.IRoleSearchConditions): Promise<IRole[]>;
20
+ aggregatePermissions(params: {
21
+ roleName: {
22
+ $in: string[];
23
+ };
24
+ }): Promise<{
25
+ _id: string;
26
+ }[]>;
20
27
  findById(params: {
21
28
  id: string;
22
29
  }): Promise<IRole>;
@@ -97,6 +97,42 @@ class MongoRepository {
97
97
  .then((docs) => docs.map((doc) => doc.toObject()));
98
98
  });
99
99
  }
100
+ aggregatePermissions(params) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ const matchStages = [
103
+ { $match: { roleName: { $in: params.roleName.$in } } }
104
+ ];
105
+ const aggregate = this.roleModel.aggregate([
106
+ // ...(typeof params.sort?.productID === 'number')
107
+ // ? [{ $sort: { productID: params.sort.productID } }]
108
+ // : [],
109
+ ...matchStages,
110
+ {
111
+ $unwind: {
112
+ path: '$permissions'
113
+ }
114
+ },
115
+ {
116
+ $project: {
117
+ _id: 0,
118
+ permission: '$permissions'
119
+ }
120
+ },
121
+ {
122
+ $group: {
123
+ _id: '$permission'
124
+ }
125
+ }
126
+ ]);
127
+ // if (typeof params.limit === 'number' && params.limit > 0) {
128
+ // const page: number = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
129
+ // aggregate.limit(params.limit * page)
130
+ // .skip(params.limit * (page - 1));
131
+ // }
132
+ return aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
133
+ .exec();
134
+ });
135
+ }
100
136
  findById(params) {
101
137
  return __awaiter(this, void 0, void 0, function* () {
102
138
  const doc = yield this.roleModel.findOne({
@@ -18,6 +18,10 @@ export declare function searchPermissions(params: {
18
18
  member: MemberRepo;
19
19
  role: RoleRepo;
20
20
  }) => Promise<{
21
- roleNames: string[];
22
- permissions: IPermission[];
21
+ hasRole: {
22
+ roleName: string;
23
+ }[];
24
+ permissions: {
25
+ _id: string;
26
+ }[];
23
27
  }>;
@@ -16,18 +16,32 @@ exports.searchPermissions = void 0;
16
16
  function searchPermissions(params) {
17
17
  return (repos) => __awaiter(this, void 0, void 0, function* () {
18
18
  let permissions = [];
19
- // プロジェクトメンバーを検索
20
- const projectMembers = yield repos.member.search({
19
+ const hasRole = yield repos.member.aggregateRoleNames({
21
20
  project: { id: { $eq: params.project.id } },
22
21
  member: { id: { $eq: params.member.id } }
23
22
  });
24
- // 持っているロールを検索
25
- const roleNames = projectMembers.reduce((a, b) => [...a, ...(Array.isArray(b.member.hasRole)) ? b.member.hasRole.map((r) => r.roleName) : []], []);
26
- const roles = yield repos.role.search({ roleName: { $in: roleNames } });
27
- // 権限をまとめる
28
- permissions = roles.reduce((a, b) => [...a, ...b.permissions], []);
29
- permissions = [...new Set(permissions)];
30
- return { roleNames, permissions };
23
+ if (hasRole.length > 0) {
24
+ permissions = yield repos.role.aggregatePermissions({ roleName: { $in: hasRole.map((r) => r.roleName) } });
25
+ }
26
+ return { hasRole, permissions };
27
+ // let permissions: IPermission[] = [];
28
+ // const projectMembers = await repos.member.search({
29
+ // project: { id: { $eq: params.project.id } },
30
+ // member: { id: { $eq: params.member.id } }
31
+ // });
32
+ // // 持っているロールを検索
33
+ // const roleNames = projectMembers.reduce<string[]>(
34
+ // (a, b) => [...a, ...(Array.isArray(b.member.hasRole)) ? b.member.hasRole.map((r) => r.roleName) : []],
35
+ // []
36
+ // );
37
+ // const roles = await repos.role.search({ roleName: { $in: roleNames } });
38
+ // // 権限をまとめる
39
+ // permissions = roles.reduce<string[]>(
40
+ // (a, b) => [...a, ...b.permissions],
41
+ // []
42
+ // );
43
+ // permissions = [...new Set(permissions)];
44
+ // return { roleNames, permissions };
31
45
  });
32
46
  }
33
47
  exports.searchPermissions = searchPermissions;
package/package.json CHANGED
@@ -117,5 +117,5 @@
117
117
  "postversion": "git push origin --tags",
118
118
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
119
119
  },
120
- "version": "21.2.0-alpha.145"
120
+ "version": "21.2.0-alpha.146"
121
121
  }