"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const uniqid = require("uniqid");
const factory = require("../factory");
const offer_1 = require("./mongoose/model/offer");
const offerCatalog_1 = require("./mongoose/model/offerCatalog");
const productOffer_1 = require("./mongoose/model/productOffer");
/**
* オファーリポジトリ
*/
class MongoRepository {
constructor(connection) {
this.offerModel = connection.model(offer_1.default.modelName);
this.offerCatalogModel = connection.model(offerCatalog_1.default.modelName);
this.productOfferModel = connection.model(productOffer_1.default.modelName);
}
// tslint:disable-next-line:max-func-body-length
static CREATE_OFFER_MONGO_CONDITIONS(params) {
// MongoDB検索条件
const andConditions = [];
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.project !== undefined) {
if (Array.isArray(params.project.ids)) {
andConditions.push({
'project.id': {
$exists: true,
$in: params.project.ids
}
});
}
}
if (params.id !== undefined) {
andConditions.push({ _id: new RegExp(params.id, 'i') });
}
if (Array.isArray(params.ids)) {
andConditions.push({ _id: { $in: params.ids } });
}
if (params.identifier !== undefined) {
andConditions.push({ identifier: new RegExp(params.identifier, 'i') });
}
if (Array.isArray(params.identifiers)) {
andConditions.push({ identifier: { $in: params.identifiers } });
}
if (params.name !== undefined) {
andConditions.push({
$or: [
{
'name.ja': {
$exists: true,
$regex: new RegExp(params.name, 'i')
}
},
{
'name.en': {
$exists: true,
$regex: new RegExp(params.name, 'i')
}
},
{
'alternateName.ja': {
$exists: true,
$regex: new RegExp(params.name, 'i')
}
},
{
'alternateName.en': {
$exists: true,
$regex: new RegExp(params.name, 'i')
}
}
]
});
}
if (params.priceSpecification !== undefined) {
if (typeof params.priceSpecification.maxPrice === 'number') {
andConditions.push({
'priceSpecification.price': {
$exists: true,
$lte: params.priceSpecification.maxPrice
}
});
}
if (typeof params.priceSpecification.minPrice === 'number') {
andConditions.push({
'priceSpecification.price': {
$exists: true,
$gte: params.priceSpecification.minPrice
}
});
}
if (params.priceSpecification.accounting !== undefined) {
if (typeof params.priceSpecification.accounting.maxAccountsReceivable === 'number') {
andConditions.push({
'priceSpecification.accounting.accountsReceivable': {
$exists: true,
$lte: params.priceSpecification.accounting.maxAccountsReceivable
}
});
}
if (typeof params.priceSpecification.accounting.minAccountsReceivable === 'number') {
andConditions.push({
'priceSpecification.accounting.accountsReceivable': {
$exists: true,
$gte: params.priceSpecification.accounting.minAccountsReceivable
}
});
}
}
if (params.priceSpecification.referenceQuantity !== undefined) {
if (typeof params.priceSpecification.referenceQuantity.value === 'number') {
andConditions.push({
'priceSpecification.referenceQuantity.value': {
$exists: true,
$eq: params.priceSpecification.referenceQuantity.value
}
});
}
}
}
if (params.category !== undefined) {
if (Array.isArray(params.category.ids)) {
andConditions.push({
'category.id': {
$exists: true,
$in: params.category.ids
}
});
}
}
return andConditions;
}
static CREATE_OFFER_CATALOG_MONGO_CONDITIONS(params) {
// MongoDB検索条件
const andConditions = [];
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.project !== undefined) {
if (Array.isArray(params.project.ids)) {
andConditions.push({
'project.id': {
$exists: true,
$in: params.project.ids
}
});
}
}
if (params.id !== undefined) {
andConditions.push({ _id: new RegExp(params.id, 'i') });
}
if (params.identifier !== undefined) {
andConditions.push({ identifier: new RegExp(params.identifier, 'i') });
}
if (params.name !== undefined) {
andConditions.push({
$or: [
{ 'name.ja': new RegExp(params.name, 'i') },
{ 'name.en': new RegExp(params.name, 'i') }
]
});
}
if (Array.isArray(params.ticketTypes)) {
andConditions.push({
ticketTypes: {
$in: params.ticketTypes
}
});
}
return andConditions;
}
findByOfferCatalogId(params) {
return __awaiter(this, void 0, void 0, function* () {
const ticketTypeGroup = yield this.offerCatalogModel.findById(params.offerCatalog.id, {
__v: 0,
createdAt: 0,
updatedAt: 0
})
.exec()
.then((doc) => {
if (doc === null) {
throw new factory.errors.NotFound(this.offerCatalogModel.modelName);
}
return doc.toObject();
});
return this.offerModel.find({ _id: { $in: ticketTypeGroup.ticketTypes } }, {
__v: 0,
createdAt: 0,
updatedAt: 0
})
.exec()
.then((docs) => docs.map((doc) => doc.toObject()));
});
}
/**
* 券種グループを保管する
*/
saveOfferCatalog(params) {
return __awaiter(this, void 0, void 0, function* () {
let doc;
if (params.id === '') {
const id = uniqid();
doc = yield this.offerCatalogModel.create(Object.assign({}, params, { _id: id }));
}
else {
doc = yield this.offerCatalogModel.findOneAndUpdate({ _id: params.id }, params, { upsert: false, new: true })
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.offerCatalogModel.modelName);
}
}
return doc.toObject();
});
}
findOfferCatalogById(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.offerCatalogModel.findOne({
_id: params.id
}, {
__v: 0,
createdAt: 0,
updatedAt: 0
})
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.offerCatalogModel.modelName);
}
return doc.toObject();
});
}
countOfferCatalogs(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_OFFER_CATALOG_MONGO_CONDITIONS(params);
return this.offerCatalogModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {})
.setOptions({ maxTimeMS: 10000 })
.exec();
});
}
/**
* 券種グループを検索する
*/
searchOfferCatalogs(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_OFFER_CATALOG_MONGO_CONDITIONS(params);
const query = this.offerCatalogModel.find((conditions.length > 0) ? { $and: conditions } : {}, {
__v: 0,
createdAt: 0,
updatedAt: 0
});
if (params.limit !== undefined && params.page !== undefined) {
query.limit(params.limit)
.skip(params.limit * (params.page - 1));
}
return query.sort({ _id: 1 })
.setOptions({ maxTimeMS: 10000 })
.exec()
.then((docs) => docs.map((doc) => doc.toObject()));
});
}
/**
* 券種グループを削除する
*/
deleteOfferCatalog(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.offerCatalogModel.findOneAndRemove({
_id: params.id
})
.exec();
});
}
findOfferById(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.offerModel.findOne({
_id: params.id
}, {
__v: 0,
createdAt: 0,
updatedAt: 0
})
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.offerModel.modelName);
}
return doc.toObject();
});
}
countOffers(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_OFFER_MONGO_CONDITIONS(params);
return this.offerModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {})
.setOptions({ maxTimeMS: 10000 })
.exec();
});
}
/**
* 券種を検索する
*/
searchOffers(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_OFFER_MONGO_CONDITIONS(params);
const query = this.offerModel.find((conditions.length > 0) ? { $and: conditions } : {}, {
__v: 0,
createdAt: 0,
updatedAt: 0
});
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.limit !== undefined && params.page !== undefined) {
query.limit(params.limit)
.skip(params.limit * (params.page - 1));
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.sort !== undefined) {
query.sort(params.sort);
}
return query.setOptions({ maxTimeMS: 10000 })
.exec()
.then((docs) => docs.map((doc) => doc.toObject()));
});
}
/**
* 券種を保管する
*/
saveOffer(params) {
return __awaiter(this, void 0, void 0, function* () {
let doc;
if (params.id === '') {
const id = uniqid();
doc = yield this.offerModel.create(Object.assign({}, params, { _id: id }));
}
else {
doc = yield this.offerModel.findOneAndUpdate({ _id: params.id }, params, { upsert: false, new: true })
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.offerModel.modelName);
}
}
return doc.toObject();
});
}
/**
* 券種を削除する
*/
deleteOffer(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.offerModel.findOneAndRemove({
_id: params.id
})
.exec();
});
}
saveProductOffer(params) {
return __awaiter(this, void 0, void 0, function* () {
let doc;
if (params.id === '') {
const id = uniqid();
doc = yield this.productOfferModel.create(Object.assign({}, params, { _id: id }));
}
else {
doc = yield this.productOfferModel.findOneAndUpdate({ _id: params.id }, params, { upsert: false, new: true })
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.productOfferModel.modelName);
}
}
return doc.toObject();
});
}
countProductOffers(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_OFFER_MONGO_CONDITIONS(params);
return this.productOfferModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {})
.setOptions({ maxTimeMS: 10000 })
.exec();
});
}
searchProductOffers(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_OFFER_MONGO_CONDITIONS(params);
const query = this.productOfferModel.find((conditions.length > 0) ? { $and: conditions } : {}, {
__v: 0,
createdAt: 0,
updatedAt: 0
});
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.limit !== undefined && params.page !== undefined) {
query.limit(params.limit)
.skip(params.limit * (params.page - 1));
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.sort !== undefined) {
query.sort(params.sort);
}
return query.setOptions({ maxTimeMS: 10000 })
.exec()
.then((docs) => docs.map((doc) => doc.toObject()));
});
}
deleteProductOffer(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.productOfferModel.findOneAndRemove({
_id: params.id
})
.exec();
});
}
}
exports.MongoRepository = MongoRepository;