-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: migration script that converts userId to userOid in chats
- Loading branch information
Showing
3 changed files
with
48 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters