UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

200 lines (199 loc) 8.59 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BusStopRepo = void 0; const factory = require("../../factory"); const settings_1 = require("../../settings"); const civicStructure_1 = require("../mongoose/schemas/civicStructure"); /** * ターミナルリポジトリ */ class BusStopRepo { constructor(connection) { this.civicStructureModel = connection.model(civicStructure_1.modelName, (0, civicStructure_1.createSchema)()); } // tslint:disable-next-line:max-func-body-length static CREATE_BUS_STOP_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e, _f, _g, _h; const andConditions = [{ typeOf: { $eq: factory.placeType.BusStop } }]; const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq; if (typeof projectIdEq === 'string') { andConditions.push({ 'project.id': { $eq: projectIdEq } }); } const branchCodeEq = (_c = params.branchCode) === null || _c === void 0 ? void 0 : _c.$eq; if (typeof branchCodeEq === 'string') { andConditions.push({ branchCode: { $exists: true, $eq: branchCodeEq } }); } const branchCodeRegex = (_d = params.branchCode) === null || _d === void 0 ? void 0 : _d.$regex; if (typeof branchCodeRegex === 'string' && branchCodeRegex.length > 0) { andConditions.push({ branchCode: { $exists: true, $regex: new RegExp(branchCodeRegex) } }); } const branchCodeIn = (_e = params.branchCode) === null || _e === void 0 ? void 0 : _e.$in; if (Array.isArray(branchCodeIn)) { andConditions.push({ branchCode: { $exists: true, $in: branchCodeIn } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ const idEq = (_f = params.id) === null || _f === void 0 ? void 0 : _f.$eq; if (typeof idEq === 'string') { andConditions.push({ _id: { $eq: idEq } }); } const idIn = (_g = params.id) === null || _g === void 0 ? void 0 : _g.$in; if (Array.isArray(idIn)) { andConditions.push({ _id: { $in: idIn } }); } const nameRegex = (_h = params.name) === null || _h === void 0 ? void 0 : _h.$regex; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (typeof nameRegex === 'string' && nameRegex.length > 0) { andConditions.push({ $or: [ { 'name.ja': { $exists: true, $regex: new RegExp(nameRegex) } }, { 'name.en': { $exists: true, $regex: new RegExp(nameRegex) } } ] }); } return andConditions; } saveBusStop(params) { return __awaiter(this, void 0, void 0, function* () { let doc; if (typeof params.id !== 'string' || params.id.length === 0) { doc = yield this.civicStructureModel.create(params); } else { // 上書き禁止属性を除外(2022-08-24~) const { id, branchCode, project, typeOf } = params, updateFields = __rest(params, ["id", "branchCode", "project", "typeOf"]); doc = yield this.civicStructureModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true }) .exec(); } if (doc === null) { throw new factory.errors.NotFound(this.civicStructureModel.modelName); } return doc.toObject(); }); } findBusStopByBranchCode(params) { return __awaiter(this, void 0, void 0, function* () { return this.civicStructureModel.findOne({ typeOf: { $eq: factory.placeType.BusStop }, 'project.id': { $eq: params.project.id }, branchCode: { $eq: params.branchCode } }) .exec() .then((doc) => { if (doc === null) { throw new factory.errors.NotFound(`${factory.placeType.BusStop} ${params.branchCode}`); } return doc.toObject(); }); }); } searchBusStops(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const conditions = BusStopRepo.CREATE_BUS_STOP_MONGO_CONDITIONS(params); // containsPlaceを含めるとデータサイズが大きくなるので、検索結果には含めない const query = this.civicStructureModel.find((conditions.length > 0) ? { $and: conditions } : {}, { __v: 0, createdAt: 0, updatedAt: 0, containsPlace: 0 }); if (typeof params.limit === 'number' && params.limit > 0) { const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; query.limit(params.limit) .skip(params.limit * (page - 1)); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.branchCode) !== undefined) { query.sort({ branchCode: params.sort.branchCode }); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec() .then((docs) => docs.map((doc) => doc.toObject())); }); } findBusStopById(params, projection) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.civicStructureModel.findOne({ typeOf: { $eq: factory.placeType.BusStop }, _id: { $eq: params.id } }, Object.assign({ __v: 0, createdAt: 0, updatedAt: 0 }, projection)) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.civicStructureModel.modelName); } return doc.toObject(); }); } deleteBusStopById(params) { return __awaiter(this, void 0, void 0, function* () { yield this.civicStructureModel.findOneAndDelete({ typeOf: { $eq: factory.placeType.BusStop }, _id: { $eq: params.id }, 'project.id': { $eq: params.project.id } }) .exec() .then((doc) => { if (doc === null) { throw new factory.errors.NotFound(this.civicStructureModel.modelName); } }); }); } } exports.BusStopRepo = BusStopRepo;