@chevre/domain 21.2.0-alpha.4 → 21.2.0-alpha.41

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.
Files changed (196) hide show
  1. package/example/src/chevre/aggregation/aggregateSystem.ts +58 -31
  2. package/example/src/chevre/countDelayedTasks.ts +17 -0
  3. package/example/src/chevre/countDelayedTransactions.ts +60 -0
  4. package/example/src/chevre/countMoneyTransferTransaction.ts +36 -0
  5. package/example/src/chevre/createManyEventsIfNotExist.ts +4 -1
  6. package/example/src/chevre/lockStockHolder.ts +48 -0
  7. package/example/src/chevre/migrateOrderAdditionalProperties.ts +85 -0
  8. package/example/src/chevre/migrateStockHolderKeys.ts +89 -0
  9. package/example/src/chevre/saveTasks.ts +13 -10
  10. package/example/src/chevre/searchAbortedTasks.ts +36 -0
  11. package/example/src/chevre/task/executeTasks.ts +26 -0
  12. package/example/src/chevre/transaction/findCreditCard.ts +1 -1
  13. package/example/src/chevre/transaction/makeExpired.ts +18 -0
  14. package/example/src/chevre/transaction/orderMembershipService.ts +1 -1
  15. package/example/src/chevre/transaction/startExportTasks.ts +20 -0
  16. package/example/src/chevre/updateTransaction.ts +1 -1
  17. package/lib/chevre/credentials.d.ts +1 -0
  18. package/lib/chevre/credentials.js +2 -1
  19. package/lib/chevre/eventEmitter/assetTransaction.d.ts +21 -0
  20. package/lib/chevre/eventEmitter/assetTransaction.js +25 -0
  21. package/lib/chevre/eventEmitter/task.d.ts +18 -0
  22. package/lib/chevre/eventEmitter/task.js +25 -0
  23. package/lib/chevre/eventEmitter/transaction.d.ts +21 -0
  24. package/lib/chevre/eventEmitter/transaction.js +25 -0
  25. package/lib/chevre/eventEmitter.d.ts +4 -0
  26. package/lib/chevre/eventEmitter.js +9 -0
  27. package/lib/chevre/index.d.ts +3 -2
  28. package/lib/chevre/index.js +5 -3
  29. package/lib/chevre/repo/account.js +0 -4
  30. package/lib/chevre/repo/accountTitle.js +0 -4
  31. package/lib/chevre/repo/accountTransaction.d.ts +0 -10
  32. package/lib/chevre/repo/accountTransaction.js +0 -57
  33. package/lib/chevre/repo/accountingReport.js +0 -4
  34. package/lib/chevre/repo/action.js +0 -4
  35. package/lib/chevre/repo/additionalProperty.js +0 -4
  36. package/lib/chevre/repo/aggregation.js +0 -4
  37. package/lib/chevre/repo/assetTransaction.d.ts +16 -6
  38. package/lib/chevre/repo/assetTransaction.js +174 -50
  39. package/lib/chevre/repo/categoryCode.js +0 -4
  40. package/lib/chevre/repo/code.js +0 -4
  41. package/lib/chevre/repo/comment.d.ts +4 -1
  42. package/lib/chevre/repo/comment.js +20 -9
  43. package/lib/chevre/repo/confirmationNumber.d.ts +0 -1
  44. package/lib/chevre/repo/confirmationNumber.js +3 -15
  45. package/lib/chevre/repo/creativeWork.d.ts +1 -3
  46. package/lib/chevre/repo/creativeWork.js +0 -4
  47. package/lib/chevre/repo/customer.js +0 -4
  48. package/lib/chevre/repo/emailMessage.d.ts +1 -27
  49. package/lib/chevre/repo/emailMessage.js +0 -4
  50. package/lib/chevre/repo/event.d.ts +5 -1
  51. package/lib/chevre/repo/event.js +0 -4
  52. package/lib/chevre/repo/member.js +0 -4
  53. package/lib/chevre/repo/merchantReturnPolicy.d.ts +1 -3
  54. package/lib/chevre/repo/merchantReturnPolicy.js +0 -4
  55. package/lib/chevre/repo/mongoose/schemas/account.d.ts +5 -2
  56. package/lib/chevre/repo/mongoose/schemas/account.js +1 -0
  57. package/lib/chevre/repo/mongoose/schemas/accountTitle.d.ts +30 -3
  58. package/lib/chevre/repo/mongoose/schemas/accountTitle.js +1 -0
  59. package/lib/chevre/repo/mongoose/schemas/accountTransaction.d.ts +32 -3
  60. package/lib/chevre/repo/mongoose/schemas/accountTransaction.js +2 -1
  61. package/lib/chevre/repo/mongoose/schemas/accountingReport.d.ts +14 -3
  62. package/lib/chevre/repo/mongoose/schemas/accountingReport.js +1 -0
  63. package/lib/chevre/repo/mongoose/schemas/action.d.ts +44 -3
  64. package/lib/chevre/repo/mongoose/schemas/action.js +2 -1
  65. package/lib/chevre/repo/mongoose/schemas/additionalProperty.d.ts +18 -3
  66. package/lib/chevre/repo/mongoose/schemas/additionalProperty.js +1 -0
  67. package/lib/chevre/repo/mongoose/schemas/aggregation.d.ts +5 -2
  68. package/lib/chevre/repo/mongoose/schemas/aggregation.js +1 -0
  69. package/lib/chevre/repo/mongoose/schemas/assetTransaction.d.ts +45 -3
  70. package/lib/chevre/repo/mongoose/schemas/assetTransaction.js +3 -1
  71. package/lib/chevre/repo/mongoose/schemas/authorization.d.ts +20 -3
  72. package/lib/chevre/repo/mongoose/schemas/authorization.js +2 -1
  73. package/lib/chevre/repo/mongoose/schemas/categoryCode.d.ts +26 -3
  74. package/lib/chevre/repo/mongoose/schemas/categoryCode.js +1 -0
  75. package/lib/chevre/repo/mongoose/schemas/comments.d.ts +28 -3
  76. package/lib/chevre/repo/mongoose/schemas/comments.js +2 -1
  77. package/lib/chevre/repo/mongoose/schemas/creativeWork.d.ts +44 -3
  78. package/lib/chevre/repo/mongoose/schemas/creativeWork.js +1 -0
  79. package/lib/chevre/repo/mongoose/schemas/customer.d.ts +26 -3
  80. package/lib/chevre/repo/mongoose/schemas/customer.js +1 -0
  81. package/lib/chevre/repo/mongoose/schemas/emailMessages.d.ts +22 -3
  82. package/lib/chevre/repo/mongoose/schemas/emailMessages.js +1 -0
  83. package/lib/chevre/repo/mongoose/schemas/event.d.ts +72 -3
  84. package/lib/chevre/repo/mongoose/schemas/event.js +1 -0
  85. package/lib/chevre/repo/mongoose/schemas/member.d.ts +14 -3
  86. package/lib/chevre/repo/mongoose/schemas/member.js +1 -0
  87. package/lib/chevre/repo/mongoose/schemas/merchantReturnPolicy.d.ts +22 -3
  88. package/lib/chevre/repo/mongoose/schemas/merchantReturnPolicy.js +1 -0
  89. package/lib/chevre/repo/mongoose/schemas/offer.d.ts +66 -3
  90. package/lib/chevre/repo/mongoose/schemas/offer.js +1 -0
  91. package/lib/chevre/repo/mongoose/schemas/offerCatalog.d.ts +28 -3
  92. package/lib/chevre/repo/mongoose/schemas/offerCatalog.js +1 -0
  93. package/lib/chevre/repo/mongoose/schemas/order.d.ts +52 -3
  94. package/lib/chevre/repo/mongoose/schemas/order.js +2 -1
  95. package/lib/chevre/repo/mongoose/schemas/ownershipInfo.d.ts +26 -3
  96. package/lib/chevre/repo/mongoose/schemas/ownershipInfo.js +2 -1
  97. package/lib/chevre/repo/mongoose/schemas/place.d.ts +50 -3
  98. package/lib/chevre/repo/mongoose/schemas/place.js +1 -0
  99. package/lib/chevre/repo/mongoose/schemas/priceSpecification.d.ts +30 -3
  100. package/lib/chevre/repo/mongoose/schemas/priceSpecification.js +1 -0
  101. package/lib/chevre/repo/mongoose/schemas/product.d.ts +32 -3
  102. package/lib/chevre/repo/mongoose/schemas/product.js +1 -0
  103. package/lib/chevre/repo/mongoose/schemas/project.d.ts +24 -3
  104. package/lib/chevre/repo/mongoose/schemas/project.js +1 -0
  105. package/lib/chevre/repo/mongoose/schemas/reservation.d.ts +62 -3
  106. package/lib/chevre/repo/mongoose/schemas/reservation.js +2 -1
  107. package/lib/chevre/repo/mongoose/schemas/role.d.ts +12 -3
  108. package/lib/chevre/repo/mongoose/schemas/role.js +1 -0
  109. package/lib/chevre/repo/mongoose/schemas/seller.d.ts +32 -3
  110. package/lib/chevre/repo/mongoose/schemas/seller.js +1 -0
  111. package/lib/chevre/repo/mongoose/schemas/serviceOutput.d.ts +12 -3
  112. package/lib/chevre/repo/mongoose/schemas/serviceOutput.js +2 -1
  113. package/lib/chevre/repo/mongoose/schemas/task.d.ts +31 -3
  114. package/lib/chevre/repo/mongoose/schemas/task.js +3 -1
  115. package/lib/chevre/repo/mongoose/schemas/telemetry.d.ts +22 -3
  116. package/lib/chevre/repo/mongoose/schemas/telemetry.js +1 -0
  117. package/lib/chevre/repo/mongoose/schemas/transaction.d.ts +41 -3
  118. package/lib/chevre/repo/mongoose/schemas/transaction.js +3 -1
  119. package/lib/chevre/repo/mongoose/schemas/trip.d.ts +12 -3
  120. package/lib/chevre/repo/mongoose/schemas/trip.js +1 -0
  121. package/lib/chevre/repo/offer.js +0 -5
  122. package/lib/chevre/repo/offerCatalog.js +0 -4
  123. package/lib/chevre/repo/order.d.ts +25 -0
  124. package/lib/chevre/repo/order.js +5 -4
  125. package/lib/chevre/repo/orderNumber.d.ts +0 -1
  126. package/lib/chevre/repo/orderNumber.js +2 -12
  127. package/lib/chevre/repo/ownershipInfo.js +0 -4
  128. package/lib/chevre/repo/permit.js +0 -4
  129. package/lib/chevre/repo/place.d.ts +5 -1
  130. package/lib/chevre/repo/place.js +1 -10
  131. package/lib/chevre/repo/priceSpecification.js +0 -4
  132. package/lib/chevre/repo/product.js +0 -4
  133. package/lib/chevre/repo/project.js +0 -4
  134. package/lib/chevre/repo/reservation.js +0 -4
  135. package/lib/chevre/repo/role.js +0 -4
  136. package/lib/chevre/repo/seller.js +0 -4
  137. package/lib/chevre/repo/serviceOutput.js +0 -4
  138. package/lib/chevre/repo/serviceOutputIdentifier.d.ts +0 -1
  139. package/lib/chevre/repo/serviceOutputIdentifier.js +2 -12
  140. package/lib/chevre/repo/stockHolder.d.ts +1 -0
  141. package/lib/chevre/repo/stockHolder.js +74 -7
  142. package/lib/chevre/repo/task.d.ts +38 -2
  143. package/lib/chevre/repo/task.js +90 -10
  144. package/lib/chevre/repo/telemetry.js +0 -4
  145. package/lib/chevre/repo/transaction.d.ts +17 -2
  146. package/lib/chevre/repo/transaction.js +176 -56
  147. package/lib/chevre/repo/transactionNumber.d.ts +0 -1
  148. package/lib/chevre/repo/transactionNumber.js +2 -12
  149. package/lib/chevre/repo/trip.js +0 -4
  150. package/lib/chevre/service/aggregation/system.d.ts +67 -22
  151. package/lib/chevre/service/aggregation/system.js +101 -89
  152. package/lib/chevre/service/assetTransaction/cancelReservation.js +1 -5
  153. package/lib/chevre/service/assetTransaction/moneyTransfer.js +1 -1
  154. package/lib/chevre/service/assetTransaction/pay.js +2 -2
  155. package/lib/chevre/service/assetTransaction/refund.js +1 -1
  156. package/lib/chevre/service/assetTransaction/registerService.js +1 -2
  157. package/lib/chevre/service/assetTransaction/reserve.js +1 -1
  158. package/lib/chevre/service/assetTransaction.d.ts +6 -2
  159. package/lib/chevre/service/assetTransaction.js +9 -5
  160. package/lib/chevre/service/notification.d.ts +3 -1
  161. package/lib/chevre/service/notification.js +28 -7
  162. package/lib/chevre/service/offer/event/voidTransaction.d.ts +2 -0
  163. package/lib/chevre/service/offer/event/voidTransaction.js +17 -1
  164. package/lib/chevre/service/offer.js +1 -1
  165. package/lib/chevre/service/order/onOrderStatusChanged.js +1 -1
  166. package/lib/chevre/service/order/placeOrder.js +1 -1
  167. package/lib/chevre/service/order/returnOrder.js +1 -5
  168. package/lib/chevre/service/order/sendOrder.js +1 -1
  169. package/lib/chevre/service/payment/any/onPaid.js +1 -1
  170. package/lib/chevre/service/payment/any/onRefund.js +1 -1
  171. package/lib/chevre/service/payment/any.js +25 -10
  172. package/lib/chevre/service/product.js +1 -5
  173. package/lib/chevre/service/reserve/cancelReservation.js +0 -2
  174. package/lib/chevre/service/reserve/potentialActions/onReservationCanceled.d.ts +0 -3
  175. package/lib/chevre/service/reserve/potentialActions/onReservationCanceled.js +20 -20
  176. package/lib/chevre/service/reserve/potentialActions/onReservationCheckedIn.js +2 -3
  177. package/lib/chevre/service/reserve/potentialActions/onReservationConfirmed.js +2 -31
  178. package/lib/chevre/service/reserve/potentialActions/onReservationUsed.js +2 -3
  179. package/lib/chevre/service/reserve/useReservation.js +1 -8
  180. package/lib/chevre/service/task/confirmRegisterServiceTransaction.js +1 -1
  181. package/lib/chevre/service/task/returnPayTransaction.js +1 -5
  182. package/lib/chevre/service/task/voidReserveTransaction.js +3 -1
  183. package/lib/chevre/service/task.d.ts +9 -0
  184. package/lib/chevre/service/task.js +43 -4
  185. package/lib/chevre/service/transaction/moneyTransfer.js +1 -3
  186. package/lib/chevre/service/transaction/placeOrder/exportTasks/factory.js +30 -29
  187. package/lib/chevre/service/transaction/placeOrder.js +1 -5
  188. package/lib/chevre/service/transaction/placeOrderInProgress.js +2 -2
  189. package/lib/chevre/service/transaction/returnOrder.js +1 -5
  190. package/lib/chevre/service/transaction.d.ts +6 -0
  191. package/lib/chevre/service/transaction.js +9 -7
  192. package/lib/chevre/settings.d.ts +3 -2
  193. package/lib/chevre/settings.js +9 -8
  194. package/package.json +4 -4
  195. package/example/src/chevre/migrateMoneyTransferPendingTransactionIdentifier.ts +0 -96
  196. package/example/src/chevre/migrateMovieAdditionalProperties.ts +0 -98
