UNPKG

@briswell/bw-domain

Version:

Domain Library for Node.js

110 lines (109 loc) 4.99 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 factory = require("@briswell/bw-factory"); const Sequelize = require("sequelize"); const fileStatus_1 = require("../model/fileStatus"); const common_1 = require("../utils/common"); class FileStatusRepository { constructor(db) { this.fileStatusModel = fileStatus_1.default(db); } /** * SQSキューメッセージのIDからレコードを作成 * @param sqsMessageId SQSキューメッセージのID * @param userId ログインしているのユーザーID、デフォルトは1=root */ create(sqsMessageId, userId = 1) { return __awaiter(this, void 0, void 0, function* () { yield this.fileStatusModel.create({ status: 1, sqsId: sqsMessageId, updatedBy: userId, createdBy: userId }); }); } /** * ステータス更新、完了したら、ファイルパスも追加する * @param attributes 検索条件+更新ステータス+ファイルパス * @param userId ログインしているのユーザーID、デフォルトは2=AWSLambda Function */ updateStatus(attributes, userId = 2) { return __awaiter(this, void 0, void 0, function* () { if ((attributes.id === undefined && attributes.sqsId === undefined) || (attributes.status === 3 && attributes.filePath === undefined) || attributes.status === undefined) { throw new factory.errors.Argument('情報が足りません。'); } yield this.fileStatusModel.update({ status: attributes.status, filePath: attributes.filePath, updatedBy: userId }, { where: { [Sequelize.Op.or]: { id: attributes.id, sqsId: attributes.sqsId } } }); }); } /** * ファイル検索 * @param params 検索条件 * @returns ファイル情報一覧 */ 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.createdAt === 'object' && !(strippedParams.where.createdAt instanceof Date)) { const createdAt = strippedParams.where.createdAt; where.createdAt = {}; if (createdAt.from !== undefined) { where.createdAt = Object.assign({}, where.createdAt, { [Sequelize.Op.gte]: createdAt.from }); } if (createdAt.to !== undefined) { where.createdAt = Object.assign({}, where.createdAt, { [Sequelize.Op.lte]: createdAt.to }); } } // 更新日の条件がある場合 if (typeof strippedParams.where.updatedAt === 'object' && !(strippedParams.where.updatedAt instanceof Date)) { const updatedAt = strippedParams.where.updatedAt; where.updatedAt = {}; if (updatedAt.from !== undefined) { where.updatedAt = Object.assign({}, where.updatedAt, { [Sequelize.Op.gte]: updatedAt.from }); } if (updatedAt.to !== undefined) { where.updatedAt = Object.assign({}, where.updatedAt, { [Sequelize.Op.lte]: updatedAt.to }); } } } 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.fileStatusModel.findAndCount(findOption); }); } } exports.default = FileStatusRepository;