UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

158 lines (157 loc) 7.67 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.CustomerRepo = void 0; const customer_1 = require("./mongoose/schemas/customer"); const factory = require("../factory"); const settings_1 = require("../settings"); const AVAILABLE_PROJECT_FIELDS = [ 'additionalProperty', 'contactPoint', // 'email', 'branchCode', 'name', 'project', 'typeOf', 'url' // 'telephone'f ]; /** * 顧客リポジトリ */ class CustomerRepo { constructor(connection) { this.customerModel = connection.model(customer_1.modelName, (0, customer_1.createSchema)()); } static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e, _f; 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 branchCodeRegex = (_c = params.branchCode) === null || _c === void 0 ? void 0 : _c.$regex; if (typeof branchCodeRegex === 'string' && branchCodeRegex.length > 0) { andConditions.push({ branchCode: { $regex: new RegExp(branchCodeRegex) } }); } const idEq = (_d = params.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof idEq === 'string') { andConditions.push({ _id: { $eq: idEq } }); } const nameRegex = (_e = params.name) === null || _e === void 0 ? void 0 : _e.$regex; 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) } } ] }); } const additionalPropertyElemMatch = (_f = params.additionalProperty) === null || _f === void 0 ? void 0 : _f.$elemMatch; if (additionalPropertyElemMatch !== undefined && additionalPropertyElemMatch !== null) { andConditions.push({ additionalProperty: { $exists: true, $elemMatch: additionalPropertyElemMatch } }); } return andConditions; } save(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; let savedId; if (params.id === undefined) { const _c = params.attributes, { id, $unset } = _c, creatingDoc = __rest(_c, ["id", "$unset"]); const result = yield this.customerModel.insertMany(creatingDoc, { 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(`customer not saved unexpectedly. result:${JSON.stringify(result)}`); } savedId = insertedId; } else { const _d = params.attributes, { id, branchCode, project, typeOf, $unset } = _d, updateFields = __rest(_d, ["id", "branchCode", "project", "typeOf", "$unset"]); const doc = yield this.customerModel.findOneAndUpdate({ _id: { $eq: params.id } }, Object.assign({ $set: updateFields }, ($unset !== undefined) ? { $unset } : undefined), { upsert: false, new: true, projection: { _id: 1, id: { $toString: '$_id' } } }) .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.customerModel.modelName); } savedId = params.id; } return { id: savedId }; }); } /** * 顧客検索 */ projectFields(conditions, inclusion) { return __awaiter(this, void 0, void 0, function* () { var _a; const andConditions = CustomerRepo.CREATE_MONGO_CONDITIONS(conditions); let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key)); } else { // if (Array.isArray(exclusion) && exclusion.length > 0) { // positiveProjectionFields = positiveProjectionFields.filter((key) => !exclusion.includes(key)); // } } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); const query = this.customerModel.find((andConditions.length > 0) ? { $and: andConditions } : {}, projection); if (typeof conditions.limit === 'number' && conditions.limit > 0) { const page = (typeof conditions.page === 'number' && conditions.page > 0) ? conditions.page : 1; query.limit(conditions.limit) .skip(conditions.limit * (page - 1)); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (((_a = conditions.sort) === null || _a === void 0 ? void 0 : _a.branchCode) !== undefined) { query.sort({ branchCode: conditions.sort.branchCode }); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() // 2024-09-03~ .exec(); }); } deleteById(params) { return __awaiter(this, void 0, void 0, function* () { yield this.customerModel.findOneAndDelete({ _id: { $eq: params.id } }, { projection: { _id: 1 } }) .exec(); }); } getCursor(conditions, projection) { return this.customerModel.find(conditions, projection) .sort({ branchCode: factory.sortType.Ascending }) .cursor(); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.customerModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } } exports.CustomerRepo = CustomerRepo;