UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

124 lines (123 loc) 5.46 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.EmailMessageRepo = void 0; const emailMessages_1 = require("./mongoose/schemas/emailMessages"); const factory = require("../factory"); const settings_1 = require("../settings"); /** * Eメールメッセージリポジトリ */ class EmailMessageRepo { constructor(connection) { this.emailMessageModel = connection.model(emailMessages_1.modelName, (0, emailMessages_1.createSchema)()); } static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e; // MongoDB検索条件 const andConditions = [{ typeOf: factory.creativeWorkType.EmailMessage }]; 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 identifierEq = (_c = params.identifier) === null || _c === void 0 ? void 0 : _c.$eq; if (typeof identifierEq === 'string') { andConditions.push({ identifier: { $eq: identifierEq } }); } const aboutIdentifierEq = (_e = (_d = params.about) === null || _d === void 0 ? void 0 : _d.identifier) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof aboutIdentifierEq === 'string') { andConditions.push({ 'about.identifier': { $exists: true, $eq: aboutIdentifierEq } }); } return andConditions; } save(params) { return __awaiter(this, void 0, void 0, function* () { let doc; if (typeof params.id !== 'string') { doc = yield this.emailMessageModel.create(params); } else { // 上書き禁止属性を除外(2022-08-24~) const { id, identifier, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "project", "typeOf"]); doc = yield this.emailMessageModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true }) .exec(); } if (doc === null) { throw new factory.errors.NotFound(this.emailMessageModel.modelName); } return doc.toObject(); }); } findById(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.emailMessageModel.findOne({ _id: params.id }, { __v: 0, createdAt: 0, updatedAt: 0 }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.emailMessageModel.modelName); } return doc.toObject(); }); } search(params) { return __awaiter(this, void 0, void 0, function* () { const conditions = EmailMessageRepo.CREATE_MONGO_CONDITIONS(params); const query = this.emailMessageModel.find({ $and: conditions }, { __v: 0, createdAt: 0, updatedAt: 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 (params.sort !== undefined) { query.sort(params.sort); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec() .then((docs) => docs.map((doc) => doc.toObject())); }); } deleteById(params) { return __awaiter(this, void 0, void 0, function* () { yield this.emailMessageModel.findOneAndDelete({ _id: { $eq: params.id } }, { projection: { _id: 1 } }) .exec(); }); } } exports.EmailMessageRepo = EmailMessageRepo;