@chevre/domain 21.2.0-alpha.0 → 21.2.0-alpha.1

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.
@@ -3,10 +3,12 @@ import { RedisClientType } from 'redis';
3
3
  * 確認番号リポジトリ
4
4
  */
5
5
  export declare class RedisRepository {
6
+ private static readonly REDIS_KEY_PREFIX_NEW;
6
7
  private static readonly REDIS_KEY_PREFIX;
7
8
  private readonly redisClient;
8
9
  constructor(redisClient: RedisClientType);
9
- static ALIGN_DIGITS(params: string): string;
10
+ private static alignDigits;
11
+ private static createKey;
10
12
  /**
11
13
  * 発行する
12
14
  */
@@ -16,6 +16,7 @@ const util = require("util");
16
16
  // tslint:disable-next-line:no-require-imports no-var-requires
17
17
  const fpe = require('node-fpe');
18
18
  const factory = require("../factory");
19
+ const settings_1 = require("../settings");
19
20
  const CONFIRMATION_NUMBER_MIN_LENGH = 4;
20
21
  /**
21
22
  * 確認番号リポジトリ
@@ -24,7 +25,7 @@ class RedisRepository {
24
25
  constructor(redisClient) {
25
26
  this.redisClient = redisClient;
26
27
  }
27
- static ALIGN_DIGITS(params) {
28
+ static alignDigits(params) {
28
29
  let aligndNumber = String(params);
29
30
  if (aligndNumber.length < CONFIRMATION_NUMBER_MIN_LENGH) {
30
31
  aligndNumber = `${'0'.repeat(CONFIRMATION_NUMBER_MIN_LENGH)}${aligndNumber}`
@@ -32,6 +33,21 @@ class RedisRepository {
32
33
  }
33
34
  return aligndNumber;
34
35
  }
36
+ static createKey(params) {
37
+ const useNewKey = params.orderDate instanceof Date
38
+ && moment(params.orderDate)
39
+ .isSameOrAfter(settings_1.USE_NEW_ORDER_NUMBER_KEY_FROM);
40
+ if (useNewKey) {
41
+ return util.format('%s:%s', RedisRepository.REDIS_KEY_PREFIX_NEW, moment(params.orderDate)
42
+ .tz('Asia/Tokyo')
43
+ .format('YYMM'));
44
+ }
45
+ else {
46
+ return util.format('%s:%s', RedisRepository.REDIS_KEY_PREFIX, moment(params.orderDate)
47
+ .tz('Asia/Tokyo')
48
+ .format('YYMM'));
49
+ }
50
+ }
35
51
  /**
36
52
  * 発行する
37
53
  */
@@ -41,44 +57,29 @@ class RedisRepository {
41
57
  const TTL = moment(params.orderDate)
42
58
  .add(1, 'month')
43
59
  .diff(moment(params.orderDate), 'seconds');
44
- const key = util.format('%s:%s', RedisRepository.REDIS_KEY_PREFIX, moment(params.orderDate)
45
- .tz('Asia/Tokyo')
46
- .format('YYMM'));
60
+ const key = RedisRepository.createKey({ orderDate: params.orderDate });
47
61
  const [incrReply] = yield this.redisClient.multi()
48
62
  .incr(key)
49
63
  .expire(key, TTL)
50
64
  .exec();
51
- // if (!Array.isArray(results)) {
52
- // // なぜかresults: nullのことがあるのでハンドリング
53
- // throw new factory.errors.ServiceUnavailable('incr confirmationNumber result not array');
54
- // }
55
65
  // tslint:disable-next-line:no-single-line-block-comment
56
- /* istanbul ignore else: please write tests */
57
- if (typeof incrReply === 'number') {
58
- const no = incrReply;
59
- // debug('no incremented.', no);
60
- // 桁数調整
61
- let confirmationNumber = RedisRepository.ALIGN_DIGITS(String(no));
62
- // debug('encrypting confirmationNumber...', confirmationNumber);
63
- // checkdigit
64
- const cd = cdigit.luhn.compute(confirmationNumber);
65
- // debug('check digit:', cd);
66
- confirmationNumber = fpe({ password: cd })
67
- .encrypt(confirmationNumber);
68
- confirmationNumber = `${cd}${confirmationNumber}`;
69
- // console.log(
70
- // 'decrypted confirmationNumber:',
71
- // fpe({ password: confirmationNumber.slice(0, 1) })
72
- // .decrypt(confirmationNumber.slice(1))
73
- // );
74
- return confirmationNumber;
75
- }
76
- else {
66
+ /* istanbul ignore if */
67
+ if (typeof incrReply !== 'number') {
77
68
  // 基本的にありえないフロー
78
- throw new factory.errors.ServiceUnavailable('Confirmation number not published');
69
+ throw new factory.errors.ServiceUnavailable('confirmation number not incremented unexpectedly');
79
70
  }
71
+ // 桁数調整
72
+ let confirmationNumber = RedisRepository.alignDigits(incrReply);
73
+ // checkdigit
74
+ const cd = cdigit.luhn.compute(confirmationNumber);
75
+ // debug('check digit:', cd);
76
+ confirmationNumber = fpe({ password: cd })
77
+ .encrypt(confirmationNumber);
78
+ confirmationNumber = `${cd}${confirmationNumber}`;
79
+ return confirmationNumber;
80
80
  });
81
81
  }
