@chevre/domain
Version:
Chevre Domain Library for Node.js
96 lines (95 loc) • 4.48 kB
JavaScript
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.ServiceOutputIdentifierRepo = void 0;
const cdigit = require("cdigit");
const moment = require("moment-timezone");
// tslint:disable-next-line:no-require-imports no-var-requires
const fpe = require('node-fpe');
const setting_1 = require("./mongoose/schemas/setting");
const transactionNumber_1 = require("./mongoose/schemas/transactionNumber");
const transactionNumberCounter_1 = require("./transactionNumberCounter");
/**
* サービスアウトプット識別子リポジトリ
*/
class ServiceOutputIdentifierRepo {
constructor(params) {
const { connection } = params;
this.settingModel = connection.model(setting_1.modelName, (0, setting_1.createSchema)());
this.counterRepo = new transactionNumberCounter_1.TransactionNumberCounterRepo(params);
}
// private static createKey(params: {
// startDate: Date;
// timestamp: string;
// }): string {
// return util.format(
// '%s:%s',
// ServiceOutputIdentifierRepo.REDIS_KEY_PREFIX,
// params.timestamp
// );
// }
/**
* タイムスタンプから発行する
*/
publishByTimestamp(params) {
return __awaiter(this, void 0, void 0, function* () {
const timestamp = moment(params.startDate)
.valueOf()
.toString();
let dataFeedExpires;
const dataFeedIdentifier = timestamp;
let incrReply;
const useMongoBySettings = yield this.useMongoBySettings(params);
if (useMongoBySettings) {
dataFeedExpires = moment(params.startDate)
.add(1, 'minute') // ミリ秒でカウントしていくので、予約日時後1分で十分
.toDate();
incrReply = yield this.counterRepo.incrementByMongo({
identifier: dataFeedIdentifier,
includedInDataCatalog: { identifier: transactionNumber_1.DataCatalogIdentifier.serviceOutputIdentifier },
expires: dataFeedExpires
});
}
else {
dataFeedExpires = moment(params.startDate)
.add(1, 'minute') // ミリ秒でカウントしていくので、予約日時後1分で十分
.toDate();
incrReply = yield this.counterRepo.incrementByRedis({
identifier: dataFeedIdentifier,
includedInDataCatalog: { identifier: transactionNumber_1.DataCatalogIdentifier.serviceOutputIdentifier },
expires: dataFeedExpires
});
}
let identifier = `${timestamp}${incrReply}`;
// checkdigit
const cd = cdigit.luhn.compute(identifier);
identifier = fpe({ password: cd })
.encrypt(identifier);
identifier = `${cd}${identifier}`;
return identifier;
});
}
useMongoBySettings(params) {
return __awaiter(this, void 0, void 0, function* () {
const setting = yield this.settingModel.findOne({ 'project.id': { $eq: '*' } }, {
_id: 0,
useMongo4transactionNumberFrom: 1
})
.lean()
.exec();
const useMongo4transactionNumberFrom = setting === null || setting === void 0 ? void 0 : setting.useMongo4transactionNumberFrom;
return useMongo4transactionNumberFrom instanceof Date
&& moment(params.startDate)
.isSameOrAfter(moment(useMongo4transactionNumberFrom));
});
}
}
exports.ServiceOutputIdentifierRepo = ServiceOutputIdentifierRepo;
;