@chevre/domain 21.8.0-alpha.4 → 21.8.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 (87) hide show
  1. package/example/src/chevre/countOffers.ts +32 -0
  2. package/example/src/chevre/importOffersFromCOA.ts +7 -1
  3. package/example/src/chevre/migrateAggregateOffers.ts +75 -0
  4. package/example/src/chevre/migrateOrderPaymentMethodIdentifier.ts +81 -0
  5. package/example/src/chevre/migratePayTransactionPaymentMethodId.ts +72 -0
  6. package/example/src/chevre/migratePayTransactionPaymentMethodIdentifier.ts +78 -0
  7. package/example/src/chevre/processPay.ts +3 -4
  8. package/example/src/chevre/publishPermitOwnershipInfoToken.ts +56 -0
  9. package/example/src/chevre/pullAddOnsFromOffer.ts +26 -0
  10. package/example/src/chevre/searchOffersByCatalog.ts +9 -4
  11. package/example/src/chevre/searchOffersFromAggregateOffer.ts +167 -0
  12. package/example/src/chevre/sync2aggregateOffer.ts +27 -0
  13. package/example/src/chevre/syncAggregateOffer.ts +25 -0
  14. package/lib/chevre/repo/assetTransaction.d.ts +16 -1
  15. package/lib/chevre/repo/assetTransaction.js +54 -2
  16. package/lib/chevre/repo/mongoose/schemas/aggregateOffer.d.ts +71 -0
  17. package/lib/chevre/repo/mongoose/schemas/aggregateOffer.js +176 -0
  18. package/lib/chevre/repo/mongoose/schemas/offer.js +1 -0
  19. package/lib/chevre/repo/mongoose/schemas/order.d.ts +3 -0
  20. package/lib/chevre/repo/mongoose/schemas/order.js +1 -0
  21. package/lib/chevre/repo/mongoose/schemas/task.d.ts +3 -0
  22. package/lib/chevre/repo/mongoose/schemas/task.js +8 -0
  23. package/lib/chevre/repo/offer.d.ts +71 -14
  24. package/lib/chevre/repo/offer.js +1061 -87
  25. package/lib/chevre/repo/order.d.ts +15 -0
  26. package/lib/chevre/repo/order.js +34 -1
  27. package/lib/chevre/repo/task.d.ts +7 -1
  28. package/lib/chevre/repo/task.js +100 -0
  29. package/lib/chevre/service/aggregation/event/aggregateScreeningEvent.js +2 -2
  30. package/lib/chevre/service/aggregation/event/aggregateUseActionsOnEvent.js +2 -2
  31. package/lib/chevre/service/assetTransaction/pay/account/validation.js +2 -2
  32. package/lib/chevre/service/assetTransaction/pay/factory.js +26 -18
  33. package/lib/chevre/service/assetTransaction/pay/potentialActions.js +3 -3
  34. package/lib/chevre/service/assetTransaction/pay.js +65 -46
  35. package/lib/chevre/service/assetTransaction/refund/factory.js +8 -2
  36. package/lib/chevre/service/assetTransaction/reserve.js +105 -31
  37. package/lib/chevre/service/delivery.js +17 -0
  38. package/lib/chevre/service/offer/event/authorize.js +18 -19
  39. package/lib/chevre/service/offer/event/factory.js +8 -8
  40. package/lib/chevre/service/offer/event/importFromCOA.d.ts +2 -0
  41. package/lib/chevre/service/offer/event/searchEventTicketOffers.js +2 -2
  42. package/lib/chevre/service/offer/event/voidTransaction.js +7 -2
  43. package/lib/chevre/service/offer/product/searchProductOffers.js +1 -1
  44. package/lib/chevre/service/order/confirmPayTransaction.d.ts +0 -2
  45. package/lib/chevre/service/order/confirmPayTransaction.js +29 -40
  46. package/lib/chevre/service/order/onAssetTransactionStatusChanged.d.ts +27 -0
  47. package/lib/chevre/service/order/onAssetTransactionStatusChanged.js +226 -0
  48. package/lib/chevre/service/order/onOrderStatusChanged/factory.d.ts +8 -6
  49. package/lib/chevre/service/order/onOrderStatusChanged/factory.js +80 -57
  50. package/lib/chevre/service/order/onOrderStatusChanged.js +104 -81
  51. package/lib/chevre/service/order/payOrder.d.ts +2 -10
  52. package/lib/chevre/service/order/payOrder.js +4 -45
  53. package/lib/chevre/service/order/placeOrder.js +11 -13
  54. package/lib/chevre/service/order.d.ts +3 -1
  55. package/lib/chevre/service/order.js +6 -2
  56. package/lib/chevre/service/payment/any/factory.js +33 -8
  57. package/lib/chevre/service/payment/any.js +30 -21
  58. package/lib/chevre/service/payment/creditCard.js +12 -12
  59. package/lib/chevre/service/payment/movieTicket/validation.js +2 -2
  60. package/lib/chevre/service/payment/movieTicket.js +10 -11
  61. package/lib/chevre/service/payment/paymentCard.js +9 -12
  62. package/lib/chevre/service/project.js +1 -1
  63. package/lib/chevre/service/reserve/potentialActions/onReservationCanceled.d.ts +3 -0
  64. package/lib/chevre/service/reserve/potentialActions/onReservationCanceled.js +1 -4
  65. package/lib/chevre/service/reserve/potentialActions/onReservationCheckedIn.js +1 -5
  66. package/lib/chevre/service/reserve/potentialActions/onReservationUsed.d.ts +3 -0
  67. package/lib/chevre/service/reserve/potentialActions/onReservationUsed.js +1 -4
  68. package/lib/chevre/service/task/confirmPayTransaction.js +1 -3
  69. package/lib/chevre/service/task/importOffersFromCOA.js +3 -0
  70. package/lib/chevre/service/task/onAssetTransactionStatusChanged.d.ts +6 -0
  71. package/lib/chevre/service/task/onAssetTransactionStatusChanged.js +37 -0
  72. package/lib/chevre/service/task/onOrderPaymentCompleted.d.ts +6 -0
  73. package/lib/chevre/service/task/onOrderPaymentCompleted.js +35 -0
  74. package/lib/chevre/service/task/returnPayTransaction.js +8 -3
  75. package/lib/chevre/service/task/syncAggregateOffer.d.ts +7 -0
  76. package/lib/chevre/service/task/syncAggregateOffer.js +23 -0
  77. package/lib/chevre/service/transaction/placeOrderInProgress/result.js +32 -16
  78. package/lib/chevre/service/transaction/placeOrderInProgress/validation/validateMovieTicket.js +13 -11
  79. package/lib/chevre/service/transaction/placeOrderInProgress/validation.js +53 -17
  80. package/lib/chevre/service/transaction/placeOrderInProgress.js +4 -1
  81. package/lib/chevre/service/transaction/returnOrder/potentialActions/returnPaymentMethod.js +7 -6
  82. package/lib/chevre/service/transaction/returnOrder.js +5 -1
  83. package/lib/chevre/settings.d.ts +4 -2
  84. package/lib/chevre/settings.js +5 -5
  85. package/package.json +3 -3
  86. package/example/src/chevre/migrateAuthorizePaymentActionResult.ts +0 -83
  87. package/example/src/chevre/migrateEventOrganizer.ts +0 -154
@@ -23,20 +23,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.MongoRepository = void 0;
24
24
  const uniqid = require("uniqid");
25
25
  const factory = require("../factory");
26
+ const aggregateOffer_1 = require("./mongoose/schemas/aggregateOffer");
26
27
  const offer_1 = require("./mongoose/schemas/offer");
27
28
  const offerCatalog_1 = require("./mongoose/schemas/offerCatalog");
29
+ const task_1 = require("./mongoose/schemas/task");
30
+ const task_2 = require("../eventEmitter/task");
28
31
  const settings_1 = require("../settings");
32
+ const OFFERS_ARRAY_INDEX_NAME = 'offerIndex';
29
33
  /**
30
34
  * オファーリポジトリ
31
35
  */
