@chevre/domain 26.0.0-alpha.25 → 26.0.0-alpha.27

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 (43) hide show
  1. package/lib/chevre/repo/adminAssetTransaction.d.ts +132 -0
  2. package/lib/chevre/repo/adminAssetTransaction.js +504 -0
  3. package/lib/chevre/repo/adminTransaction.d.ts +131 -0
  4. package/lib/chevre/repo/adminTransaction.js +237 -0
  5. package/lib/chevre/repo/aggregateAssetTransaction.d.ts +43 -0
  6. package/lib/chevre/repo/aggregateAssetTransaction.js +166 -0
  7. package/lib/chevre/repo/aggregateTransaction.d.ts +39 -0
  8. package/lib/chevre/repo/aggregateTransaction.js +166 -0
  9. package/lib/chevre/repo/assetTransaction.d.ts +10 -235
  10. package/lib/chevre/repo/assetTransaction.js +48 -636
  11. package/lib/chevre/repo/mongoose/schemas/aggregateReservation.js +8 -9
  12. package/lib/chevre/repo/mongoose/schemas/event.js +7 -0
  13. package/lib/chevre/repo/mongoose/schemas/eventOffer.js +7 -0
  14. package/lib/chevre/repo/mongoose/schemas/pendingReservation.js +11 -4
  15. package/lib/chevre/repo/transaction.d.ts +1 -187
  16. package/lib/chevre/repo/transaction.js +2 -383
  17. package/lib/chevre/repository.d.ts +20 -5
  18. package/lib/chevre/repository.js +46 -13
  19. package/lib/chevre/service/aggregation/system.d.ts +7 -7
  20. package/lib/chevre/service/aggregation/system.js +4 -4
  21. package/lib/chevre/service/assetTransaction/cancelReservation/start.js +10 -17
  22. package/lib/chevre/service/assetTransaction/refund.js +2 -2
  23. package/lib/chevre/service/offer/event/voidTransaction/processVoidTransaction4chevre.js +1 -3
  24. package/lib/chevre/service/order/onAssetTransactionStatusChanged/isDeliverable.js +1 -49
  25. package/lib/chevre/service/order/onAssetTransactionStatusChanged/onPayTransactionConfirmed.js +2 -2
  26. package/lib/chevre/service/payment/any/processVoidPayTransaction.js +1 -1
  27. package/lib/chevre/service/reserve/cancelReservation.js +3 -3
  28. package/lib/chevre/service/reserve/checkInReservation.js +2 -2
  29. package/lib/chevre/service/reserve/confirmReservation.js +1 -1
  30. package/lib/chevre/service/reserve/useReservation.js +1 -1
  31. package/lib/chevre/service/task/deleteTransaction.js +2 -4
  32. package/lib/chevre/service/task/returnPayTransaction.js +2 -2
  33. package/lib/chevre/service/transaction/deleteTransaction/deletePayTransactionsByPlaceOrder.js +2 -2
  34. package/lib/chevre/service/transaction/deleteTransaction/deleteReservationsByPlaceOrder.js +2 -2
  35. package/lib/chevre/service/transaction/deleteTransaction.d.ts +2 -2
  36. package/lib/chevre/service/transaction/deleteTransaction.js +2 -2
  37. package/lib/chevre/service/transaction/placeOrder/confirm.js +1 -1
  38. package/lib/chevre/service/validation/validateOrder.js +2 -2
  39. package/package.json +2 -2
  40. package/lib/chevre/repo/mongoose/schemas/ownershipInfo.d.ts +0 -13
  41. package/lib/chevre/repo/mongoose/schemas/ownershipInfo.js +0 -82
  42. package/lib/chevre/repo/ownershipInfo.d.ts +0 -20
  43. package/lib/chevre/repo/ownershipInfo.js +0 -130
