UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

97 lines (96 loc) 4.79 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.TicketRepo = void 0; const factory = require("../factory"); const settings_1 = require("../settings"); const ticket_1 = require("./mongoose/schemas/ticket"); const AVAILABLE_PROJECT_FIELDS = [ 'project', 'typeOf', 'ticketToken', 'dateIssued', 'issuedBy' ]; /** * チケットリポジトリ */ class TicketRepo { constructor(connection) { this.ticketModel = connection.model(ticket_1.modelName, (0, ticket_1.createSchema)()); } static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d; 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 idEq = (_c = params.id) === null || _c === void 0 ? void 0 : _c.$eq; if (typeof idEq === 'string') { andConditions.push({ _id: { $eq: idEq } }); } const ticketTokenEq = (_d = params.ticketToken) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof ticketTokenEq === 'string') { andConditions.push({ ticketToken: { $eq: ticketTokenEq } }); } return andConditions; } /** * 承認コードからチケットを発行する */ issueByTicketToken(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const { ticketToken, project, issuedBy } = params; const ticketIssuedBy = (typeof (issuedBy === null || issuedBy === void 0 ? void 0 : issuedBy.id) === 'string' && issuedBy.typeOf === factory.organizationType.Corporation) ? issuedBy : { id: project.id, typeOf: factory.organizationType.Project }; const creatingTicket = { dateIssued: new Date(), issuedBy: ticketIssuedBy, project: { id: project.id, typeOf: factory.organizationType.Project }, ticketToken, typeOf: 'Ticket' }; const result = yield this.ticketModel.insertMany(creatingTicket, { rawResult: true }); const insertedId = (_b = (_a = result.insertedIds) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.toHexString(); if (typeof insertedId !== 'string') { throw new factory.errors.Internal(`ticket not saved unexpectedly. result:${JSON.stringify(result)}`); } return { id: insertedId }; }); } projectFields(params, inclusion) { return __awaiter(this, void 0, void 0, function* () { var _a; const conditions = TicketRepo.CREATE_MONGO_CONDITIONS(params); let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key)); } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); const query = this.ticketModel.find((conditions.length > 0) ? { $and: conditions } : {}, 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)); } if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.dateIssued) !== undefined) { query.sort({ dateIssued: params.sort.dateIssued }); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() // 2024-08-20~ .exec(); }); } } exports.TicketRepo = TicketRepo;