@chevre/domain 21.27.0-alpha.9 → 21.27.0

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 (58) hide show
  1. package/example/src/chevre/{aggregateEventReservation.ts → aggregation/aggregateEventReservation.ts} +2 -3
  2. package/example/src/chevre/aggregation/aggregateOffersOnEvent.ts +82 -0
  3. package/example/src/chevre/{aggregateAllEvents.ts → aggregation/createAggregateEventTasks.ts} +26 -13
  4. package/example/src/chevre/aggregation/searchAggregateReservations.ts +33 -0
  5. package/example/src/chevre/searchEvents.ts +4 -4
  6. package/example/src/chevre/transaction/processPlaceOrder.ts +1 -2
  7. package/example/src/chevre/unsetUnnecessaryFields.ts +4 -4
  8. package/lib/chevre/credentials.d.ts +7 -0
  9. package/lib/chevre/credentials.js +10 -6
  10. package/lib/chevre/repo/action.d.ts +2 -2
  11. package/lib/chevre/repo/action.js +64 -49
  12. package/lib/chevre/repo/aggregateReservation.d.ts +68 -0
  13. package/lib/chevre/repo/aggregateReservation.js +135 -0
  14. package/lib/chevre/repo/event.d.ts +16 -14
  15. package/lib/chevre/repo/event.js +62 -20
  16. package/lib/chevre/repo/mongoose/schemas/action.js +18 -0
  17. package/lib/chevre/repo/mongoose/schemas/aggregateReservation.d.ts +5 -0
  18. package/lib/chevre/repo/mongoose/schemas/aggregateReservation.js +84 -0
  19. package/lib/chevre/repo/mongoose/schemas/event.js +1 -1
  20. package/lib/chevre/repo/offer.d.ts +5 -0
  21. package/lib/chevre/repository.d.ts +5 -0
  22. package/lib/chevre/repository.js +15 -2
  23. package/lib/chevre/service/aggregation/event/aggregateOffers.d.ts +31 -0
  24. package/lib/chevre/service/aggregation/event/aggregateOffers.js +290 -0
  25. package/lib/chevre/service/aggregation/event/aggregateScreeningEvent.d.ts +3 -4
  26. package/lib/chevre/service/aggregation/event/aggregateScreeningEvent.js +33 -238
  27. package/lib/chevre/service/aggregation/event/aggregateUseActionsOnEvent.d.ts +2 -0
  28. package/lib/chevre/service/aggregation/event/aggregateUseActionsOnEvent.js +19 -15
  29. package/lib/chevre/service/aggregation/event/findEventOffers.d.ts +2 -2
  30. package/lib/chevre/service/aggregation/event/findEventOffers.js +0 -2
  31. package/lib/chevre/service/aggregation/event.d.ts +2 -1
  32. package/lib/chevre/service/aggregation/event.js +3 -1
  33. package/lib/chevre/service/assetTransaction/pay.d.ts +4 -1
  34. package/lib/chevre/service/assetTransaction/pay.js +53 -27
  35. package/lib/chevre/service/assetTransaction/reserve.js +2 -1
  36. package/lib/chevre/service/event.js +16 -23
  37. package/lib/chevre/service/notification.js +0 -102
  38. package/lib/chevre/service/offer/onEventChanged.d.ts +25 -0
  39. package/lib/chevre/service/offer/onEventChanged.js +232 -0
  40. package/lib/chevre/service/offer.d.ts +0 -40
  41. package/lib/chevre/service/offer.js +1 -184
  42. package/lib/chevre/service/payment/any.d.ts +4 -2
  43. package/lib/chevre/service/payment/any.js +76 -48
  44. package/lib/chevre/service/payment/creditCard.js +14 -7
  45. package/lib/chevre/service/payment/movieTicket/checkByIdentifier.js +1 -9
  46. package/lib/chevre/service/reserve/potentialActions/onReservationCanceled.js +2 -2
  47. package/lib/chevre/service/reserve/potentialActions/onReservationCheckedIn.js +2 -2
  48. package/lib/chevre/service/reserve/potentialActions/onReservationConfirmed.js +2 -2
  49. package/lib/chevre/service/reserve/potentialActions/onReservationUsed.js +2 -2
  50. package/lib/chevre/service/task/aggregateOffers.d.ts +7 -0
  51. package/lib/chevre/service/task/aggregateOffers.js +47 -0
  52. package/lib/chevre/service/task/aggregateScreeningEvent.js +0 -2
  53. package/lib/chevre/service/task/aggregateUseActionsOnEvent.js +2 -0
  54. package/lib/chevre/service/task/onEventChanged.js +2 -2
  55. package/lib/chevre/service/transaction/returnOrder/preStart.js +0 -18
  56. package/lib/chevre/settings.d.ts +1 -0
  57. package/lib/chevre/settings.js +2 -1
  58. package/package.json +3 -3
