@chevre/domain 21.15.0-alpha.2 → 21.15.0-alpha.4

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,31 @@
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 sellerRepo = await chevre.repository.Seller.createInstance(mongoose.connection);
12
+
13
+ const sellers = await sellerRepo.searchByAggregate(
14
+ {
15
+ limit: 5,
16
+ page: 1,
17
+ sort: { branchCode: chevre.factory.sortType.Descending }
18
+ // project: { id: { $eq: project.id } }
19
+ // paymentAccepted: { paymentMethodType: { $eq: 'Cash' } },
20
+ // hasMerchantReturnPolicy: { applicablePaymentMethod: {} }
21
+ },
22
+ [],
23
+ ['hasMerchantReturnPolicy', 'additionalProperty', 'project', 'name', 'typeOf', 'url', 'telephone']
24
+ );
25
+ console.log('sellers found', sellers, sellers[0]?.hasMerchantReturnPolicy);
26
+ console.log(sellers.length, 'sellers found');
27
+ }
28
+
29
+ main()
30
+ .then()
31
+ .catch(console.error);
@@ -1,31 +1,45 @@
1
1
  // tslint:disable:no-console
2
- import * as moment from 'moment';
3
2
  import * as mongoose from 'mongoose';
4
3
 
5
4
  import { chevre } from '../../../lib/index';
6
5
 
7
- const PROJECT_ID = String(process.env.PROJECT_ID);
6
+ // const PROJECT_ID = String(process.env.PROJECT_ID);
8
7
 
9
8
  async function main() {
10
9
  await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
11
10
 
12
- const transactionRepo = await chevre.repository.Transaction.createInstance(mongoose.connection);
13
-
14
11
  let updateResult: any;
15
- updateResult = await transactionRepo.unsetUnnecessaryFields({
12
+
13
+ const sellerRepo = await chevre.repository.Seller.createInstance(mongoose.connection);
14
+ updateResult = await sellerRepo.unsetUnnecessaryFields({
16
15
  filter: {
17
16
  // 'project.id': { $eq: PROJECT_ID },
18
- typeOf: { $eq: chevre.factory.transactionType.PlaceOrder },
19
- status: { $eq: chevre.factory.transactionStatusType.Confirmed },
20
- startDate: {
21
- $lte: moment('2023-08-16T07:26:00Z')
22
- .toDate()
23
- },
24
- 'object.authorizeActions': { $exists: true }
17
+ typeOf: { $eq: chevre.factory.organizationType.Corporation },
18
+ areaServed: { $exists: true }
19
+ },
20
+ $unset: { areaServed: 1 }
21
+ });
22
+ console.log('unset processed.', updateResult);
23
+
24
+ const placeRepo = await chevre.repository.Place.createInstance(mongoose.connection);
25
+ updateResult = await placeRepo.unsetUnnecessaryFieldsFromMovieTheater({
26
+ filter: {
27
+ $and: [
28
+ { typeOf: { $eq: chevre.factory.placeType.MovieTheater } },
29
+ {
30
+ $or: [
31
+ { 'offers.project': { $exists: true } },
32
+ { 'offers.priceCurrency': { $exists: true } }
33
+ ]
34
+ }
35
+ ]
25
36
  },
26
- $unset: { 'object.authorizeActions': 1 }
37
+ $unset: {
38
+ 'offers.project': 1,
39
+ 'offers.priceCurrency': 1
40
+ }
27
41
  });
28
- console.log('unset processed.', updateResult, 'PROJECT_ID:', PROJECT_ID);
42
+ console.log('unset processed.', updateResult);
29
43
  }
30
44
 
31
45
  main()
@@ -8,12 +8,12 @@ const modelName = 'Seller';
8
8
  exports.modelName = modelName;
9
9
  const schemaDefinition = {
10
10
  additionalProperty: [mongoose_1.SchemaTypes.Mixed],
11
- areaServed: [mongoose_1.SchemaTypes.Mixed],
11
+ // areaServed: [SchemaTypes.Mixed],
12
12
  branchCode: String,
13
13
  hasMerchantReturnPolicy: [mongoose_1.SchemaTypes.Mixed],
14
14
  makesOffer: [mongoose_1.SchemaTypes.Mixed],
15
15
  name: mongoose_1.SchemaTypes.Mixed,
16
- parentOrganization: mongoose_1.SchemaTypes.Mixed,
16
+ // parentOrganization: SchemaTypes.Mixed,
17
17
  paymentAccepted: [mongoose_1.SchemaTypes.Mixed],
18
18
  project: mongoose_1.SchemaTypes.Mixed,
19
19
  typeOf: {
@@ -283,14 +283,10 @@ export declare class MongoRepository {
283
283
  };
284
284
  typeOf: factory.placeType.ScreeningRoom;
285
285
  }>;
286
- unsetContainsPlaceFromMovieTheater(params: {
287
- /**
288
- * 施設ID
289
- */
290
- id: string;
291
- }): Promise<{
292
- id: string;
293
- } | undefined>;
286
+ unsetUnnecessaryFieldsFromMovieTheater(params: {
287
+ filter: any;
288
+ $unset: any;
289
+ }): Promise<import("mongodb").UpdateResult>;
294
290
  deleteManyByParentOrganizationId(params: {
295
291
  project: {
296
292
  id: string;
@@ -1287,23 +1287,10 @@ class MongoRepository {
1287
1287
  return doc.toObject();
1288
1288
  });
1289
1289
  }
1290
- unsetContainsPlaceFromMovieTheater(params) {
1290
+ unsetUnnecessaryFieldsFromMovieTheater(params) {
1291
1291
  return __awaiter(this, void 0, void 0, function* () {
1292
- return this.placeModel.findOneAndUpdate({
1293
- _id: { $eq: params.id },
1294
- typeOf: { $eq: factory.placeType.MovieTheater },
1295
- 'containsPlace.branchCode': { $exists: true }
1296
- }, {
1297
- $unset: { containsPlace: 1 }
1298
- })
1299
- .select({ _id: 1 })
1300
- .exec()
1301
- .then((doc) => {
1302
- if (doc === null) {
1303
- return;
1304
- }
1305
- return doc.toObject();
1306
- });
1292
+ return this.placeModel.updateMany(params.filter, { $unset: params.$unset })
1293
+ .exec();
1307
1294
  });
1308
1295
  }
1309
1296
  deleteManyByParentOrganizationId(params) {
@@ -22,9 +22,10 @@
22
22
  /// <reference types="mongoose/types/validation" />
23
23
  /// <reference types="mongoose/types/virtuals" />
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
- import { Connection } from 'mongoose';
25
+ import { AnyExpression, Connection, FilterQuery } from 'mongoose';
26
26
  import * as factory from '../factory';
27
27
  export type ISeller = factory.seller.ISeller;
28
+ export type ISellerByAggregate = Pick<ISeller, 'additionalProperty' | 'branchCode' | 'hasMerchantReturnPolicy' | 'id' | 'name' | 'project' | 'telephone' | 'typeOf' | 'url'>;
28
29
  export interface IMemberSearchConditions {
29
30
  /**
30
31
  * 販売者メンバーで絞る場合
@@ -37,17 +38,17 @@ export interface IMemberSearchConditions {
37
38
  };
38
39
  };
39
40
  }
41
+ type IKeyOfProjection = keyof ISeller | '_id';
40
42
  /**
41
43
  * 販売者リポジトリ
42
44
  */
43
45
  export declare class MongoRepository {
44
46
  private readonly organizationModel;
45
47
  constructor(connection: Connection);
46
- static CREATE_MONGO_CONDITIONS(params: factory.seller.ISearchConditions & IMemberSearchConditions): any[];
47
- /**
48
- * 特定販売者検索
49
- * searchに置き換え(2023-07-13~)
50
- */
48
+ static CREATE_MONGO_CONDITIONS(params: factory.seller.ISearchConditions & IMemberSearchConditions): FilterQuery<factory.seller.ISeller>[];
49
+ static CREATE_AGGREGATE_SELLERS_PROJECTION(inclusion: string[], exclusion: (keyof ISellerByAggregate)[]): {
50
+ [field: string]: AnyExpression;
51
+ };
51
52
  /**
52
53
  * 販売者を保管する
53
54
  */
@@ -55,10 +56,14 @@ export declare class MongoRepository {
55
56
  id?: string;
56
57
  attributes: factory.seller.ISeller;
57
58
  }): Promise<ISeller>;
59
+ /**
60
+ * 集計検索(publicな属性検索が目的)
61
+ */
62
+ searchByAggregate(conditions: factory.seller.ISearchConditions & IMemberSearchConditions, inclusion: (keyof ISellerByAggregate)[], exclusion: (keyof ISellerByAggregate)[]): Promise<ISellerByAggregate[]>;
58
63
  /**
59
64
  * 販売者検索
60
65
  */
61
- search(conditions: factory.seller.ISearchConditions & IMemberSearchConditions, inclusion: string[], exclusion: string[]): Promise<ISeller[]>;
66
+ search(conditions: factory.seller.ISearchConditions & IMemberSearchConditions, inclusion: IKeyOfProjection[], exclusion: IKeyOfProjection[]): Promise<ISeller[]>;
62
67
  /**
63
68
  * 対応決済方法を検索する
64
69
  */
@@ -96,5 +101,10 @@ export declare class MongoRepository {
96
101
  id: string;
97
102
  };
98
103
  }): Promise<void>;
99
- getCursor(conditions: any, projection: any): import("mongoose").Cursor<any, import("mongoose").QueryOptions<any>>;
104
+ getCursor(conditions: FilterQuery<factory.seller.ISeller>, projection: any): import("mongoose").Cursor<any, import("mongoose").QueryOptions<any>>;
105
+ unsetUnnecessaryFields(params: {
106
+ filter: any;
107
+ $unset: any;
108
+ }): Promise<import("mongodb").UpdateResult>;
100
109
  }
110
+ export {};
@@ -35,7 +35,6 @@ class MongoRepository {
35
35
  // tslint:disable-next-line:max-func-body-length
36
36
  static CREATE_MONGO_CONDITIONS(params) {
37
37
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
38
- // MongoDB検索条件
39
38
  const andConditions = [];
40
39
  const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
41
40
  if (typeof projectIdEq === 'string') {
@@ -143,41 +142,47 @@ class MongoRepository {
143
142
  }
144
143
  return andConditions;
145
144
  }
146
- /**
147
- * 特定販売者検索
148
- * searchに置き換え(2023-07-13~)
149
- */
150
- // public async findById(
151
- // conditions: {
152
- // id: string;
153
- // },
154
- // inclusion: string[],
155
- // exclusion: string[]
156
- // ): Promise<ISeller> {
157
- // let projection: { [key: string]: number } = {};
158
- // if (Array.isArray(inclusion) && inclusion.length > 0) {
159
- // inclusion.forEach((field) => {
160
- // projection[field] = 1;
161
- // });
162
- // } else {
163
- // projection = {
164
- // __v: 0,
165
- // createdAt: 0,
166
- // updatedAt: 0
167
- // };
168
- // if (Array.isArray(exclusion) && exclusion.length > 0) {
169
- // exclusion.forEach((field) => {
170
- // projection[field] = 0;
171
- // });
172
- // }
173
- // }
174
- // const doc = await this.organizationModel.findOne({ _id: conditions.id }, projection)
175
- // .exec();
176
- // if (doc === null) {
177
- // throw new factory.errors.NotFound(this.organizationModel.modelName);
178
- // }
179
- // return doc.toObject();
180
- // }
145
+ static CREATE_AGGREGATE_SELLERS_PROJECTION(inclusion, exclusion) {
146
+ const projectStage = {
147
+ _id: 0,
148
+ id: { $toString: '$_id' },
149
+ additionalProperty: '$additionalProperty',
150
+ branchCode: '$branchCode',
151
+ hasMerchantReturnPolicy: {
152
+ $cond: {
153
+ if: { $isArray: '$hasMerchantReturnPolicy' },
154
+ then: {
155
+ $cond: {
156
+ if: { $eq: [{ $size: '$hasMerchantReturnPolicy' }, 0] },
157
+ then: [],
158
+ else: [{
159
+ url: { $first: '$hasMerchantReturnPolicy.url' },
160
+ typeOf: { $first: '$hasMerchantReturnPolicy.typeOf' }
161
+ }]
162
+ }
163
+ },
164
+ else: []
165
+ }
166
+ },
167
+ name: '$name',
168
+ project: '$project',
169
+ typeOf: '$typeOf',
170
+ url: '$url',
171
+ telephone: '$telephone'
172
+ };
173
+ if (inclusion.length > 0) {
174
+ // no op
175
+ }
176
+ else if (exclusion.length > 0) {
177
+ exclusion.forEach((field) => {
178
+ if (typeof projectStage[field] === 'string' || typeof projectStage[field] === 'object') {
179
+ // tslint:disable-next-line:no-dynamic-delete
180
+ delete projectStage[field];
181
+ }
182
+ });
183
+ }
184
+ return projectStage;
185
+ }
181
186
  /**
182
187
  * 販売者を保管する
183
188
  */
@@ -204,6 +209,33 @@ class MongoRepository {
204
209
  return organization;
205
210
  });
206
211
  }
212
+ /**
213
+ * 集計検索(publicな属性検索が目的)
214
+ */
215
+ searchByAggregate(conditions, inclusion, exclusion) {
216
+ var _a;
217
+ return __awaiter(this, void 0, void 0, function* () {
218
+ const matchStages = MongoRepository.CREATE_MONGO_CONDITIONS(conditions)
219
+ .map((c) => ({ $match: c }));
220
+ const projectStage = MongoRepository.CREATE_AGGREGATE_SELLERS_PROJECTION(inclusion, exclusion);
221
+ const sortByBranchCode = (_a = conditions.sort) === null || _a === void 0 ? void 0 : _a.branchCode;
222
+ const aggregate = this.organizationModel.aggregate([
223
+ ...matchStages,
224
+ ...(typeof sortByBranchCode === 'number')
225
+ ? [{ $sort: { branchCode: sortByBranchCode } }]
226
+ : [],
227
+ { $project: projectStage }
228
+ ]);
229
+ // tslint:disable-next-line:no-single-line-block-comment
230
+ /* istanbul ignore else */
231
+ if (typeof conditions.limit === 'number' && conditions.limit > 0) {
232
+ const page = (typeof conditions.page === 'number' && conditions.page > 0) ? conditions.page : 1;
233
+ aggregate.limit(conditions.limit * page)
234
+ .skip(conditions.limit * (page - 1));
235
+ }
236
+ return aggregate.exec();
237
+ });
238
+ }
207
239
  /**
208
240
  * 販売者検索
209
241
  */
@@ -309,5 +341,11 @@ class MongoRepository {
309
341
  .sort({ branchCode: factory.sortType.Ascending })
310
342
  .cursor();
311
343
  }
344
+ unsetUnnecessaryFields(params) {
345
+ return __awaiter(this, void 0, void 0, function* () {
346
+ return this.organizationModel.updateMany(params.filter, { $unset: params.$unset })
347
+ .exec();
348
+ });
349
+ }
312
350
  }
313
351
  exports.MongoRepository = MongoRepository;
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.15.0-alpha.2"
120
+ "version": "21.15.0-alpha.4"
121
121
  }
@@ -1,28 +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 sellerRepo = await chevre.repository.Seller.createInstance(mongoose.connection);
12
-
13
- const sellers = await sellerRepo.search(
14
- {
15
- project: { id: { $eq: project.id } },
16
- paymentAccepted: { paymentMethodType: { $eq: 'Cash' } },
17
- hasMerchantReturnPolicy: { applicablePaymentMethod: {} }
18
- },
19
- ['name'],
20
- []
21
- );
22
- console.log('sellers found', sellers);
23
- console.log(sellers.length, 'sellers found');
24
- }
25
-
26
- main()
27
- .then()
28
- .catch(console.error);