From e2dae5942d360acab998ef297e920bf76b50ad7a Mon Sep 17 00:00:00 2001 From: Developer Date: Mon, 18 May 2026 19:45:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20UserEquity=E5=88=9B=E5=BB=BA/=E5=88=A0?= =?UTF-8?q?=E9=99=A4/=E6=89=B9=E9=87=8F=E5=88=9B=E5=BB=BA=E5=90=8E?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0User.platformCount=EF=BC=9B?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BF=AE=E5=A4=8D=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/fix-platform-count.js | 31 +++++++++++++++++++++++++++++++ src/routes/userEquity.js | 11 +++++++++++ 2 files changed, 42 insertions(+) create mode 100644 scripts/fix-platform-count.js diff --git a/scripts/fix-platform-count.js b/scripts/fix-platform-count.js new file mode 100644 index 0000000..db5bee0 --- /dev/null +++ b/scripts/fix-platform-count.js @@ -0,0 +1,31 @@ +const mongoose = require('mongoose'); +const User = require('../src/models/User'); +const UserEquity = require('../src/models/UserEquity'); + +async function fixPlatformCount() { + try { + await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/member-assistant'); + console.log('MongoDB connected'); + + const users = await User.find({}).select('_id'); + console.log(`Found ${users.length} users`); + + let updated = 0; + for (const user of users) { + const count = await UserEquity.countDocuments({ owner: user._id }); + await User.findByIdAndUpdate(user._id, { platformCount: count }); + updated++; + if (updated % 100 === 0) { + console.log(`Progress: ${updated}/${users.length}`); + } + } + + console.log(`Done! Updated ${updated} users.`); + process.exit(0); + } catch (error) { + console.error('Error:', error); + process.exit(1); + } +} + +fixPlatformCount(); diff --git a/src/routes/userEquity.js b/src/routes/userEquity.js index ea400a8..ca55803 100644 --- a/src/routes/userEquity.js +++ b/src/routes/userEquity.js @@ -2,6 +2,12 @@ const express = require('express'); const router = express.Router(); const { auth } = require('../middleware/auth'); const UserEquity = require('../models/UserEquity'); +const User = require('../models/User'); + +async function updateUserPlatformCount(userId) { + const count = await UserEquity.countDocuments({ owner: userId }); + await User.findByIdAndUpdate(userId, { platformCount: count }); +} const requireVip = (req, res, next) => { if (!req.user.isVip) { @@ -174,6 +180,7 @@ router.post('/', auth, requireVip, async (req, res, next) => { if (updateTime) equityData.updateTime = updateTime; const equity = await UserEquity.create(equityData); + await updateUserPlatformCount(req.user._id); res.status(201).json({ success: true, @@ -252,6 +259,8 @@ router.post('/batch', auth, requireVip, async (req, res, next) => { } } + await updateUserPlatformCount(req.user._id); + res.status(201).json({ success: true, data: { @@ -424,6 +433,8 @@ router.delete('/:id', auth, requireVip, async (req, res, next) => { }); } + await updateUserPlatformCount(req.user._id); + res.json({ success: true, message: '权益已删除'