@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.
27 lines (25 loc) • 1.21 kB
JavaScript
import mongoose from "mongoose";
const { Schema } = mongoose;
import { isFourDigitString, isMMYY, trimIfString } from '../validation/index.js';
/**
* Card model for storing user card information.
*
* @typedef {Object} Card
* @property {ObjectId} user - Reference to the User who owns the card.
* @property {String} encryptedCardDetails - Encrypted card details.
* @property {String} cardType - Type of card (e.g., "visa", "mastercard").
* @property {String} last4Digits - Last 4 digits of the card number.
* @property {String} expiry - Card expiry date (MM/YY).
*/
const CardSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' },
encryptedCardDetails: { type: String },
cardType: { type: String, set: trimIfString },
last4Digits: { type: String, validate: { validator: (v) => !v || isFourDigitString(v), message: 'last4Digits must be 4 digits' } },
expiry: { type: String, validate: { validator: (v) => !v || isMMYY(v), message: 'expiry must be in MM/YY format' } },
});
// Indexes for common lookups
CardSchema.index({ user: 1 });
CardSchema.index({ cardType: 1 });
const Card = mongoose.model('Card', CardSchema);
export default Card;