@chevre/domain 21.2.0-alpha.124 → 21.2.0-alpha.126

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.
@@ -85,7 +85,12 @@ export declare class StockHolderRepository {
85
85
  /**
86
86
  * 座席をロックする
87
87
  */
88
- lock(lockKey: ILockKey): Promise<void>;
88
+ lock(lockKey: ILockKey & {
89
+ /**
90
+ * テスト目的の強制オプション
91
+ */
92
+ useMongooseForcibly?: boolean;
93
+ }): Promise<void>;
89
94
  /**
90
95
  * 座席ロックを解除する
91
96
  */
@@ -155,6 +160,5 @@ export declare class StockHolderRepository {
155
160
  };
156
161
  }): Promise<ISearchSubReservationResult[]>;
157
162
  private checkIfConflicted;
158
- private checkIfConflictedBetweenRedisAndMongo;
159
163
  private initializeHoldReservation;
160
164
  }
@@ -75,6 +75,9 @@ class StockHolderRepository {
75
75
  * 新リポジトリを使用するかどうか
76
76
  */
77
77
  static useMongoose(params) {
78
+ if (params.useMongooseForcibly === true) {
79
+ return true;
80
+ }
78
81
  if (settings_1.USE_NEW_STOCK_HOLDER_REPO_IDS.includes(params.eventId)) {
79
82
  return true;
80
83
  }
@@ -85,8 +88,12 @@ class StockHolderRepository {
85
88
  */
86
89
  lockIfNotLimitExceeded(lockKey, maximum) {
87
90
  return __awaiter(this, void 0, void 0, function* () {
88
- if (StockHolderRepository.useMongoose({ eventId: lockKey.eventId, startDate: lockKey.startDate })) {
89
- yield this.checkIfConflictedBetweenRedisAndMongo({ key: '', eventId: lockKey.eventId });
91
+ const useMongoose = StockHolderRepository.useMongoose({ eventId: lockKey.eventId, startDate: lockKey.startDate });
92
+ if (useMongoose) {
93
+ if (!(lockKey.expires instanceof Date)) {
94
+ throw new factory.errors.Argument('expires', 'must be Date');
95
+ }
96
+ yield this.checkIfConflicted({ key: '', eventId: lockKey.eventId, useMongoose });
90
97
  // reservationCount<=nであれば$push+$incする
91
98
  yield this.initializeHoldReservation({ project: lockKey.project, eventId: lockKey.eventId, startDate: lockKey.startDate });
92
99
  const addedReservationCount = lockKey.offers.length;
@@ -135,8 +142,17 @@ class StockHolderRepository {
135
142
  */
136
143
  lock(lockKey) {
137
144
  return __awaiter(this, void 0, void 0, function* () {
138
- if (StockHolderRepository.useMongoose({ eventId: lockKey.eventId, startDate: lockKey.startDate })) {
139
- yield this.checkIfConflictedBetweenRedisAndMongo({ key: '', eventId: lockKey.eventId });
145
+ if (!(lockKey.expires instanceof Date)) {
146
+ throw new factory.errors.Argument('expires', 'must be Date');
147
+ }
148
+ const useMongoose = StockHolderRepository.useMongoose({
149
+ eventId: lockKey.eventId,
150
+ startDate: lockKey.startDate,
151
+ useMongooseForcibly: lockKey.useMongooseForcibly
152
+ });
153
+ const key = StockHolderRepository.createKey({ eventId: lockKey.eventId, startDate: lockKey.startDate });
154
+ yield this.checkIfConflicted({ key, eventId: lockKey.eventId, useMongoose });
155
+ if (useMongoose) {
140
156
  yield this.initializeHoldReservation({ project: lockKey.project, eventId: lockKey.eventId, startDate: lockKey.startDate });
141
157
  const addedReservationCount = lockKey.offers.length;
142
158
  const subReservations = lockKey.offers.map((offer) => StockHolderRepository.offer2subReservation(offer));
@@ -164,11 +180,6 @@ class StockHolderRepository {
164
180
  });
165
181
  }
166
182
  else {
167
- if (!(lockKey.expires instanceof Date)) {
168
- throw new factory.errors.Argument('expires', 'must be Date');
169
- }
170
- const key = StockHolderRepository.createKey({ eventId: lockKey.eventId, startDate: lockKey.startDate });
171
- yield this.checkIfConflicted({ key, eventId: lockKey.eventId });
172
183
  const value = lockKey.holder;
173
184
  const multi = this.redisClient.multi();
174
185
  const fields = lockKey.offers.map((offer) => StockHolderRepository.offer2field(offer));
@@ -215,8 +226,10 @@ class StockHolderRepository {
215
226
  */
216
227
  unlock(params) {
217
228
  return __awaiter(this, void 0, void 0, function* () {
218
- if (StockHolderRepository.useMongoose({ eventId: params.eventId, startDate: params.startDate })) {
219
- yield this.checkIfConflictedBetweenRedisAndMongo({ key: '', eventId: params.eventId });
229
+ const useMongoose = StockHolderRepository.useMongoose({ eventId: params.eventId, startDate: params.startDate });
230
+ const key = StockHolderRepository.createKey({ eventId: params.eventId, startDate: params.startDate });
231
+ yield this.checkIfConflicted({ key, eventId: params.eventId, useMongoose });
232
+ if (useMongoose) {
220
233
  // [id]あるいは[seatNumber+seatSection]でreservations.subReservationsから$pull+$incする
221
234
  yield this.initializeHoldReservation({ project: params.project, eventId: params.eventId, startDate: params.startDate });
222
235
  const subReservation = StockHolderRepository.offer2subReservation(params.offer);
@@ -240,8 +253,6 @@ class StockHolderRepository {
240
253
  // docが存在しなくてもよい
241
254
  }
242
255
  else {
243
- const key = StockHolderRepository.createKey({ eventId: params.eventId, startDate: params.startDate });
244
- yield this.checkIfConflicted({ key, eventId: params.eventId });
245
256
  const field = StockHolderRepository.offer2field(params.offer);
246
257
  yield this.redisClient.multi()
247
258
  .hDel(key, field)
@@ -306,7 +317,7 @@ class StockHolderRepository {
306
317
  return __awaiter(this, void 0, void 0, function* () {
307
318
  if (StockHolderRepository.useMongoose({ eventId: params.eventId, startDate: params.startDate })) {
308
319
  // [id]あるいは[seatNumber+seatSection]でreservationNumberを返す
309
- yield this.initializeHoldReservation({ project: params.project, eventId: params.eventId, startDate: params.startDate });
320
+ // await this.initializeHoldReservation({ project: params.project, eventId: params.eventId, startDate: params.startDate });
310
321
  const subReservation = StockHolderRepository.offer2subReservation(params.offer);
311
322
  const matchStages = [
312
323
  {
@@ -380,7 +391,7 @@ class StockHolderRepository {
380
391
  return __awaiter(this, void 0, void 0, function* () {
381
392
  if (StockHolderRepository.useMongoose({ eventId: params.eventId, startDate: params.startDate })) {
382
393
  // [id]あるいは[seatNumber+seatSection]のリストでreservationNumberのリストを返す
383
- yield this.initializeHoldReservation({ project: params.project, eventId: params.eventId, startDate: params.startDate });
394
+ // await this.initializeHoldReservation({ project: params.project, eventId: params.eventId, startDate: params.startDate });
384
395
  const subReservations = params.offers.map((offer) => StockHolderRepository.offer2subReservation(offer));
385
396
  const matchStages = [
386
397
  {
@@ -515,7 +526,13 @@ class StockHolderRepository {
515
526
  _id: 0,
516
527
  id: '$reservations.subReservation.id',
517
528
  identifier: '$reservations.subReservation.identifier',
518
- reservationFor: '$reservationFor',
529
+ reservationFor: {
530
+ id: '$reservationFor.id',
531
+ startDate: '$reservationFor.startDate',
532
+ aggregateReservation: {
533
+ reservationCount: '$reservationCount'
534
+ }
535
+ },
519
536
  reservationNumber: '$reservations.reservationNumber',
520
537
  reservedTicket: '$reservations.subReservation.reservedTicket'
521
538
  }
@@ -535,29 +552,46 @@ class StockHolderRepository {
535
552
  // 旧キーと新キーの両方存在検証(念のため)
536
553
  const oldKey = StockHolderRepository.createKey({ eventId: params.eventId, startDate: new Date('2020-01-01T00:00:00Z') });
537
554
  const newKey = StockHolderRepository.createKey({ eventId: params.eventId, startDate: new Date('2030-01-01T00:00:00Z') });
538
- if (params.key !== oldKey) {
555
+ if (params.useMongoose) {
556
+ // newもoldも存在するはずがない
539
557
  // newの場合oldが存在するはずがない
540
558
  const existingOldKeyCount = yield this.redisClient.exists(oldKey);
541
559
  debug('existingOldKeyCount:', existingOldKeyCount);
542
560
  if (existingOldKeyCount > 0) {
543
- throw new factory.errors.ServiceUnavailable('stockHolder keys conflicted');
561
+ throw new factory.errors.ServiceUnavailable(`stockHolder storage conflicted. eventId:${params.eventId}`);
544
562
  }
545
- }
546
- if (params.key !== newKey) {
547
563
  // oldの場合newが存在するはずがない
548
564
  const existingNewKeyCount = yield this.redisClient.exists(newKey);
549
565
  debug('existingNewKeyCount:', existingNewKeyCount);
550
566
  if (existingNewKeyCount > 0) {
551
- throw new factory.errors.ServiceUnavailable('stockHolder keys conflicted');
567
+ throw new factory.errors.ServiceUnavailable(`stockHolder storage conflicted. eventId:${params.eventId}`);
568
+ }
569
+ }
570
+ else {
571
+ // collectionにdocumentが存在するはずがない
572
+ const existingHoldReservationCount = yield this.holdReservationModel.count({ 'reservationFor.id': { $eq: params.eventId } })
573
+ .exec();
574
+ debug('existingHoldReservationCount:', existingHoldReservationCount);
575
+ if (existingHoldReservationCount > 0) {
576
+ throw new factory.errors.ServiceUnavailable(`stockHolder storage conflicted. eventId:${params.eventId}`);
577
+ }
578
+ if (params.key !== oldKey) {
579
+ // newの場合oldが存在するはずがない
580
+ const existingOldKeyCount = yield this.redisClient.exists(oldKey);
581
+ debug('existingOldKeyCount:', existingOldKeyCount);
582
+ if (existingOldKeyCount > 0) {
583
+ throw new factory.errors.ServiceUnavailable(`stockHolder keys conflicted. eventId:${params.eventId}`);
584
+ }
585
+ }
586
+ if (params.key !== newKey) {
587
+ // oldの場合newが存在するはずがない
588
+ const existingNewKeyCount = yield this.redisClient.exists(newKey);
589
+ debug('existingNewKeyCount:', existingNewKeyCount);
590
+ if (existingNewKeyCount > 0) {
591
+ throw new factory.errors.ServiceUnavailable(`stockHolder keys conflicted. eventId:${params.eventId}`);
592
+ }
552
593
  }
553
594
  }
554
- });
555
- }
556
- // tslint:disable-next-line:prefer-function-over-method
557
- checkIfConflictedBetweenRedisAndMongo(__) {
558
- return __awaiter(this, void 0, void 0, function* () {
559
- // tslint:disable-next-line:no-suspicious-comment
560
- // TODO implement
561
595
  });
562
596
  }
563
597
  initializeHoldReservation(params) {
@@ -575,12 +609,11 @@ class StockHolderRepository {
575
609
  },
576
610
  reservations: []
577
611
  };
578
- const initializedHoldReservation = yield this.holdReservationModel.findOneAndUpdate({ 'reservationFor.id': { $eq: params.eventId } }, {
612
+ const initializedResult = yield this.holdReservationModel.updateOne({ 'reservationFor.id': { $eq: params.eventId } }, {
579
613
  $setOnInsert: aggregateReservation
580
- }, { new: true, upsert: true })
581
- .select({ _id: 1 })
614
+ }, { upsert: true })
582
615
  .exec();
583
- debug('holdReservation initialized', initializedHoldReservation, params.eventId);
616
+ debug('holdReservation initialized', initializedResult, params.eventId);
584
617
  });
585
618
  }
586
619
  }
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.124"
120
+ "version": "21.2.0-alpha.126"
121
121
  }