"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 event_1 = require("./mongoose/model/event");
/**
* イベントリポジトリ
*/
class MongoRepository {
constructor(connection) {
this.eventModel = connection.model(event_1.default.modelName);
}
// tslint:disable-next-line:cyclomatic-complexity max-func-body-length
static CREATE_MONGO_CONDITIONS(conditions) {
const andConditions = [
{
typeOf: conditions.typeOf
}
];
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (conditions.project !== undefined) {
if (Array.isArray(conditions.project.ids)) {
andConditions.push({
'project.id': {
$exists: true,
$in: conditions.project.ids
}
});
}
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(conditions.eventStatuses)) {
andConditions.push({
eventStatus: { $in: conditions.eventStatuses }
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (conditions.inSessionFrom !== undefined) {
andConditions.push({
endDate: { $gte: conditions.inSessionFrom }
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (conditions.inSessionThrough !== undefined) {
andConditions.push({
startDate: { $lte: conditions.inSessionThrough }
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (conditions.startFrom !== undefined) {
andConditions.push({
startDate: { $gte: conditions.startFrom }
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (conditions.startThrough !== undefined) {
andConditions.push({
startDate: { $lte: conditions.startThrough }
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (conditions.endFrom !== undefined) {
andConditions.push({
endDate: { $gte: conditions.endFrom }
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (conditions.endThrough !== undefined) {
andConditions.push({
endDate: { $lte: conditions.endThrough }
});
}
let params;
switch (conditions.typeOf) {
case factory.eventType.ScreeningEvent:
params = conditions;
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.name !== undefined) {
andConditions.push({
$or: [
{ 'name.ja': new RegExp(params.name, 'i') },
{ 'name.en': new RegExp(params.name, 'i') }
]
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.superEvent !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.superEvent.ids)) {
andConditions.push({
'superEvent.id': {
$exists: true,
$in: params.superEvent.ids
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.superEvent.locationBranchCodes)) {
andConditions.push({
'superEvent.location.branchCode': {
$exists: true,
$in: params.superEvent.locationBranchCodes
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.superEvent.workPerformedIdentifiers)) {
andConditions.push({
'superEvent.workPerformed.identifier': {
$exists: true,
$in: params.superEvent.workPerformedIdentifiers
}
});
}
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.availableFrom instanceof Date) {
andConditions.push({
'offers.availabilityEnds': {
$exists: true,
$gte: params.offers.availableFrom
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.availableThrough instanceof Date) {
andConditions.push({
'offers.availabilityStarts': {
$exists: true,
$lte: params.offers.availableThrough
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.validFrom instanceof Date) {
andConditions.push({
'offers.validThrough': {
$exists: true,
$gte: params.offers.validFrom
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.validThrough instanceof Date) {
andConditions.push({
'offers.validFrom': {
$exists: true,
$lte: params.offers.validThrough
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.offers.ids)) {
andConditions.push({
'offers.id': {
$exists: true,
$in: params.offers.ids
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.itemOffered !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.itemOffered.serviceOutput !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.itemOffered.serviceOutput.reservedTicket !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.itemOffered.serviceOutput.reservedTicket.ticketedSeat !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.offers.itemOffered.serviceOutput.reservedTicket.ticketedSeat.typeOfs)) {
andConditions.push({
'offers.itemOffered.serviceOutput.reservedTicket.ticketedSeat.typeOf': {
$exists: true,
$in: params.offers.itemOffered.serviceOutput.reservedTicket.ticketedSeat.typeOfs
}
});
}
}
}
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.itemOffered.serviceType !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.offers.itemOffered.serviceType.ids)) {
andConditions.push({
'offers.itemOffered.serviceType.id': {
$exists: true,
$in: params.offers.itemOffered.serviceType.ids
}
});
}
}
}
}
break;
case factory.eventType.ScreeningEventSeries:
params = conditions;
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.name !== undefined) {
andConditions.push({
$or: [
{ 'name.ja': new RegExp(params.name, 'i') },
{ 'name.en': new RegExp(params.name, 'i') },
{ kanaName: new RegExp(params.name, 'i') }
]
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.location !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.location.branchCodes)) {
andConditions.push({
'location.branchCode': {
$exists: true,
$in: params.location.branchCodes
}
});
}
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.workPerformed !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (Array.isArray(params.workPerformed.identifiers)) {
andConditions.push({
'workPerformed.identifier': { $in: params.workPerformed.identifiers }
});
}
}
break;
default:
}
return andConditions;
}
/**
* 複数イベントを作成する
*/
createMany(params) {
return __awaiter(this, void 0, void 0, function* () {
const docs = yield this.eventModel.insertMany(params.map((p) => {
return Object.assign({ _id: uniqid() }, p);
}));
return docs.map((doc) => doc.toObject());
});
}
/**
* イベントを保管する
*/
save(params) {
return __awaiter(this, void 0, void 0, function* () {
let doc;
if (params.id === undefined) {
const id = uniqid();
doc = yield this.eventModel.create(Object.assign({}, params.attributes, { _id: id }));
}
else {
doc = yield this.eventModel.findOneAndUpdate({
_id: params.id,
typeOf: params.attributes.typeOf
}, params.attributes, { upsert: (params.upsert !== undefined) ? params.upsert : false, new: true })
.exec();
if (doc === null) {
throw new factory.errors.NotFound('Event');
}
}
return doc.toObject();
});
}
count(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
return this.eventModel.countDocuments({ $and: conditions })
.setOptions({ maxTimeMS: 10000 })
.exec();
});
}
/**
* イベントを検索する
*/
search(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
const query = this.eventModel.find({ $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()));
});
}
findById(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.eventModel.findOne({
_id: params.id
}, {
__v: 0,
createdAt: 0,
updatedAt: 0
})
.exec();
if (doc === null) {
throw new factory.errors.NotFound('Event');
}
return doc.toObject();
});
}
/**
* イベントをキャンセルする
*/
cancel(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.eventModel.findOneAndUpdate({
_id: params.id
}, { eventStatus: factory.eventStatusType.EventCancelled }, { new: true })
.exec();
});
}
}
exports.MongoRepository = MongoRepository;