UNPKG

@ufdevsllc/authme2.0

Version:

SDK for license management and remote monitoring with automatic system tracking, license validation, and remote control capabilities

92 lines (86 loc) 2.91 kB
import mongoose from 'mongoose'; const licenseSchema = new mongoose.Schema({ licenseKey: { type: String, required: [true, 'License key is required'], unique: true, trim: true, minlength: [10, 'License key must be at least 10 characters long'], maxlength: [100, 'License key cannot exceed 100 characters'] }, distributionLimit: { type: Number, required: [true, 'Distribution limit is required'], min: [1, 'Distribution limit must be at least 1'], max: [10000, 'Distribution limit cannot exceed 10000'] }, currentDistributions: { type: Number, default: 0, min: [0, 'Current distributions cannot be negative'], validate: { validator: function (value) { return value <= this.distributionLimit; }, message: 'Current distributions cannot exceed distribution limit' } }, isActive: { type: Boolean, default: true, required: true }, createdAt: { type: Date, default: Date.now, required: true }, expiresAt: { type: Date, required: [true, 'Expiration date is required'], validate: { validator: function (value) { return value > this.createdAt; }, message: 'Expiration date must be after creation date' } }, vendorId: { type: String, required: [true, 'Vendor ID is required'], trim: true, minlength: [3, 'Vendor ID must be at least 3 characters long'], maxlength: [50, 'Vendor ID cannot exceed 50 characters'] }, productId: { type: String, required: [true, 'Product ID is required'], trim: true, minlength: [3, 'Product ID must be at least 3 characters long'], maxlength: [50, 'Product ID cannot exceed 50 characters'] } }, { timestamps: true, collection: 'licenses' }); // Index for efficient queries licenseSchema.index({ vendorId: 1, productId: 1 }); licenseSchema.index({ isActive: 1 }); licenseSchema.index({ expiresAt: 1 }); // Virtual to check if license is expired licenseSchema.virtual('isExpired').get(function () { return new Date() > this.expiresAt; }); // Method to check if license can accept new distributions licenseSchema.methods.canAcceptNewDistribution = function () { return this.isActive && !this.isExpired && this.currentDistributions < this.distributionLimit; }; // Method to increment distribution count licenseSchema.methods.incrementDistribution = function () { if (this.canAcceptNewDistribution()) { this.currentDistributions += 1; return this.save(); } throw new Error('Cannot increment distribution: limit reached or license inactive'); }; export default mongoose.model('License', licenseSchema);