@@ -0,0 +1,131 @@
1
+ import type { Connection } from 'mongoose';
2
+ import { factory } from '../factory';
3
+ import { IDocType } from './mongoose/schemas/transaction';
4
+ /**
5
+ * 取引管理リポジトリ
6
+ */
7
+ export declare class AdminTransactionRepo {
8
+ private readonly transactionModel;
9
+ constructor(connection: Connection);
10
+ countPotentiallyExportTasks(params: {
11
+ endDate: {
12
+ $lt: Date;
13
+ };
14
+ limit?: number;
15
+ }): Promise<{
16
+ count: number;
17
+ }>;
18
+ /**
19
+ * tasksExportAction.actionStatusがActiveActionStatusのまま一定時間経過した取引について
20
+ * PotentialActionStatusに変更する
21
+ * 2025-03-10~
22
+ */
23
+ reExportAction(params: {
24
+ startDate: {
25
+ $lt: Date;
26
+ };
27
+ }): Promise<import("mongoose").UpdateWriteOpResult>;
28
+ /**
29
+ * add(2025-03-13~)
30
+ */
31
+ countPotentiallyExpired(params: {
32
+ expires: {
33
+ $lt: Date;
34
+ };
35
+ limit?: number;
36
+ }): Promise<{
37
+ count: number;
38
+ }>;
39
+ /**
40
+ * add(2025-03-03~)
41
+ */
42
+ makeOneExpiredIfExists(params: {
43
+ expires: {
44
+ $lt: Date;
45
+ };
46
+ }): Promise<Pick<factory.transaction.ITransaction<factory.transactionType>, 'id' | 'typeOf'> | undefined>;
47
+ findByIdAndDelete(params: {
48
+ id: string;
49
+ }): Promise<void>;
50
+ unsetUnnecessaryFields(params: {
51
+ filter: any;
52
+ $unset: any;
53
+ }): Promise<import("mongoose").UpdateWriteOpResult>;
54
+ getCursor(conditions: any, projection: any, sort?: factory.sortType): import("mongoose").Cursor<import("mongoose").Document<unknown, Record<string, never>, IDocType, import("./mongoose/virtuals").IVirtuals, {}> & (({
55
+ object: import("@chevre/factory/lib/chevre/transaction/placeOrder").IObject;
56
+ error?: any;
57
+ project: import("@chevre/factory/lib/chevre/transaction").IProject;
58
+ seller: import("@chevre/factory/lib/chevre/transaction/placeOrder").ISeller;
59
+ typeOf: factory.transactionType.PlaceOrder;
60
+ expires: Date;
61
+ result?: import("@chevre/factory/lib/chevre/transaction/placeOrder").IResult | undefined;
62
+ startDate: Date;
63
+ endDate?: Date | undefined;
64
+ instrument?: import("@chevre/factory/lib/chevre/transaction/placeOrder").IInstrument | undefined;
65
+ agent: import("@chevre/factory/lib/chevre/transaction/placeOrder").IAgent;
66
+ status: factory.transactionStatusType;
67
+ tasksExportAction?: import("@chevre/factory/lib/chevre/transaction").ITasksExportAction | undefined;
68
+ potentialActions?: undefined;
69
+ } & {
70
+ _id: import("mongoose").Types.ObjectId;
71
+ } & {
72
+ __v: number;
73
+ }) | ({
74
+ object: import("@chevre/factory/lib/chevre/transaction/returnOrder").IObject;
75
+ error?: any;
76
+ project: import("@chevre/factory/lib/chevre/transaction").IProject;
77
+ seller: import("@chevre/factory/lib/chevre/transaction").ISeller;
78
+ typeOf: factory.transactionType.ReturnOrder;
79
+ expires: Date;
80
+ result?: import("@chevre/factory/lib/chevre/transaction/returnOrder").IResult | undefined;
81
+ startDate: Date;
82
+ endDate?: Date | undefined;
83
+ agent: import("@chevre/factory/lib/chevre/transaction").IAgent;
84
+ recipient?: undefined;
85
+ status: factory.transactionStatusType;
86
+ tasksExportAction?: import("@chevre/factory/lib/chevre/transaction").ITasksExportAction | undefined;
87
+ potentialActions?: import("@chevre/factory/lib/chevre/transaction/returnOrder").IPotentialActions | undefined;
88
+ } & {
89
+ _id: import("mongoose").Types.ObjectId;
90
+ } & {
91
+ __v: number;
92
+ })), import("mongoose").QueryOptions<IDocType>, (import("mongoose").Document<unknown, Record<string, never>, IDocType, import("./mongoose/virtuals").IVirtuals, {}> & (({
93
+ object: import("@chevre/factory/lib/chevre/transaction/placeOrder").IObject;
94
+ error?: any;
95
+ project: import("@chevre/factory/lib/chevre/transaction").IProject;
96
+ seller: import("@chevre/factory/lib/chevre/transaction/placeOrder").ISeller;
97
+ typeOf: factory.transactionType.PlaceOrder;
98
+ expires: Date;
99
+ result?: import("@chevre/factory/lib/chevre/transaction/placeOrder").IResult | undefined;
100
+ startDate: Date;
101
+ endDate?: Date | undefined;
102
+ instrument?: import("@chevre/factory/lib/chevre/transaction/placeOrder").IInstrument | undefined;
103
+ agent: import("@chevre/factory/lib/chevre/transaction/placeOrder").IAgent;
104
+ status: factory.transactionStatusType;
105
+ tasksExportAction?: import("@chevre/factory/lib/chevre/transaction").ITasksExportAction | undefined;
106
+ potentialActions?: undefined;
107
+ } & {
108
+ _id: import("mongoose").Types.ObjectId;
109
+ } & {
110
+ __v: number;
111
+ }) | ({
112
+ object: import("@chevre/factory/lib/chevre/transaction/returnOrder").IObject;
113
+ error?: any;
114
+ project: import("@chevre/factory/lib/chevre/transaction").IProject;
115
+ seller: import("@chevre/factory/lib/chevre/transaction").ISeller;
116
+ typeOf: factory.transactionType.ReturnOrder;
117
+ expires: Date;
118
+ result?: import("@chevre/factory/lib/chevre/transaction/returnOrder").IResult | undefined;
119
+ startDate: Date;
120
+ endDate?: Date | undefined;
121
+ agent: import("@chevre/factory/lib/chevre/transaction").IAgent;
122
+ recipient?: undefined;
123
+ status: factory.transactionStatusType;
124
+ tasksExportAction?: import("@chevre/factory/lib/chevre/transaction").ITasksExportAction | undefined;
125
+ potentialActions?: import("@chevre/factory/lib/chevre/transaction/returnOrder").IPotentialActions | undefined;
126
+ } & {
127
+ _id: import("mongoose").Types.ObjectId;
128
+ } & {
129
+ __v: number;
130
+ }))) | null>;
131
+ }
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AdminTransactionRepo = void 0;
4
+ const transaction_1 = require("../eventEmitter/transaction");
5
+ const factory_1 = require("../factory");
6
+ const settings_1 = require("../settings");
7
+ const transaction_2 = require("./mongoose/schemas/transaction");
8
+ /**
9
+ * 取引管理リポジトリ
10
+ */
11
+ class AdminTransactionRepo {
12
+ transactionModel;
13
+ constructor(connection) {
14
+ this.transactionModel = connection.model(transaction_2.modelName, (0, transaction_2.createSchema)());
15
+ }
16
+ async countPotentiallyExportTasks(params) {
17
+ const { endDate, limit } = params;
18
+ const endDateLt = endDate?.$lt;
19
+ if (!(endDateLt instanceof Date)) {
20
+ throw new factory_1.factory.errors.Argument('endDate.$lt', 'must be Date');
21
+ }
22
+ const query = this.transactionModel.countDocuments({
23
+ status: {
24
+ $in: [
25
+ factory_1.factory.transactionStatusType.Canceled,
26
+ factory_1.factory.transactionStatusType.Confirmed,
27
+ factory_1.factory.transactionStatusType.Expired
28
+ ]
29
+ },
30
+ 'tasksExportAction.actionStatus': {
31
+ $exists: true,
32
+ $eq: factory_1.factory.actionStatusType.PotentialActionStatus
33
+ },
34
+ endDate: { $exists: true, $lt: endDateLt }
35
+ });
36
+ if (typeof limit === 'number' && limit >= 0) {
37
+ query.limit(limit);
38
+ }
39
+ const count = await query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
40
+ .exec();
41
+ return { count };
42
+ }
43
+ /**
44
+ * tasksExportAction.actionStatusがActiveActionStatusのまま一定時間経過した取引について
45
+ * PotentialActionStatusに変更する
46
+ * 2025-03-10~
47
+ */
48
+ async reExportAction(params) {
49
+ const { startDate } = params;
50
+ if (!(startDate.$lt instanceof Date)) {
51
+ throw new factory_1.factory.errors.Argument('startDate.$lt', 'must be Date');
52
+ }
53
+ return this.transactionModel.updateMany({
54
+ 'tasksExportAction.actionStatus': {
55
+ $exists: true,
56
+ $eq: factory_1.factory.actionStatusType.ActiveActionStatus
57
+ },
58
+ 'tasksExportAction.startDate': {
59
+ $exists: true,
60
+ $lt: startDate.$lt
61
+ }
62
+ }, {
63
+ $set: {
64
+ tasksExportAction: {
65
+ actionStatus: factory_1.factory.actionStatusType.PotentialActionStatus
66
+ }
67
+ }
68
+ })
69
+ .exec();
70
+ }
71
+ // /**
72
+ // * タスクエクスポートの遅延している取引について明示的にemitTransactionStatusChangedを実行する
73
+ // */
74
+ // public async exportTasksMany(params: {
75
+ // now: Date;
76
+ // delayInSeconds: number;
77
+ // status: {
78
+ // $in: factory.transactionStatusType[];
79
+ // };
80
+ // limit: number;
81
+ // }) {
82
+ // const delayedTransactions = await this.transactionModel.find({
83
+ // status: { $in: params.status.$in },
84
+ // 'tasksExportAction.actionStatus': {
85
+ // $exists: true,
86
+ // $eq: factory.actionStatusType.PotentialActionStatus
87
+ // },
88
+ // endDate: {
89
+ // $exists: true,
90
+ // $lt: moment(params.now)
91
+ // .add(-params.delayInSeconds, 'seconds')
92
+ // .toDate()
93
+ // }
94
+ // })
95
+ // .select({
96
+ // _id: 0,
97
+ // id: { $toString: '$_id' },
98
+ // typeOf: 1,
99
+ // status: 1
100
+ // })
101
+ // .limit(params.limit)
102
+ // .setOptions({ maxTimeMS: MONGO_MAX_TIME_MS })
103
+ // .lean<Pick<factory.transaction.ITransaction<factory.transactionType>, 'id' | 'status' | 'typeOf'>[]>()
104
+ // .exec();
105
+ // if (delayedTransactions.length > 0) {
106
+ // delayedTransactions.forEach((delayedTransaction) => {
107
+ // transactionEventEmitter.emitTransactionStatusChanged({
108
+ // id: delayedTransaction.id,
109
+ // typeOf: delayedTransaction.typeOf,
110
+ // status: delayedTransaction.status
111
+ // });
112
+ // });
113
+ // }
114
+ // return delayedTransactions;
115
+ // }
116
+ /**
117
+ * add(2025-03-13~)
118
+ */
119
+ async countPotentiallyExpired(params) {
120
+ const { expires, limit } = params;
121
+ const query = this.transactionModel.countDocuments({
122
+ status: { $eq: factory_1.factory.transactionStatusType.InProgress },
123
+ expires: { $lt: expires.$lt }
124
+ });
125
+ if (typeof limit === 'number' && limit >= 0) {
126
+ query.limit(limit);
127
+ }
128
+ const count = await query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
129
+ .exec();
130
+ return { count };
131
+ }
132
+ /**
133
+ * add(2025-03-03~)
134
+ */
135
+ async makeOneExpiredIfExists(params) {
136
+ const endDate = new Date();
137
+ const sort = {
138
+ expires: factory_1.factory.sortType.Ascending
139
+ };
140
+ const doc = await this.transactionModel.findOneAndUpdate({
141
+ status: { $eq: factory_1.factory.transactionStatusType.InProgress },
142
+ expires: { $lt: params.expires.$lt }
143
+ }, {
144
+ status: factory_1.factory.transactionStatusType.Expired,
145
+ endDate
146
+ }, {
147
+ new: true,
148
+ projection: {
149
+ _id: 0,
150
+ id: { $toString: '$_id' },
151
+ typeOf: 1
152
+ }
153
+ })
154
+ .sort(sort)
155
+ .lean()
156
+ .exec();
157
+ if (doc === null) {
158
+ // no op
159
+ return;
160
+ }
161
+ else {
162
+ transaction_1.transactionEventEmitter.emitTransactionStatusChanged({
163
+ id: doc.id,
164
+ typeOf: doc.typeOf,
165
+ status: factory_1.factory.transactionStatusType.Expired
166
+ });
167
+ return doc;
168
+ }
169
+ }
170
+ // /**
171
+ // * 取引を期限切れにする
172
+ // */
173
+ // public async makeExpired(params: {
174
+ // expires: { $lt: Date };
175
+ // limit: number; // add limit(2025-02-27~)
176
+ // }): Promise<Pick<factory.transaction.ITransaction<factory.transactionType>, 'id' | 'typeOf'>[]> {
177
+ // if (typeof params.limit !== 'number' || params.limit === 0) {
178
+ // throw new factory.errors.ArgumentNull('limit');
179
+ // }
180
+ // const sort: { [key in keyof factory.transaction.ITransaction<factory.transactionType>]?: factory.sortType } = {
181
+ // expires: factory.sortType.Ascending
182
+ // };
183
+ // // IDをemitしたいのでまずリスト検索(2023-04-27~)
184
+ // const expiringTransactions =
185
+ // await this.transactionModel.find({
186
+ // status: { $eq: factory.transactionStatusType.InProgress },
187
+ // expires: { $lt: params.expires.$lt }
188
+ // })
189
+ // .select({
190
+ // _id: 0,
191
+ // id: { $toString: '$_id' },
192
+ // typeOf: 1
193
+ // })
194
+ // .limit(params.limit)
195
+ // .sort(sort) // sort(2025-03-03~)
196
+ // .setOptions({ maxTimeMS: MONGO_MAX_TIME_MS })
197
+ // .lean<Pick<factory.transaction.ITransaction<factory.transactionType>, 'id' | 'typeOf'>[]>()
198
+ // .exec();
199
+ // if (expiringTransactions.length > 0) {
200
+ // // ステータスと期限を見て更新
201
+ // await this.transactionModel.updateMany(
202
+ // {
203
+ // _id: { $in: expiringTransactions.map((t) => t.id) },
204
+ // status: { $eq: factory.transactionStatusType.InProgress },
205
+ // expires: { $lt: params.expires.$lt }
206
+ // },
207
+ // {
208
+ // status: factory.transactionStatusType.Expired,
209
+ // endDate: new Date()
210
+ // }
211
+ // )
212
+ // .exec();
213
+ // expiringTransactions.forEach((expiringTransaction) => {
214
+ // transactionEventEmitter.emitTransactionStatusChanged({
215
+ // id: expiringTransaction.id,
216
+ // typeOf: expiringTransaction.typeOf,
217
+ // status: factory.transactionStatusType.Expired
218
+ // });
219
+ // });
220
+ // }
221
+ // return expiringTransactions;
222
+ // }
223
+ async findByIdAndDelete(params) {
224
+ await this.transactionModel.findByIdAndDelete(params.id)
225
+ .exec();
226
+ }
227
+ async unsetUnnecessaryFields(params) {
228
+ return this.transactionModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false })
229
+ .exec();
230
+ }
231
+ getCursor(conditions, projection, sort) {
232
+ return this.transactionModel.find(conditions, projection)
233
+ .sort({ startDate: (sort === factory_1.factory.sortType.Ascending) ? sort : factory_1.factory.sortType.Descending })
234
+ .cursor();
235
+ }
236
+ }
237
+ exports.AdminTransactionRepo = AdminTransactionRepo;
@@ -0,0 +1,43 @@
1
+ import type { Connection } from 'mongoose';
2
+ import { factory } from '../factory';
3
+ interface IAggregationByStatus {
4
+ transactionCount: number;
5
+ avgDuration: number;
6
+ maxDuration: number;
7
+ minDuration: number;
8
+ percentilesDuration: {
9
+ name: string;
10
+ value: number;
11
+ }[];
12
+ reservationCount: number;
13
+ avgGraceTime: number;
14
+ maxGraceTime: number;
15
+ minGraceTime: number;
16
+ }
17
+ interface IStatus {
18
+ status: factory.transactionStatusType;
19
+ aggregation: IAggregationByStatus;
20
+ }
21
+ export interface IAggregateReserve {
22
+ statuses: IStatus[];
23
+ }
24
+ /**
25
+ * 資産取引集計リポジトリ
26
+ */
27
+ export declare class AggregateAssetTransactionRepo {
28
+ private readonly transactionModel;
29
+ constructor(connection: Connection);
30
+ aggregateAssetTransaction(params: {
31
+ project?: {
32
+ id?: {
33
+ $eq?: string;
34
+ $ne?: string;
35
+ };
36
+ };
37
+ startFrom: Date;
38
+ startThrough: Date;
39
+ typeOf: factory.assetTransactionType;
40
+ }): Promise<IAggregateReserve>;
41
+ private agggregateByStatus;
42
+ }
43
+ export {};
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AggregateAssetTransactionRepo = void 0;
4
+ const assetTransaction_1 = require("./mongoose/schemas/assetTransaction");
5
+ const factory_1 = require("../factory");
6
+ /**
7
+ * 資産取引集計リポジトリ
8
+ */
9
+ class AggregateAssetTransactionRepo {
10
+ transactionModel;
11
+ constructor(connection) {
12
+ this.transactionModel = connection.model(assetTransaction_1.modelName, (0, assetTransaction_1.createSchema)());
13
+ }
14
+ async aggregateAssetTransaction(params) {
15
+ const statuses = await Promise.all([
16
+ factory_1.factory.transactionStatusType.Confirmed,
17
+ factory_1.factory.transactionStatusType.Canceled,
18
+ factory_1.factory.transactionStatusType.Expired
19
+ ].map(async (transactionStatus) => {
20
+ const matchConditions = {
21
+ startDate: {
22
+ $gte: params.startFrom,
23
+ $lte: params.startThrough
24
+ },
25
+ typeOf: { $eq: params.typeOf },
26
+ status: { $eq: transactionStatus },
27
+ ...(typeof params.project?.id?.$ne === 'string')
28
+ ? { 'project.id': { $ne: params.project.id.$ne } }
29
+ : undefined,
30
+ ...(typeof params.project?.id?.$eq === 'string')
31
+ ? { 'project.id': { $eq: params.project.id.$eq } }
32
+ : undefined
33
+ };
34
+ return this.agggregateByStatus({ matchConditions, status: transactionStatus });
35
+ }));
36
+ return { statuses };
37
+ }
38
+ async agggregateByStatus(params) {
39
+ const matchConditions = params.matchConditions;
40
+ const transactionStatus = params.status;
41
+ const aggregations = await this.transactionModel.aggregate([
42
+ {
43
+ $match: matchConditions
44
+ },
45
+ {
46
+ $project: {
47
+ duration: { $subtract: ['$endDate', '$startDate'] },
48
+ graceTime: {
49
+ $cond: {
50
+ if: { $eq: ['$typeOf', factory_1.factory.assetTransactionType.Reserve] },
51
+ then: { $subtract: ['$object.reservationFor.startDate', '$startDate'] },
52
+ else: 0
53
+ }
54
+ },
55
+ status: '$status',
56
+ startDate: '$startDate',
57
+ endDate: '$endDate',
58
+ typeOf: '$typeOf',
59
+ reservationCount: {
60
+ $cond: {
61
+ if: { $isArray: '$object.subReservation' },
62
+ then: { $size: '$object.subReservation' },
63
+ else: 0
64
+ }
65
+ }
66
+ }
67
+ },
68
+ {
69
+ $group: {
70
+ _id: '$typeOf',
71
+ transactionCount: { $sum: 1 },
72
+ maxDuration: { $max: '$duration' },
73
+ minDuration: { $min: '$duration' },
74
+ avgDuration: { $avg: '$duration' },
75
+ reservationCount: { $sum: '$reservationCount' },
76
+ maxGraceTime: { $max: '$graceTime' },
77
+ minGraceTime: { $min: '$graceTime' },
78
+ avgGraceTime: { $avg: '$graceTime' }
79
+ }
80
+ },
81
+ {
82
+ $project: {
83
+ _id: 0,
84
+ transactionCount: '$transactionCount',
85
+ avgDuration: '$avgDuration',
86
+ maxDuration: '$maxDuration',
87
+ minDuration: '$minDuration',
88
+ reservationCount: '$reservationCount',
89
+ avgGraceTime: '$avgGraceTime',
90
+ maxGraceTime: '$maxGraceTime',
91
+ minGraceTime: '$minGraceTime'
92
+ }
93
+ }
94
+ ])
95
+ .exec();
96
+ const percents = [50, 95, 99];
97
+ if (aggregations.length === 0) {
98
+ return {
99
+ status: transactionStatus,
100
+ aggregation: {
101
+ transactionCount: 0,
102
+ avgDuration: 0,
103
+ maxDuration: 0,
104
+ minDuration: 0,
105
+ percentilesDuration: percents.map((percent) => {
106
+ return {
107
+ name: String(percent),
108
+ value: 0
109
+ };
110
+ }),
111
+ reservationCount: 0,
112
+ avgGraceTime: 0,
113
+ maxGraceTime: 0,
114
+ minGraceTime: 0
115
+ }
116
+ };
117
+ }
118
+ const ranks4percentile = percents.map((percentile) => {
119
+ return {
120
+ percentile,
121
+ rank: Math.floor(aggregations[0].transactionCount * percentile / 100)
122
+ };
123
+ });
124
+ const aggregations2 = await this.transactionModel.aggregate([
125
+ {
126
+ $match: matchConditions
127
+ },
128
+ {
129
+ $project: {
130
+ duration: { $subtract: ['$endDate', '$startDate'] },
131
+ status: '$status',
132
+ startDate: '$startDate',
133
+ endDate: '$endDate',
134
+ typeOf: '$typeOf'
135
+ }
136
+ },
137
+ { $sort: { duration: 1 } },
138
+ {
139
+ $group: {
140
+ _id: '$typeOf',
141
+ durations: { $push: '$duration' }
142
+ }
143
+ },
144
+ {
145
+ $project: {
146
+ _id: 0,
147
+ percentilesDuration: ranks4percentile.map((rank) => {
148
+ return {
149
+ name: String(rank.percentile),
150
+ value: { $arrayElemAt: ['$durations', rank.rank] }
151
+ };
152
+ })
153
+ }
154
+ }
155
+ ])
156
+ .exec();
157
+ return {
158
+ status: transactionStatus,
159
+ aggregation: {
160
+ ...aggregations[0],
161
+ ...aggregations2[0]
162
+ }
163
+ };
164
+ }
165
+ }
166
+ exports.AggregateAssetTransactionRepo = AggregateAssetTransactionRepo;
@@ -0,0 +1,39 @@
1
+ import type { Connection } from 'mongoose';
2
+ import { factory } from '../factory';
3
+ interface IAggregationByStatus {
4
+ transactionCount: number;
5
+ avgDuration: number;
6
+ maxDuration: number;
7
+ minDuration: number;
8
+ percentilesDuration: {
9
+ name: string;
10
+ value: number;
11
+ }[];
12
+ }
13
+ interface IStatus {
14
+ status: factory.transactionStatusType;
15
+ aggregation: IAggregationByStatus;
16
+ }
17
+ export interface IAggregatePlaceOrder {
18
+ statuses: IStatus[];
19
+ }
20
+ /**
21
+ * 取引集計リポジトリ
22
+ */
23
+ export declare class AggregateTransactionRepo {
24
+ private readonly transactionModel;
25
+ constructor(connection: Connection);
26
+ aggregatePlaceOrder(params: {
27
+ project?: {
28
+ id?: {
29
+ $ne?: string;
30
+ };
31
+ };
32
+ startFrom: Date;
33
+ startThrough: Date;
34
+ typeOf: factory.transactionType;
35
+ clientId?: string;
36
+ }): Promise<IAggregatePlaceOrder>;
37
+ private agggregateByStatus;
38
+ }
39
+ export {};