UNPKG

@briswell/bw-domain

Version:

Domain Library for Node.js

181 lines (180 loc) 8.82 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const Sequelize = require("sequelize"); const quotation_1 = require("../model/quotation"); const quotationDetail_1 = require("../model/quotationDetail"); const common_1 = require("../utils/common"); class QuotationRepository { constructor(db) { this.sequelize = db; this.quotationModel = quotation_1.default(db); this.quotationDetailModel = quotationDetail_1.default(db); this.quotationModel.hasMany(this.quotationDetailModel); } search(params) { return __awaiter(this, void 0, void 0, function* () { const strippedParams = common_1.stripUndefinedField(params); const findOption = strippedParams; if (strippedParams.where !== undefined) { const where = findOption.where; // 見積日の条件がある場合 if (typeof strippedParams.where.quotationDate === 'object' && !(strippedParams.where.quotationDate instanceof Date)) { const quotationDate = strippedParams.where.quotationDate; where.quotationDate = {}; if (quotationDate.from !== undefined) { where.quotationDate = Object.assign({}, where.quotationDate, { [Sequelize.Op.gte]: quotationDate.from }); } if (quotationDate.to !== undefined) { where.quotationDate = Object.assign({}, where.quotationDate, { [Sequelize.Op.lte]: quotationDate.to }); } } // 見積有効期限の条件がある場合 if (typeof strippedParams.where.quotationExpirationDate === 'object' && !(strippedParams.where.quotationExpirationDate instanceof Date)) { const quotationExpirationDate = strippedParams.where.quotationExpirationDate; where.quotationExpirationDate = {}; if (quotationExpirationDate.from !== undefined) { where.quotationExpirationDate = Object.assign({}, where.quotationExpirationDate, { [Sequelize.Op.gte]: quotationExpirationDate.from }); } if (quotationExpirationDate.to !== undefined) { where.quotationExpirationDate = Object.assign({}, where.quotationExpirationDate, { [Sequelize.Op.lte]: quotationExpirationDate.to }); } } if (strippedParams.where.customerName !== undefined) { where.customerName = { [Sequelize.Op.like]: `%${strippedParams.where.customerName}%` }; } if (strippedParams.where.customerEmail !== undefined) { where.customerEmail = { [Sequelize.Op.like]: `%${strippedParams.where.customerEmail}%` }; } if (strippedParams.where.customerTel !== undefined) { where.customerTel = { [Sequelize.Op.like]: `%${strippedParams.where.customerTel}%` }; } } // findOption.include = [ this.quotationDetailModel ]; if (params.sort !== undefined) { findOption.order = [['id', params.sort]]; } if (params.limit !== undefined) { findOption.limit = params.limit; } if (params.offset !== undefined) { findOption.offset = params.offset; } return this.quotationModel.findAndCount(findOption); }); } searchDetail(quotationId) { return __awaiter(this, void 0, void 0, function* () { const findOption = { where: { id: quotationId }, include: [this.quotationDetailModel] }; return this.quotationModel.findAll(findOption); }); } /** * IDがある場合更新する、ない場合新しい作成する * @param params データ */ upsert(params) { return __awaiter(this, void 0, void 0, function* () { params.updatedBy = params.userId; delete params.userId; let resultId; if (params.id !== undefined) { // 編集 const transaction = yield this.sequelize.transaction(); try { if (params.quotationDetails !== undefined && params.quotationDetails.length > 0) { const quotationDetailIds = []; for (const detail of params.quotationDetails) { detail.updatedBy = params.updatedBy; if (detail.id === undefined) { // 見積詳細作成 // 必須フィールドをセットする detail.quotationId = params.id; detail.createdBy = params.updatedBy; const create = yield this.quotationDetailModel.create(detail); quotationDetailIds.push(create.id); } else { // 見積詳細編集 yield this.quotationDetailModel.update(detail, { where: { id: detail.id } }); quotationDetailIds.push(detail.id); } } if (quotationDetailIds.length > 0) { // 明細を削除 yield this.quotationDetailModel.destroy({ where: { id: { [Sequelize.Op.notIn]: quotationDetailIds }, quotationId: params.id } }); } } else { // 全ての明細を削除 yield this.quotationDetailModel.destroy({ where: { quotationId: params.id } }); } yield this.quotationModel.update(params, { where: { id: params.id } }); resultId = params.id; yield transaction.commit(); } catch (err) { yield transaction.rollback(); throw err; } } else { // 作成 params.createdBy = params.updatedBy; let temp; if (params.quotationDetails !== undefined && params.quotationDetails.length > 0) { params.quotationDetails.forEach((detail) => { // 必須フィールドをセットする detail.createdBy = params.updatedBy; detail.updatedBy = params.updatedBy; }); const transaction = yield this.sequelize.transaction(); try { temp = yield this.quotationModel.create(params, { include: [this.quotationDetailModel] }); yield transaction.commit(); } catch (err) { yield transaction.rollback(); throw err; } } else { // 見積詳細がない場合 temp = yield this.quotationModel.create(params); } resultId = temp.id; } const result = yield this.quotationModel.findById(resultId, { include: [this.quotationDetailModel] }); return result; }); } } exports.default = QuotationRepository;