@@ -0,0 +1,290 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.aggregateOffers = void 0;
13
+ const createDebug = require("debug");
14
+ const moment = require("moment-timezone");
15
+ const factory = require("../../../factory");
16
+ const findEventOffers_1 = require("./findEventOffers");
17
+ const debug = createDebug('chevre-domain:service:aggregation');
18
+ /**
19
+ * イベントに対するオファー集計
20
+ */
21
+ function aggregateOffers(params) {
22
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
23
+ // 集計対象イベント検索
24
+ const event = yield repos.event.findMinimizedIndividualEventById({ id: params.id });
25
+ const eventCapacity = (yield repos.event.search({
26
+ limit: 1,
27
+ page: 1,
28
+ id: { $eq: params.id },
29
+ typeOf: params.typeOf
30
+ }, ['maximumAttendeeCapacity', 'remainingAttendeeCapacity'], [])).shift();
31
+ debug('processing aggregateOffersByEvent...', eventCapacity);
32
+ yield aggregateOffersByEvent({
33
+ event: Object.assign(Object.assign(Object.assign({}, event), (typeof (eventCapacity === null || eventCapacity === void 0 ? void 0 : eventCapacity.maximumAttendeeCapacity) === 'number')
34
+ ? { maximumAttendeeCapacity: eventCapacity.maximumAttendeeCapacity }
35
+ : undefined), (typeof (eventCapacity === null || eventCapacity === void 0 ? void 0 : eventCapacity.remainingAttendeeCapacity) === 'number')
36
+ ? { remainingAttendeeCapacity: eventCapacity.remainingAttendeeCapacity }
37
+ : undefined)
38
+ })(repos);
39
+ });
40
+ }
41
+ exports.aggregateOffers = aggregateOffers;
42
+ function aggregateOffersByEvent(params) {
43
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
44
+ var _a;
45
+ const now = new Date();
46
+ const event = params.event;
47
+ // 施設取得は冗長なので、ルーム検索に変更(2023-01-30~)
48
+ let movieTheaterId;
49
+ if (params.event.typeOf === factory.eventType.ScreeningEvent) {
50
+ movieTheaterId = params.event.superEvent.location.id;
51
+ }
52
+ else {
53
+ movieTheaterId = String((_a = params.event.offers) === null || _a === void 0 ? void 0 : _a.itemOffered.availableChannel.serviceLocation.containedInPlace.id);
54
+ }
55
+ const screeningRoom = yield repos.place.findScreeningRoomsByBranchCode({
56
+ project: { id: event.project.id },
57
+ branchCode: { $eq: event.location.branchCode },
58
+ containedInPlace: { id: { $eq: movieTheaterId } }
59
+ });
60
+ // オファーごとの集計
61
+ const aggregateOffer = yield aggregateOfferByEvent({
62
+ aggregateDate: now,
63
+ event,
64
+ screeningRoom: screeningRoom
65
+ })(repos);
66
+ debug('offers aggregated', aggregateOffer);
67
+ const update = {
68
+ $set: Object.assign({}, (typeof (aggregateOffer === null || aggregateOffer === void 0 ? void 0 : aggregateOffer.typeOf) === 'string') ? { aggregateOffer: aggregateOffer } : undefined)
69
+ // $unset: {
70
+ // noExistingAttributeName: 1 // $unsetは空だとエラーになるので
71
+ // }
72
+ };
73
+ debug('update:', update);
74
+ // 廃止(2024-03-28~)
75
+ // const saveResult = await repos.event.updateAggregationById<typeof event.typeOf>({ id: event.id }, update);
76
+ // debug('aggregateOffersByEvent processd', saveResult);
77
+ // aggregateReservationsにも保管(2024-03-25~)
78
+ yield repos.aggregateReservation.save({
79
+ project: event.project,
80
+ reservationFor: {
81
+ id: event.id,
82
+ typeOf: event.typeOf,
83
+ startDate: moment(event.startDate)
84
+ .toDate()
85
+ }
86
+ }, {
87
+ $set: { aggregateOffer }
88
+ });
89
+ });
90
+ }
91
+ function reservedSeatsAvailable(params) {
92
+ var _a, _b, _c;
93
+ const eventOffers = params.event.offers;
94
+ return ((_c = (_b = (_a = eventOffers === null || eventOffers === void 0 ? void 0 : eventOffers.itemOffered) === null || _a === void 0 ? void 0 : _a.serviceOutput) === null || _b === void 0 ? void 0 : _b.reservedTicket) === null || _c === void 0 ? void 0 : _c.ticketedSeat) !== undefined;
95
+ }
96
+ function aggregateOfferByEvent(params) {
97
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
98
+ var _a;
99
+ const availableOffers = yield (0, findEventOffers_1.findEventOffers)(params)(repos);
100
+ // オファーごとの予約集計
101
+ const offersWithAggregateReservation = [];
102
+ for (const o of availableOffers) {
103
+ const { maximumAttendeeCapacity, remainingAttendeeCapacity, aggregateReservation } = yield aggregateReservationByOffer({
104
+ aggregateDate: params.aggregateDate,
105
+ event: params.event,
106
+ screeningRoom: params.screeningRoom,
107
+ offer: o
108
+ })(repos);
109
+ offersWithAggregateReservation.push(Object.assign(Object.assign(Object.assign({ typeOf: o.typeOf, id: o.id, identifier: o.identifier, aggregateReservation: aggregateReservation }, (typeof maximumAttendeeCapacity === 'number') ? { maximumAttendeeCapacity } : undefined), (typeof remainingAttendeeCapacity === 'number') ? { remainingAttendeeCapacity } : undefined), (typeof ((_a = o.category) === null || _a === void 0 ? void 0 : _a.codeValue) === 'string') ? { category: o.category } : undefined));
110
+ }
111
+ return {
112
+ typeOf: factory.offerType.AggregateOffer,
113
+ aggregateDate: params.aggregateDate,
114
+ offerCount: availableOffers.length,
115
+ offers: offersWithAggregateReservation
116
+ };
117
+ });
118
+ }
119
+ function aggregateReservationByOffer(params) {
120
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
121
+ let reservationCount4offer;
122
+ let attendeeCount4offer;
123
+ let checkInCount4offer;
124
+ let reservationType = factory.reservationType.EventReservation;
125
+ if (params.event.typeOf === factory.eventType.Event) {
126
+ reservationType = factory.reservationType.BusReservation;
127
+ }
128
+ reservationCount4offer = yield repos.reservation.count({
129
+ typeOf: reservationType,
130
+ reservationFor: { id: { $eq: params.event.id } },
131
+ reservationStatuses: [factory.reservationStatusType.ReservationConfirmed],
132
+ reservedTicket: { ticketType: { ids: [params.offer.id] } }
133
+ });
134
+ attendeeCount4offer = yield repos.reservation.count({
135
+ typeOf: reservationType,
136
+ reservationFor: { id: { $eq: params.event.id } },
137
+ reservationStatuses: [factory.reservationStatusType.ReservationConfirmed],
138
+ reservedTicket: { ticketType: { ids: [params.offer.id] } },
139
+ attended: true
140
+ });
141
+ checkInCount4offer = yield repos.reservation.count({
142
+ typeOf: reservationType,
143
+ reservationFor: { id: { $eq: params.event.id } },
144
+ reservationStatuses: [factory.reservationStatusType.ReservationConfirmed],
145
+ reservedTicket: { ticketType: { ids: [params.offer.id] } },
146
+ checkedIn: true
147
+ });
148
+ const { maximumAttendeeCapacity, remainingAttendeeCapacity } = yield calculateCapacityByOffer(params)(repos);
149
+ return Object.assign(Object.assign({ aggregateReservation: {
150
+ typeOf: 'AggregateReservation',
151
+ // aggregateDate: params.aggregateDate, // 最適化につき廃止(2024-03-26~)
152
+ reservationCount: reservationCount4offer,
153
+ attendeeCount: attendeeCount4offer,
154
+ checkInCount: checkInCount4offer
155
+ } }, (typeof maximumAttendeeCapacity === 'number') ? { maximumAttendeeCapacity } : undefined), (typeof remainingAttendeeCapacity === 'number') ? { remainingAttendeeCapacity } : undefined);
156
+ });
157
+ }
158
+ /**
159
+ * オファーごとのキャパシティを算出する
160
+ */
161
+ function calculateCapacityByOffer(params) {
162
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
163
+ var _a;
164
+ let maximumAttendeeCapacity;
165
+ let remainingAttendeeCapacity;
166
+ if (reservedSeatsAvailable({ event: params.event })) {
167
+ // 基本的にはイベントのキャパシティに同じ
168
+ maximumAttendeeCapacity = params.event.maximumAttendeeCapacity;
169
+ remainingAttendeeCapacity = params.event.remainingAttendeeCapacity;
170
+ // 座席タイプ制約のあるオファーの場合
171
+ const eligibleSeatingTypes = params.offer.eligibleSeatingType;
172
+ if (Array.isArray(eligibleSeatingTypes)) {
173
+ const filterByEligibleSeatingTypeResult = yield filterByEligibleSeatingType({
174
+ event: params.event,
175
+ screeningRoom: params.screeningRoom,
176
+ eligibleSeatingTypes: eligibleSeatingTypes.map((e) => e.codeValue)
177
+ })(repos);
178
+ maximumAttendeeCapacity = filterByEligibleSeatingTypeResult.maximumAttendeeCapacity;
179
+ remainingAttendeeCapacity = filterByEligibleSeatingTypeResult.remainingAttendeeCapacity;
180
+ }
181
+ // 適用サブ予約がある場合
182
+ const eligibleSubReservation = params.offer.eligibleSubReservation;
183
+ if (Array.isArray(eligibleSubReservation)) {
184
+ // 適用サブ予約の座席タイプごとにキャパシティ算出
185
+ const capacities = yield Promise.all(eligibleSubReservation
186
+ .filter((subReservation) => typeof subReservation.amountOfThisGood === 'number' && subReservation.amountOfThisGood > 0)
187
+ .map((subReservation) => __awaiter(this, void 0, void 0, function* () {
188
+ const filterByEligibleSeatingTypeResult = yield filterByEligibleSeatingType({
189
+ event: params.event,
190
+ screeningRoom: params.screeningRoom,
191
+ eligibleSeatingTypes: [subReservation.typeOfGood.seatingType]
192
+ })(repos);
193
+ return {
194
+ maximumAttendeeCapacity: Math.floor(filterByEligibleSeatingTypeResult.maximumAttendeeCapacity / subReservation.amountOfThisGood),
195
+ remainingAttendeeCapacity: Math.floor(filterByEligibleSeatingTypeResult.remainingAttendeeCapacity / subReservation.amountOfThisGood)
196
+ };
197
+ })));
198
+ // 座席タイプごとのキャパシティの中から、最小数を選択する
199
+ maximumAttendeeCapacity = Math.min(...(typeof maximumAttendeeCapacity === 'number') ? [maximumAttendeeCapacity] : [], ...capacities.map((c) => c.maximumAttendeeCapacity));
200
+ remainingAttendeeCapacity = Math.min(...(typeof remainingAttendeeCapacity === 'number') ? [remainingAttendeeCapacity] : [], ...capacities.map((c) => c.remainingAttendeeCapacity));
201
+ }
202
+ // 単価スペックの単位が1より大きい場合
203
+ const referenceQuantityValue = (_a = params.offer.priceSpecification) === null || _a === void 0 ? void 0 : _a.referenceQuantity.value;
204
+ if (typeof referenceQuantityValue === 'number' && referenceQuantityValue > 1) {
205
+ if (typeof maximumAttendeeCapacity === 'number') {
206
+ maximumAttendeeCapacity -= maximumAttendeeCapacity % referenceQuantityValue;
207
+ }
208
+ if (typeof remainingAttendeeCapacity === 'number') {
209
+ remainingAttendeeCapacity -= remainingAttendeeCapacity % referenceQuantityValue;
210
+ }
211
+ }
212
+ }
213
+ // レート制限がある場合、考慮する
214
+ if (yield isHoldByRateLimit(params)({ offerRateLimit: repos.offerRateLimit })) {
215
+ remainingAttendeeCapacity = 0;
216
+ }
217
+ return { maximumAttendeeCapacity, remainingAttendeeCapacity };
218
+ });
219
+ }
220
+ function isHoldByRateLimit(params) {
221
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
222
+ var _a, _b;
223
+ let isHold = false;
224
+ const scope = (_a = params.offer.validRateLimit) === null || _a === void 0 ? void 0 : _a.scope;
225
+ const unitInSeconds = (_b = params.offer.validRateLimit) === null || _b === void 0 ? void 0 : _b.unitInSeconds;
226
+ if (typeof scope === 'string' && typeof unitInSeconds === 'number') {
227
+ const rateLimitKey = {
228
+ reservedTicket: {
229
+ ticketType: {
230
+ validRateLimit: { scope: scope, unitInSeconds: unitInSeconds }
231
+ }
232
+ },
233
+ reservationFor: {
234
+ startDate: moment(params.event.startDate)
235
+ .toDate()
236
+ },
237
+ reservationNumber: ''
238
+ };
239
+ const holder = yield repos.offerRateLimit.getHolder(rateLimitKey);
240
+ // ロックされていれば在庫0
241
+ if (typeof holder === 'string' && holder.length > 0) {
242
+ isHold = true;
243
+ }
244
+ }
245
+ return isHold;
246
+ });
247
+ }
248
+ function filterByEligibleSeatingType(params) {
249
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
250
+ // 適用座席タイプに絞る
251
+ const eligibleSeatOffers = (Array.isArray(params.screeningRoom.containsPlace))
252
+ ? params.screeningRoom.containsPlace.reduce((a, b) => {
253
+ return [
254
+ ...a,
255
+ ...(Array.isArray(b.containsPlace))
256
+ ? b.containsPlace.filter((place) => {
257
+ const seatingTypes = (Array.isArray(place.seatingType)) ? place.seatingType
258
+ : (typeof place.seatingType === 'string') ? [place.seatingType]
259
+ : [];
260
+ return seatingTypes.some((seatingTypeCodeValue) => params.eligibleSeatingTypes.some((eligibleSeatingType) => eligibleSeatingType === seatingTypeCodeValue));
261
+ })
262
+ .map((place) => {
263
+ return {
264
+ seatSection: b.branchCode,
265
+ seatNumber: place.branchCode
266
+ };
267
+ })
268
+ : []
269
+ ];
270
+ }, [])
271
+ : [];
272
+ const maximumAttendeeCapacity = eligibleSeatOffers.length;
273
+ let remainingAttendeeCapacity;
274
+ if (maximumAttendeeCapacity > 0) {
275
+ const availabilities = yield repos.stockHolder.searchHolders({
276
+ project: { id: params.event.project.id },
277
+ eventId: params.event.id,
278
+ startDate: moment(params.event.startDate)
279
+ .toDate(),
280
+ hasTicketedSeat: true,
281
+ offers: eligibleSeatOffers
282
+ });
283
+ remainingAttendeeCapacity = availabilities.filter((holder) => typeof holder !== 'string').length;
284
+ }
285
+ else {
286
+ remainingAttendeeCapacity = 0;
287
+ }
288
+ return { maximumAttendeeCapacity, remainingAttendeeCapacity };
289
+ });
290
+ }
@@ -3,13 +3,12 @@ import type { MongoRepository as OfferRepo } from '../../../repo/offer';
3
3
  import type { MongoRepository as OfferCatalogRepo } from '../../../repo/offerCatalog';