82
82
  }
83
+ RedisRepository.REDIS_KEY_PREFIX_NEW = 'confirmationNumber';
83
84
  RedisRepository.REDIS_KEY_PREFIX = 'cinerino:confirmationNumber';
84
85
  exports.RedisRepository = RedisRepository;
@@ -25,18 +25,15 @@ export interface IUnlockKey {
25
25
  offer: IOffer;
26
26
  }
27
27
  /**
28
- * イベントの座席在庫リポジトリ
28
+ * イベントストックホルダーリポジトリ
29
29
  */
30
30
  export declare class RedisRepository {
31
31
  static KEY_PREFIX_NEW: string;
32
32
  static KEY_PREFIX: string;
33
33
  private readonly redisClient;
34
34
  constructor(redisClient: RedisClientType);
35
- static OFFER2FIELD(params: IOffer): string;
36
- static CREATE_KEY(params: {
37
- eventId: string;
38
- startDate: Date;
39
- }): string;
35
+ private static offer2field;
36
+ private static createKey;
40
37
  /**
41
38
  * 座席をロックする(maxキャパシティチェック有)
42
39
  */
@@ -16,13 +16,13 @@ const factory = require("../../factory");
16
16
  const settings_1 = require("../../settings");
17
17
  const debug = createDebug('chevre-domain:repo');
18
18
  /**
19
- * イベントの座席在庫リポジトリ
19
+ * イベントストックホルダーリポジトリ
20
20
  */
21
21
  class RedisRepository {
22
22
  constructor(redisClient) {
23
23
  this.redisClient = redisClient;
24
24
  }
25
- static OFFER2FIELD(params) {
25
+ static offer2field(params) {
26
26
  var _a, _b;
27
27
  // 予約IDをfieldにする場合
28
28
  const serviceOutputId = (_b = (_a = params.itemOffered) === null || _a === void 0 ? void 0 : _a.serviceOutput) === null || _b === void 0 ? void 0 : _b.id;
@@ -31,10 +31,10 @@ class RedisRepository {
31
31
  }
32
32
  return `${params.seatSection}:${params.seatNumber}`;
33
33
  }
34
- static CREATE_KEY(params) {
34
+ static createKey(params) {
35
35
  const useNewKey = params.startDate instanceof Date
36
36
  && moment(params.startDate)
37
- .isAfter(settings_1.USE_NEW_EVENT_AVAILABILITY_KEY_FROM);
37
+ .isSameOrAfter(settings_1.USE_NEW_EVENT_AVAILABILITY_KEY_FROM);
38
38
  if (useNewKey) {
39
39
  return `${RedisRepository.KEY_PREFIX_NEW}:${params.eventId}`;
40
40
  }
@@ -47,7 +47,7 @@ class RedisRepository {
47
47
  */
48
48
  lockIfNotLimitExceeded(lockKey, maximum) {
49
49
  return __awaiter(this, void 0, void 0, function* () {
50
- const key = RedisRepository.CREATE_KEY({ eventId: lockKey.eventId, startDate: lockKey.startDate });
50
+ const key = RedisRepository.createKey({ eventId: lockKey.eventId, startDate: lockKey.startDate });
51
51
  yield this.redisClient.watch(key);
52
52
  const hashCount = yield this.redisClient.hLen(key);
53
53
  // Process result
@@ -64,10 +64,10 @@ class RedisRepository {
64
64
  */
65
65
  lock(lockKey) {
66
66
  return __awaiter(this, void 0, void 0, function* () {
67
- const key = RedisRepository.CREATE_KEY({ eventId: lockKey.eventId, startDate: lockKey.startDate });
67
+ const key = RedisRepository.createKey({ eventId: lockKey.eventId, startDate: lockKey.startDate });
68
68
  const value = lockKey.holder;
69
69
  const multi = this.redisClient.multi();
70
- const fields = lockKey.offers.map((offer) => RedisRepository.OFFER2FIELD(offer));
70
+ const fields = lockKey.offers.map((offer) => RedisRepository.offer2field(offer));
71
71
  fields.forEach((field) => {
72
72
  multi.hSetNX(key, field, value);
73
73
  });
@@ -103,8 +103,8 @@ class RedisRepository {
103
103
  */
104
104
  unlock(params) {
105
105
  return __awaiter(this, void 0, void 0, function* () {
106
- const key = RedisRepository.CREATE_KEY({ eventId: params.eventId, startDate: params.startDate });
107
- const field = RedisRepository.OFFER2FIELD(params.offer);
106
+ const key = RedisRepository.createKey({ eventId: params.eventId, startDate: params.startDate });
107
+ const field = RedisRepository.offer2field(params.offer);
108
108
  yield this.redisClient.multi()
109
109
  .hDel(key, field)
110
110
  .exec();
@@ -115,7 +115,7 @@ class RedisRepository {
115
115
  */
116
116
  findUnavailableOffersByEventId(params) {
117
117
  return __awaiter(this, void 0, void 0, function* () {
118
- const key = RedisRepository.CREATE_KEY({ eventId: params.eventId, startDate: params.startDate });
118
+ const key = RedisRepository.createKey({ eventId: params.eventId, startDate: params.startDate });
119
119
  const reply = yield this.redisClient.hGetAll(key);
120
120
  let offers = [];
121
121
  if (reply !== null) {
@@ -134,7 +134,7 @@ class RedisRepository {
134
134
  */
135
135
  countUnavailableOffers(params) {
136
136
  return __awaiter(this, void 0, void 0, function* () {
137
- const key = RedisRepository.CREATE_KEY({ eventId: params.event.id, startDate: params.event.startDate });
137
+ const key = RedisRepository.createKey({ eventId: params.event.id, startDate: params.event.startDate });
138
138
  const reply = yield this.redisClient.hLen(key);
139
139
  let fieldCount = 0;
140
140
  if (typeof reply === 'number') {
@@ -148,8 +148,8 @@ class RedisRepository {
148
148
  */
149
149
  getHolder(params) {
150
150
  return __awaiter(this, void 0, void 0, function* () {
151
- const key = RedisRepository.CREATE_KEY({ eventId: params.eventId, startDate: params.startDate });
152
- const field = RedisRepository.OFFER2FIELD(params.offer);
151
+ const key = RedisRepository.createKey({ eventId: params.eventId, startDate: params.startDate });
152
+ const field = RedisRepository.offer2field(params.offer);
153
153
  return this.redisClient.hGet(key, field);
154
154
  });
155
155
  }
@@ -159,9 +159,9 @@ class RedisRepository {
159
159
  */
160
160
  searchAvailability(params) {
161
161
  return __awaiter(this, void 0, void 0, function* () {
162
- const key = RedisRepository.CREATE_KEY({ eventId: params.eventId, startDate: params.startDate });
162
+ const key = RedisRepository.createKey({ eventId: params.eventId, startDate: params.startDate });
163
163
  const fields = params.offers.map((o) => {
164
- return RedisRepository.OFFER2FIELD(o);
164
+ return RedisRepository.offer2field(o);
165
165
  });
166
166
  const result = yield this.redisClient.hmGet(key, fields);
167
167
  if (!Array.isArray(result)) {
@@ -3,9 +3,11 @@ import { RedisClientType } from 'redis';
3
3
  * 注文番号リポジトリ
4
4
  */
5
5
  export declare class RedisRepository {
6
- static REDIS_KEY_PREFIX: string;
6
+ private static readonly REDIS_KEY_PREFIX_NEW;
7
+ private static readonly REDIS_KEY_PREFIX;
7
8
  private readonly redisClient;
8
9
  constructor(redisClient: RedisClientType);
10
+ private static createKey;
9
11
  /**
10
12
  * タイムスタンプから発行する
11
13
  */
@@ -16,6 +16,8 @@ const util = require("util");
16
16
  // tslint:disable-next-line:no-require-imports no-var-requires
17
17
  const fpe = require('node-fpe');
18
18
  const factory = require("../factory");
19
+ const settings_1 = require("../settings");
20
+ const ORDER_NUMBER_SEPARATOR = '-';
19
21
  /**
20
22
  * 注文番号リポジトリ
21
23
  */
@@ -23,6 +25,17 @@ class RedisRepository {
23
25
  constructor(redisClient) {
24
26
  this.redisClient = redisClient;
25
27
  }
28
+ static createKey(params) {
29
+ const useNewKey = params.orderDate instanceof Date
30
+ && moment(params.orderDate)
31
+ .isSameOrAfter(settings_1.USE_NEW_ORDER_NUMBER_KEY_FROM);
32
+ if (useNewKey) {
33
+ return util.format('%s:%s:%s', RedisRepository.REDIS_KEY_PREFIX_NEW, params.projectPrefix, params.timestamp);
34
+ }
35
+ else {
36
+ return util.format('%s:%s:%s', RedisRepository.REDIS_KEY_PREFIX, params.projectPrefix, params.timestamp);
37
+ }
38
+ }
26
39
  /**
27
40
  * タイムスタンプから発行する
28
41
  */
@@ -37,42 +50,35 @@ class RedisRepository {
37
50
  const TTL = moment(params.orderDate)
38
51
  .add(1, 'minute') // ミリ秒でカウントしていくので、注文日時後1分で十分
39
52
  .diff(now, 'seconds');
40
- const key = util.format('%s:%s:%s', RedisRepository.REDIS_KEY_PREFIX, projectPrefix, timestamp);
53
+ const key = RedisRepository.createKey({ orderDate: params.orderDate, projectPrefix, timestamp });
41
54
  const [incrReply] = yield this.redisClient.multi()
42
55
  .incr(key)
43
56
  .expire(key, TTL)
44
57
  .exec();
45
- // if (!Array.isArray(results)) {
46
- // // なぜかresults: nullのことがあるのでハンドリング
47
- // throw new factory.errors.ServiceUnavailable('incr orderNumber result not array');
48
- // }
49
58
  // tslint:disable-next-line:no-single-line-block-comment
50
- /* istanbul ignore else: please write tests */
51
- if (typeof incrReply === 'number') {
52
- let orderNumber = timestamp;
53
- const no = incrReply;
54
- orderNumber = `${orderNumber}${no}`;
55
- // checkdigit
56
- const cd = cdigit.luhn.compute(orderNumber);
57
- const cipher = fpe({ password: cd });
58
- orderNumber = cipher.encrypt(orderNumber);
59
- orderNumber = `${projectPrefix}${cd}${orderNumber}`;
60
- orderNumber = `${[
61
- // tslint:disable-next-line:no-magic-numbers
62
- orderNumber.slice(0, 4),
63
- // tslint:disable-next-line:no-magic-numbers
64
- orderNumber.slice(4, 11),
65
- // tslint:disable-next-line:no-magic-numbers
66
- orderNumber.slice(11)
67
- ].join('-')}`;
68
- return orderNumber;
69
- }
70
- else {
59
+ /* istanbul ignore if */
60
+ if (typeof incrReply !== 'number') {
71
61
  // 基本的にありえないフロー
72
- throw new factory.errors.ServiceUnavailable('Order number not published');
62
+ throw new factory.errors.ServiceUnavailable('order number not incremented unexpectedly');
73
63
  }
64
+ let orderNumber = `${timestamp}${incrReply}`;
65
+ // checkdigit
66
+ const cd = cdigit.luhn.compute(orderNumber);
67
+ orderNumber = fpe({ password: cd })
68
+ .encrypt(orderNumber);
69
+ orderNumber = `${projectPrefix}${cd}${orderNumber}`;
70
+ orderNumber = `${[
71
+ // tslint:disable-next-line:no-magic-numbers
72
+ orderNumber.slice(0, 4),
73
+ // tslint:disable-next-line:no-magic-numbers
74
+ orderNumber.slice(4, 11),
75
+ // tslint:disable-next-line:no-magic-numbers
76
+ orderNumber.slice(11)
77
+ ].join(ORDER_NUMBER_SEPARATOR)}`;
78
+ return orderNumber;
74
79
  });
75
80
  }
76
81
  }
82
+ RedisRepository.REDIS_KEY_PREFIX_NEW = 'orderNumber';
77
83
  RedisRepository.REDIS_KEY_PREFIX = 'cinerino:orderNumber';
78
84
  exports.RedisRepository = RedisRepository;
@@ -30,6 +30,7 @@ export declare const DEFAULT_PAYMENT_METHOD_TYPE_FOR_CREDIT_CARD: string;
30
30
  export declare const USE_ASSET_TRANSACTION_SYNC_PROCESSING: boolean;
31
31
  export declare const USE_PAY_ASSET_TRANSACTION_SYNC_PROCESSING: boolean;
32
32
  export declare const USE_NEW_EVENT_AVAILABILITY_KEY_FROM: moment.Moment;
33
+ export declare const USE_NEW_ORDER_NUMBER_KEY_FROM: moment.Moment;
33
34
  /**
34
35
  * グローバル設定
35
36
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.settings = exports.USE_NEW_EVENT_AVAILABILITY_KEY_FROM = exports.USE_PAY_ASSET_TRANSACTION_SYNC_PROCESSING = exports.USE_ASSET_TRANSACTION_SYNC_PROCESSING = exports.DEFAULT_PAYMENT_METHOD_TYPE_FOR_CREDIT_CARD = exports.DEFAULT_SENDER_EMAIL = exports.TRANSACTION_CANCELED_STORAGE_PERIOD_IN_DAYS = exports.TRANSACTION_CONFIRMED_STORAGE_PERIOD_IN_DAYS = exports.ASSET_TRANSACTION_STORAGE_PERIOD_IN_DAYS = exports.ABORTED_TASKS_WITHOUT_REPORT = void 0;
3
+ exports.settings = exports.USE_NEW_ORDER_NUMBER_KEY_FROM = exports.USE_NEW_EVENT_AVAILABILITY_KEY_FROM = exports.USE_PAY_ASSET_TRANSACTION_SYNC_PROCESSING = exports.USE_ASSET_TRANSACTION_SYNC_PROCESSING = exports.DEFAULT_PAYMENT_METHOD_TYPE_FOR_CREDIT_CARD = exports.DEFAULT_SENDER_EMAIL = exports.TRANSACTION_CANCELED_STORAGE_PERIOD_IN_DAYS = exports.TRANSACTION_CONFIRMED_STORAGE_PERIOD_IN_DAYS = exports.ASSET_TRANSACTION_STORAGE_PERIOD_IN_DAYS = exports.ABORTED_TASKS_WITHOUT_REPORT = void 0;
4
4
  const moment = require("moment");
5
5
  const factory = require("./factory");
6
6
  const transactionWebhookUrls = (typeof process.env.INFORM_TRANSACTION_URL === 'string')
@@ -49,6 +49,9 @@ exports.USE_PAY_ASSET_TRANSACTION_SYNC_PROCESSING = process.env.USE_PAY_ASSET_TR
49
49
  exports.USE_NEW_EVENT_AVAILABILITY_KEY_FROM = (typeof process.env.USE_NEW_EVENT_AVAILABILITY_KEY_FROM === 'string')
50
50
  ? moment(process.env.USE_NEW_EVENT_AVAILABILITY_KEY_FROM)
51
51
  : moment('2023-07-23T15:00:00Z');
52
+ exports.USE_NEW_ORDER_NUMBER_KEY_FROM = (typeof process.env.USE_NEW_ORDER_NUMBER_KEY_FROM === 'string')
53
+ ? moment(process.env.USE_NEW_ORDER_NUMBER_KEY_FROM)
54
+ : moment('2023-04-30T15:00:00Z');
52
55
  /**
53
56
  * グローバル設定
54
57
  */
package/package.json CHANGED
@@ -117,5 +117,5 @@
117
117
  "postversion": "git push origin --tags",
118
118
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
119
119
  },
120
- "version": "21.2.0-alpha.0"
120
+ "version": "21.2.0-alpha.1"
121
121
  }