@chevre/domain 23.2.0-alpha.47 → 23.2.0-alpha.48

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.
@@ -25,10 +25,16 @@ async function testGeneration() {
25
25
  });
26
26
 
27
27
  // 復号して元のデータに戻るかチェック
28
- const decoded = await orderNumberRepo.decrypt(orderNumber);
29
- const isSuccess = decoded === `${ts.valueOf()}${(i + 1).toString()
30
- // tslint:disable-next-line:no-magic-numbers
31
- .padStart(2, '0')}`;
28
+ let decoded: string;
29
+ let isSuccess: boolean = false;
30
+ try {
31
+ decoded = await orderNumberRepo.decrypt(orderNumber);
32
+ isSuccess = decoded === `${ts.valueOf()}${(i + 1).toString()
33
+ // tslint:disable-next-line:no-magic-numbers
34
+ .padStart(2, '0')}`;
35
+ } catch (error) {
36
+ decoded = error.message;
37
+ }
32
38
 
33
39
  results.push({
34
40
  timestamp: ts.valueOf()
@@ -1,4 +1,5 @@
1
1
  // tslint:disable:no-console
2
+ import * as moment from 'moment';
2
3
  import * as mongoose from 'mongoose';
3
4
 
4
5
  import { chevre } from '../../../../lib/index';
@@ -17,12 +18,14 @@ async function main() {
17
18
  const setting = await settingRepo.findOne({ project: { id: { $eq: '*' } } }, ['orderNumber', 'transactionNumber']);
18
19
  console.log(setting);
19
20
 
20
- if (typeof setting?.orderNumber?.version !== 'string') {
21
+ if (typeof setting?.orderNumber?.version !== 'string' || !(setting?.orderNumber?.validFrom instanceof Date)) {
21
22
  console.log('updating orderNumber...');
22
23
  await settingRepo.updateByProject(
23
24
  { project: { id: { $eq: '*' } } },
24
25
  {
25
26
  orderNumber: {
27
+ validFrom: moment('2026-01-31T15:00:00Z')
28
+ .toDate(),
26
29
  fpeSecret: FPE_SECRET,
27
30
  version: 'A'
28
31
  }
@@ -31,12 +34,14 @@ async function main() {
31
34
  console.log('orderNumber updated.');
32
35
  }
33
36
 
34
- if (typeof setting?.transactionNumber?.version !== 'string') {
37
+ if (typeof setting?.transactionNumber?.version !== 'string' || !(setting?.transactionNumber?.validFrom instanceof Date)) {
35
38
  console.log('updating orderNumber...');
36
39
  await settingRepo.updateByProject(
37
40
  { project: { id: { $eq: '*' } } },
38
41
  {
39
42
  transactionNumber: {
43
+ validFrom: moment('2026-01-31T15:00:00Z')
44
+ .toDate(),
40
45
  fpeSecret: FPE_SECRET,
41
46
  version: '1'
42
47
  }
@@ -111,16 +111,18 @@ export interface IJWTSetting {
111
111
  algorithm: Algorithm;
112
112
  }
113
113
  export interface IOrderNumberSetting {
114
+ validFrom: Date;
114
115
  /**
115
116
  * fpe暗号鍵
116
117
  */
117
118
  fpeSecret: string;
118
119
  /**
119
- * "1"
120
+ * "A"
120
121
  */
121
122
  version: string;
122
123
  }
123
124
  export interface ITransactionNumberSetting {
125
+ validFrom: Date;
124
126
  /**
125
127
  * fpe暗号鍵
126
128
  */
@@ -8,6 +8,7 @@ export declare class OrderNumberRepo {
8
8
  constructor(params: {
9
9
  connection: Connection;
10
10
  });
11
+ static GENERATE_VERSION0(timestamp: string, seq: number): string;
11
12
  /**
12
13
  * タイムスタンプから発行する
13
14
  */
@@ -44,12 +44,21 @@ class OrderNumberRepo {
44
44
  // params.timestamp
45
45
  // );
46
46
  // }
47
+ static GENERATE_VERSION0(timestamp, seq) {
48
+ let orderNumber = `${timestamp}${seq}`;
49
+ // checkdigit
50
+ const cd = cdigit.luhn.compute(orderNumber);
51
+ orderNumber = fpe({ password: cd })
52
+ .encrypt(orderNumber);
53
+ orderNumber = `${cd}${orderNumber}`;
54
+ return orderNumber;
55
+ }
47
56
  /**
48
57
  * タイムスタンプから発行する
49
58
  */
50
59
  publishByTimestamp(params) {
51
60
  return __awaiter(this, void 0, void 0, function* () {
52
- const { fpeSecret, version } = yield this.findSetting();
61
+ const { fpeSecret, version, validFrom } = yield this.findSetting();
53
62
  const timestamp = moment(params.orderDate)
54
63
  .valueOf()
55
64
  .toString();
@@ -68,7 +77,9 @@ class OrderNumberRepo {
68
77
  expires: dataFeedExpires
69
78
  });
70
79
  let orderNumber;
71
- if (typeof fpeSecret === 'string' && typeof version === 'string') {
80
+ const useVersioning = validFrom instanceof Date && moment(validFrom)
81
+ .isSameOrBefore(params.orderDate);
82
+ if (typeof fpeSecret === 'string' && typeof version === 'string' && useVersioning) {
72
83
  const transactionFactory = new transactionNumber_1.TransactionNumberFactory({ fpeSecret, version });
73
84
  orderNumber = transactionFactory.generate(timestamp, incrReply);
74
85
  orderNumber = `${projectPrefix.at(0)}${orderNumber}`;
@@ -82,12 +93,14 @@ class OrderNumberRepo {
82
93
  ].join(ORDER_NUMBER_SEPARATOR)}`;
83
94
  }
84
95
  else {
85
- orderNumber = `${timestamp}${incrReply}`;
86
- // checkdigit
87
- const cd = cdigit.luhn.compute(orderNumber);
88
- orderNumber = fpe({ password: cd })
89
- .encrypt(orderNumber);
90
- orderNumber = `${projectPrefix}${cd}${orderNumber}`;
96
+ orderNumber = OrderNumberRepo.GENERATE_VERSION0(timestamp, incrReply);
97
+ orderNumber = `${projectPrefix}${orderNumber}`;
98
+ // orderNumber = `${timestamp}${incrReply}`;
99
+ // // checkdigit
100
+ // const cd = cdigit.luhn.compute(orderNumber);
101
+ // orderNumber = fpe({ password: cd })
102
+ // .encrypt(orderNumber);
103
+ // orderNumber = `${projectPrefix}${cd}${orderNumber}`;
91
104
  orderNumber = `${[
92
105
  // tslint:disable-next-line:no-magic-numbers
93
106
  orderNumber.slice(0, 4),
@@ -128,14 +141,17 @@ class OrderNumberRepo {
128
141
  if (setting === null || setting.orderNumber === undefined) {
129
142
  return {};
130
143
  }
131
- const { fpeSecret, version } = setting.orderNumber;
144
+ const { fpeSecret, version, validFrom } = setting.orderNumber;
132
145
  if (typeof fpeSecret !== 'string' || fpeSecret === '') {
133
146
  throw new factory.errors.NotFound('setting.orderNumber.secret');
134
147
  }
135
148
  if (typeof version !== 'string' || version === '') {
136
149
  throw new factory.errors.NotFound('setting.orderNumber.version');
137
150
  }
138
- return { fpeSecret, version };
151
+ if (!(validFrom instanceof Date)) {
152
+ throw new factory.errors.NotFound('setting.orderNumber.validFrom must be Date');
153
+ }
154
+ return { fpeSecret, version, validFrom };
139
155
  });
140
156
  }
141
157
  }
@@ -32,7 +32,7 @@ class TransactionNumberRepo {
32
32
  */
33
33
  publishByTimestamp(params) {
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
- const { fpeSecret, version } = yield this.findSetting();
35
+ const { fpeSecret, version, validFrom } = yield this.findSetting();
36
36
  const timestamp = moment(params.startDate)
37
37
  .valueOf()
38
38
  .toString();
@@ -49,23 +49,11 @@ class TransactionNumberRepo {
49
49
  expires: dataFeedExpires
50
50
  });
51
51
  let transactionNumber;
52
- // let secret: string | undefined;
53
- if (typeof fpeSecret === 'string' && typeof version === 'string') {
52
+ const useVersioning = validFrom instanceof Date && moment(validFrom)
53
+ .isSameOrBefore(params.startDate);
54
+ if (typeof fpeSecret === 'string' && typeof version === 'string' && useVersioning) {
54
55
  const transactionFactory = new transactionNumber_1.TransactionNumberFactory({ fpeSecret, version });
55
56
  transactionNumber = transactionFactory.generate(timestamp, incrReply);
56
- // // tslint:disable-next-line:no-magic-numbers
57
- // const saltDigit = randomInt(0, 10);
58
- // const saltStr = saltDigit.toString();
59
- // secret = `${fpeSecret}${saltStr}`;
60
- // const cipher = fpe2({ secret });
61
- // // incrReplyが 0〜99 なら "00"〜"99" になり(15桁)
62
- // // incrReplyが 100〜999 なら "100"〜"999" になる(16桁)
63
- // const rawBody = `${timestamp}${incrReply.toString()
64
- // // tslint:disable-next-line:no-magic-numbers
65
- // .padStart(2, '0')}`;
66
- // const encryptedBody = cipher.encrypt(rawBody);
67
- // // ソルトを「あえて」2文字目に含める
68
- // transactionNumber = `${version}${saltStr}${encryptedBody}`;
69
57
  }
70
58
  else {
71
59
  transactionNumber = `${timestamp}${incrReply}`;
@@ -103,14 +91,17 @@ class TransactionNumberRepo {
103
91
  if (setting === null || setting.transactionNumber === undefined) {
104
92
  return {};
105
93
  }
106
- const { fpeSecret, version } = setting.transactionNumber;
94
+ const { fpeSecret, version, validFrom } = setting.transactionNumber;
107
95
  if (typeof fpeSecret !== 'string' || fpeSecret === '') {
108
96
  throw new factory.errors.NotFound('setting.transactionNumber.secret');
109
97
  }
110
98
  if (typeof version !== 'string' || version === '') {
111
99
  throw new factory.errors.NotFound('setting.transactionNumber.version');
112
100
  }
113
- return { fpeSecret, version };
101
+ if (!(validFrom instanceof Date)) {
102
+ throw new factory.errors.NotFound('setting.transactionNumber.validFrom must be Date');
103
+ }
104
+ return { fpeSecret, version, validFrom };
114
105
  });
115
106
  }
116
107
  }
package/package.json CHANGED
@@ -118,5 +118,5 @@
118
118
  "postversion": "git push origin --tags",
119
119
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
120
120
  },
121
- "version": "23.2.0-alpha.47"
121
+ "version": "23.2.0-alpha.48"
122
122
  }