@chevre/domain 24.0.0-alpha.14 → 24.0.0-alpha.16
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.
- package/lib/chevre/repo/transaction/moneyTransfer.d.ts +61 -0
- package/lib/chevre/repo/transaction/moneyTransfer.js +379 -0
- package/lib/chevre/repo/transaction/returnOrder.d.ts +22 -0
- package/lib/chevre/repo/transaction/returnOrder.js +135 -7
- package/lib/chevre/repo/transaction.d.ts +1 -52
- package/lib/chevre/repo/transaction.js +442 -409
- package/lib/chevre/repository.d.ts +5 -0
- package/lib/chevre/repository.js +11 -0
- package/lib/chevre/service/task/deleteTransaction.js +3 -1
- package/lib/chevre/service/transaction/deleteTransaction.d.ts +2 -0
- package/lib/chevre/service/transaction/deleteTransaction.js +2 -2
- package/lib/chevre/service/transaction/moneyTransfer.d.ts +5 -5
- package/lib/chevre/service/transaction/moneyTransfer.js +5 -5
- package/lib/chevre/service/transaction/returnOrder/preStart.d.ts +1 -1
- package/lib/chevre/service/transaction/returnOrder.d.ts +16 -14
- package/lib/chevre/service/transaction/returnOrder.js +4 -4
- package/lib/chevre/service/transaction.d.ts +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Connection, FilterQuery } from 'mongoose';
|
|
2
|
+
import * as factory from '../../factory';
|
|
3
|
+
type IKeyOfProjection = keyof Pick<factory.transaction.ITransaction<factory.transactionType.MoneyTransfer>, 'agent' | 'endDate' | 'expires' | 'project' | 'startDate' | 'status' | 'typeOf'> | Exclude<keyof factory.transaction.ITransaction<factory.transactionType.MoneyTransfer>, 'id'>;
|
|
4
|
+
type IOmittedFieldsInProgress = 'tasksExportAction' | 'tasksExportedAt' | 'tasksExportationStatus';
|
|
5
|
+
export type IMoneyTransferTransactionInProgress = Omit<factory.transaction.moneyTransfer.ITransaction, IOmittedFieldsInProgress>;
|
|
6
|
+
type IStartedTransactionFields = 'expires' | 'id' | 'startDate' | 'status';
|
|
7
|
+
export type IStartedTransaction = Pick<factory.transaction.ITransaction<factory.transactionType>, IStartedTransactionFields>;
|
|
8
|
+
/**
|
|
9
|
+
* 通貨転送取引リポジトリ
|
|
10
|
+
*/
|
|
11
|
+
export declare class MoneyTransferRepo {
|
|
12
|
+
private readonly transactionModel;
|
|
13
|
+
constructor(connection: Connection);
|
|
14
|
+
static CREATE_MONGO_CONDITIONS(params: factory.transaction.ISearchConditions<factory.transactionType.MoneyTransfer>): FilterQuery<import("@chevre/factory/lib/assetTransaction/cancelReservation").ITransaction | import("@chevre/factory/lib/assetTransaction/moneyTransfer").ITransaction | import("@chevre/factory/lib/assetTransaction/pay").ITransaction | import("@chevre/factory/lib/assetTransaction/refund").ITransaction | import("@chevre/factory/lib/assetTransaction/registerService").ITransaction | import("@chevre/factory/lib/assetTransaction/reserve").ITransaction>[];
|
|
15
|
+
/**
|
|
16
|
+
* 取引を開始する
|
|
17
|
+
*/
|
|
18
|
+
startMoneyTransfer(params: factory.transaction.IStartParams<factory.transactionType.MoneyTransfer>): Promise<IStartedTransaction>;
|
|
19
|
+
/**
|
|
20
|
+
* 特定取引検索
|
|
21
|
+
*/
|
|
22
|
+
findMoneyTransferById(params: {
|
|
23
|
+
typeOf: factory.transactionType.MoneyTransfer;
|
|
24
|
+
id: string;
|
|
25
|
+
}, inclusion: IKeyOfProjection[]): Promise<factory.transaction.ITransaction<factory.transactionType.MoneyTransfer>>;
|
|
26
|
+
/**
|
|
27
|
+
* 進行中の取引を取得する
|
|
28
|
+
*/
|
|
29
|
+
findMoneyTransferInProgressById(params: {
|
|
30
|
+
typeOf: factory.transactionType.MoneyTransfer;
|
|
31
|
+
id: string;
|
|
32
|
+
}, inclusion: IKeyOfProjection[]): Promise<IMoneyTransferTransactionInProgress>;
|
|
33
|
+
/**
|
|
34
|
+
* 取引を確定する
|
|
35
|
+
*/
|
|
36
|
+
confirmMoneyTransfer(params: {
|
|
37
|
+
typeOf: factory.transactionType.MoneyTransfer;
|
|
38
|
+
id: string;
|
|
39
|
+
result: factory.transaction.IResult<factory.transactionType.MoneyTransfer>;
|
|
40
|
+
potentialActions: factory.transaction.IPotentialActions<factory.transactionType.MoneyTransfer>;
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* 取引を中止する
|
|
44
|
+
*/
|
|
45
|
+
cancelMoneyTransfer(params: {
|
|
46
|
+
typeOf: factory.transactionType.MoneyTransfer;
|
|
47
|
+
id: string;
|
|
48
|
+
}): Promise<void>;
|
|
49
|
+
countMoneyTransfer(params: factory.transaction.ISearchConditions<factory.transactionType.MoneyTransfer>): Promise<{
|
|
50
|
+
count: number;
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* 取引を検索する
|
|
54
|
+
*/
|
|
55
|
+
findMoneyTransferTransactions(params: factory.transaction.ISearchConditions<factory.transactionType.MoneyTransfer> & {
|
|
56
|
+
inclusion: IKeyOfProjection[];
|
|
57
|
+
}): Promise<(Pick<factory.transaction.ITransaction<factory.transactionType.MoneyTransfer>, IKeyOfProjection> & {
|
|
58
|
+
id: string;
|
|
59
|
+
})[]>;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.MoneyTransferRepo = void 0;
|
|
30
|
+
const moment_1 = __importDefault(require("moment"));
|
|
31
|
+
const transaction_1 = require("../../eventEmitter/transaction");
|
|
32
|
+
const factory = __importStar(require("../../factory"));
|
|
33
|
+
const settings_1 = require("../../settings");
|
|
34
|
+
const transaction_2 = require("../mongoose/schemas/transaction");
|
|
35
|
+
const AVAILABLE_PROJECT_FIELDS = [
|
|
36
|
+
'project',
|
|
37
|
+
'status',
|
|
38
|
+
'typeOf',
|
|
39
|
+
'agent',
|
|
40
|
+
'recipient',
|
|
41
|
+
'seller',
|
|
42
|
+
'error',
|
|
43
|
+
'result',
|
|
44
|
+
'object',
|
|
45
|
+
'expires',
|
|
46
|
+
'startDate',
|
|
47
|
+
'endDate',
|
|
48
|
+
'tasksExportAction',
|
|
49
|
+
'potentialActions',
|
|
50
|
+
// 'instrument'
|
|
51
|
+
];
|
|
52
|
+
/**
|
|
53
|
+
* 通貨転送取引リポジトリ
|
|
54
|
+
*/
|
|
55
|
+
class MoneyTransferRepo {
|
|
56
|
+
transactionModel;
|
|
57
|
+
constructor(connection) {
|
|
58
|
+
this.transactionModel = connection.model(transaction_2.modelName, (0, transaction_2.createSchema)());
|
|
59
|
+
}
|
|
60
|
+
static CREATE_MONGO_CONDITIONS(params) {
|
|
61
|
+
if (params.typeOf !== factory.transactionType.MoneyTransfer) {
|
|
62
|
+
throw new factory.errors.Argument('typeOf', `must be ${factory.transactionType.MoneyTransfer}`);
|
|
63
|
+
}
|
|
64
|
+
const andConditions = [];
|
|
65
|
+
const projectIdEq = params.project?.id?.$eq;
|
|
66
|
+
if (typeof projectIdEq === 'string') {
|
|
67
|
+
andConditions.push({ 'project.id': { $eq: projectIdEq } });
|
|
68
|
+
}
|
|
69
|
+
if (typeof params.typeOf === 'string') {
|
|
70
|
+
andConditions.push({ typeOf: { $eq: params.typeOf } });
|
|
71
|
+
}
|
|
72
|
+
if (params.startFrom !== undefined) {
|
|
73
|
+
andConditions.push({
|
|
74
|
+
startDate: { $gte: params.startFrom }
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
if (params.startThrough !== undefined) {
|
|
78
|
+
andConditions.push({
|
|
79
|
+
startDate: { $lte: params.startThrough }
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (params.endFrom !== undefined) {
|
|
83
|
+
andConditions.push({
|
|
84
|
+
endDate: {
|
|
85
|
+
$exists: true,
|
|
86
|
+
$gte: params.endFrom
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (params.endThrough !== undefined) {
|
|
91
|
+
andConditions.push({
|
|
92
|
+
endDate: {
|
|
93
|
+
$exists: true,
|
|
94
|
+
$lt: params.endThrough
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (Array.isArray(params.ids)) {
|
|
99
|
+
andConditions.push({
|
|
100
|
+
_id: { $in: params.ids }
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (Array.isArray(params.statuses)) {
|
|
104
|
+
andConditions.push({ status: { $in: params.statuses } });
|
|
105
|
+
}
|
|
106
|
+
const statusIn = params.status?.$in;
|
|
107
|
+
if (Array.isArray(statusIn)) {
|
|
108
|
+
andConditions.push({ status: { $in: statusIn } });
|
|
109
|
+
}
|
|
110
|
+
if (params.agent !== undefined) {
|
|
111
|
+
if (params.agent.typeOf !== undefined) {
|
|
112
|
+
andConditions.push({
|
|
113
|
+
'agent.typeOf': {
|
|
114
|
+
$exists: true,
|
|
115
|
+
$eq: params.agent.typeOf
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (Array.isArray(params.agent.ids)) {
|
|
120
|
+
andConditions.push({
|
|
121
|
+
'agent.id': {
|
|
122
|
+
$exists: true,
|
|
123
|
+
$in: params.agent.ids
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (Array.isArray(params.agent.identifiers)) {
|
|
128
|
+
andConditions.push({
|
|
129
|
+
'agent.identifier': {
|
|
130
|
+
$exists: true,
|
|
131
|
+
$in: params.agent.identifiers
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const tasksExportActionStatusIn = params.tasksExportAction?.actionStatus?.$in;
|
|
137
|
+
if (Array.isArray(tasksExportActionStatusIn)) {
|
|
138
|
+
andConditions.push({ 'tasksExportAction.actionStatus': { $exists: true, $in: tasksExportActionStatusIn } });
|
|
139
|
+
}
|
|
140
|
+
const tasksExportActionStatusEq = params.tasksExportAction?.actionStatus?.$eq;
|
|
141
|
+
if (typeof tasksExportActionStatusEq === 'string') {
|
|
142
|
+
andConditions.push({ 'tasksExportAction.actionStatus': { $exists: true, $eq: tasksExportActionStatusEq } });
|
|
143
|
+
}
|
|
144
|
+
switch (params.typeOf) {
|
|
145
|
+
case factory.transactionType.PlaceOrder:
|
|
146
|
+
throw new factory.errors.NotImplemented(`use PlaceOrderRepo`);
|
|
147
|
+
case factory.transactionType.ReturnOrder:
|
|
148
|
+
throw new factory.errors.NotImplemented(`use ReturnOrderRepo`);
|
|
149
|
+
default:
|
|
150
|
+
}
|
|
151
|
+
return andConditions;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 取引を開始する
|
|
155
|
+
*/
|
|
156
|
+
// public async startNoPlaceOrder<
|
|
157
|
+
async startMoneyTransfer(params) {
|
|
158
|
+
const status = factory.transactionStatusType.InProgress;
|
|
159
|
+
const tasksExportAction = { actionStatus: factory.actionStatusType.PotentialActionStatus };
|
|
160
|
+
// const tasksExportationStatus = factory.transactionTasksExportationStatus.Unexported; // discontinue(2024-06-20~)
|
|
161
|
+
const startDate = new Date();
|
|
162
|
+
// let expires: Date;
|
|
163
|
+
const { typeOf } = params;
|
|
164
|
+
const expires = params.expires;
|
|
165
|
+
let creatingTransaction;
|
|
166
|
+
if (typeOf === factory.transactionType.MoneyTransfer) {
|
|
167
|
+
const { agent, project, object, seller, recipient } = params;
|
|
168
|
+
creatingTransaction = {
|
|
169
|
+
status, startDate, expires, typeOf, tasksExportAction,
|
|
170
|
+
agent, project, seller, object, recipient
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
throw new factory.errors.NotImplemented(`${typeOf} not implemented`);
|
|
175
|
+
}
|
|
176
|
+
// reimplemnt with insertMany(2024-05-30~)
|
|
177
|
+
const result = await this.transactionModel.insertMany(creatingTransaction, { rawResult: true });
|
|
178
|
+
const id = result.insertedIds?.[0]?.toHexString();
|
|
179
|
+
if (typeof id !== 'string') {
|
|
180
|
+
throw new factory.errors.Internal('transaction not saved');
|
|
181
|
+
}
|
|
182
|
+
// 取引開始時にも取引イベントエミッター連携(2024-03-21~)
|
|
183
|
+
transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id, typeOf, status });
|
|
184
|
+
return { expires, id, startDate, status }; // minimize response(2024-05-30~)
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* 特定取引検索
|
|
188
|
+
*/
|
|
189
|
+
async findMoneyTransferById(params, inclusion // make required(2024-05-31~)
|
|
190
|
+
) {
|
|
191
|
+
let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
|
|
192
|
+
if (Array.isArray(inclusion) && inclusion.length > 0) {
|
|
193
|
+
positiveProjectionFields = positiveProjectionFields.filter((key) => inclusion.includes(key));
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
throw new factory.errors.NotImplemented('inclusion must be specified'); // 2024-08-26~
|
|
197
|
+
}
|
|
198
|
+
const projection = {
|
|
199
|
+
_id: 0,
|
|
200
|
+
id: { $toString: '$_id' },
|
|
201
|
+
...Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))
|
|
202
|
+
};
|
|
203
|
+
const doc = await this.transactionModel.findOne({
|
|
204
|
+
_id: { $eq: params.id },
|
|
205
|
+
typeOf: { $eq: params.typeOf }
|
|
206
|
+
}, projection)
|
|
207
|
+
.lean() // 2024-08-26~
|
|
208
|
+
.exec();
|
|
209
|
+
if (doc === null) {
|
|
210
|
+
throw new factory.errors.NotFound(this.transactionModel.modelName, `${params.typeOf} not found`);
|
|
211
|
+
}
|
|
212
|
+
return doc;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 進行中の取引を取得する
|
|
216
|
+
*/
|
|
217
|
+
async findMoneyTransferInProgressById(params, inclusion // make required(2024-05-29~)
|
|
218
|
+
) {
|
|
219
|
+
let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
|
|
220
|
+
if (Array.isArray(inclusion) && inclusion.length > 0) {
|
|
221
|
+
positiveProjectionFields = positiveProjectionFields.filter((key) => inclusion.includes(key));
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
throw new factory.errors.NotImplemented('inclusion must be specified'); // 2024-08-26~
|
|
225
|
+
}
|
|
226
|
+
const projection = {
|
|
227
|
+
_id: 0,
|
|
228
|
+
id: { $toString: '$_id' },
|
|
229
|
+
...Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))
|
|
230
|
+
};
|
|
231
|
+
const filter = {
|
|
232
|
+
_id: { $eq: params.id },
|
|
233
|
+
typeOf: { $eq: params.typeOf },
|
|
234
|
+
status: { $eq: factory.transactionStatusType.InProgress },
|
|
235
|
+
// expiresを条件に追加(2023-05-12~),
|
|
236
|
+
expires: { $gt: new Date() }
|
|
237
|
+
};
|
|
238
|
+
const doc = await this.transactionModel.findOne(filter, projection)
|
|
239
|
+
.lean() // 2024-08-26~
|
|
240
|
+
.exec();
|
|
241
|
+
if (doc === null) {
|
|
242
|
+
throw new factory.errors.NotFound(this.transactionModel.modelName, `${params.typeOf} ${factory.transactionStatusType.InProgress} not found`);
|
|
243
|
+
}
|
|
244
|
+
return doc;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* 取引を確定する
|
|
248
|
+
*/
|
|
249
|
+
// public async confirmNoPlaceOrder<
|
|
250
|
+
async confirmMoneyTransfer(params) {
|
|
251
|
+
const endDate = new Date();
|
|
252
|
+
const doc = await this.transactionModel.findOneAndUpdate({
|
|
253
|
+
_id: { $eq: params.id },
|
|
254
|
+
typeOf: { $eq: params.typeOf },
|
|
255
|
+
status: { $eq: factory.transactionStatusType.InProgress },
|
|
256
|
+
expires: { $gt: endDate } // add expires(2025-02-27~)
|
|
257
|
+
}, {
|
|
258
|
+
status: factory.transactionStatusType.Confirmed, // ステータス変更
|
|
259
|
+
endDate,
|
|
260
|
+
// 'object.authorizeActions': params.authorizeActions,
|
|
261
|
+
result: params.result, // resultを更新
|
|
262
|
+
potentialActions: params.potentialActions // resultを更新
|
|
263
|
+
}, {
|
|
264
|
+
new: true,
|
|
265
|
+
projection: { _id: 1 }
|
|
266
|
+
})
|
|
267
|
+
.lean()
|
|
268
|
+
.exec();
|
|
269
|
+
// NotFoundであれば取引状態確認
|
|
270
|
+
if (doc === null) {
|
|
271
|
+
const { expires, status } = await this.findMoneyTransferById({ typeOf: params.typeOf, id: params.id }, ['expires', 'status']);
|
|
272
|
+
if (status === factory.transactionStatusType.Confirmed) {
|
|
273
|
+
// すでに確定済の場合スルー
|
|
274
|
+
}
|
|
275
|
+
else if (status === factory.transactionStatusType.Expired) {
|
|
276
|
+
throw new factory.errors.Argument('Transaction id', 'Already expired');
|
|
277
|
+
}
|
|
278
|
+
else if (status === factory.transactionStatusType.Canceled) {
|
|
279
|
+
throw new factory.errors.Argument('Transaction id', 'Already canceled');
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
if ((0, moment_1.default)(expires)
|
|
283
|
+
.isSameOrBefore((0, moment_1.default)(endDate))) {
|
|
284
|
+
throw new factory.errors.Argument('Transaction id', 'potentially expired');
|
|
285
|
+
}
|
|
286
|
+
throw new factory.errors.NotFound(this.transactionModel.modelName, `${params.typeOf} ${factory.transactionStatusType.InProgress} not found`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
transaction_1.transactionEventEmitter.emitTransactionStatusChanged({
|
|
290
|
+
id: params.id,
|
|
291
|
+
typeOf: params.typeOf,
|
|
292
|
+
status: factory.transactionStatusType.Confirmed
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 取引を中止する
|
|
297
|
+
*/
|
|
298
|
+
async cancelMoneyTransfer(params) {
|
|
299
|
+
const endDate = new Date();
|
|
300
|
+
// 進行中ステータスの取引を中止する
|
|
301
|
+
const doc = await this.transactionModel.findOneAndUpdate({
|
|
302
|
+
typeOf: params.typeOf,
|
|
303
|
+
_id: params.id,
|
|
304
|
+
status: factory.transactionStatusType.InProgress
|
|
305
|
+
}, {
|
|
306
|
+
status: factory.transactionStatusType.Canceled,
|
|
307
|
+
endDate: endDate
|
|
308
|
+
}, {
|
|
309
|
+
new: true,
|
|
310
|
+
projection: { _id: 1 }
|
|
311
|
+
})
|
|
312
|
+
.lean()
|
|
313
|
+
.exec();
|
|
314
|
+
// NotFoundであれば取引状態確認
|
|
315
|
+
if (doc === null) {
|
|
316
|
+
const { status } = await this.findMoneyTransferById(params, ['status']);
|
|
317
|
+
if (status === factory.transactionStatusType.Canceled) {
|
|
318
|
+
// すでに中止済の場合スルー
|
|
319
|
+
}
|
|
320
|
+
else if (status === factory.transactionStatusType.Expired) {
|
|
321
|
+
throw new factory.errors.Argument('Transaction id', 'Transaction already expired');
|
|
322
|
+
}
|
|
323
|
+
else if (status === factory.transactionStatusType.Confirmed) {
|
|
324
|
+
throw new factory.errors.Argument('Transaction id', 'Confirmed transaction unable to cancel');
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
throw new factory.errors.NotFound(this.transactionModel.modelName, `${params.typeOf} ${factory.transactionStatusType.InProgress} not found`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
transaction_1.transactionEventEmitter.emitTransactionStatusChanged({
|
|
331
|
+
id: params.id,
|
|
332
|
+
typeOf: params.typeOf,
|
|
333
|
+
status: factory.transactionStatusType.Canceled
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
async countMoneyTransfer(params) {
|
|
337
|
+
const { limit } = params;
|
|
338
|
+
const conditions = MoneyTransferRepo.CREATE_MONGO_CONDITIONS(params);
|
|
339
|
+
const query = this.transactionModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {});
|
|
340
|
+
if (typeof limit === 'number' && limit >= 0) {
|
|
341
|
+
query.limit(limit);
|
|
342
|
+
}
|
|
343
|
+
const count = await query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
|
|
344
|
+
.exec();
|
|
345
|
+
return { count };
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* 取引を検索する
|
|
349
|
+
*/
|
|
350
|
+
async findMoneyTransferTransactions(params) {
|
|
351
|
+
const { inclusion } = params;
|
|
352
|
+
const conditions = MoneyTransferRepo.CREATE_MONGO_CONDITIONS(params);
|
|
353
|
+
let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
|
|
354
|
+
if (Array.isArray(inclusion) && inclusion.length > 0) {
|
|
355
|
+
positiveProjectionFields = positiveProjectionFields.filter((key) => inclusion.includes(key));
|
|
356
|
+
}
|
|
357
|
+
const projection = {
|
|
358
|
+
_id: 0,
|
|
359
|
+
id: { $toString: '$_id' },
|
|
360
|
+
...Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))
|
|
361
|
+
};
|
|
362
|
+
const query = this.transactionModel.find((conditions.length > 0) ? { $and: conditions } : {})
|
|
363
|
+
.select(projection);
|
|
364
|
+
if (typeof params.limit === 'number' && params.limit > 0) {
|
|
365
|
+
const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
|
|
366
|
+
query.limit(params.limit)
|
|
367
|
+
.skip(params.limit * (page - 1));
|
|
368
|
+
}
|
|
369
|
+
if (params.sort?.startDate !== undefined) {
|
|
370
|
+
query.sort({ startDate: params.sort.startDate });
|
|
371
|
+
}
|
|
372
|
+
// const explainResult = await (<any>query).explain();
|
|
373
|
+
// console.log(explainResult[0].executionStats.allPlansExecution.map((e: any) => e.executionStages.inputStage));
|
|
374
|
+
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
|
|
375
|
+
.lean()
|
|
376
|
+
.exec();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
exports.MoneyTransferRepo = MoneyTransferRepo;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { Connection, FilterQuery } from 'mongoose';
|
|
2
2
|
import * as factory from '../../factory';
|
|
3
3
|
type IKeyOfProjection = keyof Pick<factory.transaction.ITransaction<factory.transactionType.ReturnOrder>, 'agent' | 'endDate' | 'expires' | 'project' | 'startDate' | 'status' | 'typeOf'> | Exclude<keyof factory.transaction.ITransaction<factory.transactionType.ReturnOrder>, 'id'>;
|
|
4
|
+
type IStartedTransactionFields = 'expires' | 'id' | 'startDate' | 'status';
|
|
5
|
+
export type IStartedTransaction = Pick<factory.transaction.ITransaction<factory.transactionType.ReturnOrder>, IStartedTransactionFields>;
|
|
4
6
|
/**
|
|
5
7
|
* 返品取引リポジトリ
|
|
6
8
|
*/
|
|
@@ -19,5 +21,25 @@ export declare class ReturnOrderRepo {
|
|
|
19
21
|
}): Promise<(Pick<factory.transaction.ITransaction<factory.transactionType.ReturnOrder>, IKeyOfProjection> & {
|
|
20
22
|
id: string;
|
|
21
23
|
})[]>;
|
|
24
|
+
/**
|
|
25
|
+
* 取引を開始する
|
|
26
|
+
*/
|
|
27
|
+
startReturnOrder(params: factory.transaction.IStartParams<factory.transactionType.ReturnOrder>): Promise<IStartedTransaction>;
|
|
28
|
+
/**
|
|
29
|
+
* 特定取引検索
|
|
30
|
+
*/
|
|
31
|
+
findReturnOrderById(params: {
|
|
32
|
+
typeOf: factory.transactionType.ReturnOrder;
|
|
33
|
+
id: string;
|
|
34
|
+
}, inclusion: IKeyOfProjection[]): Promise<factory.transaction.ITransaction<factory.transactionType.ReturnOrder>>;
|
|
35
|
+
/**
|
|
36
|
+
* 取引を確定する
|
|
37
|
+
*/
|
|
38
|
+
confirmReturnOrder(params: {
|
|
39
|
+
typeOf: factory.transactionType.ReturnOrder;
|
|
40
|
+
id: string;
|
|
41
|
+
result: factory.transaction.IResult<factory.transactionType.ReturnOrder>;
|
|
42
|
+
potentialActions: factory.transaction.IPotentialActions<factory.transactionType.ReturnOrder>;
|
|
43
|
+
}): Promise<void>;
|
|
22
44
|
}
|
|
23
45
|
export {};
|
|
@@ -22,12 +22,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
25
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
29
|
exports.ReturnOrderRepo = void 0;
|
|
27
|
-
|
|
30
|
+
const moment_1 = __importDefault(require("moment"));
|
|
31
|
+
const transaction_1 = require("../../eventEmitter/transaction");
|
|
28
32
|
const factory = __importStar(require("../../factory"));
|
|
29
33
|
const settings_1 = require("../../settings");
|
|
30
|
-
const
|
|
34
|
+
const transaction_2 = require("../mongoose/schemas/transaction");
|
|
31
35
|
const AVAILABLE_PROJECT_FIELDS = [
|
|
32
36
|
'project',
|
|
33
37
|
'status',
|
|
@@ -45,17 +49,13 @@ const AVAILABLE_PROJECT_FIELDS = [
|
|
|
45
49
|
'potentialActions',
|
|
46
50
|
// 'instrument'
|
|
47
51
|
];
|
|
48
|
-
// type IOmittedFieldsInProgress = 'tasksExportAction' | 'tasksExportedAt' | 'tasksExportationStatus';
|
|
49
|
-
// export type IMoneyTransferTransactionInProgress = Omit<factory.transaction.moneyTransfer.ITransaction, IOmittedFieldsInProgress>;
|
|
50
|
-
// type IStartedTransactionFields = 'expires' | 'id' | 'startDate' | 'status';
|
|
51
|
-
// export type IStartedTransaction = Pick<factory.transaction.ITransaction<factory.transactionType>, IStartedTransactionFields>;
|
|
52
52
|
/**
|
|
53
53
|
* 返品取引リポジトリ
|
|
54
54
|
*/
|
|
55
55
|
class ReturnOrderRepo {
|
|
56
56
|
transactionModel;
|
|
57
57
|
constructor(connection) {
|
|
58
|
-
this.transactionModel = connection.model(
|
|
58
|
+
this.transactionModel = connection.model(transaction_2.modelName, (0, transaction_2.createSchema)());
|
|
59
59
|
}
|
|
60
60
|
static CREATE_MONGO_CONDITIONS(params) {
|
|
61
61
|
if (params.typeOf !== factory.transactionType.ReturnOrder) {
|
|
@@ -209,5 +209,133 @@ class ReturnOrderRepo {
|
|
|
209
209
|
.lean()
|
|
210
210
|
.exec();
|
|
211
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* 取引を開始する
|
|
214
|
+
*/
|
|
215
|
+
async startReturnOrder(params) {
|
|
216
|
+
const status = factory.transactionStatusType.InProgress;
|
|
217
|
+
const tasksExportAction = { actionStatus: factory.actionStatusType.PotentialActionStatus };
|
|
218
|
+
// const tasksExportationStatus = factory.transactionTasksExportationStatus.Unexported; // discontinue(2024-06-20~)
|
|
219
|
+
const startDate = new Date();
|
|
220
|
+
let expires;
|
|
221
|
+
const { typeOf } = params;
|
|
222
|
+
// expiresInSecondsの指定があれば優先して適用する(2022-11-25~)
|
|
223
|
+
if (typeof params.expiresInSeconds === 'number' && params.expiresInSeconds > 0) {
|
|
224
|
+
expires = (0, moment_1.default)(startDate)
|
|
225
|
+
.add(params.expiresInSeconds, 'seconds')
|
|
226
|
+
.toDate();
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
throw new factory.errors.ArgumentNull('expiresInSeconds');
|
|
230
|
+
}
|
|
231
|
+
let creatingTransaction;
|
|
232
|
+
if (typeOf === factory.transactionType.ReturnOrder) {
|
|
233
|
+
const { agent, project, object, seller } = params;
|
|
234
|
+
creatingTransaction = {
|
|
235
|
+
status, startDate, expires, typeOf, tasksExportAction,
|
|
236
|
+
agent, project, seller, object
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
throw new factory.errors.NotImplemented(`${typeOf} not implemented`);
|
|
241
|
+
}
|
|
242
|
+
// reimplemnt with insertMany(2024-05-30~)
|
|
243
|
+
const result = await this.transactionModel.insertMany(creatingTransaction, { rawResult: true });
|
|
244
|
+
const id = result.insertedIds?.[0]?.toHexString();
|
|
245
|
+
if (typeof id !== 'string') {
|
|
246
|
+
throw new factory.errors.Internal('transaction not saved');
|
|
247
|
+
}
|
|
248
|
+
// 取引開始時にも取引イベントエミッター連携(2024-03-21~)
|
|
249
|
+
transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id, typeOf, status });
|
|
250
|
+
return { expires, id, startDate, status }; // minimize response(2024-05-30~)
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* 特定取引検索
|
|
254
|
+
*/
|
|
255
|
+
async findReturnOrderById(params, inclusion // make required(2024-05-31~)
|
|
256
|
+
) {
|
|
257
|
+
let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
|
|
258
|
+
if (Array.isArray(inclusion) && inclusion.length > 0) {
|
|
259
|
+
positiveProjectionFields = positiveProjectionFields.filter((key) => inclusion.includes(key));
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
throw new factory.errors.NotImplemented('inclusion must be specified'); // 2024-08-26~
|
|
263
|
+
}
|
|
264
|
+
const projection = {
|
|
265
|
+
_id: 0,
|
|
266
|
+
id: { $toString: '$_id' },
|
|
267
|
+
...Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))
|
|
268
|
+
};
|
|
269
|
+
// let projection: { [key in (IKeyOfProjection<T> | '__v' | 'createdAt' | 'updatedAt')]?: AnyExpression } = {};
|
|
270
|
+
// if (Array.isArray(inclusion) && inclusion.length > 0) {
|
|
271
|
+
// inclusion.forEach((field) => {
|
|
272
|
+
// projection[field] = 1;
|
|
273
|
+
// });
|
|
274
|
+
// } else {
|
|
275
|
+
// projection = {
|
|
276
|
+
// __v: 0,
|
|
277
|
+
// createdAt: 0,
|
|
278
|
+
// updatedAt: 0
|
|
279
|
+
// };
|
|
280
|
+
// }
|
|
281
|
+
const doc = await this.transactionModel.findOne({
|
|
282
|
+
_id: { $eq: params.id },
|
|
283
|
+
typeOf: { $eq: params.typeOf }
|
|
284
|
+
}, projection)
|
|
285
|
+
.lean() // 2024-08-26~
|
|
286
|
+
.exec();
|
|
287
|
+
if (doc === null) {
|
|
288
|
+
throw new factory.errors.NotFound(this.transactionModel.modelName, `${params.typeOf} not found`);
|
|
289
|
+
}
|
|
290
|
+
return doc;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* 取引を確定する
|
|
294
|
+
*/
|
|
295
|
+
async confirmReturnOrder(params) {
|
|
296
|
+
const endDate = new Date();
|
|
297
|
+
const doc = await this.transactionModel.findOneAndUpdate({
|
|
298
|
+
_id: { $eq: params.id },
|
|
299
|
+
typeOf: { $eq: params.typeOf },
|
|
300
|
+
status: { $eq: factory.transactionStatusType.InProgress },
|
|
301
|
+
expires: { $gt: endDate } // add expires(2025-02-27~)
|
|
302
|
+
}, {
|
|
303
|
+
status: factory.transactionStatusType.Confirmed, // ステータス変更
|
|
304
|
+
endDate,
|
|
305
|
+
// 'object.authorizeActions': params.authorizeActions,
|
|
306
|
+
result: params.result, // resultを更新
|
|
307
|
+
potentialActions: params.potentialActions // resultを更新
|
|
308
|
+
}, {
|
|
309
|
+
new: true,
|
|
310
|
+
projection: { _id: 1 }
|
|
311
|
+
})
|
|
312
|
+
.lean()
|
|
313
|
+
.exec();
|
|
314
|
+
// NotFoundであれば取引状態確認
|
|
315
|
+
if (doc === null) {
|
|
316
|
+
const { expires, status } = await this.findReturnOrderById({ typeOf: params.typeOf, id: params.id }, ['expires', 'status']);
|
|
317
|
+
if (status === factory.transactionStatusType.Confirmed) {
|
|
318
|
+
// すでに確定済の場合スルー
|
|
319
|
+
}
|
|
320
|
+
else if (status === factory.transactionStatusType.Expired) {
|
|
321
|
+
throw new factory.errors.Argument('Transaction id', 'Already expired');
|
|
322
|
+
}
|
|
323
|
+
else if (status === factory.transactionStatusType.Canceled) {
|
|
324
|
+
throw new factory.errors.Argument('Transaction id', 'Already canceled');
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
if ((0, moment_1.default)(expires)
|
|
328
|
+
.isSameOrBefore((0, moment_1.default)(endDate))) {
|
|
329
|
+
throw new factory.errors.Argument('Transaction id', 'potentially expired');
|
|
330
|
+
}
|
|
331
|
+
throw new factory.errors.NotFound(this.transactionModel.modelName, `${params.typeOf} ${factory.transactionStatusType.InProgress} not found`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
transaction_1.transactionEventEmitter.emitTransactionStatusChanged({
|
|
335
|
+
id: params.id,
|
|
336
|
+
typeOf: params.typeOf,
|
|
337
|
+
status: factory.transactionStatusType.Confirmed
|
|
338
|
+
});
|
|
339
|
+
}
|
|
212
340
|
}
|
|
213
341
|
exports.ReturnOrderRepo = ReturnOrderRepo;
|