Skip to content

Commit

Permalink
fix: autoProcessingRoom cron logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cokia committed Apr 23, 2024
1 parent 620b67d commit abda400
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/schedules/autoProcessingRoom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { userModel, roomModel, chatModel } = require("../modules/stores/mongo");
const logger = require("../modules/logger");
const { MS_PER_MINUTE } = require("../modules/constants");
const { emitChatEvent } = require("../modules/socket");

// 탑승자가 1명인 상태로 탑승일이 지난 방에 대해서 정산 완료 처리
module.exports = (app) => async () => {
try {
const io = app.get("io");
const expiredDate = new Date(Date.now() - 90 * MS_PER_MINUTE).toISOString();
const arrivalDate = new Date(Date.now() - 60 * MS_PER_MINUTE).toISOString();

const candidateRooms = await roomModel.find({
$and: [
{ time: { $gte: expiredDate } },
{ time: { $lte: arrivalDate } },
{ "part.0": { $exists: true }, "part.1": { $exists: false } },
{ "part.0.settlementStatus": { $nin: ["paid", "sent"] } },
],
});

await Promise.all(
candidateRooms.map(async ({ _id: roomId, time, part }) => {
const countArrivalChat = await chatModel.countDocuments({
roomId,
type: "arrival",
});
if (countArrivalChat > 0) return;
const minuteDiff = Math.floor((Date.now() - time) / MS_PER_MINUTE);
if (minuteDiff <= 0) return;
await emitChatEvent(io, {
roomId: roomId,
type: "arrival",
content: minuteDiff.toString(),
});
// user에게 doneroom 으로 이전
const user = await userModel.findById(part[0].userId);
user.doneRooms.push(roomId);

const userOngoingRoomIndex = user.ongoingRoom.indexOf(roomId);
if (userOngoingRoomIndex === -1) {
await user.save();
return false;
}
user.ongoingRoom.splice(userOngoingRoomIndex, 1);

await user.save();

// room에 대한 정산 완료 처리 isOver
await roomModel.findByIdAndUpdate(roomId, { isOver: true });
})
);
} catch (err) {
logger.error(err);
}
};
1 change: 1 addition & 0 deletions src/schedules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const cron = require("node-cron");
const registerSchedules = (app) => {
cron.schedule("*/5 * * * *", require("./notifyBeforeDepart")(app));
cron.schedule("*/10 * * * *", require("./notifyAfterArrival")(app));
cron.schedule("*/15 * * * *", require("./autoProcessingRoom")(app));
};

module.exports = registerSchedules;

0 comments on commit abda400

Please sign in to comment.