farmcontrol-api/src/schemas/management/auditlog.schema.js

68 lines
1.4 KiB
JavaScript

import mongoose from 'mongoose';
const { Schema } = mongoose;
const auditLogSchema = new Schema(
{
changes: {
old: { type: Object, required: false },
new: { type: Object, required: false },
},
operation: {
type: String,
required: true,
},
parent: {
type: Schema.Types.ObjectId,
refPath: 'parentType',
required: true,
},
parentType: {
type: String,
required: true,
enum: [
'printer',
'job',
'subJob',
'filamentStock',
'stockEvent',
'vendor',
'part',
'host',
'file',
'product',
'material',
'filament',
'gcodeFile',
'noteType',
'note',
'user',
'documentSize',
'documentTemplate',
'documentPrinter',
], // Add other models as needed
},
owner: {
type: Schema.Types.ObjectId,
refPath: 'ownerType',
required: true,
},
ownerType: {
type: String,
required: true,
enum: ['user', 'printer', 'host'],
},
},
{ timestamps: true }
);
// Add virtual id getter
auditLogSchema.virtual('id').get(function () {
return this._id.toHexString();
});
// Configure JSON serialization to include virtuals
auditLogSchema.set('toJSON', { virtuals: true });
// Create and export the model
export const auditLogModel = mongoose.model('auditLog', auditLogSchema);