@@ -16,7 +16,6 @@ const util = require("util");
16
16
  // tslint:disable-next-line:no-require-imports no-var-requires
17
17
  const fpe = require('node-fpe');
18
18
  const factory = require("../factory");
19
- const settings_1 = require("../settings");
20
19
  /**
21
20
  * サービスアウトプット識別子リポジトリ
22
21
  */
@@ -25,15 +24,7 @@ class RedisRepository {
25
24
  this.redisClient = redisClient;
26
25
  }
27
26
  static createKey(params) {
28
- const useNewKey = params.startDate instanceof Date
29
- && moment(params.startDate)
30
- .isSameOrAfter(settings_1.USE_NEW_ORDER_NUMBER_KEY_FROM);
31
- if (useNewKey) {
32
- return util.format('%s:%s', RedisRepository.REDIS_KEY_PREFIX_NEW, params.timestamp);
33
- }
34
- else {
35
- return util.format('%s:%s', RedisRepository.REDIS_KEY_PREFIX, params.timestamp);
36
- }
27
+ return util.format('%s:%s', RedisRepository.REDIS_KEY_PREFIX, params.timestamp);
37
28
  }
38
29
  /**
39
30
  * タイムスタンプから発行する
@@ -68,6 +59,5 @@ class RedisRepository {
68
59
  });
69
60
  }
70
61
  }
71
- RedisRepository.REDIS_KEY_PREFIX_NEW = 'serviceOutputIdentifier';
72
- RedisRepository.REDIS_KEY_PREFIX = 'chevre:serviceOutputIdentifier';
62
+ RedisRepository.REDIS_KEY_PREFIX = 'serviceOutputIdentifier';
73
63
  exports.RedisRepository = RedisRepository;
@@ -75,4 +75,5 @@ export declare class StockHolderRepository {
75
75
  startDate: Date;
76
76
  offers: IOffer[];
77
77
  }): Promise<IGetHolderResult[]>;
78
+ private checkIfConflicted;
78
79
  }
@@ -22,6 +22,12 @@ class StockHolderRepository {
22
22
  constructor(redisClient) {
23
23
  this.redisClient = redisClient;
24
24
  }
25
+ // public static GET_KEY(params: {
26
+ // eventId: string;
27
+ // startDate: Date;
28
+ // }) {
29
+ // return StockHolderRepository.createKey(params);
30
+ // }
25
31
  static offer2field(params) {
26
32
  var _a, _b;
27
33
  // 予約IDをfieldにする場合
@@ -32,9 +38,11 @@ class StockHolderRepository {
32
38
  return `${params.seatSection}:${params.seatNumber}`;
33
39
  }
34
40
  static createKey(params) {
35
- const useNewKey = params.startDate instanceof Date
36
- && moment(params.startDate)
37
- .isSameOrAfter(settings_1.USE_NEW_EVENT_AVAILABILITY_KEY_FROM);
41
+ if (!(params.startDate instanceof Date)) {
42
+ throw new factory.errors.Argument('startDate', 'must be Date');
43
+ }
44
+ const useNewKey = moment(params.startDate)
45
+ .isSameOrAfter(settings_1.USE_NEW_EVENT_AVAILABILITY_KEY_FROM);
38
46
  if (useNewKey) {
39
47
  return `${StockHolderRepository.KEY_PREFIX_NEW}:${params.eventId}`;
40
48
  }
@@ -64,7 +72,11 @@ class StockHolderRepository {
64
72
  */
