@chevre/domain
Version:
Chevre Domain Library for Node.js
145 lines (144 loc) • 6.36 kB
JavaScript
"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.HasPOSRepo = void 0;
const mongoose_1 = require("mongoose");
const factory = require("../../factory");
const settings_1 = require("../../settings");
const civicStructure_1 = require("../mongoose/schemas/civicStructure");
/**
* 施設のPOSリポジトリ
*/
class HasPOSRepo {
constructor(connection, operater) {
this.civicStructureModel = connection.model(civicStructure_1.modelName, (0, civicStructure_1.createSchema)());
this.operator = operater;
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const matchStages = [
{ $match: { _id: { $eq: new mongoose_1.Types.ObjectId(this.operator.id) } } }
];
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') {
matchStages.push({ $match: { 'project.id': { $eq: projectIdEq } } });
}
const branchCodeEq = (_c = params.branchCode) === null || _c === void 0 ? void 0 : _c.$eq;
if (typeof branchCodeEq === 'string') {
matchStages.push({ $match: { 'hasPOS.branchCode': { $eq: branchCodeEq } } });
}
const branchCodeRegex = (_d = params.branchCode) === null || _d === void 0 ? void 0 : _d.$regex;
if (typeof branchCodeRegex === 'string' && branchCodeRegex.length > 0) {
matchStages.push({ $match: { 'hasPOS.branchCode': { $regex: new RegExp(branchCodeRegex) } } });
}
const aggregate = this.civicStructureModel.aggregate([
{
$unwind: {
path: '$hasPOS'
}
},
...matchStages,
{ $sort: { 'hasPOS.branchCode': factory.sortType.Ascending } },
{
$project: {
_id: 0,
id: '$hasPOS.branchCode',
branchCode: '$hasPOS.branchCode',
name: '$hasPOS.name'
}
}
]);
if (typeof params.limit === 'number' && params.limit > 0) {
const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
aggregate.limit(params.limit * page)
.skip(params.limit * (page - 1));
}
return aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.exec();
});
}
createByBranchCode(params) {
return __awaiter(this, void 0, void 0, function* () {
// 施設存在確認
let doc = yield this.civicStructureModel.findOne({
'project.id': { $eq: params.project.id },
_id: { $eq: this.operator.id }
}, { _id: 1, typeOf: 1 })
.exec();
if (doc === null) {
throw new factory.errors.NotFound(factory.placeType.MovieTheater);
}
const creatingPOS = {
id: params.branchCode, // 互換性維持対応として
branchCode: params.branchCode,
name: params.name
};
doc = yield this.civicStructureModel.findOneAndUpdate({
'project.id': { $eq: params.project.id },
_id: { $eq: this.operator.id },
'hasPOS.branchCode': { $ne: params.branchCode }
}, {
$push: { hasPOS: creatingPOS }
}, {
new: true,
projection: { _id: 1 }
})
.exec();
// 存在しなければID重複
if (doc === null) {
throw new factory.errors.AlreadyInUse('hasPOS', ['id']);
}
return doc.toObject();
});
}
updateByBranchCode(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.civicStructureModel.findOneAndUpdate({
'project.id': { $eq: params.project.id },
_id: { $eq: this.operator.id },
'hasPOS.branchCode': { $eq: params.branchCode }
}, Object.assign({}, (params.name !== undefined && params.name !== null)
? { 'hasPOS.$[pos].name': params.name }
: undefined), {
new: true,
arrayFilters: [
{ 'pos.branchCode': { $eq: params.branchCode } }
],
projection: { _id: 1 }
})
.exec();
if (doc === null) {
throw new factory.errors.NotFound('hasPOS');
}
return doc.toObject();
});
}
deleteByBranchCode(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.civicStructureModel.findOneAndUpdate({
'project.id': { $eq: params.project.id },
_id: { $eq: this.operator.id },
'hasPOS.branchCode': { $eq: params.branchCode }
}, {
$pull: { hasPOS: { branchCode: { $eq: params.branchCode } } }
}, {
projection: { _id: 1 }
})
.exec();
if (doc === null) {
throw new factory.errors.NotFound('hasPOS');
}
return doc.toObject();
});
}
}
exports.HasPOSRepo = HasPOSRepo;