32
36
  class MongoRepository {
33
37
  constructor(connection) {
38
+ this.aggregateOfferModel = connection.model(aggregateOffer_1.modelName, aggregateOffer_1.schema);
39
+ // .on('index', (error: any) => {
40
+ // if (error !== undefined) {
41
+ // // tslint:disable-next-line:no-console
42
+ // console.error('index event emitted.', error);
43
+ // }
44
+ // });
34
45
  this.offerModel = connection.model(offer_1.modelName, offer_1.schema);
35
46
  this.offerCatalogModel = connection.model(offerCatalog_1.modelName, offerCatalog_1.schema);
47
+ this.taskModel = connection.model(task_1.modelName, task_1.schema);
36
48
  }
37
49
  // tslint:disable-next-line:cyclomatic-complexity max-func-body-length
38
50
  static CREATE_OFFER_MONGO_CONDITIONS(params) {
39
- 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, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33;
51
+ 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, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36;
40
52
  // MongoDB検索条件
41
53
  const andConditions = [];
42
54
  const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
@@ -291,13 +303,16 @@ class MongoRepository {
291
303
  const addOnItemOfferedIdEq = (_29 = (_28 = (_27 = params.addOn) === null || _27 === void 0 ? void 0 : _27.itemOffered) === null || _28 === void 0 ? void 0 : _28.id) === null || _29 === void 0 ? void 0 : _29.$eq;
292
304
  if (typeof addOnItemOfferedIdEq === 'string') {
293
305
  andConditions.push({
294
- 'addOn.itemOffered.id': {
295
- $exists: true,
296
- $eq: addOnItemOfferedIdEq
297
- }
306
+ 'addOn.itemOffered.id': { $exists: true, $eq: addOnItemOfferedIdEq }
298
307
  });
299
308
  }
300
- const hasMerchantReturnPolicyIdEq = (_31 = (_30 = params.hasMerchantReturnPolicy) === null || _30 === void 0 ? void 0 : _30.id) === null || _31 === void 0 ? void 0 : _31.$eq;
309
+ const addOnItemOfferedIdIn = (_32 = (_31 = (_30 = params.addOn) === null || _30 === void 0 ? void 0 : _30.itemOffered) === null || _31 === void 0 ? void 0 : _31.id) === null || _32 === void 0 ? void 0 : _32.$in;
310
+ if (Array.isArray(addOnItemOfferedIdIn)) {
311
+ andConditions.push({
312
+ 'addOn.itemOffered.id': { $exists: true, $in: addOnItemOfferedIdIn }
313
+ });
314
+ }
315
+ const hasMerchantReturnPolicyIdEq = (_34 = (_33 = params.hasMerchantReturnPolicy) === null || _33 === void 0 ? void 0 : _33.id) === null || _34 === void 0 ? void 0 : _34.$eq;
301
316
  if (typeof hasMerchantReturnPolicyIdEq === 'string') {
302
317
  andConditions.push({
303
318
  'hasMerchantReturnPolicy.id': {
@@ -306,7 +321,7 @@ class MongoRepository {
306
321
  }
307
322
  });
308
323
  }
309
- const additionalPropertyAll = (_32 = params.additionalProperty) === null || _32 === void 0 ? void 0 : _32.$all;
324
+ const additionalPropertyAll = (_35 = params.additionalProperty) === null || _35 === void 0 ? void 0 : _35.$all;
310
325
  if (Array.isArray(additionalPropertyAll)) {
311
326
  andConditions.push({
312
327
  additionalProperty: {
@@ -315,7 +330,7 @@ class MongoRepository {
315
330
  }
316
331
  });
317
332
  }
318
- const additionalPropertyElemMatch = (_33 = params.additionalProperty) === null || _33 === void 0 ? void 0 : _33.$elemMatch;
333
+ const additionalPropertyElemMatch = (_36 = params.additionalProperty) === null || _36 === void 0 ? void 0 : _36.$elemMatch;
319
334
  if (additionalPropertyElemMatch !== undefined && additionalPropertyElemMatch !== null) {
320
335
  andConditions.push({
321
336
  additionalProperty: {
@@ -340,6 +355,436 @@ class MongoRepository {
340
355
  }
341
356
  return andConditions;
342
357
  }
358
+ // tslint:disable-next-line:cyclomatic-complexity max-func-body-length
359
+ static CREATE_AGGREGATE_OFFERS_MATCH_CONDITIONS(params) {
360
+ 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, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36;
361
+ const matchStages = [];
362
+ const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
363
+ if (typeof projectIdEq === 'string') {
364
+ matchStages.push({
365
+ $match: {
366
+ 'project.id': { $eq: projectIdEq }
367
+ }
368
+ });
369
+ }
370
+ const idEq = (_c = params.id) === null || _c === void 0 ? void 0 : _c.$eq;
371
+ if (typeof idEq === 'string') {
372
+ matchStages.push({
373
+ $match: {
374
+ 'offers.id': { $eq: idEq }
375
+ }
376
+ });
377
+ }
378
+ const idIn = (_d = params.id) === null || _d === void 0 ? void 0 : _d.$in;
379
+ if (Array.isArray(idIn)) {
380
+ matchStages.push({
381
+ $match: {
382
+ 'offers.id': {
383
+ $in: idIn
384
+ }
385
+ }
386
+ });
387
+ }
388
+ const identifierEq = (_e = params.identifier) === null || _e === void 0 ? void 0 : _e.$eq;
389
+ if (typeof identifierEq === 'string') {
390
+ matchStages.push({
391
+ $match: {
392
+ 'offers.identifier': {
393
+ $exists: true,
394
+ $eq: identifierEq
395
+ }
396
+ }
397
+ });
398
+ }
399
+ const identifierIn = (_f = params.identifier) === null || _f === void 0 ? void 0 : _f.$in;
400
+ if (Array.isArray(identifierIn)) {
401
+ matchStages.push({
402
+ $match: {
403
+ 'offers.identifier': {
404
+ $exists: true,
405
+ $in: identifierIn
406
+ }
407
+ }
408
+ });
409
+ }
410
+ const identifierRegex = (_g = params.identifier) === null || _g === void 0 ? void 0 : _g.$regex;
411
+ if (typeof identifierRegex === 'string' && identifierRegex.length > 0) {
412
+ matchStages.push({
413
+ $match: {
414
+ 'offers.identifier': {
415
+ $exists: true,
416
+ $regex: new RegExp(identifierRegex)
417
+ }
418
+ }
419
+ });
420
+ }
421
+ const nameRegex = (_h = params.name) === null || _h === void 0 ? void 0 : _h.$regex;
422
+ if (typeof nameRegex === 'string' && nameRegex.length > 0) {
423
+ const nameRegexExp = new RegExp(nameRegex);
424
+ matchStages.push({
425
+ $match: {
426
+ $or: [
427
+ {
428
+ 'offers.name.ja': {
429
+ $exists: true,
430
+ $regex: nameRegexExp
431
+ }
432
+ },
433
+ {
434
+ 'offers.name.en': {
435
+ $exists: true,
436
+ $regex: nameRegexExp
437
+ }
438
+ },
439
+ {
440
+ 'offers.alternateName.ja': {
441
+ $exists: true,
442
+ $regex: nameRegexExp
443
+ }
444
+ },
445
+ {
446
+ 'offers.alternateName.en': {
447
+ $exists: true,
448
+ $regex: nameRegexExp
449
+ }
450
+ }
451
+ ]
452
+ }
453
+ });
454
+ }
455
+ const itemOfferedTypeOfEq = (_k = (_j = params.itemOffered) === null || _j === void 0 ? void 0 : _j.typeOf) === null || _k === void 0 ? void 0 : _k.$eq;
456
+ if (typeof itemOfferedTypeOfEq === 'string') {
457
+ matchStages.push({
458
+ $match: {
459
+ 'offers.itemOffered.typeOf': {
460
+ $exists: true,
461
+ $eq: itemOfferedTypeOfEq
462
+ }
463
+ }
464
+ });
465
+ }
466
+ const categoryCodeValueIn = (_m = (_l = params.category) === null || _l === void 0 ? void 0 : _l.codeValue) === null || _m === void 0 ? void 0 : _m.$in;
467
+ if (Array.isArray(categoryCodeValueIn)) {
468
+ matchStages.push({
469
+ $match: {
470
+ 'offers.category.codeValue': {
471
+ $exists: true,
472
+ $in: categoryCodeValueIn
473
+ }
474
+ }
475
+ });
476
+ }
477
+ const eligibleMembershipTypeCodeValueEq = (_p = (_o = params.eligibleMembershipType) === null || _o === void 0 ? void 0 : _o.codeValue) === null || _p === void 0 ? void 0 : _p.$eq;
478
+ if (typeof eligibleMembershipTypeCodeValueEq === 'string') {
479
+ matchStages.push({
480
+ $match: {
481
+ 'offers.eligibleMembershipType.codeValue': {
482
+ $exists: true,
483
+ $eq: eligibleMembershipTypeCodeValueEq
484
+ }
485
+ }
486
+ });
487
+ }
488
+ const eligibleMonetaryAmountCurrencyEq = (_r = (_q = params.eligibleMonetaryAmount) === null || _q === void 0 ? void 0 : _q.currency) === null || _r === void 0 ? void 0 : _r.$eq;
489
+ if (typeof eligibleMonetaryAmountCurrencyEq === 'string') {
490
+ matchStages.push({
491
+ $match: {
492
+ 'offers.eligibleMonetaryAmount.currency': {
493
+ $exists: true,
494
+ $eq: eligibleMonetaryAmountCurrencyEq
495
+ }
496
+ }
497
+ });
498
+ }
499
+ const eligibleSeatingTypeCodeValueEq = (_t = (_s = params.eligibleSeatingType) === null || _s === void 0 ? void 0 : _s.codeValue) === null || _t === void 0 ? void 0 : _t.$eq;
500
+ if (typeof eligibleSeatingTypeCodeValueEq === 'string') {
501
+ matchStages.push({
502
+ $match: {
503
+ 'offers.eligibleSeatingType.codeValue': {
504
+ $exists: true,
505
+ $eq: eligibleSeatingTypeCodeValueEq
506
+ }
507
+ }
508
+ });
509
+ }
510
+ const appliesToMovieTicketServiceTypeExist = (_w = (_v = (_u = params.priceSpecification) === null || _u === void 0 ? void 0 : _u.appliesToMovieTicket) === null || _v === void 0 ? void 0 : _v.serviceType) === null || _w === void 0 ? void 0 : _w.$exists;
511
+ if (typeof appliesToMovieTicketServiceTypeExist === 'boolean') {
512
+ matchStages.push({
513
+ $match: {
514
+ 'offers.priceSpecification.appliesToMovieTicket.serviceType': {
515
+ $exists: appliesToMovieTicketServiceTypeExist
516
+ }
517
+ }
518
+ });
519
+ }
520
+ const appliesToMovieTicketServiceTypeEq = (_z = (_y = (_x = params.priceSpecification) === null || _x === void 0 ? void 0 : _x.appliesToMovieTicket) === null || _y === void 0 ? void 0 : _y.serviceType) === null || _z === void 0 ? void 0 : _z.$eq;
521
+ if (typeof appliesToMovieTicketServiceTypeEq === 'string') {
522
+ matchStages.push({
523
+ $match: {
524
+ 'offers.priceSpecification.appliesToMovieTicket.serviceType': {
525
+ $exists: true,
526
+ $eq: appliesToMovieTicketServiceTypeEq
527
+ }
528
+ }
529
+ });
530
+ }
531
+ const appliesToMovieTicketServiceOutputTypeOfEq = (_3 = (_2 = (_1 = (_0 = params.priceSpecification) === null || _0 === void 0 ? void 0 : _0.appliesToMovieTicket) === null || _1 === void 0 ? void 0 : _1.serviceOutput) === null || _2 === void 0 ? void 0 : _2.typeOf) === null || _3 === void 0 ? void 0 : _3.$eq;
532
+ if (typeof appliesToMovieTicketServiceOutputTypeOfEq === 'string') {
533
+ matchStages.push({
534
+ $match: {
535
+ 'offers.priceSpecification.appliesToMovieTicket.serviceOutput.typeOf': {
536
+ $exists: true,
537
+ $eq: appliesToMovieTicketServiceOutputTypeOfEq
538
+ }
539
+ }
540
+ });
541
+ }
542
+ const appliesToMovieTicketServiceOutputTypeOfNin = (_7 = (_6 = (_5 = (_4 = params.priceSpecification) === null || _4 === void 0 ? void 0 : _4.appliesToMovieTicket) === null || _5 === void 0 ? void 0 : _5.serviceOutput) === null || _6 === void 0 ? void 0 : _6.typeOf) === null || _7 === void 0 ? void 0 : _7.$nin;
543
+ if (Array.isArray(appliesToMovieTicketServiceOutputTypeOfNin)) {
544
+ matchStages.push({
545
+ $match: {
546
+ 'offers.priceSpecification.appliesToMovieTicket.serviceOutput.typeOf': {
547
+ $nin: appliesToMovieTicketServiceOutputTypeOfNin
548
+ }
549
+ }
550
+ });
551
+ }
552
+ if (params.priceSpecification !== undefined && params.priceSpecification !== null) {
553
+ const priceSpecificationPriceGte = (_8 = params.priceSpecification.price) === null || _8 === void 0 ? void 0 : _8.$gte;
554
+ if (typeof priceSpecificationPriceGte === 'number') {
555
+ matchStages.push({
556
+ $match: {
557
+ 'offers.priceSpecification.price': {
558
+ $exists: true,
559
+ $gte: priceSpecificationPriceGte
560
+ }
561
+ }
562
+ });
563
+ }
564
+ const priceSpecificationPriceLte = (_9 = params.priceSpecification.price) === null || _9 === void 0 ? void 0 : _9.$lte;
565
+ if (typeof priceSpecificationPriceLte === 'number') {
566
+ matchStages.push({
567
+ $match: {
568
+ 'offers.priceSpecification.price': {
569
+ $exists: true,
570
+ $lte: priceSpecificationPriceLte
571
+ }
572
+ }
573
+ });
574
+ }
575
+ const accountsReceivableGte = (_11 = (_10 = params.priceSpecification.accounting) === null || _10 === void 0 ? void 0 : _10.accountsReceivable) === null || _11 === void 0 ? void 0 : _11.$gte;
576
+ if (typeof accountsReceivableGte === 'number') {
577
+ matchStages.push({
578
+ $match: {
579
+ 'offers.priceSpecification.accounting.accountsReceivable': {
580
+ $exists: true,
581
+ $gte: accountsReceivableGte
582
+ }
583
+ }
584
+ });
585
+ }
586
+ const accountsReceivableLte = (_13 = (_12 = params.priceSpecification.accounting) === null || _12 === void 0 ? void 0 : _12.accountsReceivable) === null || _13 === void 0 ? void 0 : _13.$lte;
587
+ if (typeof accountsReceivableLte === 'number') {
588
+ matchStages.push({
589
+ $match: {
590
+ 'offers.priceSpecification.accounting.accountsReceivable': {
591
+ $exists: true,
592
+ $lte: accountsReceivableLte
593
+ }
594
+ }
595
+ });
596
+ }
597
+ const accountingCodeValueEq = (_16 = (_15 = (_14 = params.priceSpecification.accounting) === null || _14 === void 0 ? void 0 : _14.operatingRevenue) === null || _15 === void 0 ? void 0 : _15.codeValue) === null || _16 === void 0 ? void 0 : _16.$eq;
598
+ if (typeof accountingCodeValueEq === 'string') {
599
+ matchStages.push({
600
+ $match: {
601
+ 'offers.priceSpecification.accounting.operatingRevenue.codeValue': {
602
+ $exists: true,
603
+ $eq: accountingCodeValueEq
604
+ }
605
+ }
606
+ });
607
+ }
608
+ const accountingCodeValueIn = (_19 = (_18 = (_17 = params.priceSpecification.accounting) === null || _17 === void 0 ? void 0 : _17.operatingRevenue) === null || _18 === void 0 ? void 0 : _18.codeValue) === null || _19 === void 0 ? void 0 : _19.$in;
609
+ if (Array.isArray(accountingCodeValueIn)) {
610
+ matchStages.push({
611
+ $match: {
612
+ 'offers.priceSpecification.accounting.operatingRevenue.codeValue': {
613
+ $exists: true,
614
+ $in: accountingCodeValueIn
615
+ }
616
+ }
617
+ });
618
+ }
619
+ const referenceQuantityValueEq = (_21 = (_20 = params.priceSpecification.referenceQuantity) === null || _20 === void 0 ? void 0 : _20.value) === null || _21 === void 0 ? void 0 : _21.$eq;
620
+ if (typeof referenceQuantityValueEq === 'number') {
621
+ matchStages.push({
622
+ $match: {
623
+ 'offers.priceSpecification.referenceQuantity.value': {
624
+ $exists: true,
625
+ $eq: referenceQuantityValueEq
626
+ }
627
+ }
628
+ });
629
+ }
630
+ }
631
+ const availabilityEq = (_22 = params.availability) === null || _22 === void 0 ? void 0 : _22.$eq;
632
+ if (typeof availabilityEq === 'string') {
633
+ matchStages.push({
634
+ $match: {
635
+ 'offers.availability': { $eq: availabilityEq }
636
+ }
637
+ });
638
+ }
639
+ const availableAtOrFromIdEq = (_24 = (_23 = params.availableAtOrFrom) === null || _23 === void 0 ? void 0 : _23.id) === null || _24 === void 0 ? void 0 : _24.$eq;
640
+ if (typeof availableAtOrFromIdEq === 'string') {
641
+ matchStages.push({
642
+ $match: {
643
+ 'offers.availableAtOrFrom.id': {
644
+ $exists: true,
645
+ $eq: availableAtOrFromIdEq
646
+ }
647
+ }
648
+ });
649
+ }
650
+ const availableAtOrFromIdIn = (_26 = (_25 = params.availableAtOrFrom) === null || _25 === void 0 ? void 0 : _25.id) === null || _26 === void 0 ? void 0 : _26.$in;
651
+ if (Array.isArray(availableAtOrFromIdIn)) {
652
+ matchStages.push({
653
+ $match: {
654
+ 'offers.availableAtOrFrom.id': {
655
+ $exists: true,
656
+ $in: availableAtOrFromIdIn
657
+ }
658
+ }
659
+ });
660
+ }
661
+ const addOnItemOfferedIdEq = (_29 = (_28 = (_27 = params.addOn) === null || _27 === void 0 ? void 0 : _27.itemOffered) === null || _28 === void 0 ? void 0 : _28.id) === null || _29 === void 0 ? void 0 : _29.$eq;
662
+ if (typeof addOnItemOfferedIdEq === 'string') {
663
+ matchStages.push({
664
+ $match: {
665
+ 'offers.addOn.itemOffered.id': { $exists: true, $eq: addOnItemOfferedIdEq }
666
+ }
667
+ });
668
+ }
669
+ const addOnItemOfferedIdIn = (_32 = (_31 = (_30 = params.addOn) === null || _30 === void 0 ? void 0 : _30.itemOffered) === null || _31 === void 0 ? void 0 : _31.id) === null || _32 === void 0 ? void 0 : _32.$in;
670
+ if (Array.isArray(addOnItemOfferedIdIn)) {
671
+ matchStages.push({
672
+ $match: {
673
+ 'offers.addOn.itemOffered.id': { $exists: true, $in: addOnItemOfferedIdIn }
674
+ }
675
+ });
676
+ }
677
+ const hasMerchantReturnPolicyIdEq = (_34 = (_33 = params.hasMerchantReturnPolicy) === null || _33 === void 0 ? void 0 : _33.id) === null || _34 === void 0 ? void 0 : _34.$eq;
678
+ if (typeof hasMerchantReturnPolicyIdEq === 'string') {
679
+ matchStages.push({
680
+ $match: {
681
+ 'offers.hasMerchantReturnPolicy.id': {
682
+ $exists: true,
683
+ $eq: hasMerchantReturnPolicyIdEq
684
+ }
685
+ }
686
+ });
687
+ }
688
+ const additionalPropertyAll = (_35 = params.additionalProperty) === null || _35 === void 0 ? void 0 : _35.$all;
689
+ if (Array.isArray(additionalPropertyAll)) {
690
+ matchStages.push({
691
+ $match: {
692
+ 'offers.additionalProperty': {
693
+ $exists: true,
694
+ $all: additionalPropertyAll
695
+ }
696
+ }
697
+ });
698
+ }
699
+ const additionalPropertyElemMatch = (_36 = params.additionalProperty) === null || _36 === void 0 ? void 0 : _36.$elemMatch;
700
+ if (additionalPropertyElemMatch !== undefined && additionalPropertyElemMatch !== null) {
701
+ matchStages.push({
702
+ $match: {
703
+ 'offers.additionalProperty': {
704
+ $exists: true,
705
+ $elemMatch: additionalPropertyElemMatch
706
+ }
707
+ }
708
+ });
709
+ }
710
+ if (params.onlyValid === true) {
711
+ const now = new Date();
712
+ matchStages.push({
713
+ $match: {
714
+ $or: [
715
+ { 'offers.validFrom': { $exists: false } },
716
+ { 'offers.validFrom': { $exists: true, $lte: now } }
717
+ ]
718
+ }
719
+ }, {
720
+ $match: {
721
+ $or: [
722
+ { 'offers.validThrough': { $exists: false } },
723
+ { 'offers.validThrough': { $exists: true, $gte: now } }
724
+ ]
725
+ }
726
+ });
727
+ }
728
+ return matchStages;
729
+ }
730
+ static CREATE_AGGREGATE_OFFERS_PROJECTION(params) {
731
+ let projectStage = {
732
+ _id: 0,
733
+ offerIndex: `$${OFFERS_ARRAY_INDEX_NAME}`,
734
+ typeOf: '$offers.typeOf',
735
+ project: '$project',
736
+ id: '$offers.id',
737
+ identifier: '$offers.identifier',
738
+ name: '$offers.name',
739
+ description: '$offers.description',
740
+ category: '$offers.category',
741
+ color: '$offers.color',
742
+ additionalProperty: '$offers.additionalProperty',
743
+ advanceBookingRequirement: '$offers.advanceBookingRequirement',
744
+ alternateName: '$offers.alternateName',
745
+ addOn: '$offers.addOn',
746
+ availability: '$offers.availability',
747
+ availableAtOrFrom: '$offers.availableAtOrFrom',
748
+ hasMerchantReturnPolicy: '$offers.hasMerchantReturnPolicy',
749
+ itemOffered: '$offers.itemOffered',
750
+ priceCurrency: '$offers.priceCurrency',
751
+ priceSpecification: '$offers.priceSpecification',
752
+ eligibleCustomerType: '$offers.eligibleCustomerType',
753
+ eligibleDuration: '$offers.eligibleDuration',
754
+ eligibleMembershipType: '$offers.eligibleMembershipType',
755
+ eligibleMonetaryAmount: '$offers.eligibleMonetaryAmount',
756
+ eligibleQuantity: '$offers.eligibleQuantity',
757
+ eligibleRegion: '$offers.eligibleRegion',
758
+ eligibleSeatingType: '$offers.eligibleSeatingType',
759
+ eligibleSubReservation: '$offers.eligibleSubReservation',
760
+ settings: '$offers.settings',
761
+ validFrom: '$offers.validFrom',
762
+ validThrough: '$offers.validThrough',
763
+ validRateLimit: '$offers.validRateLimit'
764
+ };
765
+ const positiveProjectionFields = Object.keys(params)
766
+ .filter((key) => params[key] !== 0);
767
+ const negativeProjectionFields = Object.keys(params)
768
+ .filter((key) => params[key] === 0);
769
+ if (positiveProjectionFields.length > 0) {
770
+ projectStage = {
771
+ _id: 0,
772
+ offerIndex: `$${OFFERS_ARRAY_INDEX_NAME}`
773
+ };
774
+ positiveProjectionFields.forEach((field) => {
775
+ projectStage[field] = `$offers.${field}`;
776
+ });
777
+ }
778
+ else if (negativeProjectionFields.length > 0) {
779
+ negativeProjectionFields.forEach((field) => {
780
+ if (typeof projectStage[field] === 'string') {
781
+ // tslint:disable-next-line:no-dynamic-delete
782
+ delete projectStage[field];
783
+ }
784
+ });
785
+ }
786
+ return projectStage;
787
+ }
343
788
  /**
344
789
  * カタログに含まれるオファーを検索する
345
790
  * カタログに登録されたオファーの順序は保証される
@@ -385,42 +830,92 @@ class MongoRepository {
385
830
  }
386
831
  : undefined)
387
832
  }, onlyValid: params.onlyValid === true }), (typeof params.limit === 'number' && typeof params.page === 'number')
388
- ? { sort: { _id: factory.sortType.Ascending } }
833
+ // 明示的なソート指定を調整(決して重複しない属性が相応)(2023-09-07~)
834
+ ? { sort: { identifier: factory.sortType.Ascending } }
389
835
  : undefined), (typeof params.limit === 'number') ? { limit: params.limit } : undefined), (typeof params.page === 'number') ? { page: params.page } : undefined);
390
- offers = yield this.search(searchOffersConditions, params.projection);
391
- if (params.sort) {
392
- // sorting
393
- offers = offers.sort((a, b) => sortedOfferIds.indexOf(String(a.id)) - sortedOfferIds.indexOf(String(b.id)));
394
- }
836
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY_ON_SEARCH_BY_CATALOG対応(2023-09-06~)
837
+ const useOffersAsPrimary = (params.useOffersAsPrimary === true)
838
+ ? true
839
+ : !settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY_ON_SEARCH_BY_CATALOG;
840
+ offers = yield this.search(searchOffersConditions, params.projection, useOffersAsPrimary);
841
+ // 完全廃止(基本的にsortedOfferIdsと合わせて利用する想定)(2023-09-04~)
842
+ // if (params.sort) {
843
+ // // sorting
844
+ // offers = offers.sort((a, b) => sortedOfferIds.indexOf(String(a.id)) - sortedOfferIds.indexOf(String(b.id)));
845
+ // }
395
846
  }
396
847
  return { offers, sortedOfferIds };
397
848
  });
398
849
  }
399
850
  findById(params) {
400
851
  return __awaiter(this, void 0, void 0, function* () {
401
- const doc = yield this.offerModel.findOne({ _id: params.id }, {
852
+ const offers = yield this.search({
853
+ limit: 1,
854
+ page: 1,
855
+ project: { id: { $eq: params.project.id } },
856
+ id: { $eq: params.id }
857
+ });
858
+ const unitPriceOffer = offers.shift();
859
+ if (unitPriceOffer === undefined) {
860
+ throw new factory.errors.NotFound(factory.offerType.Offer);
861
+ }
862
+ return unitPriceOffer;
863
+ });
864
+ }
865
+ findAggregateOfferById(params) {
866
+ return __awaiter(this, void 0, void 0, function* () {
867
+ const doc = yield this.aggregateOfferModel.findOne({
868
+ _id: { $eq: params.id },
869
+ 'project.id': { $eq: params.project.id }
870
+ }, {
402
871
  __v: 0,
403
872
  createdAt: 0,
404
873
  updatedAt: 0
405
874
  })
406
875
  .exec();
407
876
  if (doc === null) {
408
- throw new factory.errors.NotFound(this.offerModel.modelName);
877
+ throw new factory.errors.NotFound(this.aggregateOfferModel.modelName);
409
878
  }
410
879
  return doc.toObject();
411
880
  });
412
881
  }
413
882
  count(params) {
414
883
  return __awaiter(this, void 0, void 0, function* () {
415
- const conditions = MongoRepository.CREATE_OFFER_MONGO_CONDITIONS(params);
416
- return this.offerModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {})
417
- .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
418
- .exec();
884
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-05~)
885
+ if (settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY) {
886
+ const matchStages = MongoRepository.CREATE_AGGREGATE_OFFERS_MATCH_CONDITIONS(params);
887
+ const [{ offerCount }] = yield this.aggregateOfferModel.aggregate([
888
+ {
889
+ $unwind: { path: '$offers' }
890
+ },
891
+ ...matchStages,
892
+ {
893
+ $count: 'offerCount'
894
+ }
895
+ ])
896
+ .exec();
897
+ return offerCount;
898
+ }
899
+ else {
900
+ const conditions = MongoRepository.CREATE_OFFER_MONGO_CONDITIONS(params);
901
+ return this.offerModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {})
902
+ .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
903
+ .exec();
904
+ }
419
905
  });
420
906
  }
421
- search(params, projection) {
422
- var _a;
907
+ search(params, projection,
908
+ /**
909
+ * 強制的にoffersコレクションを使用する
910
+ */
911
+ useOffersAsPrimary) {
912
+ var _a, _b;
423
913
  return __awaiter(this, void 0, void 0, function* () {
914
+ // primaryコレクションをコントロール(2023-09-05~)
915
+ const useAggregateOfferAsPrimary = settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY && (useOffersAsPrimary !== true);
916
+ if (useAggregateOfferAsPrimary) {
917
+ return this.searchFromAggregateOffer(params, projection);
918
+ }
424
919
  const conditions = MongoRepository.CREATE_OFFER_MONGO_CONDITIONS(params);
425
920
  const positiveProjectionExists = (projection !== undefined && projection !== null)
426
921
  ? Object.values(projection)
@@ -428,73 +923,224 @@ class MongoRepository {
428
923
  : false;
429
924
  const query = this.offerModel.find((conditions.length > 0) ? { $and: conditions } : {}, (positiveProjectionExists)
430
925
  ? projection
431
- : Object.assign(Object.assign({}, projection), { __v: 0, createdAt: 0, updatedAt: 0 }));
926
+ : Object.assign(Object.assign({}, projection), { __v: 0, createdAt: 0, updatedAt: 0, availableAddOn: 0 // 廃止属性の残存が確認されたので(2023-09-04~)
927
+ }));
432
928
  if (typeof params.limit === 'number' && params.limit > 0) {
433
929
  const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
434
930
  query.limit(params.limit)
435
931
  .skip(params.limit * (page - 1));
436
932
  }
437
- // tslint:disable-next-line:no-single-line-block-comment
438
- /* istanbul ignore else */
439
- if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a['priceSpecification.price']) !== undefined) {
440
- query.sort({ 'priceSpecification.price': params.sort['priceSpecification.price'] });
933
+ const sortByPrice = (_a = params.sort) === null || _a === void 0 ? void 0 : _a['priceSpecification.price'];
934
+ const sortByIdentifier = (_b = params.sort) === null || _b === void 0 ? void 0 : _b.identifier;
935
+ if (typeof sortByPrice === 'number' || typeof sortByIdentifier === 'number') {
936
+ query.sort(Object.assign(Object.assign({}, (typeof sortByPrice === 'number')
937
+ ? { 'priceSpecification.price': sortByPrice }
938
+ : undefined), (typeof sortByIdentifier === 'number')
939
+ ? { identifier: sortByIdentifier }
940
+ : undefined));
441
941
  }
442
942
  return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
443
943
  .exec()
444
944
  .then((docs) => docs.map((doc) => doc.toObject()));
445
945
  });
446
946
  }
947
+ // tslint:disable-next-line:max-func-body-length
447
948
  save(params) {
448
949
  return __awaiter(this, void 0, void 0, function* () {
449
950
  let doc;
450
- if (params.id === '') {
451
- const id = uniqid();
452
- doc = yield this.offerModel.create(Object.assign(Object.assign({}, params), { _id: id }));
951
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-05~)
952
+ if (settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY) {
953
+ let savedUnitPriceOffer;
954
+ if (params.id === '') {
955
+ const id = uniqid();
956
+ savedUnitPriceOffer = Object.assign(Object.assign({}, params), { id });
957
+ const aggregateOffer = {
958
+ typeOf: factory.offerType.AggregateOffer,
959
+ project: params.project,
960
+ offers: [savedUnitPriceOffer]
961
+ };
962
+ doc = yield this.aggregateOfferModel.create(Object.assign(Object.assign({}, aggregateOffer), { _id: id }));
963
+ }
964
+ else {
965
+ const unitPriceOffers = yield this.searchFromAggregateOffer({
966
+ limit: 1,
967
+ page: 1,
968
+ project: { id: { $eq: params.project.id } },
969
+ id: { $eq: params.id }
970
+ }, { id: 1, identifier: 1, project: 1, typeOf: 1 });
971
+ const originalUnitPriceOffer = unitPriceOffers.shift();
972
+ if (originalUnitPriceOffer === undefined) {
973
+ throw new factory.errors.NotFound(factory.offerType.Offer);
974
+ }
975
+ // 上書き禁止属性を除外
976
+ const { id, identifier, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "project", "typeOf"]);
977
+ savedUnitPriceOffer = Object.assign(Object.assign({}, updateFields), { id: originalUnitPriceOffer.id, identifier: originalUnitPriceOffer.identifier, project: originalUnitPriceOffer.project, typeOf: originalUnitPriceOffer.typeOf });
978
+ doc = yield this.aggregateOfferModel.findOneAndUpdate({ 'offers.id': params.id }, {
979
+ $set: {
980
+ 'offers.$[offer]': savedUnitPriceOffer
981
+ }
982
+ }, {
983
+ upsert: false,
984
+ new: true,
985
+ arrayFilters: [
986
+ { 'offer.id': { $eq: params.id } }
987
+ ]
988
+ })
989
+ .exec();
990
+ }
991
+ if (doc === null) {
992
+ throw new factory.errors.NotFound(this.aggregateOfferModel.modelName);
993
+ }
994
+ // 同期タスク作成(2023-09-05~)
995
+ if (typeof savedUnitPriceOffer.id === 'string') {
996
+ yield this.saveSyncTask({
997
+ project: { id: savedUnitPriceOffer.project.id },
998
+ id: { $in: [savedUnitPriceOffer.id] },
999
+ identifier: { $in: [] },
1000
+ isDeleted: false,
1001
+ typeOf: factory.offerType.AggregateOffer,
1002
+ options: { emitImmediately: true }
1003
+ });
1004
+ }
1005
+ return savedUnitPriceOffer;
453
1006
  }
454
1007
  else {
455
- // 上書き禁止属性を除外(2022-08-24~)
456
- const { id, identifier, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "project", "typeOf"]);
457
- doc = yield this.offerModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true })
458
- .exec();
459
- }
460
- if (doc === null) {
461
- throw new factory.errors.NotFound(this.offerModel.modelName);
1008
+ if (params.id === '') {
1009
+ const id = uniqid();
1010
+ doc = yield this.offerModel.create(Object.assign(Object.assign({}, params), { _id: id }));
1011
+ }
1012
+ else {
1013
+ // 上書き禁止属性を除外(2022-08-24~)
1014
+ const { id, identifier, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "project", "typeOf"]);
1015
+ doc = yield this.offerModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true })
1016
+ .exec();
1017
+ }
1018
+ if (doc === null) {
1019
+ throw new factory.errors.NotFound(factory.offerType.Offer);
1020
+ }
1021
+ // 同期タスク作成(2023-09-03~)
1022
+ if (typeof doc._id === 'string') {
1023
+ yield this.saveSyncTask({
1024
+ project: { id: params.project.id },
1025
+ id: { $in: [doc._id] },
1026
+ identifier: { $in: [] },
1027
+ isDeleted: false,
1028
+ typeOf: factory.offerType.Offer,
1029
+ options: { emitImmediately: true }
1030
+ });
1031
+ }
1032
+ return doc.toObject();
462
1033
  }
463
- return doc.toObject();
464
1034
  });