65
73
  lock(lockKey) {
66
74
  return __awaiter(this, void 0, void 0, function* () {
75
+ if (!(lockKey.expires instanceof Date)) {
76
+ throw new factory.errors.Argument('expires', 'must be Date');
77
+ }
67
78
  const key = StockHolderRepository.createKey({ eventId: lockKey.eventId, startDate: lockKey.startDate });
79
+ yield this.checkIfConflicted({ key, eventId: lockKey.eventId });
68
80
  const value = lockKey.holder;
69
81
  const multi = this.redisClient.multi();
70
82
  const fields = lockKey.offers.map((offer) => StockHolderRepository.offer2field(offer));
@@ -74,7 +86,6 @@ class StockHolderRepository {
74
86
  const results = yield multi.expireAt(key, moment(lockKey.expires)
75
87
  .unix())
76
88
  .exec();
77
- debug('results:', results);
78
89
  const lockedFields = [];
79
90
  if (Array.isArray(results)) {
80
91
  results.slice(0, fields.length)
@@ -84,17 +95,25 @@ class StockHolderRepository {
84
95
  }
85
96
  });
86
97
  }
87
- debug('locked fields:', lockedFields);
88
98
  const lockedAll = lockedFields.length === fields.length;
89
99
  debug('lockedAll?', lockedAll);
90
- if (!lockedAll) {
100
+ // expireAtReplyの検証も追加する(2023-04-19~)
101
+ const expiredAll = results.slice(fields.length)
102
+ .every((r) => (r === 1 || r === true));
103
+ debug('expiredAll?', expiredAll);
104
+ if (!lockedAll || !expiredAll) {
91
105
  if (lockedFields.length > 0) {
92
106
  // 全て仮押さえできなければ仮押さえできたものは解除
93
107
  yield this.redisClient.multi()
94
108
  .hDel(key, lockedFields)
95
109
  .exec();
96
110
  }
97
- throw new factory.errors.AlreadyInUse(factory.reservationType.EventReservation, ['ticketedSeat'], 'Already hold');
111
+ if (!lockedAll) {
112
+ throw new factory.errors.AlreadyInUse(factory.reservationType.EventReservation, ['ticketedSeat'], 'Already hold');
113
+ }
114
+ else {
115
+ throw new factory.errors.ServiceUnavailable('timeout cannot be set unexpectedly');
116
+ }
98
117
  }
99
118
  });
100
119
  }
