@chevre/domain 21.20.0-alpha.38 → 21.20.0-alpha.39

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.
@@ -0,0 +1,21 @@
1
+ // tslint:disable:no-console
2
+ import * as mongoose from 'mongoose';
3
+
4
+ import { chevre } from '../../../lib/index';
5
+
6
+ async function main() {
7
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
8
+
9
+ const transactionRepo = await chevre.repository.Transaction.createInstance(mongoose.connection);
10
+
11
+ const transaction = await transactionRepo.findById({
12
+ typeOf: chevre.factory.transactionType.PlaceOrder,
13
+ id: '65ab04d5206595479469f50c',
14
+ inclusion: ['endDate', 'id', 'object', 'project', 'seller', 'startDate', 'status', 'typeOf']
15
+ });
16
+ console.log('transaction:', transaction);
17
+ }
18
+
19
+ main()
20
+ .then(console.log)
21
+ .catch(console.error);
@@ -32,5 +32,8 @@ export declare class MongoRepository {
32
32
  };
33
33
  };
34
34
  }): Promise<import("mongodb").UpdateResult>;
35
+ deleteByOrderNumber(params: {
36
+ orderNumber: string;
37
+ }): Promise<import("mongodb").DeleteResult>;
35
38
  }
36
39
  export {};
@@ -107,5 +107,14 @@ class MongoRepository {
107
107
  .exec();
108
108
  });
109
109
  }
110
+ deleteByOrderNumber(params) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ return this.orderModel.deleteOne({
113
+ typeOf: { $eq: factory.transactionType.PlaceOrder },
114
+ orderNumber: { $eq: params.orderNumber }
115
+ })
116
+ .exec();
117
+ });
118
+ }
110
119
  }
111
120
  exports.MongoRepository = MongoRepository;
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.triggerWebhook = exports.report2developers = exports.sendEmailMessage = void 0;
13
13
  const sgMail = require("@sendgrid/mail");
14
14
  const http_status_1 = require("http-status");
15
- const request = require("request");
15
+ // import * as request from 'request';
16
16
  const util = require("util");
17
17
  const credentials_1 = require("../credentials");
18
18
  const settings_1 = require("../settings");
