@chevre/domain 22.3.0 → 22.4.0-alpha.0

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,23 @@
1
+ // tslint:disable:no-console
2
+ import * as mongoose from 'mongoose';
3
+
4
+ import { chevre } from '../../../lib/index';
5
+
6
+ const project = { id: String(process.env.PROJECT_ID) };
7
+
8
+ async function main() {
9
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
10
+
11
+ const offerCatalogRepo = await chevre.repository.OfferCatalog.createInstance(mongoose.connection);
12
+
13
+ const result = await offerCatalogRepo.findItemListElementById({
14
+ id: 'blpc770py',
15
+ project: { id: project.id }
16
+ });
17
+ console.log(result);
18
+ console.log(result.itemListElement.length);
19
+ }
20
+
21
+ main()
22
+ .then(console.log)
23
+ .catch(console.error);
@@ -8,15 +8,15 @@ const project = { id: String(process.env.PROJECT_ID) };
8
8
  async function main() {
9
9
  await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
10
10
 
11
- const additionalPropertyRepo = await chevre.repository.AdditionalProperty.createInstance(mongoose.connection);
11
+ const repo = await chevre.repository.Member.createInstance(mongoose.connection);
12
12
 
13
- const docs = await additionalPropertyRepo.projectFields(
13
+ const docs = await repo.projectFields(
14
14
  {
15
15
  limit: 1,
16
16
  page: 1,
17
17
  project: { id: { $eq: project.id } }
18
18
  },
19
- ['id', 'codeValue', 'name']
19
+ ['member']
20
20
  );
21
21
  // tslint:disable-next-line:no-null-keyword
22
22
  console.dir(docs, { depth: null });
@@ -2,7 +2,7 @@ import type { BulkWriteResult } from 'mongodb';
2
2
  import { Connection, FilterQuery } from 'mongoose';
3
3
  import { ICustomerType } from './mongoose/schemas/customerType';
4
4
  import * as factory from '../factory';
5
- type IKeyOfProjection = keyof ICustomerType | '_id';
5
+ type IKeyOfProjection = keyof ICustomerType;
6
6
  /**
7
7
  * カスタマータイプリポジトリ
8
8
  */
@@ -13,7 +13,7 @@ export declare class CustomerTypeRepo {
13
13
  /**
14
14
  * 検索
15
15
  */
16
- search(params: factory.categoryCode.ISearchConditions, inclusion: IKeyOfProjection[], exclusion: IKeyOfProjection[]): Promise<ICustomerType[]>;
16
+ projectFields(params: factory.categoryCode.ISearchConditions, inclusion: IKeyOfProjection[]): Promise<ICustomerType[]>;
17
17
  saveManyByCodeValue(params: {
18
18
  attributes: ICustomerType;
19
19
  upsert?: boolean;
@@ -12,6 +12,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CustomerTypeRepo = void 0;
13
13
  const customerType_1 = require("./mongoose/schemas/customerType");
14
14
  const settings_1 = require("../settings");
15
+ const AVAILABLE_PROJECT_FIELDS = [
16
+ 'typeOf',
17
+ 'codeValue',
18
+ 'name'
19
+ ];
15
20
  /**
16
21
  * カスタマータイプリポジトリ
17
22
  */
@@ -25,52 +30,40 @@ class CustomerTypeRepo {
25
30
  const andConditions = [];
26
31
  const codeValueEq = (_a = params.codeValue) === null || _a === void 0 ? void 0 : _a.$eq;
27
32
  if (typeof codeValueEq === 'string') {
28
- andConditions.push({ codeValue: { $exists: true, $eq: codeValueEq } });
33
+ andConditions.push({ codeValue: { $eq: codeValueEq } });
29
34
  }
30
35
  const codeValueIn = (_b = params.codeValue) === null || _b === void 0 ? void 0 : _b.$in;
31
36
  if (Array.isArray(codeValueIn)) {
32
- andConditions.push({ codeValue: { $exists: true, $in: codeValueIn } });
37
+ andConditions.push({ codeValue: { $in: codeValueIn } });
33
38
  }
34
39
  return andConditions;
35
40
  }
36
41
  /**
37
42
  * 検索
38
43
  */
39
- search(params, inclusion, exclusion) {
44
+ projectFields(params, inclusion) {
40
45
  return __awaiter(this, void 0, void 0, function* () {
41
46
  const conditions = CustomerTypeRepo.CREATE_MONGO_CONDITIONS(params);
42
- let projection = {};
47
+ let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
43
48
  if (Array.isArray(inclusion) && inclusion.length > 0) {
44
- inclusion.forEach((field) => {
45
- projection[field] = 1;
46
- });
49
+ positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key));
47
50
  }
48
51
  else {
49
- projection = {
50
- __v: 0,
51
- createdAt: 0,
52
- updatedAt: 0
53
- };
54
- if (Array.isArray(exclusion) && exclusion.length > 0) {
55
- exclusion.forEach((field) => {
56
- projection[field] = 0;
57
- });
58
- }
52
+ // no op
59
53
  }
54
+ const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1]))));
60
55
  const query = this.customerTypeModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection);
61
56
  if (typeof params.limit === 'number' && params.limit > 0) {
62
57
  const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
63
58
  query.limit(params.limit)
64
59
  .skip(params.limit * (page - 1));
65
60
  }
66
- // tslint:disable-next-line:no-single-line-block-comment
67
- /* istanbul ignore else */
68
61
  if (params.sort !== undefined) {
69
62
  query.sort(params.sort);
70
63
  }
71
64
  return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
72
- .exec()
73
- .then((docs) => docs.map((doc) => doc.toObject()));
65
+ .lean()
66
+ .exec();
74
67
  });