@@ -104,6 +123,7 @@ class StockHolderRepository {
104
123
  unlock(params) {
105
124
  return __awaiter(this, void 0, void 0, function* () {
106
125
  const key = StockHolderRepository.createKey({ eventId: params.eventId, startDate: params.startDate });
126
+ yield this.checkIfConflicted({ key, eventId: params.eventId });
107
127
  const field = StockHolderRepository.offer2field(params.offer);
108
128
  yield this.redisClient.multi()
109
129
  .hDel(key, field)
@@ -200,6 +220,53 @@ class StockHolderRepository {
200
220
  return result;
201
221
  });
202
222
  }
223
+ // public async migrateKey(params: {
224
+ // key: string;
225
+ // eventId: string;
226
+ // }): Promise<void> {
227
+ // // 旧キーと新キーの両方存在検証(念のため)
228
+ // const oldKey = StockHolderRepository.createKey({ eventId: params.eventId, startDate: new Date('2020-01-01T00:00:00Z') });
229
+ // const newKey = StockHolderRepository.createKey({ eventId: params.eventId, startDate: new Date('2030-01-01T00:00:00Z') });
230
+ // if (params.key !== oldKey) {
231
+ // // newの場合oldが存在するはずがない
232
+ // const existingOldKeyCount = await this.redisClient.exists(oldKey);
233
+ // debug('existingOldKeyCount:', existingOldKeyCount);
234
+ // if (existingOldKeyCount > 0) {
235
+ // await this.redisClient.del(oldKey);
236
+ // }
237
+ // }
238
+ // if (params.key !== newKey) {
239
+ // // oldの場合newが存在するはずがない
240
+ // const existingNewKeyCount = await this.redisClient.exists(newKey);
241
+ // debug('existingNewKeyCount:', existingNewKeyCount);
242
+ // if (existingNewKeyCount > 0) {
243
+ // await this.redisClient.del(newKey);
244
+ // }
245
+ // }
246
+ // }
247
+ checkIfConflicted(params) {
248
+ return __awaiter(this, void 0, void 0, function* () {
249
+ // 旧キーと新キーの両方存在検証(念のため)
250
+ const oldKey = StockHolderRepository.createKey({ eventId: params.eventId, startDate: new Date('2020-01-01T00:00:00Z') });
251
+ const newKey = StockHolderRepository.createKey({ eventId: params.eventId, startDate: new Date('2030-01-01T00:00:00Z') });
252
+ if (params.key !== oldKey) {
253
+ // newの場合oldが存在するはずがない
254
+ const existingOldKeyCount = yield this.redisClient.exists(oldKey);
255
+ debug('existingOldKeyCount:', existingOldKeyCount);
256
+ if (existingOldKeyCount > 0) {
257
+ throw new factory.errors.ServiceUnavailable('stockHolder keys conflicted');
258
+ }
259
+ }
260
+ if (params.key !== newKey) {
261
+ // oldの場合newが存在するはずがない
262
+ const existingNewKeyCount = yield this.redisClient.exists(newKey);
263
+ debug('existingNewKeyCount:', existingNewKeyCount);
264
+ if (existingNewKeyCount > 0) {
265
+ throw new factory.errors.ServiceUnavailable('stockHolder keys conflicted');
266
+ }
267
+ }
268
+ });
269
+ }
203
270
  }
204
271
  StockHolderRepository.KEY_PREFIX_NEW = 'stockHolder';
205
272
  StockHolderRepository.KEY_PREFIX = 'chevre:itemAvailability:screeningEvent';
@@ -17,6 +17,9 @@ interface IStatus {
17
17
  export interface IAggregateTask {
18
18
  statuses: IStatus[];
19
19
  }
20
+ interface IOptionOnCreate {
21
+ emitImmediately: boolean;
22
+ }
20
23
  /**
21
24
  * タスクリポジトリ
22
25
  */
@@ -24,13 +27,43 @@ export declare class MongoRepository {
24
27
  readonly taskModel: typeof Model;
25
28
  constructor(connection: Connection);
26
29
  static CREATE_MONGO_CONDITIONS(params: factory.task.ISearchConditions): any[];
27
- saveMany(taskAttributes: factory.task.IAttributes<factory.taskName>[]): Promise<{
30
+ saveMany(taskAttributes: factory.task.IAttributes<factory.taskName>[], options: IOptionOnCreate): Promise<{
28
31
  id: string;
29
32
  }[]>;
30
- createInformTaskIfNotExist(params: factory.task.IAttributes<factory.taskName.TriggerWebhook>): Promise<void>;
33
+ createInformTaskIfNotExist(params: factory.task.IAttributes<factory.taskName.TriggerWebhook>, options: IOptionOnCreate): Promise<void>;
34
+ executeById(params: {
35
+ id: string;
36
+ executor: {
37
+ name: string;
38
+ };
39
+ }): Promise<factory.task.ITask<factory.taskName> | null>;
31
40
  executeOneByName<T extends factory.taskName>(params: {
32
41
  name: T;
42
+ executor: {
43
+ name: string;
44
+ };
33
45
  }): Promise<factory.task.ITask<T> | null>;
46
+ /**
47
+ * 一定期間遅延したタスクを実行する
48
+ */
49
+ executeTasks(params: {
50
+ now: Date;
51
+ /**
52
+ * 指定期間遅延しているタスクを実行する
53
+ */
54
+ delayInSeconds: number;
55
+ limit: number;
56
+ name: {
57
+ /**
58
+ * 指定タスクのみ実行する
59
+ */
60
+ $in?: factory.taskName[];
61
+ /**
62
+ * 指定タスク以外のみ実行する
63
+ */
64
+ $nin?: factory.taskName[];
65
+ };
66
+ }): Promise<Pick<import("@chevre/factory/lib/task").ITask | import("@chevre/factory/lib/task/deleteAssetTransaction").ITask | import("@chevre/factory/lib/task/deleteOrder").ITask | import("@chevre/factory/lib/task/deleteTransaction").ITask | import("@chevre/factory/lib/task/sendEmailMessage").ITask | import("@chevre/factory/lib/task/triggerWebhook").ITask | import("@chevre/factory/lib/task/confirmMoneyTransfer").ITask | import("@chevre/factory/lib/task/confirmRegisterService").ITask | import("@chevre/factory/lib/task/confirmPayTransaction").ITask | import("@chevre/factory/lib/task/confirmRegisterServiceTransaction").ITask | import("@chevre/factory/lib/task/confirmReserveTransaction").ITask | import("@chevre/factory/lib/task/deleteMember").ITask | import("@chevre/factory/lib/task/givePointAward").ITask | import("@chevre/factory/lib/task/orderProgramMembership").ITask | import("@chevre/factory/lib/task/placeOrder").ITask | import("@chevre/factory/lib/task/returnOrder").ITask | import("@chevre/factory/lib/task/returnMoneyTransfer").ITask | import("@chevre/factory/lib/task/returnPayTransaction").ITask | import("@chevre/factory/lib/task/returnPointAward").ITask | import("@chevre/factory/lib/task/returnReserveTransaction").ITask | import("@chevre/factory/lib/task/sendOrder").ITask | import("@chevre/factory/lib/task/voidMoneyTransferTransaction").ITask | import("@chevre/factory/lib/task/voidPayTransaction").ITask | import("@chevre/factory/lib/task/voidRegisterServiceTransaction").ITask | import("@chevre/factory/lib/task/voidReserveTransaction").ITask, "id" | "name" | "status">[]>;
34
67
  retry(params: {
35
68
  intervalInMinutes: number;
36
69
  }): Promise<void>;
@@ -61,6 +94,9 @@ export declare class MongoRepository {
61
94
  deleteByName(params: {
62
95
  name: factory.taskName;
63
96
  }): Promise<import("mongodb").DeleteResult>;
97
+ countDelayedTasks(params: {
98
+ delayInSeconds: number;
99
+ }): Promise<number>;
64
100
  aggregateTask(params: {
65
101
  project?: {
66
102
  id?: {
@@ -12,8 +12,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MongoRepository = void 0;
13
13
  const moment = require("moment");
14
14
  const factory = require("../factory");
15
- const onIndexCreated_1 = require("./mongoose/onIndexCreated");
16
15
  const task_1 = require("./mongoose/schemas/task");
16
+ const task_2 = require("../eventEmitter/task");
17
17
  /**
18
18
  * タスク実行時のソート条件
19
19
  */
@@ -27,9 +27,6 @@ const sortOrder4executionOfTasks = {
27
27
  class MongoRepository {
28
28
  constructor(connection) {
29
29
  this.taskModel = connection.model(task_1.modelName, task_1.schema);
30
- if (connection.get('autoIndex') === true) {
31
- this.taskModel.on(onIndexCreated_1.INDEX_EVENT, onIndexCreated_1.onIndexCreated);
32
- }
33
30
  }
34
31
  // tslint:disable-next-line:max-func-body-length
35
32
  static CREATE_MONGO_CONDITIONS(params) {
@@ -153,27 +150,37 @@ class MongoRepository {
153
150
  }
154
151
  return andConditions;
155
152
  }
156
- saveMany(taskAttributes) {
153
+ saveMany(taskAttributes, options) {
157
154
  return __awaiter(this, void 0, void 0, function* () {
155
+ const emitImmediately = (options === null || options === void 0 ? void 0 : options.emitImmediately) === true;
158
156
  if (taskAttributes.length > 0) {
159
157
  const result = yield this.taskModel.insertMany(taskAttributes, { ordered: false, rawResult: true });
160
158
  if (result.insertedCount !== taskAttributes.length) {
161
159
  throw new factory.errors.ServiceUnavailable('all tasks not saved');
162
160
  }
163
- // return result.ops;
164
- return Object.values(result.insertedIds)
161
+ const savedTasks = Object.values(result.insertedIds)
165
162
  .map((objectId) => {
166
163
  return { id: objectId.toHexString() };
167
164
  });
165
+ if (emitImmediately) {
166
+ taskAttributes.forEach((savedTask) => {
167
+ task_2.taskEventEmitter.emitTaskStatusChanged({
168
+ name: savedTask.name,
169
+ status: factory.taskStatus.Ready
170
+ });
171
+ });
172
+ }
173
+ // return result.ops;
174
+ return savedTasks;
168
175
  }
169
176
  else {
170
177
  return [];
171
178
  }
172
179
  });
173
180
  }
174
- createInformTaskIfNotExist(params) {
181
+ createInformTaskIfNotExist(params, options) {
175
182
  return __awaiter(this, void 0, void 0, function* () {
176
- yield this.taskModel.findOneAndUpdate({
183
+ const createdTask = yield this.taskModel.findOneAndUpdate({
177
184
  'project.id': { $eq: params.project.id },
178
185
  name: params.name,
179
186
  'data.object.id': {
@@ -181,7 +188,38 @@ class MongoRepository {
181
188
  $eq: String(params.data.object.id)
182
189
  }
183
190
  }, { $setOnInsert: params }, { new: true, upsert: true })
191
+ .select({ _id: 1 })
184
192
  .exec();
193
+ if (options.emitImmediately) {
194
+ task_2.taskEventEmitter.emitTaskStatusChanged({
195
+ id: createdTask.id,
196
+ name: params.name,
197
+ status: factory.taskStatus.Ready
198
+ });
199
+ }
200
+ });
201
+ }
202
+ executeById(params) {
203
+ return __awaiter(this, void 0, void 0, function* () {
204
+ const doc = yield this.taskModel.findOneAndUpdate({
205
+ status: factory.taskStatus.Ready,
206
+ runsAt: { $lt: new Date() },
207
+ _id: { $eq: params.id }
208
+ }, {
209
+ status: factory.taskStatus.Running,
210
+ lastTriedAt: new Date(),
211
+ $inc: {
212
+ remainingNumberOfTries: -1,
213
+ numberOfTried: 1 // トライ回数増やす
214
+ },
215
+ executor: params.executor
216
+ }, { new: true })
217
+ .exec();
218
+ if (doc === null) {
219
+ // tslint:disable-next-line:no-null-keyword
220
+ return null;
221
+ }
222
+ return doc.toObject();
185
223
  });
186
224
  }
187
225
  executeOneByName(params) {
@@ -196,7 +234,8 @@ class MongoRepository {
196
234
  $inc: {
197
235
  remainingNumberOfTries: -1,
198
236
  numberOfTried: 1 // トライ回数増やす
199
- }
237
+ },
238
+ executor: params.executor
200
239
  }, { new: true })
201
240
  .sort(sortOrder4executionOfTasks)
202
241
  .exec();
@@ -207,6 +246,35 @@ class MongoRepository {
207
246
  return doc.toObject();
208
247
  });
209
248
  }
249
+ /**
250
+ * 一定期間遅延したタスクを実行する
251
+ */
252
+ executeTasks(params) {
253
+ return __awaiter(this, void 0, void 0, function* () {
254
+ const runsAtLt = moment(params.now)
255
+ .add(-params.delayInSeconds, 'seconds')
256
+ .toDate();
257
+ const delayedTasks = yield this.taskModel.find(Object.assign(Object.assign({ status: { $eq: factory.taskStatus.Ready }, runsAt: { $lt: runsAtLt } }, (Array.isArray(params.name.$in)) ? { name: { $in: params.name.$in } } : undefined), (Array.isArray(params.name.$nin)) ? { name: { $nin: params.name.$nin } } : undefined))
258
+ .select({
259
+ _id: 1,
260
+ name: 1,
261
+ status: 1
262
+ })
263
+ .limit(params.limit)
264
+ .setOptions({ maxTimeMS: 10000 })
265
+ .exec();
266
+ if (delayedTasks.length > 0) {
267
+ delayedTasks.forEach((delayedTask) => {
268
+ task_2.taskEventEmitter.emitTaskStatusChanged({
269
+ id: delayedTask.id,
270
+ name: delayedTask.name,
271
+ status: delayedTask.status
272
+ });
273
+ });
274
+ }
275
+ return delayedTasks;
276
+ });
277
+ }
210
278
  retry(params) {
211
279
  return __awaiter(this, void 0, void 0, function* () {
212
280
  const lastTriedAtShoudBeLessThan = moment()
@@ -344,6 +412,18 @@ class MongoRepository {
344
412
  .exec();
345
413
  });
346
414
  }
415
+ countDelayedTasks(params) {
416
+ return __awaiter(this, void 0, void 0, function* () {
417
+ const runsAtLt = moment()
418
+ .add(-params.delayInSeconds, 'seconds')
419
+ .toDate();
420
+ return this.taskModel.count({
421
+ status: factory.taskStatus.Ready,
422
+ runsAt: { $lt: runsAtLt }
423
+ })
424
+ .exec();
425
+ });
426
+ }
347
427
  aggregateTask(params) {
348
428
  return __awaiter(this, void 0, void 0, function* () {
349
429
  const statuses = yield Promise.all([
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoRepository = void 0;
4
- const onIndexCreated_1 = require("./mongoose/onIndexCreated");
5
4
  const telemetry_1 = require("./mongoose/schemas/telemetry");
6
5
  /**
7
6
  * 測定リポジトリ
@@ -9,9 +8,6 @@ const telemetry_1 = require("./mongoose/schemas/telemetry");
9
8
  class MongoRepository {
10
9
  constructor(connection) {
11
10
  this.telemetryModel = connection.model(telemetry_1.modelName, telemetry_1.schema);
12
- if (connection.get('autoIndex') === true) {
13
- this.telemetryModel.on(onIndexCreated_1.INDEX_EVENT, onIndexCreated_1.onIndexCreated);
14
- }
15
11
  }
16
12
  }
17
13
  exports.MongoRepository = MongoRepository;
@@ -40,6 +40,8 @@ export declare class MongoRepository {
40
40
  findById<T extends factory.transactionType>(params: {
41
41
  typeOf: T;
42
42
  id: string;
43
+ inclusion?: string[];
44
+ exclusion?: string[];
43
45
  }): Promise<factory.transaction.ITransaction<T>>;
44
46
  /**
45
47
  * 進行中の取引を取得する
@@ -111,7 +113,13 @@ export declare class MongoRepository {
111
113
  $in: T[];
112
114
  };
113
115
  status: factory.transactionStatusType;
114
- }): Promise<factory.transaction.ITransaction<T> | null>;
116
+ id?: string;
117
+ tasksExportAction: {
118
+ agent: {
119
+ name: string;
120
+ };
121
+ };
122
+ }): Promise<Pick<factory.transaction.ITransaction<T>, 'id' | 'typeOf'> | null>;
115
123
  /**
116
124
  * タスクエクスポートリトライ
117
125
  * TODO updatedAtを基準にしているが、タスクエクスポートトライ日時を持たせた方が安全か?
@@ -119,6 +127,13 @@ export declare class MongoRepository {
119
127
  reexportTasks(params: {
120
128
  intervalInMinutes: number;
121
129
  }): Promise<void>;
130
+ /**
131
+ * タスクエクスポートの遅延している取引について明示的にemitTransactionStatusChangedを実行する
132
+ */
133
+ exportTasks(params: {
134
+ now: Date;
135
+ delayInSeconds: number;
136
+ }): Promise<Pick<import("@chevre/factory/lib/transaction/moneyTransfer").ITransaction | import("@chevre/factory/lib/transaction/placeOrder").ITransaction | import("@chevre/factory/lib/transaction/returnOrder").ITransaction, "id" | "typeOf" | "status">[]>;
122
137
  /**
123
138
  * set task status exported by transaction id
124
139
  * IDでタスクをエクスポート済に変更する
@@ -150,7 +165,7 @@ export declare class MongoRepository {
150
165
  /**
151
166
  * 特定の取引を更新する(汎用)
152
167
  */
153
- findByIdAndUpdate(params: {
168
+ findByIdAndUpdateInProgress(params: {
154
169
  id: string;
155
170
  update: {
156
171
  $set?: any;