@@ -179,31 +179,37 @@ ${content}`;
179
179
  }
180
180
  }
181
181
  else {
182
+ throw new factory.errors.NotImplemented('only useFetchAPI implemented');
182
183
  // LINE通知APIにPOST
183
- const formData = Object.assign(Object.assign({ message: message }, (typeof imageThumbnail === 'string') ? { imageThumbnail } : undefined), (typeof imageFullsize === 'string') ? { imageFullsize } : undefined);
184
- return new Promise((resolve, reject) => {
185
- var _a;
186
- request.post({
187
- url: LINE_NOTIFY_URL,
188
- auth: { bearer: repos.accessToken },
189
- form: formData,
190
- json: true,
191
- timeout: (_a = settings_1.settings.webhook) === null || _a === void 0 ? void 0 : _a.timeout
192
- }, (error, response, body) => {
193
- if (error !== null) {
194
- reject(error);
195
- }
196
- else {
197
- switch (response.statusCode) {
198
- case http_status_1.OK:
199
- resolve();
200
- break;
201
- default:
202
- reject(new Error(body.message));
203
- }
204
- }
205
- });
206
- });
184
+ // const formData: any = {
185
+ // message: message,
186
+ // ...(typeof imageThumbnail === 'string') ? { imageThumbnail } : undefined,
187
+ // ...(typeof imageFullsize === 'string') ? { imageFullsize } : undefined
188
+ // };
189
+ // return new Promise<void>((resolve, reject) => {
190
+ // request.post(
191
+ // {
192
+ // url: LINE_NOTIFY_URL,
193
+ // auth: { bearer: repos.accessToken },
194
+ // form: formData,
195
+ // json: true,
196
+ // timeout: settings.webhook?.timeout
197
+ // },
198
+ // (error, response, body) => {
199
+ // if (error !== null) {
200
+ // reject(error);
201
+ // } else {
202
+ // switch (response.statusCode) {
203
+ // case OK:
204
+ // resolve();
205
+ // break;
206
+ // default:
207
+ // reject(new Error(body.message));
208
+ // }
209
+ // }
210
+ // }
211
+ // );
212
+ // });
207
213
  }
208
214
  });
209
215
  }
@@ -292,57 +298,59 @@ function processInformAction(params) {
292
298
  catch (err) {
293
299
  if (err.name === 'TimeoutError') {
294
300
  // tslint:disable-next-line:no-console
295
- console.error('report2developers: Timeout: It took more than 5 seconds to get the result!', err);
301
+ console.error('processInformAction: Timeout: It took more than 5 seconds to get the result!', err);
296
302
  }
297
303
  else if (err.name === 'AbortError') {
298
304
  // tslint:disable-next-line:no-console
299
- console.error('report2developers: Fetch aborted by user action (browser stop button, closing tab, etc.', err);
305
+ console.error('processInformAction: Fetch aborted by user action (browser stop button, closing tab, etc.', err);
300
306
  }
301
307
  else if (err.name === 'TypeError') {
302
308
  // tslint:disable-next-line:no-console
303
- console.error('report2developers: AbortSignal.timeout() method is not supported', err);
309
+ console.error('processInformAction: AbortSignal.timeout() method is not supported', err);
304
310
  }
305
311
  else {
306
312
  // A network error, or some other problem.
307
313
  // tslint:disable-next-line:no-console
308
- console.error(`report2developers: Error: type: ${err.name}, message: ${err.message}`, err);
314
+ console.error(`processInformAction: Error: type: ${err.name}, message: ${err.message}`, err);
309
315
  }
310
316
  throw err;
311
317
  }
312
318
  }
313
319
  else {
314
- yield new Promise((resolve, reject) => {
315
- var _a;
316
- request.post({
317
- url: url,
318
- body: {
319
- data: params.object
320
- },
321
- json: true,
322
- timeout: (_a = settings_1.settings.webhook) === null || _a === void 0 ? void 0 : _a.timeout
323
- }, (error, response, body) => {
324
- if (error instanceof Error) {
325
- reject(error);
326
- }
327
- else {
328
- switch (response.statusCode) {
329
- case http_status_1.OK:
330
- case http_status_1.CREATED:
331
- case http_status_1.ACCEPTED:
332
- case http_status_1.NO_CONTENT:
333
- result = {
334
- statusCode: response.statusCode,
335
- useFetchAPI: repos.useFetchAPI
336
- // body: body
337
- };
338
- resolve();
339
- break;
340
- default:
341
- reject(new Error(`statusCode: ${response.statusCode} body: ${body}`));
342
- }
343
- }
344
- });
345
- });
320
+ throw new factory.errors.NotImplemented('only useFetchAPI implemented');
321
+ // await new Promise<void>((resolve, reject) => {
322
+ // request.post(
323
+ // {
324
+ // url: url,
325
+ // body: {
326
+ // data: params.object
327
+ // },
328
+ // json: true,
329
+ // timeout: settings.webhook?.timeout
330
+ // },
331
+ // (error, response, body) => {
332
+ // if (error instanceof Error) {
333
+ // reject(error);
334
+ // } else {
335
+ // switch (response.statusCode) {
336
+ // case OK:
337
+ // case CREATED:
338
+ // case ACCEPTED:
339
+ // case NO_CONTENT:
340
+ // result = {
341
+ // statusCode: response.statusCode,
342
+ // useFetchAPI: repos.useFetchAPI
343
+ // // body: body
344
+ // };
345
+ // resolve();
346
+ // break;
347
+ // default:
348
+ // reject(new Error(`statusCode: ${response.statusCode} body: ${body}`));
349
+ // }
350
+ // }
351
+ // }
352
+ // );
353
+ // });
346
354
  }
347
355
  }
348
356
  }
@@ -61,10 +61,18 @@ function voidTransaction(params) {
61
61
  // USE_CREATE_ORDER_ON_OFFER_ACCEPTEDの場合、orderNumber発行済のはず
62
62
  const orderNumberByTransaction = transaction.object.orderNumber;
63
63
  if (typeof orderNumberByTransaction === 'string') {
64
- yield (0, any_1.voidAcceptedOffer)({
65
- authorizeActions,
66
- orderNumber: orderNumberByTransaction
67
- })(repos);
64
+ switch (transaction.status) {
65
+ case factory.transactionStatusType.Confirmed:
66
+ // 確定取引の場合、不要なオファーのみ除外
67
+ yield (0, any_1.voidAcceptedOffer)({
68
+ authorizeActions,
69
+ orderNumber: orderNumberByTransaction
70
+ })(repos);
71
+ break;
72
+ default:
73
+ // orderInTransaction自体を削除
74
+ yield repos.orderInTransaction.deleteByOrderNumber({ orderNumber: orderNumberByTransaction });
75
+ }
68
76
  }
69
77
  yield Promise.all(authorizeActions.map((action) => __awaiter(this, void 0, void 0, function* () {
70
78
  yield repos.action.cancelWithVoid({ typeOf: action.typeOf, id: action.id });
@@ -1,9 +1,9 @@
1
1
  import * as factory from '../../../../factory';
2
- export type IPlaceOrderTransactionAsInformObject = Pick<factory.transaction.ITransaction<factory.transactionType.PlaceOrder>, 'id' | 'typeOf' | 'agent' | 'endDate' | 'expires' | 'project' | 'seller' | 'result' | 'startDate' | 'status'>;
2
+ export type IPlaceOrderTransactionAsInformObject = Pick<factory.transaction.ITransaction<factory.transactionType.PlaceOrder>, 'id' | 'typeOf' | 'endDate' | 'project' | 'seller' | 'startDate' | 'status'>;
3
3
  /**
4
4
  * 取引のタスクを作成する
5
5
  */
6
6
  export declare function createTasks(params: {
7
- transaction: factory.transaction.ITransaction<factory.transactionType.PlaceOrder>;
7
+ transaction: Pick<factory.transaction.ITransaction<factory.transactionType.PlaceOrder>, 'endDate' | 'id' | 'object' | 'project' | 'seller' | 'startDate' | 'status' | 'typeOf'>;
8
8
  runsAt: Date;
9
9
  }): factory.task.IAttributes<factory.taskName>[];
@@ -9,7 +9,6 @@ const settings_1 = require("../../../../settings");
9
9
  */
10
10
  // tslint:disable-next-line:max-func-body-length
11
11
  function createTasks(params) {
12
- var _a;
13
12
  const taskAttributes = [];
14
13
  const transaction = params.transaction;
15
14
  const taskRunsAt = params.runsAt;
@@ -18,12 +17,9 @@ function createTasks(params) {
18
17
  const informObject = {
19
18
  id: transaction.id,
20
19
  typeOf: transaction.typeOf,
21
- agent: transaction.agent,
22
20
  endDate: transaction.endDate,
23
- expires: transaction.expires,
24
21
  project: transaction.project,
25
22
  seller: transaction.seller,
26
- result: transaction.result,
27
23
  startDate: transaction.startDate,
28
24
  status: transaction.status
29
25
  };
@@ -121,25 +117,26 @@ function createTasks(params) {
121
117
  taskAttributes.push(...triggerWebhookTaskAttributes, deleteTransactionTask, voidPaymentTaskAttributes, voidReserveTaskAttributes);
122
118
  switch (transaction.status) {
123
119
  case factory.transactionStatusType.Confirmed:
124
- const potentialActions = transaction.potentialActions;
125
- if (potentialActions === undefined) {
126
- throw new factory.errors.NotFound('Transaction PotentialActions');
120
+ // const potentialActions = transaction.potentialActions;
121
+ // if (potentialActions === undefined) {
122
+ // throw new factory.errors.NotFound('Transaction PotentialActions');
123
+ // }
124
+ // const orderFromTransaction = transaction.result?.order;
125
+ // if (orderFromTransaction === undefined) {
126
+ // throw new factory.errors.NotFound('Transaction Result');
127
+ // }
128
+ const confirmationNumber = transaction.object.confirmationNumber;
129
+ if (typeof confirmationNumber !== 'string' || confirmationNumber.length === 0) {
130
+ throw new factory.errors.NotFound('transaction.object.confirmationNumber');
127
131
  }
128
- const orderFromTransaction = (_a = transaction.result) === null || _a === void 0 ? void 0 : _a.order;
129
- if (orderFromTransaction === undefined) {
130
- throw new factory.errors.NotFound('Transaction Result');
132
+ const orderNumber = transaction.object.orderNumber;
133
+ if (typeof orderNumber !== 'string' || orderNumber.length === 0) {
134
+ throw new factory.errors.NotFound('transaction.object.orderNumber');
131
135
  }
132
- const orderActionAttributes = potentialActions.order;
136
+ // const orderActionAttributes = potentialActions.order;
133
137
  const placeOrderTaskData = {
134
- project: orderActionAttributes.project,
135
- object: {
136
- confirmationNumber: orderFromTransaction.confirmationNumber,
137
- orderNumber: orderActionAttributes.object.orderNumber
138
- }
139
- // 廃止(2023-08-21~)
140
- // ...(orderActionAttributes.potentialActions !== undefined)
141
- // ? { potentialActions: orderActionAttributes.potentialActions }
142
- // : undefined
138
+ project: transaction.project,
139
+ object: { confirmationNumber, orderNumber }
143
140
  };
144
141
  const placeOrderTaskAttributes = {
145
142
  project: transaction.project,
@@ -23,7 +23,8 @@ function exportTasksById(params) {
23
23
  return (repos) => __awaiter(this, void 0, void 0, function* () {
24
24
  const transaction = yield repos.transaction.findById({
25
25
  typeOf: factory.transactionType.PlaceOrder,
26
- id: params.id
26
+ id: params.id,
27
+ inclusion: ['endDate', 'id', 'object', 'project', 'seller', 'startDate', 'status', 'typeOf']
27
28
  });
28
29
  // タスク実行日時バッファの指定があれば調整
29
30
  let taskRunsAt = new Date();
@@ -24,7 +24,7 @@ exports.preStart = void 0;
24
24
  const createDebug = require("debug");
25
25
  const http_status_1 = require("http-status");
26
26
  const moment = require("moment-timezone");
27
- const request = require("request");
27
+ // import * as request from 'request';
28
28
  const factory = require("../../../factory");
29
29
  const debug = createDebug('chevre-domain:service');
30
30
  /**
@@ -287,53 +287,80 @@ function createOrder4returnPolicy(params) {
287
287
  };
288
288
  }
289
289
  const TIMEOUT = 5000;
290
+ // tslint:disable-next-line:max-func-body-length
290
291
  function getReturnPolicyByProject(params) {
292
+ var _a;
291
293
  return __awaiter(this, void 0, void 0, function* () {
292
- return new Promise((resolve, reject) => {
293
- request.post({
294
- url: params.sameAs,
295
- body: {
296
- order: createOrder4returnPolicy({ order: params.order })
294
+ // implement with fetch(2024-01-20~)
295
+ try {
296
+ const res = yield fetch(params.sameAs, {
297
+ method: 'POST',
298
+ headers: {
299
+ 'Content-Type': 'application/json'
297
300
  },
298
- json: true,
299
- timeout: TIMEOUT
300
- }, (error, response, body) => {
301
- var _a;
302
- if (error instanceof Error) {
303
- reject(error);
304
- }
305
- else {
306
- switch (response.statusCode) {
307
- case http_status_1.OK:
308
- case http_status_1.CREATED:
309
- case http_status_1.ACCEPTED:
310
- case http_status_1.NO_CONTENT:
311
- if (typeof ((_a = body.restockingFee) === null || _a === void 0 ? void 0 : _a.value) !== 'number') {
312
- reject(new Error('invalid return policy'));
313
- }
314
- else {
315
- resolve({
316
- typeOf: 'MerchantReturnPolicy',
317
- restockingFee: {
318
- currency: factory.priceCurrency.JPY,
319
- typeOf: 'MonetaryAmount',
320
- value: body.restockingFee.value
321
- }
322
- });
301
+ body: JSON.stringify({
302
+ order: createOrder4returnPolicy({ order: params.order })
303
+ }),
304
+ // body data type must match "Content-Type" header
305
+ signal: AbortSignal.timeout(TIMEOUT)
306
+ });
307
+ let body;
308
+ try {
309
+ body = yield res.json();
310
+ }
311
+ catch (error) {
312
+ // no op
313
+ }
314
+ let returnPolicy;
315
+ switch (res.status) {
316
+ case http_status_1.OK:
317
+ case http_status_1.CREATED:
318
+ case http_status_1.ACCEPTED:
319
+ case http_status_1.NO_CONTENT:
320
+ if (typeof ((_a = body === null || body === void 0 ? void 0 : body.restockingFee) === null || _a === void 0 ? void 0 : _a.value) !== 'number') {
321
+ throw new Error('invalid return policy');
322
+ }
323
+ else {
324
+ returnPolicy = {
325
+ typeOf: 'MerchantReturnPolicy',
326
+ restockingFee: {
327
+ currency: factory.priceCurrency.JPY,
328
+ typeOf: 'MonetaryAmount',
329
+ value: body.restockingFee.value
323
330
  }
324
- break;
325
- case http_status_1.NOT_FOUND:
326
- reject(new factory.errors.Argument('Seller', 'has no applicable return policies'));
327
- break;
328
- default:
329
- reject({
330
- statusCode: response.statusCode,
331
- body: body
332
- });
331
+ };
333
332
  }
334
- }
335
- });
336
- });
333
+ break;
334
+ case http_status_1.NOT_FOUND:
335
+ throw new factory.errors.Argument('Seller', 'has no applicable return policies');
336
+ // break;
337
+ default:
338
+ // tslint:disable-next-line:no-console
339
+ console.error('custom return policy temporarily unavailable.', res.status, body);
340
+ throw new factory.errors.ServiceUnavailable(`custom return policy temporarily unavailable. statusCode: ${res.status} body: ${body}`);
341
+ }
342
+ return returnPolicy;
343
+ }
344
+ catch (err) {
345
+ if (err.name === 'TimeoutError') {
346
+ // tslint:disable-next-line:no-console
347
+ console.error('getReturnPolicyByProject: Timeout: It took more than 5 seconds to get the result!', err);
348
+ }
349
+ else if (err.name === 'AbortError') {
350
+ // tslint:disable-next-line:no-console
351
+ console.error('getReturnPolicyByProject: Fetch aborted by user action (browser stop button, closing tab, etc.', err);
352
+ }
353
+ else if (err.name === 'TypeError') {
354
+ // tslint:disable-next-line:no-console
355
+ console.error('getReturnPolicyByProject: AbortSignal.timeout() method is not supported', err);
356
+ }
357
+ else {
358
+ // A network error, or some other problem.
359
+ // tslint:disable-next-line:no-console
360
+ console.error(`getReturnPolicyByProject: Error: type: ${err.name}, message: ${err.message}`, err);
361
+ }
362
+ throw err;
363
+ }
337
364
  });
338
365
  }
339
366
  /**
package/package.json CHANGED
@@ -28,7 +28,6 @@
28
28
  "moment-timezone": "^0.5.33",
29
29
  "node-fpe": "1.0.0",
30
30
  "pug": "^2.0.4",
31
- "request": "^2.88.2",
32
31
  "uniqid": "^5.4.0",
33
32
  "uuid": "^3.4.0"
34
33
  },
@@ -46,8 +45,6 @@
46
45
  "@types/node": "18.19.2",
47
46
  "@types/power-assert": "^1.5.3",
48
47
  "@types/pug": "^2.0.4",
49
- "@types/request": "^2.48.5",
50
- "@types/request-promise-native": "^1.0.18",
51
48
  "@types/sinon": "^4.3.3",
52
49
  "@types/sinon-mongoose": "^1.3.11",
53
50
  "@types/uniqid": "^4.1.3",
@@ -115,5 +112,5 @@
115
112
  "postversion": "git push origin --tags",
116
113
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
117
114
  },
118
- "version": "21.20.0-alpha.38"
115
+ "version": "21.20.0-alpha.39"
119
116
  }