UNPKG

@briswell/bw-domain

Version:

Domain Library for Node.js

191 lines (190 loc) 8.73 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 billing_1 = require("../model/billing"); const sales_1 = require("../model/sales"); const salesDetail_1 = require("../model/salesDetail"); const common_1 = require("../utils/common"); class SalesRepository { constructor(db) { this.sequelize = db; this.salesModel = sales_1.default(db); this.salesDetailModel = salesDetail_1.default(db); this.billingModel = billing_1.default(db); this.salesModel.hasMany(this.salesDetailModel, { foreignKey: 'salesId' }); this.salesModel.belongsTo(this.billingModel, { foreignKey: 'billingId' }); } 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.billingDate === 'object' && !(strippedParams.where.billingDate instanceof Date)) { const billingDate = strippedParams.where.billingDate; where.billingDate = {}; if (billingDate.from !== undefined) { where.billingDate = Object.assign({}, where.billingDate, { [Sequelize.Op.gte]: billingDate.from }); } if (billingDate.to !== undefined) { where.billingDate = Object.assign({}, where.billingDate, { [Sequelize.Op.lte]: billingDate.to }); } } // 支払期限日の条件がある場合 if (typeof strippedParams.where.paymentDate === 'object' && !(strippedParams.where.paymentDate instanceof Date)) { const paymentDate = strippedParams.where.paymentDate; where.paymentDate = {}; if (paymentDate.from !== undefined) { where.paymentDate = Object.assign({}, where.paymentDate, { [Sequelize.Op.gte]: paymentDate.from }); } if (paymentDate.to !== undefined) { where.paymentDate = Object.assign({}, where.paymentDate, { [Sequelize.Op.lte]: paymentDate.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}%` }; } } 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.salesModel.findAndCount(findOption); }); } /** * 売上詳細検索 * @param salesId 売上ID */ searchDetail(salesId) { return __awaiter(this, void 0, void 0, function* () { const findOption = { where: { id: salesId }, include: [this.salesDetailModel, this.billingModel] }; return this.salesModel.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.salesDetails !== undefined && params.salesDetails.length > 0) { const salesDetailIds = []; for (const detail of params.salesDetails) { detail.updatedBy = params.updatedBy; if (detail.id === undefined) { // 見積詳細作成 // 必須フィールドをセットする detail.salesId = params.id; detail.createdBy = params.updatedBy; const create = yield this.salesDetailModel.create(detail); salesDetailIds.push(create.id); } else { // 見積詳細編集 yield this.salesDetailModel.update(detail, { where: { id: detail.id } }); salesDetailIds.push(detail.id); } } if (salesDetailIds.length > 0) { // 明細を削除 yield this.salesDetailModel.destroy({ where: { id: { [Sequelize.Op.notIn]: salesDetailIds }, salesId: params.id } }); } } else { // 全ての明細を削除 yield this.salesDetailModel.destroy({ where: { salesId: params.id } }); } yield this.salesModel.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.salesDetails !== undefined && params.salesDetails.length > 0) { params.salesDetails.forEach((detail) => { // 必須フィールドをセットする detail.createdBy = params.updatedBy; detail.updatedBy = params.updatedBy; }); const transaction = yield this.sequelize.transaction(); try { temp = yield this.salesModel.create(params, { include: [this.salesDetailModel] }); transaction.commit(); } catch (err) { transaction.rollback(); throw err; } } else { // 見積詳細がない場合 temp = yield this.salesModel.create(params); } resultId = temp.id; } const result = yield this.salesModel.findById(resultId, { include: [this.salesDetailModel] }); return result; }); } } exports.default = SalesRepository;