Spaces:
Running
Running
| const mongoose = require('mongoose'); | |
| const stockSubscriptionSchema = new mongoose.Schema( | |
| { | |
| user: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User', | |
| required: [true, 'Subscription must belong to a user'], | |
| index: true, | |
| }, | |
| product: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'Product', | |
| required: [true, 'Subscription must reference a product'], | |
| index: true, | |
| }, | |
| notifiedAt: { | |
| type: Date, | |
| default: null, | |
| // Set when the user has been notified; null means still waiting | |
| }, | |
| }, | |
| { | |
| timestamps: true, | |
| }, | |
| ); | |
| // Prevent duplicate subscriptions per user+product | |
| stockSubscriptionSchema.index({ user: 1, product: 1 }, { unique: true }); | |
| const StockSubscription = mongoose.model( | |
| 'StockSubscription', | |
| stockSubscriptionSchema, | |
| ); | |
| module.exports = StockSubscription; | |