@briswell/bw-domain
Version:
Domain Library for Node.js
82 lines (81 loc) • 3.55 kB
JavaScript
;
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 claim_1 = require("../model/claim");
const customer_1 = require("../model/customer");
const common_1 = require("../utils/common");
class ClaimRepository {
constructor(db) {
this.claimModel = claim_1.default(db);
this.customerModel = customer_1.default(db);
this.claimModel.belongsTo(this.customerModel);
this.customerModel.hasMany(this.claimModel);
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
const strippedParams = common_1.stripUndefinedField(params);
const findOption = strippedParams;
findOption.include = [this.customerModel];
if (strippedParams.where !== undefined) {
const where = findOption.where;
if (typeof strippedParams.where.customer === 'object') {
const customerCondition = {};
if (strippedParams.where.customer.name !== undefined) {
customerCondition.name = {
[Sequelize.Op.like]: `%${strippedParams.where.customer.name}%`
};
}
delete strippedParams.where.customer;
findOption.include = [{
model: this.customerModel,
where: customerCondition
}];
}
if (where.title !== undefined) {
where.title = { [Sequelize.Op.like]: `%${where.title}%` };
}
}
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.claimModel.findAndCount(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) { // 編集
yield this.claimModel.update(params, { where: { id: params.id } });
resultId = params.id;
}
else { // 作成
params.createdBy = params.updatedBy;
const temp = yield this.claimModel.create(params);
resultId = temp.id;
}
const result = yield this.claimModel.findById(resultId);
return result;
});
}
}
exports.default = ClaimRepository;