0

我正在创建一个网站,普通用户可以发布他们访问过的可爱地点。为了通过网站获利,我想为用户提供高级访问权限,以便能够在普通用户发布的发布位置附近发布他们的住宿。这些计划是 1 个月、3 个月、6 个月和年度访问,因此在他们购买了这段时间之后,他们对发布住宿的访问将过期,这些住宿帖子将被删除或暂停。现在我想要的是您帮助我了解如何将这种访问权限授予用户。使用 MongoDB 工具或会话或...?我是新手,所以我找不到解决方案,所以我在这里问。这是我在 Mongoose 和 MongoDB 中的用户模式:

const mongoose = require('mongoose')
const Schema = mongoose.Schema
const Campground = require('./campground')
const Residence = require('./residence')
const Review = require('./review')
const passportLocalMongoose = require('passport-local-mongoose')
const { cloudinary } = require('../cloudinary')





const UserSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    },
    campgrounds: [
        {
            type: Schema.Types.ObjectId,
            ref: Campground
        }
    ],
    assets: [{
        type: Schema.Types.ObjectId,
        ref: 'Residence'
    }],
    reviews: [{
        type: Schema.Types.ObjectId,
        ref: 'Reviews'
    }]
})



UserSchema.plugin(passportLocalMongoose)


UserSchema.post('findOneAndDelete', async function (doc) {
    if (doc) {
        await Residence.deleteMany({
            _id: {
                $in: doc.assets
            }
        })
    }
})

UserSchema.post('findOneAndDelete', async function (doc) {
    if (doc) {
        await Campground.deleteMany({
            _id: {
                $in: doc.campgrounds
            }
        })
    }
})

UserSchema.post('findOneAndDelete', async function (doc) {
    if (doc) {
        await Review.deleteMany({
            _id: {
                $in: doc.reviews
            }
        })
    }
})



module.exports = mongoose.model('User', UserSchema)

谢谢你的时间!!!

4

0 回答 0