@chevre/domain
Version:
Chevre Domain Library for Node.js
105 lines (104 loc) • 4.68 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.aggregate = aggregate;
/**
* プロジェクト集計サービス
*/
const createDebug = require("debug");
const moment = require("moment");
const factory = require("../../factory");
const debug = createDebug('chevre-domain:service');
function aggregate(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
const now = new Date();
// 集計対象プロジェクト検索
const project = yield repos.project.findById({
id: params.project.id,
inclusion: ['typeOf']
});
// 予約集計
const { aggregateReservation } = yield aggregateReservationOnProject({
aggregateDate: now,
project: { id: project.id, typeOf: project.typeOf },
reservationFor: {
startFrom: moment(params.reservationFor.startFrom)
.toDate(),
startThrough: moment(params.reservationFor.startThrough)
.toDate()
}
})(repos);
debug('aggregated. aggregateReservation:', aggregateReservation);
// 保管
yield repos.project.updateAggregateReservation({
id: project.id,
aggregateReservation
});
});
}
function aggregateReservationOnProject(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
let attendeeCount = 0;
let checkInCount = 0;
let reservationCount = 0;
const reservationForConditions = {
startFrom: moment(params.reservationFor.startFrom)
.add(-1, 'day')
.toDate()
};
while (reservationForConditions.startThrough === undefined
|| moment(params.reservationFor.startThrough)
.isAfter(moment(reservationForConditions.startThrough))) {
reservationForConditions.startFrom = moment(reservationForConditions.startFrom)
.add(1, 'day')
.toDate();
reservationForConditions.startThrough = moment(reservationForConditions.startFrom)
.add(1, 'day')
.add(-1, 'millisecond')
.toDate();
debug('counting...', reservationForConditions);
reservationCount += yield repos.reservation.count({
project: { id: { $eq: params.project.id } },
typeOf: factory.reservationType.EventReservation,
reservationFor: reservationForConditions,
reservationStatuses: [factory.reservationStatusType.ReservationConfirmed]
});
attendeeCount += yield repos.reservation.count({
project: { id: { $eq: params.project.id } },
typeOf: factory.reservationType.EventReservation,
reservationFor: reservationForConditions,
// reservationStatuses: [factory.reservationStatusType.ReservationConfirmed],
attended: true
});
checkInCount += yield repos.reservation.count({
project: { id: { $eq: params.project.id } },
typeOf: factory.reservationType.EventReservation,
reservationFor: reservationForConditions,
// reservationStatuses: [factory.reservationStatusType.ReservationConfirmed],
checkedIn: true
});
}
return {
aggregateReservation: {
typeOf: 'AggregateReservation',
aggregateDate: params.aggregateDate,
reservationFor: {
startDate: `${moment(params.reservationFor.startFrom)
.toISOString()}/${moment(params.reservationFor.startThrough)
.toISOString()}`
},
attendeeCount,
checkInCount,
reservationCount
}
};
});
}
;