From e54cca304d16a873b4a1953b78a809a4575c6d8b Mon Sep 17 00:00:00 2001 From: static Date: Tue, 21 Jan 2025 18:56:43 +0900 Subject: [PATCH] Add: migration script that converts userId to userOid in chats --- scripts/chatContentUserIdUpdater.js | 41 +++++++++++++++++++++++++++++ src/modules/socket.js | 7 ++--- src/services/rooms.js | 10 +++---- 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 scripts/chatContentUserIdUpdater.js diff --git a/scripts/chatContentUserIdUpdater.js b/scripts/chatContentUserIdUpdater.js new file mode 100644 index 00000000..0063542a --- /dev/null +++ b/scripts/chatContentUserIdUpdater.js @@ -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!"); +}); diff --git a/src/modules/socket.js b/src/modules/socket.js index b509d230..36b1f1bc 100644 --- a/src/modules/socket.js +++ b/src/modules/socket.js @@ -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; }) ); @@ -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} 채팅 및 알림 전송에 성공하면 true, 중간에 오류가 발생하면 false를 반환합니다. diff --git a/src/services/rooms.js b/src/services/rooms.js index ed93bcf8..42ec6e1b 100644 --- a/src/services/rooms.js +++ b/src/services/rooms.js @@ -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, }); @@ -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, }); @@ -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, }); @@ -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, }); @@ -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, });