@briswell/bw-domain
Version:
Domain Library for Node.js
78 lines (77 loc) • 3.32 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 customer_1 = require("../model/customer");
const common_1 = require("../utils/common");
class CustomerRepository {
constructor(db) {
this.customerModel = customer_1.default(db);
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
const findOption = common_1.stripUndefinedField(params);
if (params.sort !== undefined) {
findOption.order = [['id', params.sort]];
}
if (findOption.where !== undefined) {
const where = findOption.where;
if (where.name !== undefined) {
findOption.where = Object.assign({}, where, { [Sequelize.Op.or]: {
name: { [Sequelize.Op.like]: `%${where.name}%` },
kana: { [Sequelize.Op.like]: `%${where.name}%` },
} });
delete findOption.where.name;
}
if (where.email !== undefined) {
where.email = { [Sequelize.Op.like]: `%${where.email}%` };
}
if (where.tel !== undefined) {
where.tel = { [Sequelize.Op.like]: `%${where.tel}%` };
}
}
if (params.limit !== undefined) {
findOption.limit = params.limit;
}
if (params.offset !== undefined) {
findOption.offset = params.offset;
}
return this.customerModel.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) { // 編集
const updateResult = yield this.customerModel.update(params, {
where: { id: params.id, updatedAt: params.lastUpdate }
});
if (updateResult[0] === 0) {
return null;
}
resultId = params.id;
}
else { // 作成
params.createdBy = params.updatedBy;
const temp = yield this.customerModel.create(params);
resultId = temp.id;
}
const result = yield this.customerModel.findById(resultId);
return result;
});
}
}
exports.default = CustomerRepository;