75
68
  }
76
69
  saveManyByCodeValue(params) {
@@ -24,6 +24,7 @@
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
25
  import type { Connection } from 'mongoose';
26
26
  import * as factory from '../factory';
27
+ type IKeyOfProjection = keyof factory.iam.IMember;
27
28
  /**
28
29
  * IAMメンバーリポジトリ
29
30
  */
@@ -31,7 +32,7 @@ export declare class MemberRepo {
31
32
  private readonly memberModel;
32
33
  constructor(connection: Connection);
33
34
  static CREATE_MONGO_CONDITIONS(params: factory.iam.ISearchConditions): any[];
34
- search(params: factory.iam.ISearchConditions): Promise<factory.iam.IMember[]>;
35
+ projectFields(params: factory.iam.ISearchConditions, inclusion: IKeyOfProjection[]): Promise<factory.iam.IMember[]>;
35
36
  aggregateRoleNames(params: {
36
37
  project: {
37
38
  id: {
@@ -62,7 +63,7 @@ export declare class MemberRepo {
62
63
  /**
63
64
  * メンバー作成
64
65
  */
65
- create(params: factory.iam.IMember[]): Promise<factory.iam.IMember[]>;
66
+ create(params: factory.iam.IMember[]): Promise<void>;
66
67
  /**
67
68
  * メンバー更新
68
69
  */
@@ -163,3 +164,4 @@ export declare class MemberRepo {
163
164
  $unset: any;
164
165
  }): Promise<import("mongoose").UpdateWriteOpResult>;
165
166
  }
167
+ export {};
@@ -11,8 +11,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MemberRepo = void 0;
13
13
  const factory = require("../factory");
14
- const member_1 = require("./mongoose/schemas/member");
15
14
  const settings_1 = require("../settings");
15
+ const member_1 = require("./mongoose/schemas/member");
16
+ const AVAILABLE_PROJECT_FIELDS = [
17
+ 'project',
18
+ 'typeOf',
19
+ 'member'
20
+ ];
16
21
  /**
17
22
  * IAMメンバーリポジトリ
18
23
  */
@@ -72,29 +77,31 @@ class MemberRepo {
72
77
  }
73
78
  return andConditions;
74
79
  }
75
- search(params) {
80
+ projectFields(params, inclusion) {
76
81
  return __awaiter(this, void 0, void 0, function* () {
77
82
  const conditions = MemberRepo.CREATE_MONGO_CONDITIONS(params);
78
- const query = this.memberModel.find((conditions.length > 0) ? { $and: conditions } : {}, {
79
- __v: 0,
80
- createdAt: 0,
81
- updatedAt: 0
82
- });
83
+ let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
84
+ if (Array.isArray(inclusion) && inclusion.length > 0) {
85
+ positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key));
86
+ }
87
+ else {
88
+ // no op
89
+ }
90
+ const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1]))));
91
+ const query = this.memberModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection);
83
92
  if (typeof params.limit === 'number' && params.limit > 0) {
84
93
  const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
85
94
  query.limit(params.limit)
86
95
  .skip(params.limit * (page - 1));
87
96
  }
