@chevre/domain 21.8.0-alpha.36 → 21.8.0-alpha.37

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.
@@ -853,7 +853,7 @@ class MongoRepository {
853
853
  });
854
854
  const unitPriceOffer = offers.shift();
855
855
  if (unitPriceOffer === undefined) {
856
- throw new factory.errors.NotFound(this.offerModel.modelName);
856
+ throw new factory.errors.NotFound(factory.offerType.Offer);
857
857
  }
858
858
  return unitPriceOffer;
859
859
  });
@@ -1010,7 +1010,7 @@ class MongoRepository {
1010
1010
  .exec();
1011
1011
  }
1012
1012
  if (doc === null) {
1013
- throw new factory.errors.NotFound(this.offerModel.modelName);
1013
+ throw new factory.errors.NotFound(factory.offerType.Offer);
1014
1014
  }
1015
1015
  // 同期タスク作成(2023-09-03~)
1016
1016
  if (typeof doc._id === 'string') {
@@ -1030,54 +1030,111 @@ class MongoRepository {
1030
1030
  /**
1031
1031
  * sskts専用オファー保管
1032
1032
  */
1033
+ // tslint:disable-next-line:max-func-body-length
1033
1034
  saveManyByIdentifier(params) {
1034
1035
  return __awaiter(this, void 0, void 0, function* () {
1035
- // tslint:disable-next-line:no-suspicious-comment
1036
- // TODO USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-05~)
1037
- // if (USE_AGGREGATE_OFFERS_AS_PRIMARY) {
1038
- // } else {
1039
- // }
1040
- const bulkWriteOps = [];
1041
- if (Array.isArray(params)) {
1042
- params.forEach((p) => {
1043
- const id = uniqid();
1044
- const $set = Object.assign({}, p.attributes);
1045
- if (typeof $set.id === 'string') {
1046
- delete $set.id;
1047
- }
1048
- bulkWriteOps.push({
1049
- updateOne: {
1050
- filter: {
1051
- 'project.id': {
1052
- $eq: p.attributes.project.id
1036
+ // USE_AGGREGATE_OFFERS_AS_PRIMARY対応(2023-09-06~)
1037
+ if (settings_1.USE_AGGREGATE_OFFERS_AS_PRIMARY) {
1038
+ const insertBulkWriteOps = [];
1039
+ const updateBulkWriteOps = [];
1040
+ if (Array.isArray(params)) {
1041
+ params.forEach((p) => {
1042
+ const newOfferId = uniqid();
1043
+ const unitPriceOffer = Object.assign(Object.assign({}, p.attributes), { id: newOfferId });
1044
+ const aggregateOffer = {
1045
+ project: p.attributes.project,
1046
+ typeOf: factory.offerType.AggregateOffer,
1047
+ offers: [unitPriceOffer]
1048
+ };
1049
+ insertBulkWriteOps.push({
1050
+ updateOne: {
1051
+ filter: {
1052
+ 'project.id': { $eq: p.attributes.project.id },
1053
+ 'offers.identifier': { $exists: true, $eq: p.attributes.identifier }
1053
1054
  },
1054
- identifier: {
1055
- $exists: true,
1056
- $eq: p.attributes.identifier
1057
- }
1058
- },
1059
- update: {
1060
- $set: $set,
1061
- $setOnInsert: {
1062
- _id: id
1063
- }
1064
- },
1065
- upsert: (p.upsert !== undefined) ? p.upsert : false
1066
- }
1055
+ update: {
1056
+ $setOnInsert: Object.assign(Object.assign({}, aggregateOffer), { _id: newOfferId })
1057
+ },
1058
+ upsert: true
1059
+ }
1060
+ });
1061
+ const { id } = unitPriceOffer, $set = __rest(unitPriceOffer, ["id"]);
1062
+ updateBulkWriteOps.push({
1063
+ updateOne: {
1064
+ filter: {
1065
+ 'project.id': { $eq: unitPriceOffer.project.id },
1066
+ 'offers.identifier': { $exists: true, $eq: unitPriceOffer.identifier }
1067
+ },
1068
+ update: {
1069
+ // 特定の属性のみ更新する(IDを上書きできないので)
1070
+ $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))
1071
+ ? { 'offers.0.eligibleMembershipType': $set.eligibleMembershipType }
1072
+ : undefined), (Array.isArray($set.eligibleMonetaryAmount))
1073
+ ? { 'offers.0.eligibleMonetaryAmount': $set.eligibleMonetaryAmount }
1074
+ : undefined)
1075
+ },
1076
+ upsert: false
1077
+ }
1078
+ });
1067
1079
  });
1068
- });
1080
+ }
1081
+ if (insertBulkWriteOps.length > 0) {
1082
+ yield this.aggregateOfferModel.bulkWrite(insertBulkWriteOps, { ordered: false });
1083
+ yield this.aggregateOfferModel.bulkWrite(updateBulkWriteOps, { ordered: false });
1084
+ // 同期タスク作成(2023-09-03~)
1085
+ yield this.saveSyncTask({
1086
+ project: { id: params[0].attributes.project.id },
1087
+ id: { $in: [] },
1088
+ identifier: { $in: params.map((savingOffer) => savingOffer.attributes.identifier) },
1089
+ isDeleted: false,
1090
+ typeOf: factory.offerType.AggregateOffer,
1091
+ options: { emitImmediately: true }
1092
+ });
1093
+ }
1069
1094
  }
1070
- if (bulkWriteOps.length > 0) {
1071
- yield this.offerModel.bulkWrite(bulkWriteOps, { ordered: false });
1072
- // 同期タスク作成(2023-09-03~)
1073
- yield this.saveSyncTask({
1074
- project: { id: params[0].attributes.project.id },
1075
- id: { $in: [] },
1076
- identifier: { $in: params.map((savingOffer) => savingOffer.attributes.identifier) },
1077
- isDeleted: false,
1078
- typeOf: factory.offerType.Offer,
1079
- options: { emitImmediately: true }
1080
- });
1095
+ else {
1096
+ const bulkWriteOps = [];
1097
+ if (Array.isArray(params)) {
1098
+ params.forEach((p) => {
1099
+ const id = uniqid();
1100
+ const $set = Object.assign({}, p.attributes);
1101
+ if (typeof $set.id === 'string') {
1102
+ delete $set.id;
1103
+ }
1104
+ bulkWriteOps.push({
1105
+ updateOne: {
1106
+ filter: {
1107
+ 'project.id': {
1108
+ $eq: p.attributes.project.id
1109
+ },
1110
+ identifier: {
1111
+ $exists: true,
1112
+ $eq: p.attributes.identifier
1113
+ }
1114
+ },
1115
+ update: {
1116
+ $set: $set,
1117
+ $setOnInsert: {
1118
+ _id: id
1119
+ }
1120
+ },
1121
+ upsert: (p.upsert !== undefined) ? p.upsert : false
1122
+ }
1123
+ });
1124
+ });
1125
+ }
1126
+ if (bulkWriteOps.length > 0) {
1127
+ yield this.offerModel.bulkWrite(bulkWriteOps, { ordered: false });
1128
+ // 同期タスク作成(2023-09-03~)
1129
+ yield this.saveSyncTask({
1130
+ project: { id: params[0].attributes.project.id },
1131
+ id: { $in: [] },
1132
+ identifier: { $in: params.map((savingOffer) => savingOffer.attributes.identifier) },
1133
+ isDeleted: false,
1134
+ typeOf: factory.offerType.Offer,
1135
+ options: { emitImmediately: true }
1136
+ });
1137
+ }
1081
1138
  }
1082
1139
  });
1083
1140
  }
@@ -1326,7 +1383,26 @@ class MongoRepository {
1326
1383
  .exec();
1327
1384
  }
1328
1385
  if (Array.isArray(identifierIn) && identifierIn.length > 0) {
1329
- throw new factory.errors.NotImplemented('USE_AGGREGATE_OFFERS_AS_PRIMARY not implemented');
1386
+ aggregateOffers = yield this.aggregateOfferModel.aggregate([
1387
+ {
1388
+ $unwind: {
1389
+ path: '$offers'
1390
+ }
1391
+ },
1392
+ {
1393
+ $match: {
1394
+ 'project.id': { $eq: params.project.id },
1395
+ 'offers.identifier': { $exists: true, $in: identifierIn }
1396
+ }
1397
+ },
1398
+ {
1399
+ $project: {
1400
+ _id: 0,
1401
+ offer: '$offers'
1402
+ }
1403
+ }
1404
+ ])
1405
+ .exec();
1330
1406
  }
1331
1407
  const bulkWriteOps = [];
1332
1408
  if (aggregateOffers.length > 0) {
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.8.0-alpha.36"
120
+ "version": "21.8.0-alpha.37"
121
121
  }