UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

92 lines (91 loc) 4.09 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfirmationNumberRepo = void 0; const cdigit = require("cdigit"); const moment = require("moment-timezone"); // import { RedisClientType } from 'redis'; // tslint:disable-next-line:no-require-imports no-var-requires const fpe = require('node-fpe'); // import { createSchema as createSettingSchema, modelName as settingModelName } from './mongoose/schemas/setting'; const transactionNumber_1 = require("./mongoose/schemas/transactionNumber"); const transactionNumberCounter_1 = require("./transactionNumberCounter"); const CONFIRMATION_NUMBER_MIN_LENGH = 4; const TIMEZONE = 'Asia/Tokyo'; /** * 確認番号リポジトリ */ class ConfirmationNumberRepo { constructor(params) { // const { connection } = params; // this.settingModel = connection.model(settingModelName, createSettingSchema()); this.counterRepo = new transactionNumberCounter_1.TransactionNumberCounterRepo(params); } static alignDigits(params) { let aligndNumber = String(params); if (aligndNumber.length < CONFIRMATION_NUMBER_MIN_LENGH) { aligndNumber = `${'0'.repeat(CONFIRMATION_NUMBER_MIN_LENGH)}${aligndNumber}` .slice(-CONFIRMATION_NUMBER_MIN_LENGH); } return aligndNumber; } // private static createKey(params: { // orderDate: Date; // }): string { // return util.format( // '%s:%s', // ConfirmationNumberRepo.REDIS_KEY_PREFIX_NEW, // moment(params.orderDate) // .tz(TIMEZONE) // .format('YYMM') // ); // } static createDataFeedIdentifier(params) { return moment(params.orderDate) .tz(TIMEZONE) .format('YYMM'); } static count2confirmationNumber(params) { // 桁数調整 let confirmationNumber = ConfirmationNumberRepo.alignDigits(params.count); // checkdigit const cd = cdigit.luhn.compute(confirmationNumber); // debug('check digit:', cd); confirmationNumber = fpe({ password: cd }) .encrypt(confirmationNumber); confirmationNumber = `${cd}${confirmationNumber}`; return confirmationNumber; } /** * 発行する */ publish(params) { return __awaiter(this, void 0, void 0, function* () { let dataFeedExpires; const dataFeedIdentifier = ConfirmationNumberRepo.createDataFeedIdentifier({ orderDate: params.orderDate }); let incrReply; // support publishByMongo(2025-05-23~) // const useMongoBySettings = await this.useMongoBySettings(params); // データ保管期間はとりあえず2 months dataFeedExpires = moment(params.orderDate) // tslint:disable-next-line:no-magic-numbers .add(2, 'months') .toDate(); incrReply = yield this.counterRepo.incrementByMongo({ identifier: dataFeedIdentifier, includedInDataCatalog: { identifier: transactionNumber_1.DataCatalogIdentifier.confirmationNumber }, expires: dataFeedExpires }); return ConfirmationNumberRepo.count2confirmationNumber({ count: incrReply }); }); } } exports.ConfirmationNumberRepo = ConfirmationNumberRepo;