@everytravel/shared
Version:
A comprehensive shared package for Everytravel containing Mongoose models and CRUD operations for hotel booking, user management, and transaction handling. Updated with improved model syntax and enhanced error handling.
47 lines (38 loc) • 1.74 kB
JavaScript
import mongoose from "mongoose";
const { Schema } = mongoose;
import { isCurrencyCode } from '../validation/index.js';
/**
* Wallet model for storing user wallet information, balances, and linked cards.
*
* @typedef {Object} Wallet
* @property {ObjectId} user - Reference to the User who owns the wallet.
* @property {Object} balance - Wallet balance details.
* @property {Number} balance.amount - Amount in the wallet.
* @property {String} balance.currency - Currency code (e.g., USD).
* @property {Number} rewardsPoints - Total rewards points accumulated.
* @property {Number} cashbackTotal - Total cashback earned.
* @property {ObjectId[]} linkedCards - References to linked Card documents.
* @property {ObjectId[]} transactions - References to Transaction documents.
* @property {Date} createdAt - Wallet creation timestamp.
* @property {Date} updatedAt - Last update timestamp.
*/
const WalletSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' },
balance: {
amount: { type: Number, min: 0 },
currency: { type: String, validate: { validator: (v) => !v || isCurrencyCode(v), message: 'currency must be a 3-letter code' } },
},
rewardsPoints: { type: Number, min: 0 },
cashbackTotal: { type: Number, min: 0 },
linkedCards: [{ type: Schema.Types.ObjectId, ref: 'Card' }],
transactions: [{ type: Schema.Types.ObjectId, ref: 'Transaction' }],
createdAt: {
type: Date,
default: Date.now
},
updatedAt: Date,
});
// Indexes to optimize lookups and reporting
WalletSchema.index({ user: 1 });
WalletSchema.index({ updatedAt: -1 });
export default mongoose.model('Wallet', WalletSchema);