feat: UserEquity创建/删除/批量创建后自动更新User.platformCount;新增修复脚本

This commit is contained in:
Developer
2026-05-18 19:45:26 +08:00
parent 58b74257c7
commit e2dae5942d
2 changed files with 42 additions and 0 deletions
+11
View File
@@ -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: '权益已删除'