UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

181 lines (180 loc) 7.57 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.ServiceOutputRepo = void 0; const serviceOutput_1 = require("./mongoose/schemas/serviceOutput"); const factory = require("../factory"); const settings_1 = require("../settings"); /** * サービスアウトプットリポジトリ */ class ServiceOutputRepo { constructor(connection) { this.serviceOutputModel = connection.model(serviceOutput_1.modelName, (0, serviceOutput_1.createSchema)()); } // tslint:disable-next-line:max-func-body-length static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; // MongoDB検索条件 const andConditions = []; 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 typeOfEq = (_c = params.typeOf) === null || _c === void 0 ? void 0 : _c.$eq; if (typeof typeOfEq === 'string') { andConditions.push({ typeOf: { $eq: typeOfEq } }); } const idEq = (_d = params.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof idEq === 'string') { andConditions.push({ _id: { $eq: idEq } }); } const identifierEq = (_e = params.identifier) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof identifierEq === 'string') { andConditions.push({ identifier: { $exists: true, $eq: identifierEq } }); } const identifierIn = (_f = params.identifier) === null || _f === void 0 ? void 0 : _f.$in; if (Array.isArray(identifierIn)) { andConditions.push({ identifier: { $exists: true, $in: identifierIn } }); } const accessCodeEq = (_g = params.accessCode) === null || _g === void 0 ? void 0 : _g.$eq; if (typeof accessCodeEq === 'string') { andConditions.push({ accessCode: { $exists: true, $eq: accessCodeEq } }); } const issuedByIdEq = (_j = (_h = params.issuedBy) === null || _h === void 0 ? void 0 : _h.id) === null || _j === void 0 ? void 0 : _j.$eq; if (typeof issuedByIdEq === 'string') { andConditions.push({ 'issuedBy.id': { $exists: true, $eq: issuedByIdEq } }); } const issuedThroughIdEq = (_l = (_k = params.issuedThrough) === null || _k === void 0 ? void 0 : _k.id) === null || _l === void 0 ? void 0 : _l.$eq; if (typeof issuedThroughIdEq === 'string') { andConditions.push({ 'issuedThrough.id': { $exists: true, $eq: issuedThroughIdEq } }); } const issuedThroughTypeOfEq = (_o = (_m = params.issuedThrough) === null || _m === void 0 ? void 0 : _m.typeOf) === null || _o === void 0 ? void 0 : _o.$eq; if (typeof issuedThroughTypeOfEq === 'string') { andConditions.push({ 'issuedThrough.typeOf': { $exists: true, $eq: issuedThroughTypeOfEq } }); } const issuedThroughServiceTypeCodeValueEq = (_r = (_q = (_p = params.issuedThrough) === null || _p === void 0 ? void 0 : _p.serviceType) === null || _q === void 0 ? void 0 : _q.codeValue) === null || _r === void 0 ? void 0 : _r.$eq; if (typeof issuedThroughServiceTypeCodeValueEq === 'string') { andConditions.push({ 'issuedThrough.serviceType.codeValue': { $exists: true, $eq: issuedThroughServiceTypeCodeValueEq } }); } return andConditions; } /** * 許可証検索 */ search(params, projection) { return __awaiter(this, void 0, void 0, function* () { const conditions = ServiceOutputRepo.CREATE_MONGO_CONDITIONS(params); const query = this.serviceOutputModel.find((conditions.length > 0) ? { $and: conditions } : {}, Object.assign({ __v: 0, createdAt: 0, updatedAt: 0 }, projection)); 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())); }); } /** * 許可証発行 */ issue(params) { return __awaiter(this, void 0, void 0, function* () { yield this.serviceOutputModel.create(params); }); } /** * 許可証有効化 */ activate(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.serviceOutputModel.findOneAndUpdate({ typeOf: params.typeOf, identifier: params.identifier }, Object.assign({ validFrom: params.validFrom }, (params.validUntil instanceof Date) ? { validUntil: params.validUntil } : undefined)) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.serviceOutputModel.modelName); } return doc.toObject(); }); } deleteByProject(params) { return __awaiter(this, void 0, void 0, function* () { yield this.serviceOutputModel.deleteMany({ 'project.id': { $eq: params.project.id } }) .exec(); }); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.serviceOutputModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } } exports.ServiceOutputRepo = ServiceOutputRepo;