Skip to content

Commit

Permalink
Add: migration script that converts userId to userOid in chats
Browse files Browse the repository at this point in the history
  • Loading branch information
kmc7468 committed Jan 21, 2025
1 parent 069241b commit e54cca3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
41 changes: 41 additions & 0 deletions scripts/chatContentUserIdUpdater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { MongoClient } = require("mongodb");
const { mongo: mongoUrl } = require("@/loadenv");

const client = new MongoClient(mongoUrl);
const db = client.db("taxi");
const chats = db.collection("chats");
const users = db.collection("users");

async function convertUserIdToOid(userId) {
const user = await users.findOne({ id: userId, withdraw: false }, "_id");
if (!user) throw new Error(`User not found: ${userId}`);
return user._id.toString();
}

async function run() {
try {
for await (const doc of chats.find()) {
if (doc.type === "in" || doc.type === "out") {
const inOutUserIds = doc.content.split("|");
const inOutUserOids = await Promise.all(
inOutUserIds.map(convertUserIdToOid)
);
await chats.updateOne(
{ _id: doc._id },
{ $set: { content: inOutUserOids.join("|") } }
);
} else if (doc.type === "payment" || doc.type === "settlement") {
const userId = doc.content;
const userOid = await convertUserIdToOid(userId);
await chats.updateOne({ _id: doc._id }, { $set: { content: userOid } });
}
}
} catch (err) {
console.error(err);
} finally {
await client.close();
}
}
run().then(() => {
console.log("Done!");
});
7 changes: 2 additions & 5 deletions src/modules/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ const transformChatsForRoom = async (chats) => {
const inOutUserIds = chat.content.split("|");
chat.inOutNames = await Promise.all(
inOutUserIds.map(async (userId) => {
const user = await userModel.findOne(
{ id: userId, withdraw: false }, // NOTE: SSO uid 쓰는 곳
"nickname"
);
const user = await userModel.findById(userId, "nickname");
return user?.nickname;
})
);
Expand Down Expand Up @@ -115,7 +112,7 @@ const getMessageBody = (type, nickname = "", content = "") => {
* @param {Object} chat - 채팅 메시지 내용입니다.
* @param {string} chat.roomId - 채팅 및 채팅 알림을 보낼 방의 ObjectId입니다.
* @param {string} chat.type - 채팅 메시지의 유형입니다. "text" | "s3img" | "in" | "out" | "payment" | "settlement" | "account" | "departure" | "arrival" 입니다.
* @param {string} chat.content - 채팅 메시지의 본문입니다. chat.type이 "s3img"인 경우에는 채팅의 objectId입니다. chat.type이 "in"이거나 "out"인 경우 입퇴장한 사용자의 id(!==ObjectId)입니다.
* @param {string} chat.content - 채팅 메시지의 본문입니다. chat.type이 "s3img"인 경우에는 채팅의 objectId입니다. chat.type이 "in"이거나 "out"인 경우 입퇴장한 사용자의 oid입니다.
* @param {string} chat.authorId - optional. 채팅을 보낸 사용자의 ObjectId입니다.
* @param {Date?} chat.time - optional. 채팅 메시지 전송 시각입니다.
* @return {Promise<Boolean>} 채팅 및 알림 전송에 성공하면 true, 중간에 오류가 발생하면 false를 반환합니다.
Expand Down
10 changes: 5 additions & 5 deletions src/services/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const createHandler = async (req, res) => {
await emitChatEvent(req.app.get("io"), {
roomId: room._id,
type: "in",
content: user.id,
content: user._id,
authorId: user._id,
});

Expand Down Expand Up @@ -299,7 +299,7 @@ const joinHandler = async (req, res) => {
await emitChatEvent(req.app.get("io"), {
roomId: room._id,
type: "in",
content: user.id,
content: user._id,
authorId: user._id,
});

Expand Down Expand Up @@ -386,7 +386,7 @@ const abortHandler = async (req, res) => {
await emitChatEvent(req.app.get("io"), {
roomId: room._id,
type: "out",
content: user.id,
content: user._id,
authorId: user._id,
});

Expand Down Expand Up @@ -584,7 +584,7 @@ const commitSettlementHandler = async (req, res) => {
await emitChatEvent(req.app.get("io"), {
roomId,
type: "settlement",
content: user.id,
content: user._id,
authorId: user._id,
});

Expand Down Expand Up @@ -657,7 +657,7 @@ const commitPaymentHandler = async (req, res) => {
await emitChatEvent(req.app.get("io"), {
roomId,
type: "payment",
content: user.id,
content: user._id,
authorId: user._id,
});

Expand Down

0 comments on commit e54cca3

Please sign in to comment.