4
4
  import { MongoRepository as PlaceRepo } from '../../../repo/place';
5
5
  import type { MongoRepository as ProductRepo } from '../../../repo/product';
6
- import type { MongoRepository as ProjectRepo } from '../../../repo/project';
7
6
  import { RedisRepository as OfferRateLimitRepo } from '../../../repo/rateLimit/offer';
8
7
  import type { MongoRepository as ReservationRepo } from '../../../repo/reservation';
9
8
  import type { StockHolderRepository as StockHolderRepo } from '../../../repo/stockHolder';
10
9
  import type { MongoRepository as TaskRepo } from '../../../repo/task';
11
10
  import * as factory from '../../../factory';
12
- export type IAggregateScreeningEventOperation<T> = (repos: {
11
+ export interface IAggregateScreeningEventRepos {
13
12
  event: EventRepo;
14
13
  stockHolder: StockHolderRepo;
15
14
  offer: OfferRepo;
@@ -17,10 +16,10 @@ export type IAggregateScreeningEventOperation<T> = (repos: {
17
16
  offerRateLimit: OfferRateLimitRepo;
18
17
  place: PlaceRepo;
19
18
  product: ProductRepo;
20
- project: ProjectRepo;
21
19
  reservation: ReservationRepo;
22
20
  task: TaskRepo;
23
- }) => Promise<T>;
21
+ }
22
+ export type IAggregateScreeningEventOperation<T> = (repos: IAggregateScreeningEventRepos) => Promise<T>;
24
23
  /**
25
24
  * イベントデータをID指定で集計する
26
25
  */