465
1035
  }
1036
+ /**
1037
+ * sskts専用オファー保管
1038
+ */
1039
+ // tslint:disable-next-line:max-func-body-length
466
1040
  saveManyByIdentifier(params) {
467
1041
  return __awaiter(this, void 0, void 0, function* () {
468
- const bulkWriteOps = [];
469
- if (Array.isArray(params)) {
470
- params.forEach((p) => {
471
- const id = uniqid();
472
- const $set = Object.assign({}, p.attributes);
473
- if (typeof $set.id === 'string') {
474
- delete $set.id;
475
- }
476
- bulkWriteOps.push({
477
- updateOne: {
478
- filter: {
479
- 'project.id': {
480
- $eq: p.attributes.project.id
1042
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-06~)
1043
+ if (settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY) {
1044
+ const insertBulkWriteOps = [];
1045
+ const updateBulkWriteOps = [];
1046
+ if (Array.isArray(params)) {
1047
+ params.forEach((p) => {
1048
+ const newOfferId = uniqid();
1049
+ const unitPriceOffer = Object.assign(Object.assign({}, p.attributes), { id: newOfferId });
1050
+ const aggregateOffer = {
1051
+ project: p.attributes.project,
1052
+ typeOf: factory.offerType.AggregateOffer,
1053
+ offers: [unitPriceOffer]
1054
+ };
1055
+ insertBulkWriteOps.push({
1056
+ updateOne: {
1057
+ filter: {
1058
+ 'project.id': { $eq: p.attributes.project.id },
1059
+ 'offers.identifier': { $exists: true, $eq: p.attributes.identifier }
481
1060
  },
482
- identifier: {
483
- $exists: true,
484
- $eq: p.attributes.identifier
485
- }
486
- },
487
- update: {
488
- $set: $set,
489
- $setOnInsert: { _id: id }
490
- },
491
- upsert: (p.upsert !== undefined) ? p.upsert : false
492
- }
1061
+ update: {
1062
+ $setOnInsert: Object.assign(Object.assign({}, aggregateOffer), { _id: newOfferId })
1063
+ },
1064
+ upsert: true
1065
+ }
1066
+ });
1067
+ const { id } = unitPriceOffer, $set = __rest(unitPriceOffer, ["id"]);
1068
+ updateBulkWriteOps.push({
1069
+ updateOne: {
1070
+ filter: {
1071
+ 'project.id': { $eq: unitPriceOffer.project.id },
1072
+ 'offers.identifier': { $exists: true, $eq: unitPriceOffer.identifier }
1073
+ },
1074
+ update: {
1075
+ // 特定の属性のみ更新する(IDを上書きできないので)
1076
+ $set: Object.assign(Object.assign({ 'offers.0.name': $set.name, 'offers.0.description': $set.description, 'offers.0.alternateName': $set.alternateName, 'offers.0.priceSpecification': $set.priceSpecification, 'offers.0.additionalProperty': $set.additionalProperty }, (Array.isArray($set.eligibleMembershipType))
1077
+ ? { 'offers.0.eligibleMembershipType': $set.eligibleMembershipType }
1078
+ : undefined), (Array.isArray($set.eligibleMonetaryAmount))
1079
+ ? { 'offers.0.eligibleMonetaryAmount': $set.eligibleMonetaryAmount }
1080
+ : undefined)
1081
+ },
1082
+ upsert: false
1083
+ }
1084
+ });
493
1085
  });
494
- });
1086
+ }
1087
+ if (insertBulkWriteOps.length > 0) {
1088
+ yield this.aggregateOfferModel.bulkWrite(insertBulkWriteOps, { ordered: false });
1089
+ yield this.aggregateOfferModel.bulkWrite(updateBulkWriteOps, { ordered: false });
1090
+ // 同期タスク作成(2023-09-03~)
1091
+ yield this.saveSyncTask({
1092
+ project: { id: params[0].attributes.project.id },
1093
+ id: { $in: [] },
1094
+ identifier: { $in: params.map((savingOffer) => savingOffer.attributes.identifier) },
1095
+ isDeleted: false,
1096
+ typeOf: factory.offerType.AggregateOffer,
1097
+ options: { emitImmediately: true }
1098
+ });
1099
+ }
495
1100
  }
496
- if (bulkWriteOps.length > 0) {
497
- yield this.offerModel.bulkWrite(bulkWriteOps, { ordered: false });
1101
+ else {
1102
+ const bulkWriteOps = [];
1103
+ if (Array.isArray(params)) {
1104
+ params.forEach((p) => {
1105
+ const id = uniqid();
1106
+ const $set = Object.assign({}, p.attributes);
1107
+ if (typeof $set.id === 'string') {
1108
+ delete $set.id;
1109
+ }
1110
+ bulkWriteOps.push({
1111
+ updateOne: {
1112
+ filter: {
1113
+ 'project.id': {
1114
+ $eq: p.attributes.project.id
1115
+ },
1116
+ identifier: {
1117
+ $exists: true,
1118
+ $eq: p.attributes.identifier
1119
+ }
1120
+ },
1121
+ update: {
1122
+ $set: $set,
1123
+ $setOnInsert: {
1124
+ _id: id
1125
+ }
1126
+ },
1127
+ upsert: (p.upsert !== undefined) ? p.upsert : false
1128
+ }
1129
+ });
1130
+ });
1131
+ }
1132
+ if (bulkWriteOps.length > 0) {
1133
+ yield this.offerModel.bulkWrite(bulkWriteOps, { ordered: false });
1134
+ // 同期タスク作成(2023-09-03~)
1135
+ yield this.saveSyncTask({
1136
+ project: { id: params[0].attributes.project.id },
1137
+ id: { $in: [] },
1138
+ identifier: { $in: params.map((savingOffer) => savingOffer.attributes.identifier) },
1139
+ isDeleted: false,
1140
+ typeOf: factory.offerType.Offer,
1141
+ options: { emitImmediately: true }
1142
+ });
1143
+ }
498
1144
  }
499
1145
  });
500
1146
  }
@@ -506,42 +1152,370 @@ class MongoRepository {
506
1152
  if (params.addOn.itemOffered.id.$in.length === 0) {
507
1153
  return;
508
1154
  }
509
- return this.offerModel.updateMany({
510
- 'project.id': { $eq: params.project.id },
511
- 'addOn.itemOffered.id': { $exists: true, $in: params.addOn.itemOffered.id.$in }
512
- }, {
513
- $pull: {
514
- addOn: {
515
- 'itemOffered.id': { $in: params.addOn.itemOffered.id.$in }
1155
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-05~)
1156
+ if (settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY) {
1157
+ const conditions = {
1158
+ 'project.id': { $eq: params.project.id },
1159
+ 'offers.addOn.itemOffered.id': { $exists: true, $in: params.addOn.itemOffered.id.$in }
1160
+ };
1161
+ const updatingOffers = yield this.searchFromAggregateOffer({
1162
+ project: { id: { $eq: params.project.id } },
1163
+ addOn: { itemOffered: { id: { $in: params.addOn.itemOffered.id.$in } } }
1164
+ }, { id: 1 });
1165
+ const result = yield this.aggregateOfferModel.updateMany(conditions, {
1166
+ $pull: {
1167
+ 'offers.$[offer].addOn': {
1168
+ 'itemOffered.id': { $in: params.addOn.itemOffered.id.$in }
1169
+ }
516
1170
  }
1171
+ }, {
1172
+ arrayFilters: [
1173
+ { 'offer.addOn.itemOffered.id': { $exists: true, $in: params.addOn.itemOffered.id.$in } }
1174
+ ]
1175
+ })
1176
+ .exec();
1177
+ // 同期タスク作成(2023-09-05~)
1178
+ if (updatingOffers.length > 0) {
1179
+ yield this.saveSyncTask({
1180
+ project: { id: params.project.id },
1181
+ id: { $in: updatingOffers.map((offer) => String(offer.id)) },
1182
+ identifier: { $in: [] },
1183
+ isDeleted: false,
1184
+ typeOf: factory.offerType.AggregateOffer,
1185
+ options: { emitImmediately: true }
1186
+ });
517
1187
  }
518
- })
519
- .exec();
1188
+ return result;
1189
+ }
1190
+ else {
1191
+ const conditions = {
1192
+ 'project.id': { $eq: params.project.id },
1193
+ 'addOn.itemOffered.id': { $exists: true, $in: params.addOn.itemOffered.id.$in }
1194
+ };
1195
+ const updatingOffers = yield this.offerModel.find(conditions, { _id: 1 })
1196
+ .exec()
1197
+ .then((docs) => docs.map((doc) => doc.toObject()));
1198
+ const result = yield this.offerModel.updateMany(conditions, {
1199
+ $pull: {
1200
+ addOn: {
1201
+ 'itemOffered.id': { $in: params.addOn.itemOffered.id.$in }
1202
+ }
1203
+ }
1204
+ })
1205
+ .exec();
1206
+ // 同期タスク作成(2023-09-03~)
1207
+ if (updatingOffers.length > 0) {
1208
+ yield this.saveSyncTask({
1209
+ project: { id: params.project.id },
1210
+ id: { $in: updatingOffers.map((offer) => offer.id) },
1211
+ identifier: { $in: [] },
1212
+ isDeleted: false,
1213
+ typeOf: factory.offerType.Offer,
1214
+ options: { emitImmediately: true }
1215
+ });
1216
+ }
1217
+ return result;
1218
+ }
520
1219
  });
521
1220
  }
522
1221
  deleteById(params) {
523
1222
  return __awaiter(this, void 0, void 0, function* () {
524
- yield this.offerModel.findOneAndRemove({ _id: params.id })
525
- .exec();
526
- });
527
- }
528
- deleteByProject(params) {
529
- return __awaiter(this, void 0, void 0, function* () {
530
- yield this.offerModel.deleteMany({
531
- 'project.id': { $eq: params.project.id }
532
- })
533
- .exec();
1223
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-05~)
1224
+ if (settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY) {
1225
+ yield this.aggregateOfferModel.findOneAndRemove({ _id: { $eq: params.id } }, { projection: { _id: 1 } })
1226
+ .exec();
1227
+ // 同期タスク作成(2023-09-03~)
1228
+ yield this.saveSyncTask({
1229
+ project: { id: params.project.id },
1230
+ id: { $in: [params.id] },
1231
+ identifier: { $in: [] },
1232
+ isDeleted: true,
1233
+ typeOf: factory.offerType.AggregateOffer,
1234
+ options: { emitImmediately: true }
1235
+ });
1236
+ }
1237
+ else {
1238
+ yield this.offerModel.findOneAndRemove({ _id: { $eq: params.id } }, { projection: { _id: 1 } })
1239
+ .exec();
1240
+ // 同期タスク作成(2023-09-03~)
1241
+ yield this.saveSyncTask({
1242
+ project: { id: params.project.id },
1243
+ id: { $in: [params.id] },
1244
+ identifier: { $in: [] },
1245
+ isDeleted: true,
1246
+ typeOf: factory.offerType.Offer,
1247
+ options: { emitImmediately: true }
1248
+ });
1249
+ }
534
1250
  });
535
1251
  }
1252
+ // public async deleteByProject(params: {
1253
+ // project: { id: string };
1254
+ // }): Promise<void> {
1255
+ // await this.offerModel.deleteMany({
1256
+ // 'project.id': { $eq: params.project.id }
1257
+ // })
1258
+ // .exec();
1259
+ // }
536
1260
  getCursor(conditions, projection) {
537
1261
  return this.offerModel.find(conditions, projection)
538
1262
  .sort({ 'priceSpecification.price': factory.sortType.Descending })
539
1263
  .cursor();
540
1264
  }
541
- unsetUnnecessaryFields(params) {
1265
+ // public async unsetUnnecessaryFields(params: {
1266
+ // filter: any;
1267
+ // $unset: any;
1268
+ // }) {
1269
+ // return this.offerModel.updateMany(
1270
+ // params.filter,
1271
+ // { $unset: params.$unset }
1272
+ // )
1273
+ // .exec();
1274
+ // }
1275
+ // tslint:disable-next-line:cyclomatic-complexity max-func-body-length
1276
+ sync2aggregateOffer(params) {
1277
+ var _a, _b, _c, _d;
542
1278
  return __awaiter(this, void 0, void 0, function* () {
543
- return this.offerModel.updateMany(params.filter, { $unset: params.$unset })
544
- .exec();
1279
+ if (params.typeOf === factory.offerType.Offer) {
1280
+ const idIn = (_a = params.id) === null || _a === void 0 ? void 0 : _a.$in;
1281
+ const identifierIn = (_b = params.identifier) === null || _b === void 0 ? void 0 : _b.$in;
1282
+ if (params.isDeleted === true) {
1283
+ if (Array.isArray(idIn) && idIn.length > 0) {
1284
+ yield this.aggregateOfferModel.deleteMany({
1285
+ 'project.id': { $eq: params.project.id },
1286
+ _id: { $in: idIn }
1287
+ })
1288
+ .exec();
1289
+ }
1290
+ // コードで削除することは現状ない
1291
+ // if (Array.isArray(identifierIn) && identifierIn.length > 0) {
1292
+ // await this.aggregateOfferModel.deleteMany({
1293
+ // 'project.id': { $eq: params.project.id },
1294
+ // 'offers.identifier': { $exists: true, $in: identifierIn }
1295
+ // })
1296
+ // .exec();
1297
+ // }
1298
+ }
1299
+ else {
1300
+ let unitPriceOffers = [];
1301
+ if (Array.isArray(idIn) && idIn.length > 0) {
1302
+ unitPriceOffers = yield this.offerModel.find({
1303
+ 'project.id': { $eq: params.project.id },
1304
+ _id: { $in: idIn }
1305
+ }, {
1306
+ __v: 0,
1307
+ createdAt: 0,
1308
+ updatedAt: 0,
1309
+ offers: 0,
1310
+ availableAddOn: 0 // 廃止属性の残存が確認されたので(2023-09-04~)
1311
+ })
1312
+ .exec()
1313
+ .then((docs) => docs.map((doc) => doc.toObject()));
1314
+ }
1315
+ if (Array.isArray(identifierIn) && identifierIn.length > 0) {
1316
+ unitPriceOffers = yield this.offerModel.find({
1317
+ 'project.id': { $eq: params.project.id },
1318
+ identifier: { $exists: true, $in: identifierIn }
1319
+ }, {
1320
+ __v: 0,
1321
+ createdAt: 0,
1322
+ updatedAt: 0,
1323
+ offers: 0,
1324
+ availableAddOn: 0 // 廃止属性の残存が確認されたので(2023-09-04~)
1325
+ })
1326
+ .exec()
1327
+ .then((docs) => docs.map((doc) => doc.toObject()));
1328
+ }
1329
+ const bulkWriteOps = [];
1330
+ if (unitPriceOffers.length > 0) {
1331
+ unitPriceOffers.forEach((unitPriceOffer) => {
1332
+ const { _id } = unitPriceOffer, settingUnitPriceOffer = __rest(unitPriceOffer, ["_id"]);
1333
+ const $set = {
1334
+ project: unitPriceOffer.project,
1335
+ typeOf: factory.offerType.AggregateOffer,
1336
+ offers: [settingUnitPriceOffer]
1337
+ };
1338
+ bulkWriteOps.push({
1339
+ updateOne: {
1340
+ filter: {
1341
+ 'project.id': { $eq: unitPriceOffer.project.id },
1342
+ _id: { $eq: unitPriceOffer.id }
1343
+ },
1344
+ update: {
1345
+ $set,
1346
+ $setOnInsert: { _id: unitPriceOffer.id }
1347
+ },
1348
+ upsert: true
1349
+ }
1350
+ });
1351
+ });
1352
+ }
1353
+ if (bulkWriteOps.length > 0) {
1354
+ return this.aggregateOfferModel.bulkWrite(bulkWriteOps, { ordered: false });
1355
+ }
1356
+ }
1357
+ }
1358
+ else if (params.typeOf === factory.offerType.AggregateOffer) {
1359
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-05~)
1360
+ const idIn = (_c = params.id) === null || _c === void 0 ? void 0 : _c.$in;
1361
+ const identifierIn = (_d = params.identifier) === null || _d === void 0 ? void 0 : _d.$in;
1362
+ if (params.isDeleted === true) {
1363
+ if (Array.isArray(idIn) && idIn.length > 0) {
1364
+ yield this.offerModel.deleteMany({
1365
+ 'project.id': { $eq: params.project.id },
1366
+ _id: { $in: idIn }
1367
+ })
1368
+ .exec();
1369
+ }
1370
+ }
1371
+ else {
1372
+ let aggregateOffers = [];
1373
+ if (Array.isArray(idIn) && idIn.length > 0) {
1374
+ aggregateOffers = yield this.aggregateOfferModel.aggregate([
1375
+ {
1376
+ $unwind: {
1377
+ path: '$offers'
1378
+ }
1379
+ },
1380
+ {
1381
+ $match: {
1382
+ 'project.id': { $eq: params.project.id },
1383
+ 'offers.id': { $in: idIn }
1384
+ }
1385
+ },
1386
+ {
1387
+ $project: {
1388
+ _id: 0,
1389
+ offer: '$offers'
1390
+ }
1391
+ }
1392
+ ])
1393
+ .exec();
1394
+ }
1395
+ if (Array.isArray(identifierIn) && identifierIn.length > 0) {
1396
+ aggregateOffers = yield this.aggregateOfferModel.aggregate([
1397
+ {
1398
+ $unwind: {
1399
+ path: '$offers'
1400
+ }
1401
+ },
1402
+ {
1403
+ $match: {
1404
+ 'project.id': { $eq: params.project.id },
1405
+ 'offers.identifier': { $exists: true, $in: identifierIn }
1406
+ }
1407
+ },
1408
+ {
1409
+ $project: {
1410
+ _id: 0,
1411
+ offer: '$offers'
1412
+ }
1413
+ }
1414
+ ])
1415
+ .exec();
1416
+ }
1417
+ const bulkWriteOps = [];
1418
+ if (aggregateOffers.length > 0) {
1419
+ aggregateOffers.forEach((aggregateOffer) => {
1420
+ const savingUnitPriceOffer = Object.assign(Object.assign({}, aggregateOffer.offer), { _id: String(aggregateOffer.offer.id) });
1421
+ const { id } = savingUnitPriceOffer, replacement = __rest(savingUnitPriceOffer, ["id"]);
1422
+ bulkWriteOps.push({
1423
+ replaceOne: {
1424
+ filter: {
1425
+ 'project.id': { $eq: savingUnitPriceOffer.project.id },
1426
+ _id: { $eq: savingUnitPriceOffer.id }
1427
+ },
1428
+ replacement,
1429
+ upsert: true
1430
+ }
1431
+ });
1432
+ });
1433
+ }
1434
+ if (bulkWriteOps.length > 0) {
1435
+ // console.log('bulkWriteOps:', bulkWriteOps[0].replaceOne);
1436
+ return this.offerModel.bulkWrite(bulkWriteOps, { ordered: false });
1437
+ }
1438
+ }
1439
+ }
1440
+ });
1441
+ }
1442
+ /**
1443
+ * オファー同期専用のタスク保管
1444
+ */
1445
+ saveSyncTask(params) {
1446
+ return __awaiter(this, void 0, void 0, function* () {
1447
+ const syncAggregateOfferTask = {
1448
+ project: { id: params.project.id, typeOf: factory.organizationType.Project },
1449
+ name: factory.taskName.SyncAggregateOffer,
1450
+ status: factory.taskStatus.Ready,
1451
+ runsAt: new Date(),
1452
+ remainingNumberOfTries: 3,
1453
+ numberOfTried: 0,
1454
+ executionResults: [],
1455
+ data: {
1456
+ project: { id: params.project.id },
1457
+ id: params.id,
1458
+ identifier: params.identifier,
1459
+ isDeleted: params.isDeleted,
1460
+ typeOf: params.typeOf
1461
+ }
1462
+ };
1463
+ const taskAttributes = [syncAggregateOfferTask];
1464
+ const result = yield this.taskModel.insertMany(taskAttributes, { ordered: false, rawResult: true });
1465
+ if (result.insertedCount !== taskAttributes.length) {
1466
+ throw new factory.errors.ServiceUnavailable('all tasks not saved');
1467
+ }
1468
+ const savedTasks = Object.values(result.insertedIds)
1469
+ .map((objectId) => {
1470
+ return { id: objectId.toHexString() };
1471
+ });
1472
+ if (params.options.emitImmediately === true) {
1473
+ savedTasks.forEach((savedTask) => {
1474
+ task_2.taskEventEmitter.emitTaskStatusChanged({
1475
+ id: savedTask.id,
1476
+ status: factory.taskStatus.Ready
1477
+ });
1478
+ });
1479
+ }
1480
+ return savedTasks;
1481
+ });
1482
+ }
1483
+ searchFromAggregateOffer(params, projection) {
1484
+ var _a, _b;
1485
+ return __awaiter(this, void 0, void 0, function* () {
1486
+ const matchStages = MongoRepository.CREATE_AGGREGATE_OFFERS_MATCH_CONDITIONS(params);
1487
+ const projectStage = MongoRepository.CREATE_AGGREGATE_OFFERS_PROJECTION(Object.assign({}, projection));
1488
+ const sortByPrice = (_a = params.sort) === null || _a === void 0 ? void 0 : _a['priceSpecification.price'];
1489
+ const sortByIdentifier = (_b = params.sort) === null || _b === void 0 ? void 0 : _b.identifier;
1490
+ const aggregate = this.aggregateOfferModel.aggregate([
1491
+ {
1492
+ $unwind: {
1493
+ path: '$offers',
1494
+ includeArrayIndex: OFFERS_ARRAY_INDEX_NAME
1495
+ }
1496
+ },
1497
+ ...matchStages,
1498
+ ...(typeof sortByPrice === 'number' || typeof sortByIdentifier === 'number')
1499
+ ? [
1500
+ {
1501
+ $sort: Object.assign(Object.assign({}, (typeof sortByPrice === 'number')
1502
+ ? { 'offers.priceSpecification.price': sortByPrice }
1503
+ : undefined), (typeof sortByIdentifier === 'number')
1504
+ ? { 'offers.identifier': sortByIdentifier }
1505
+ : undefined)
1506
+ }
1507
+ ]
1508
+ : [],
1509
+ { $project: projectStage }
1510
+ ]);
1511
+ // tslint:disable-next-line:no-single-line-block-comment
1512
+ /* istanbul ignore else */
1513
+ if (typeof params.limit === 'number' && params.limit > 0) {
1514
+ const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
1515
+ aggregate.limit(params.limit * page)
1516
+ .skip(params.limit * (page - 1));
1517
+ }
1518
+ return aggregate.exec();
545
1519
  });
546
1520
  }
547
1521
  }