@ufdevsllc/authme2.0
Version:
SDK for license management and remote monitoring with automatic system tracking, license validation, and remote control capabilities
192 lines (182 loc) • 5.82 kB
JavaScript
import mongoose from 'mongoose';
const remoteCommandSchema = new mongoose.Schema({
licenseKey: {
type: String,
required: [true, 'License key is required'],
trim: true,
ref: 'License'
},
command: {
type: String,
required: [true, 'Command is required'],
trim: true,
enum: {
values: ['block', 'activate', 'deactivate', 'update', 'restart', 'status', 'config'],
message: 'Command must be one of: block, activate, deactivate, update, restart, status, config'
}
},
parameters: {
type: mongoose.Schema.Types.Mixed,
default: {},
validate: {
validator: function (value) {
// Ensure parameters is an object and not too large
return typeof value === 'object' && JSON.stringify(value).length <= 10000;
},
message: 'Parameters must be an object and cannot exceed 10KB'
}
},
status: {
type: String,
required: true,
default: 'pending',
enum: {
values: ['pending', 'executing', 'executed', 'failed', 'cancelled'],
message: 'Status must be one of: pending, executing, executed, failed, cancelled'
}
},
priority: {
type: Number,
default: 1,
min: [1, 'Priority must be at least 1'],
max: [10, 'Priority cannot exceed 10']
},
createdAt: {
type: Date,
default: Date.now,
required: true
},
executedAt: {
type: Date,
validate: {
validator: function (value) {
return !value || value >= this.createdAt;
},
message: 'Execution date cannot be before creation date'
}
},
expiresAt: {
type: Date,
default: function () {
// Commands expire after 24 hours by default
return new Date(Date.now() + 24 * 60 * 60 * 1000);
},
validate: {
validator: function (value) {
return value > this.createdAt;
},
message: 'Expiration date must be after creation date'
}
},
result: {
success: {
type: Boolean,
default: null
},
message: {
type: String,
trim: true,
maxlength: [1000, 'Result message cannot exceed 1000 characters']
},
data: {
type: mongoose.Schema.Types.Mixed,
validate: {
validator: function (value) {
// Ensure result data is not too large
return !value || JSON.stringify(value).length <= 5000;
},
message: 'Result data cannot exceed 5KB'
}
},
executionTime: {
type: Number,
min: [0, 'Execution time cannot be negative']
}
},
retryCount: {
type: Number,
default: 0,
min: [0, 'Retry count cannot be negative'],
max: [5, 'Retry count cannot exceed 5']
},
maxRetries: {
type: Number,
default: 3,
min: [0, 'Max retries cannot be negative'],
max: [5, 'Max retries cannot exceed 5']
}
}, {
timestamps: true,
collection: 'remoteCommands'
});
// Indexes for efficient queries
remoteCommandSchema.index({ licenseKey: 1 });
remoteCommandSchema.index({ status: 1 });
remoteCommandSchema.index({ command: 1 });
remoteCommandSchema.index({ createdAt: 1 });
remoteCommandSchema.index({ priority: -1, createdAt: 1 });
remoteCommandSchema.index({ expiresAt: 1 });
// Virtual to check if command is expired
remoteCommandSchema.virtual('isExpired').get(function () {
return new Date() > this.expiresAt;
});
// Virtual to check if command can be retried
remoteCommandSchema.virtual('canRetry').get(function () {
return this.status === 'failed' && this.retryCount < this.maxRetries && !this.isExpired;
});
// Method to mark command as executing
remoteCommandSchema.methods.markAsExecuting = function () {
this.status = 'executing';
return this.save();
};
// Method to mark command as executed with result
remoteCommandSchema.methods.markAsExecuted = function (result = {}) {
this.status = 'executed';
this.executedAt = new Date();
this.result = {
success: true,
message: result.message || 'Command executed successfully',
data: result.data || null,
executionTime: result.executionTime || 0
};
return this.save();
};
// Method to mark command as failed with error
remoteCommandSchema.methods.markAsFailed = function (error = {}) {
this.status = 'failed';
this.executedAt = new Date();
this.result = {
success: false,
message: error.message || 'Command execution failed',
data: error.data || null,
executionTime: error.executionTime || 0
};
return this.save();
};
// Method to retry command
remoteCommandSchema.methods.retry = function () {
if (this.canRetry) {
this.status = 'pending';
this.retryCount += 1;
this.executedAt = undefined;
this.result = {};
return this.save();
}
throw new Error('Command cannot be retried');
};
// Method to cancel command
remoteCommandSchema.methods.cancel = function () {
if (this.status === 'pending') {
this.status = 'cancelled';
this.executedAt = new Date();
this.result = {
success: false,
message: 'Command was cancelled',
data: null,
executionTime: 0
};
return this.save();
}
throw new Error('Only pending commands can be cancelled');
};
export default mongoose.model('RemoteCommand', remoteCommandSchema);