UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

278 lines (277 loc) 16.2 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.SectionRepo = void 0; const factory = require("../../factory"); const settings_1 = require("../../settings"); const civicStructure_1 = require("../mongoose/schemas/civicStructure"); const place_1 = require("../mongoose/schemas/place"); /** * セクションリポジトリ */ class SectionRepo { constructor(connection) { this.civicStructureModel = connection.model(civicStructure_1.modelName, (0, civicStructure_1.createSchema)()); this.placeModel = connection.model(place_1.modelName, (0, place_1.createSchema)()); } // tslint:disable-next-line:max-func-body-length createScreeningRoomSection(screeningRoomSection) { return __awaiter(this, void 0, void 0, function* () { var _a; const screeningRoom = screeningRoomSection.containedInPlace; if (typeof (screeningRoom === null || screeningRoom === void 0 ? void 0 : screeningRoom.branchCode) !== 'string') { throw new factory.errors.ArgumentNull('containedInPlace.branchCode'); } const movieTheater = screeningRoom.containedInPlace; if (typeof (movieTheater === null || movieTheater === void 0 ? void 0 : movieTheater.branchCode) !== 'string') { throw new factory.errors.ArgumentNull('containedInPlace.containedInPlace.branchCode'); } // 施設存在確認 const movieTheaterDoc = yield this.civicStructureModel.findOne(Object.assign({ typeOf: { $eq: factory.placeType.MovieTheater }, 'project.id': { $eq: screeningRoomSection.project.id }, branchCode: { $eq: movieTheater.branchCode } }, (typeof ((_a = screeningRoomSection.parentOrganization) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { 'parentOrganization.id': { $exists: true, $eq: screeningRoomSection.parentOrganization.id } } : undefined), { _id: 1 }) .lean() .exec(); if (movieTheaterDoc === null) { throw new factory.errors.NotFound(factory.placeType.MovieTheater); } // セクションコードが存在しなければ追加する const doc = yield this.placeModel.findOneAndUpdate({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: screeningRoomSection.project.id }, 'containedInPlace.branchCode': { $exists: true, $eq: movieTheater.branchCode }, branchCode: { $eq: screeningRoom.branchCode }, 'containsPlace.branchCode': { $ne: screeningRoomSection.branchCode } }, { $push: { containsPlace: Object.assign({ typeOf: screeningRoomSection.typeOf, branchCode: screeningRoomSection.branchCode, name: screeningRoomSection.name }, (Array.isArray(screeningRoomSection.additionalProperty)) ? { additionalProperty: screeningRoomSection.additionalProperty } : undefined) } }, { new: true, projection: { 'containedInPlace.id': 1, typeOf: 1 } }) .exec(); // 存在しなければコード重複 if (doc === null) { throw new factory.errors.AlreadyInUse(factory.placeType.ScreeningRoomSection, ['branchCode']); } return doc.toObject(); }); } // tslint:disable-next-line:max-func-body-length updateScreeningRoomSection(screeningRoomSection, $unset) { return __awaiter(this, void 0, void 0, function* () { var _a; const screeningRoom = screeningRoomSection.containedInPlace; if (typeof (screeningRoom === null || screeningRoom === void 0 ? void 0 : screeningRoom.branchCode) !== 'string') { throw new factory.errors.ArgumentNull('containedInPlace.branchCode'); } const movieTheater = screeningRoom.containedInPlace; if (typeof (movieTheater === null || movieTheater === void 0 ? void 0 : movieTheater.branchCode) !== 'string') { throw new factory.errors.ArgumentNull('containedInPlace.containedInPlace.branchCode'); } const doc = yield this.placeModel.findOneAndUpdate(Object.assign({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: screeningRoomSection.project.id }, 'containedInPlace.branchCode': { $exists: true, $eq: movieTheater.branchCode }, branchCode: { $eq: screeningRoom.branchCode }, 'containsPlace.branchCode': { $eq: screeningRoomSection.branchCode } }, (typeof ((_a = screeningRoomSection.parentOrganization) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { 'parentOrganization.id': { $exists: true, $eq: screeningRoomSection.parentOrganization.id } } : undefined), Object.assign(Object.assign(Object.assign(Object.assign({ 'containsPlace.$[screeningRoomSection].branchCode': screeningRoomSection.branchCode }, (screeningRoomSection.name !== undefined && screeningRoomSection !== null) ? { 'containsPlace.$[screeningRoomSection].name': screeningRoomSection.name } : undefined), (Array.isArray(screeningRoomSection.additionalProperty)) ? { 'containsPlace.$[screeningRoomSection].additionalProperty': screeningRoomSection.additionalProperty } : undefined), (Array.isArray(screeningRoomSection.containsPlace) && screeningRoomSection.containsPlace.length > 0) ? { 'containsPlace.$[screeningRoomSection].containsPlace': screeningRoomSection.containsPlace.map((p) => { return Object.assign(Object.assign(Object.assign({ typeOf: p.typeOf, branchCode: p.branchCode }, (p.name !== undefined && p.name !== null) ? { name: p.name } : undefined), (Array.isArray(p.seatingType)) ? { seatingType: p.seatingType } : undefined), (Array.isArray(p.additionalProperty)) ? { additionalProperty: p.additionalProperty } : undefined); }) } : undefined), ($unset !== undefined && $unset !== null) ? { $unset } : undefined), { new: true, arrayFilters: [ { 'screeningRoomSection.branchCode': screeningRoomSection.branchCode } ], projection: { 'containedInPlace.id': 1, typeOf: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound(factory.placeType.ScreeningRoomSection); } return doc.toObject(); }); } // tslint:disable-next-line:cyclomatic-complexity max-func-body-length searchScreeningRoomSections(searchConditions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q; const matchStages = [{ $match: { typeOf: { $eq: factory.placeType.ScreeningRoom } } }]; const projectIdEq = (_b = (_a = searchConditions.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq; if (typeof projectIdEq === 'string') { matchStages.push({ $match: { 'project.id': { $eq: projectIdEq } } }); } const parentOrganizationIdEq = (_d = (_c = searchConditions.parentOrganization) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof parentOrganizationIdEq === 'string') { matchStages.push({ $match: { 'parentOrganization.id': { $exists: true, $eq: parentOrganizationIdEq } } }); } // 施設コード const movieTheaterBranchCodeEq = (_g = (_f = (_e = searchConditions.containedInPlace) === null || _e === void 0 ? void 0 : _e.containedInPlace) === null || _f === void 0 ? void 0 : _f.branchCode) === null || _g === void 0 ? void 0 : _g.$eq; if (typeof movieTheaterBranchCodeEq === 'string') { matchStages.push({ $match: { 'containedInPlace.branchCode': { $exists: true, $eq: movieTheaterBranchCodeEq } } }); } // ルームコード const containedInPlaceBranchCodeEq = (_j = (_h = searchConditions.containedInPlace) === null || _h === void 0 ? void 0 : _h.branchCode) === null || _j === void 0 ? void 0 : _j.$eq; if (typeof containedInPlaceBranchCodeEq === 'string') { matchStages.push({ $match: { branchCode: { $eq: containedInPlaceBranchCodeEq } } }); } // セクションコード const sectionBranchCodeEq = (_k = searchConditions === null || searchConditions === void 0 ? void 0 : searchConditions.branchCode) === null || _k === void 0 ? void 0 : _k.$eq; if (typeof sectionBranchCodeEq === 'string') { matchStages.push({ $match: { 'containsPlace.branchCode': { $exists: true, $eq: sectionBranchCodeEq } } }); } const nameCodeRegex = (_l = searchConditions.name) === null || _l === void 0 ? void 0 : _l.$regex; if (typeof nameCodeRegex === 'string') { matchStages.push({ $match: { $or: [ { 'containsPlace.name.ja': { $exists: true, $regex: new RegExp(nameCodeRegex) } }, { 'containsPlace.name.en': { $exists: true, $regex: new RegExp(nameCodeRegex) } } ] } }); } const branchCodeRegex = (_m = searchConditions.branchCode) === null || _m === void 0 ? void 0 : _m.$regex; if (typeof branchCodeRegex === 'string') { matchStages.push({ $match: { 'containsPlace.branchCode': { $exists: true, $regex: new RegExp(branchCodeRegex) } } }); } const additionalPropertyElemMatch = (_o = searchConditions.additionalProperty) === null || _o === void 0 ? void 0 : _o.$elemMatch; if (additionalPropertyElemMatch !== undefined && additionalPropertyElemMatch !== null) { matchStages.push({ $match: { 'containsPlace.additionalProperty': { $exists: true, $elemMatch: additionalPropertyElemMatch } } }); } const aggregate = this.placeModel.aggregate([ { $unwind: '$containsPlace' }, ...matchStages, { $project: Object.assign(Object.assign({ _id: 0, typeOf: '$containsPlace.typeOf', branchCode: '$containsPlace.branchCode', name: '$containsPlace.name', additionalProperty: '$containsPlace.additionalProperty' }, (((_p = searchConditions.$projection) === null || _p === void 0 ? void 0 : _p.containedInPlace) === 1) ? { containedInPlace: { typeOf: '$typeOf', branchCode: '$branchCode', name: '$name', containedInPlace: { id: '$containedInPlace.id', typeOf: '$containedInPlace.typeOf', branchCode: '$containedInPlace.branchCode', name: '$containedInPlace.name' } } } : undefined), (((_q = searchConditions.$projection) === null || _q === void 0 ? void 0 : _q.seatCount) === 1) ? { seatCount: { $cond: { if: { $isArray: '$containsPlace.containsPlace' }, then: { $size: '$containsPlace.containsPlace' }, else: 0 } } } : undefined) } ]); // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (typeof searchConditions.limit === 'number' && searchConditions.limit > 0) { const page = (typeof searchConditions.page === 'number' && searchConditions.page > 0) ? searchConditions.page : 1; aggregate.limit(searchConditions.limit * page) .skip(searchConditions.limit * (page - 1)); } return aggregate .option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } deleteScreeningRoomSection(screeningRoomSection) { return __awaiter(this, void 0, void 0, function* () { var _a; const doc = yield this.placeModel.findOneAndUpdate(Object.assign({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: screeningRoomSection.project.id }, 'containedInPlace.branchCode': { $exists: true, $eq: screeningRoomSection.containedInPlace.containedInPlace.branchCode }, branchCode: { $eq: screeningRoomSection.containedInPlace.branchCode }, 'containsPlace.branchCode': { $eq: screeningRoomSection.branchCode } }, (typeof ((_a = screeningRoomSection.parentOrganization) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { 'parentOrganization.id': { $exists: true, $eq: screeningRoomSection.parentOrganization.id } } : undefined), { $pull: { containsPlace: { branchCode: screeningRoomSection.branchCode } } }, { new: true, projection: { 'containedInPlace.id': 1, typeOf: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound(factory.placeType.ScreeningRoomSection); } return doc.toObject(); }); } } exports.SectionRepo = SectionRepo;