Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#156 택시 탑승이 혼자인 경우, 자동으로 결제 완료 및 과거 참여 방으로 이동 #194

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a6e3101
Add: change searchByUserHandler
Hyogyeong8 Nov 25, 2022
2762aba
Fix: searchByUserHandler
Hyogyeong8 Nov 25, 2022
fa7f01f
Fix: searchByUserHandler app crash
Hyogyeong8 Nov 26, 2022
98537e4
Fix: searchByUserHandler
Hyogyeong8 Nov 26, 2022
88d5899
Fix(rooms.js): searchByUserHandler remove lean
Hyogyeong8 Nov 29, 2022
97a4ad5
Fix: searchByUserHandler moving operation
Hyogyeong8 Nov 29, 2022
36136a6
Merge branch 'main' into #156-move-rooms-to-the-past-room-list-and-ch…
Hyogyeong8 Nov 29, 2022
a231e2d
Docs: searchByUserHandler
Hyogyeong8 Nov 30, 2022
739cdcc
Merge branch 'dev' into #156-move-rooms-to-the-past-room-list-and-cha…
14KGun Jan 7, 2023
dd928dc
Merge branch '#156-move-rooms-to-the-past-room-list-and-change-settle…
Hyogyeong8 Feb 28, 2023
18d2822
Merge branch 'dev' into #156-move-rooms-to-the-past-room-list-and-cha…
14KGun Sep 8, 2023
11cfed9
Remove: setTimestamp
14KGun Sep 8, 2023
fea3b9a
Merge pull request #483 from sparcs-kaist/dev
kmc7468 Mar 3, 2024
6c2ef72
Merge pull request #491 from sparcs-kaist/dev
kmc7468 Mar 10, 2024
66595a9
Merge pull request #494 from sparcs-kaist/dev
kmc7468 Mar 11, 2024
551e3bc
Merge pull request #496 from sparcs-kaist/dev
kmc7468 Mar 11, 2024
63acbe7
Merge pull request #501 from sparcs-kaist/dev
kmc7468 Mar 19, 2024
356e56e
Merge pull request #507 from sparcs-kaist/dev
kmc7468 Mar 26, 2024
620b67d
Merge remote-tracking branch 'origin/main' into #156-move-rooms-to-th…
cokia Apr 2, 2024
abda400
fix: autoProcessingRoom cron logic
cokia Apr 23, 2024
e0c6415
Merge branch 'dev' into #156-move-rooms-to-the-past-room-list-and-cha…
cokia May 21, 2024
534bdd3
Merge branch 'dev' into #156-move-rooms-to-the-past-room-list-and-cha…
cokia Sep 3, 2024
f275b14
Add: autoProcessingRooms
TaehyeonPark Feb 3, 2025
11df570
Add: merge to dev
TaehyeonPark Feb 3, 2025
3bdf0a7
Fix: confliction
TaehyeonPark Feb 4, 2025
4a59be0
Add: resolve comments
TaehyeonPark Feb 4, 2025
758b428
Add: resolve race condition
TaehyeonPark Feb 5, 2025
b75ba0f
Add: resolve comments
TaehyeonPark Feb 5, 2025
ac12dbe
Add: resolve comments
TaehyeonPark Feb 5, 2025
5077617
Add: resolve comments
TaehyeonPark Feb 5, 2025
ec34739
Add: resolve comments
TaehyeonPark Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/schedules/autoSettlement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { Express } from "express";
import { emitChatEvent } from "@/modules/socket";
import { userModel, roomModel } from "@/modules/stores/mongo";
import logger from "@/modules/logger";

const MS_PER_MINUTE = 60000;

// 탑승자가 1명인 상태로 탑승 시간이 지난 방에 대해서 정산 완료 처리
const autoSettlement = (app: Express) => async () => {
try {
const io = app.get("io");
const expiredDate = new Date(Date.now() - 60 * MS_PER_MINUTE).toISOString();
const arrivalDate = new Date(Date.now()).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"] } }, // "sent"의 경우 로직상 불가능 하지만, 문서화 측면에서 의도적으로 남겨두었음.
],
});

await Promise.all(
candidateRooms.map(async ({ _id: roomId, part }) => {
const user = await userModel.findById(part![0].user._id);
// 정산 채팅을 보냅니다.
await emitChatEvent(io, {
roomId: roomId.toString(),
type: "settlement",
content: user!.id,
authorId: user!._id.toString(),
time: null,
});

// 1명의 참여자만 존재하는 room에 대하여 정산 완료 처리.
await roomModel.findByIdAndUpdate(roomId, {
["part.0.settlementStatus"]: "paid",
settlementTotal: 1,
});

// Atomic update로 각 Room을 한번에 제거 및 추가함.
// 아토믹하게 처리하지 않을 경우 각 Promise가 동일한 user의 여러 ongoingRoom 또는 doneRoom을 동시에 수정하여 경합조건이 발생할 수 있음에 유의.
await userModel.findByIdAndUpdate(user!._id, {
$pull: { ongoingRoom: roomId },
$push: { doneRoom: roomId },
});
})
);
} catch (err) {
logger.error(err);
}
};

export default autoSettlement;
3 changes: 2 additions & 1 deletion src/schedules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import notifyBeforeDepart from "./notifyBeforeDepart";
import notifyAfterArrival from "./notifyAfterArrival";
import updateMajorTaxiFare from "./updateMajorTaxiFare";
import updateMinorTaxiFare from "./updateMinorTaxiFare";
import autoSettlement from "./autoSettlement";

const registerSchedules = (app: Express) => {
cron.schedule("*/5 * * * *", notifyBeforeDepart(app));
cron.schedule("*/10 * * * *", notifyAfterArrival(app));

cron.schedule("1,11,21,31,41,51 * * * *", autoSettlement(app));
if (naverMap.apiId && naverMap.apiKey) {
cron.schedule("0,30 * * * * ", updateMajorTaxiFare(app));
cron.schedule("0 18 * * *", updateMinorTaxiFare(app));
Expand Down
1 change: 1 addition & 0 deletions src/services/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ const searchHandler = async (req, res) => {

const searchByUserHandler = async (req, res) => {
try {
// lean()이 적용된 user를 response에 반환해줘야 하기 때문에 user를 한 번 더 지정한다.
const user = await userModel
.findOne({ id: req.userId })
.populate({
Expand Down
Loading