@chevre/domain
Version:
Chevre Domain Library for Node.js
90 lines (89 loc) • 4.53 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importFromCOA = importFromCOA;
const createDebug = require("debug");
const moment = require("moment-timezone");
const event_1 = require("../../event");
const debug = createDebug('chevre-domain:service');
/**
* イベント席数を更新する
*/
function importFromCOA(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
try {
// COAから空席状況取得
const countFreeSeatResult = yield repos.reserveService.countFreeSeat({
theaterCode: params.locationBranchCode,
begin: moment(params.importFrom)
.tz('Asia/Tokyo')
.format('YYYYMMDD'), // COAは日本時間で判断
end: moment(params.importThrough)
.tz('Asia/Tokyo')
.format('YYYYMMDD') // COAは日本時間で判断
});
debug('countFreeSeatResult:', countFreeSeatResult);
const bulkWriteOps = [];
if (Array.isArray(countFreeSeatResult.listDate)) {
for (const countFreeSeatDate of countFreeSeatResult.listDate) {
if (Array.isArray(countFreeSeatDate.listPerformance)) {
for (const countFreeSeatPerformance of countFreeSeatDate.listPerformance) {
try {
const eventId = (0, event_1.createScreeningEventIdFromCOA)({
theaterCode: countFreeSeatResult.theaterCode,
titleCode: countFreeSeatPerformance.titleCode,
titleBranchNum: countFreeSeatPerformance.titleBranchNum,
dateJouei: countFreeSeatDate.dateJouei,
screenCode: countFreeSeatPerformance.screenCode,
timeBegin: countFreeSeatPerformance.timeBegin
});
const remainingAttendeeCapacity = Math.max(0, Number(countFreeSeatPerformance.cntReserveFree));
bulkWriteOps.push({
updateOne: {
filter: {
_id: eventId,
remainingAttendeeCapacity: { $ne: remainingAttendeeCapacity }
},
update: { remainingAttendeeCapacity: remainingAttendeeCapacity }
}
});
}
catch (error) {
// tslint:disable-next-line:no-console
console.error('createScreeningEventIdFromCOA error:', error);
}
}
}
}
}
debug(bulkWriteOps.length, 'ops writing...');
if (bulkWriteOps.length > 0) {
const res = yield repos.event.bulkWrite(bulkWriteOps);
// const res = await repos.event.eventModel.bulkWrite(bulkWriteOps, { ordered: false });
debug('bulkWrite res:', res);
}
}
catch (error) {
let throwsError = true;
if (error.name === 'AbortError') {
throwsError = false;
}
if (error.name === 'COAServiceError') {
if (error.message === 'ESOCKETTIMEDOUT') {
throwsError = false;
}
}
if (throwsError) {
throw error;
}
}
});
}
;