88
- // tslint:disable-next-line:no-single-line-block-comment
89
- /* istanbul ignore else */
90
97
  if (params.sort !== undefined) {
91
98
  query.sort(params.sort);
92
99
  }
93
100
  // const explainResult = await (<any>query).explain();
94
101
  // console.log(explainResult[0].executionStats.allPlansExecution.map((e: any) => e.executionStages.inputStage));
95
102
  return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
96
- .exec()
97
- .then((docs) => docs.map((doc) => doc.toObject()));
103
+ .lean()
104
+ .exec();
98
105
  });
99
106
  }
100
107
  aggregateRoleNames(params) {
@@ -139,21 +146,21 @@ class MemberRepo {
139
146
  */
140
147
  create(params) {
141
148
  return __awaiter(this, void 0, void 0, function* () {
142
- let members = [];
149
+ // let members: factory.iam.IMember[] = [];
143
150
  if (params.length > 0) {
144
- const docs = yield this.memberModel.insertMany(params.map((p) => {
151
+ // const docs = await this.memberModel.insertMany(params.map((p) => {
152
+ yield this.memberModel.insertMany(params.map((p) => {
145
153
  return {
146
154
  project: p.project,
147
155
  typeOf: p.typeOf,
148
156
  member: p.member
149
157
  };
150
158
  }));
151
- members = docs.map((doc) => doc.toObject());
152
159
  }
153
160
  else {
154
161
  // no op
155
162
  }
156
- return members;
163
+ // return members;
157
164
  });
158
165
  }
159
166
  /**
@@ -35,16 +35,7 @@ const schemaOptions = {
35
35
  versionKey: false
36
36
  }
37
37
  };
38
- const indexes = [
39
- // [ // discontinue(2024-08-07~)
40
- // { createdAt: 1 },
41
- // { name: 'searchByCreatedAt' }
42
- // ],
43
- // [
44
- // { updatedAt: 1 },
45
- // { name: 'searchByUpdatedAt' }
46
- // ]
47
- ];
38
+ const indexes = [];
48
39
  exports.indexes = indexes;
49
40
  /**
50
41
  * カスタマータイプスキーマ
@@ -111,7 +111,11 @@ export declare class OfferCatalogRepo {
111
111
  project: {
112
112
  id: string;
113
113
  };
114
- }): Promise<Pick<factory.offerCatalog.IOfferCatalog, 'itemListElement'>>;
114
+ }): Promise<{
115
+ itemListElement: {
116
+ id: string;
117
+ }[];
118
+ }>;
115
119
  /**
116
120
  * 一つ目のitemListElementを取得する
117
121
  */
@@ -134,22 +134,25 @@ class OfferCatalogRepo {
134
134
  }
135
135
  save(params) {
136
136
  return __awaiter(this, void 0, void 0, function* () {
137
+ let savedId;
137
138
  let doc;
138
- if (params.id === '') {
139
+ if (typeof params.id !== 'string' || params.id === '') {
139
140
  const uniqid = yield Promise.resolve().then(() => require('uniqid'));
140
141
  const newId = uniqid();
141
142
  const { id, $unset } = params, creatingDoc = __rest(params, ["id", "$unset"]);
142
143
  doc = yield this.offerCatalogModel.create(Object.assign(Object.assign({}, creatingDoc), { _id: newId }));
144
+ savedId = newId;
143
145
  }
144
146
  else {
145
147
  const { id, identifier, itemOffered, project, typeOf, $unset } = params, updateFields = __rest(params, ["id", "identifier", "itemOffered", "project", "typeOf", "$unset"]); // 上書き禁止属性を除外
146
148
  doc = yield this.offerCatalogModel.findOneAndUpdate({ _id: { $eq: params.id } }, Object.assign({ $set: updateFields }, (params.$unset !== undefined) ? { $unset: params.$unset } : undefined), { upsert: false, new: true, projection: { _id: 1 } })
147
149
  .exec();
150
+ savedId = id;
148
151
  }
149
152
  if (doc === null) {
150
153
  throw new factory.errors.NotFound(this.offerCatalogModel.modelName);
151
154
  }
152
- return doc.toObject();
155
+ return { id: savedId };
153
156
  });
154
157
  }
155
158
  /**
@@ -166,9 +169,14 @@ class OfferCatalogRepo {
166
169
  }, {
167
170
  $set: updateFields,
168
171
  $setOnInsert: { _id: newId, identifier, itemOffered, project, typeOf }
169
- }, { upsert: true, new: true, projection: { _id: 1 } })
172
+ }, {
173
+ upsert: true,
174
+ new: true,
175
+ projection: { _id: 0, id: '$_id' }
176
+ })
177
+ .lean()
170
178
  .exec();
171
- return doc.toObject();
179
+ return { id: doc.id };
172
180
  });
173
181
  }
174
182
  /**
@@ -333,12 +341,13 @@ class OfferCatalogRepo {
333
341
  const doc = yield this.offerCatalogModel.findOne({
334
342
  'project.id': { $eq: params.project.id },
335
343
  _id: { $eq: params.id }
336
- }, { 'itemListElement.id': 1 })
344
+ }, { 'itemListElement.id': 1, _id: 0 })
345
+ .lean() // lean(2024-09-24~)
337
346
  .exec();
338
347
  if (doc === null) {
339
348
  throw new factory.errors.NotFound(this.offerCatalogModel.modelName);
340
349
  }
341
- return doc.toObject();
350
+ return doc;
342
351
  });
343
352
  }
344
353
  /**
@@ -103,14 +103,14 @@ function validateStartRequest(params) {
103
103
  }
104
104
  }
105
105
  // IAM存在検証(2024-02-06~)
106
- const iamMember = (yield repos.member.search({
106
+ const iamMember = (yield repos.member.projectFields({
107
107
  project: { id: { $eq: seller.project.id } },
108
108
  member: {
109
109
  typeOf: { $eq: factory.creativeWorkType.WebApplication },
110
110
  id: { $eq: clientId },
111
111
  memberOf: { typeOf: { $eq: factory.organizationType.Project } } // プロジェクトメンバーのはず
112
112
  }
113
- })).shift();
113
+ }, ['member'])).shift();
114
114
  if (iamMember === undefined) {
115
115
  throw new factory.errors.NotFound(factory.iam.RoleType.OrganizationRole);
116
116
  }
package/package.json CHANGED
@@ -110,5 +110,5 @@
110
110
  "postversion": "git push origin --tags",
111
111
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
112
112
  },
113
- "version": "22.3.0"
113
+ "version": "22.4.0-alpha.0"
114
114
  }
@@ -1,94 +0,0 @@
1
- // tslint:disable:no-console
2
- import * as mongoose from 'mongoose';
3
-
4
- import { chevre } from '../../../lib/index';
5
-
6
- // const project = { id: String(process.env.PROJECT_ID) };
7
-
8
- // tslint:disable-next-line:max-func-body-length
9
- async function main() {
10
- await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
11
-
12
- const memberRepo = await chevre.repository.Member.createInstance(mongoose.connection);
13
-
14
- const cursor = memberRepo.getCursor(
15
- {
16
- 'member.memberOf.typeOf': { $eq: chevre.factory.organizationType.Corporation }
17
- },
18
- {
19
- }
20
- );
21
- console.log('members found');
22
-
23
- let projectIds: string[] = [];
24
- let i = 0;
25
- let updateCount = 0;
26
- await cursor.eachAsync(async (doc) => {
27
- i += 1;
28
- const iamMember: chevre.factory.iam.IMember = doc.toObject();
29
-
30
- const alreadyMigrated = false;
31
- projectIds.push(iamMember.project.id);
32
-
33
- if (alreadyMigrated) {
34
- console.log(
35
- 'already exist.',
36
- iamMember.project.id,
37
- iamMember.member.memberOf.typeOf, iamMember.member.memberOf.id, i, updateCount
38
- );
39
- } else {
40
- console.log(
41
- 'updating...',
42
- iamMember.project.id,
43
- iamMember.member.memberOf.typeOf, iamMember.member.memberOf.id, i, updateCount
44
- );
45
- updateCount += 1;
46
- console.log(
47
- 'updated.',
48
- iamMember.project.id,
49
- iamMember.member.memberOf.typeOf, iamMember.member.memberOf.id, i, updateCount
50
- );
51
- }
52
- });
53
-
54
- projectIds = [...new Set(projectIds)];
55
- console.log(projectIds);
56
- console.log(i, 'members checked');
57
- console.log(updateCount, 'members updated');
58
-
59
- // 販売者メンバーの存在するプロジェクトについて、inventoryManagerにiam.roleAdminを追加
60
- const NEW_ROLE_NAME = 'iam.roleAdmin';
61
- for (const projectId of projectIds) {
62
- const inventoryManagers = await memberRepo.search({
63
- project: { id: { $eq: projectId } },
64
- member: {
65
- hasRole: { roleName: { $eq: 'inventoryManager' } },
66
- memberOf: { typeOf: { $eq: chevre.factory.organizationType.Project } }
67
- }
68
- });
69
- console.log(inventoryManagers.length, 'inventoryManagers found', projectId);
70
- for (const inventoryManager of inventoryManagers) {
71
- const alreadyRoleAdmin = inventoryManager.member.hasRole.some(({ roleName }) => roleName === NEW_ROLE_NAME);
72
- if (!alreadyRoleAdmin) {
73
- const newHasRole: chevre.factory.iam.IMemberRole[] = [
74
- { typeOf: chevre.factory.iam.RoleType.OrganizationRole, roleName: NEW_ROLE_NAME },
75
- ...inventoryManager.member.hasRole
76
- ];
77
- console.log('adding newRole', projectId, inventoryManager.member.id, newHasRole);
78
- await memberRepo.updateByMemberId({
79
- project: { id: inventoryManager.project.id },
80
- member: {
81
- id: inventoryManager.member.id,
82
- memberOf: inventoryManager.member.memberOf,
83
- hasRole: newHasRole
84
- },
85
- $unset: {}
86
- });
87
- }
88
- }
89
- }
90
- }
91
-
92
- main()
93
- .then()
94
- .catch(console.error);
@@ -1,57 +0,0 @@
1
- // tslint:disable:no-console
2
- import * as mongoose from 'mongoose';
3
-
4
- import { chevre } from '../../../lib/index';
5
-
6
- // const project = { id: String(process.env.PROJECT_ID) };
7
-
8
- async function main() {
9
- await mongoose.connect(<string>process.env.MONGOLAB_URI);
10
-
11
- const customerTypeRepo = await chevre.repository.CustomerType.createInstance(mongoose.connection);
12
-
13
- const saveResult = await customerTypeRepo.saveManyByCodeValue([
14
- {
15
- attributes: {
16
- typeOf: 'CategoryCode',
17
- codeValue: 'Enduser',
18
- name: { ja: 'エンドユーザー' }
19
- },
20
- upsert: true
21
- },
22
- {
23
- attributes: {
24
- typeOf: 'CategoryCode',
25
- codeValue: 'POS',
26
- name: { ja: 'POS' }
27
- },
28
- upsert: true
29
- },
30
- {
31
- attributes: {
32
- typeOf: 'CategoryCode',
33
- codeValue: 'TVM',
34
- name: { ja: '券売機' }
35
- },
36
- upsert: true
37
- }
38
- ]);
39
- console.log('saved,', saveResult);
40
-
41
- const categoryCodes = await customerTypeRepo.search(
42
- {
43
- limit: 100,
44
- page: 1,
45
- sort: { codeValue: chevre.factory.sortType.Ascending },
46
- codeValue: { $eq: 'Enduser' }
47
- },
48
- [],
49
- []
50
- );
51
- console.log('categoryCodes found', categoryCodes);
52
- console.log(categoryCodes.length, 'categoryCodes found');
53
- }
54
-
55
- main()
56
- .then()
57
- .catch(console.error);