feat: sync backend with frontend - add all UserEquity fields (brandIcon, brandIconImage, brandColor, platformType, hasUsedBenefit, benefit sub-fields, etc.) and new routes
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { auth } = require('../middleware/auth');
|
||||
const PlatformPreset = require('../models/PlatformPreset');
|
||||
const UserEquity = require('../models/UserEquity');
|
||||
const User = require('../models/User');
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
const presets = await PlatformPreset.find({ status: 'active' })
|
||||
.sort({ isHot: -1, sortOrder: 1 })
|
||||
.lean();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: presets
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/hot', async (req, res, next) => {
|
||||
try {
|
||||
const presets = await PlatformPreset.find({ status: 'active', isHot: true })
|
||||
.sort({ sortOrder: 1 })
|
||||
.lean();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: presets
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/import/:id', auth, async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { expireDate, price } = req.body;
|
||||
|
||||
const preset = await PlatformPreset.findById(id);
|
||||
|
||||
if (!preset) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: '预设平台不存在'
|
||||
});
|
||||
}
|
||||
|
||||
const user = await User.findById(req.user._id);
|
||||
|
||||
if (!user.isVip) {
|
||||
const userEquityCount = await UserEquity.countDocuments({ owner: req.user._id });
|
||||
if (userEquityCount >= user.platformLimit) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
error: '非会员最多只能添加15个平台,请升级会员'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!expireDate) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '缺少到期时间'
|
||||
});
|
||||
}
|
||||
|
||||
const equityData = {
|
||||
platform: preset.platform,
|
||||
type: preset.type,
|
||||
expireDate,
|
||||
price: parseFloat(price) || preset.price || 0,
|
||||
benefits: preset.benefits.map(b => ({
|
||||
name: b.name,
|
||||
type: b.type,
|
||||
typeLabel: b.typeLabel,
|
||||
expireDate,
|
||||
used: false,
|
||||
usedTime: null
|
||||
})),
|
||||
owner: req.user._id,
|
||||
status: 'active',
|
||||
note: preset.description || ''
|
||||
};
|
||||
|
||||
const equity = await UserEquity.create(equityData);
|
||||
|
||||
user.platformCount = await UserEquity.countDocuments({ owner: req.user._id });
|
||||
await user.save();
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: equity
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user