Refactor audit log and email message schemas for consistency
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good

- Standardized formatting in the `auditLog` and `emailMessage` schemas by ensuring consistent use of commas and spacing.
- Simplified the index definitions for both schemas to enhance readability.
- Improved overall schema structure while maintaining required fields and types for better clarity and maintainability.
This commit is contained in:
Tom Butcher 2026-09-12 23:29:55 +01:00
parent 44fb686cbb
commit 110ea9355a
2 changed files with 18 additions and 33 deletions

View File

@ -7,45 +7,41 @@ const auditLogSchema = new Schema(
_reference: { type: String, default: () => generateId()() },
changes: {
old: { type: Object, required: false },
new: { type: Object, required: false }
new: { type: Object, required: false },
},
operation: {
type: String,
required: true
required: true,
},
parent: {
type: Schema.Types.ObjectId,
refPath: 'parentType',
required: true
required: true,
},
parentType: {
type: String,
required: true
required: true,
},
owner: {
type: Schema.Types.ObjectId,
refPath: 'ownerType',
required: false
required: false,
},
ownerType: {
type: String,
required: false,
enum: ['user', 'printer', 'host', 'marketplace']
enum: ['user', 'printer', 'host', 'marketplace'],
},
system: {
type: Boolean,
required: true,
default: false
}
default: false,
},
},
{ timestamps: true }
);
auditLogSchema.index({
operation: 'text',
parentType: 'text',
ownerType: 'text'
});
auditLogSchema.index({ operation: 'text', parentType: 'text', ownerType: 'text' });
// Add virtual id getter
auditLogSchema.virtual('id').get(function () {

View File

@ -10,29 +10,21 @@ const emailMessageSchema = new Schema(
emailTemplate: {
type: Schema.Types.ObjectId,
ref: 'emailTemplate',
required: true
required: true,
},
objectType: { type: String, required: true },
object: {
type: Schema.Types.ObjectId,
refPath: 'objectType',
required: true
},
object: { type: Schema.Types.ObjectId, refPath: 'objectType', required: true },
emailAccount: {
type: Schema.Types.ObjectId,
ref: 'emailAccount',
required: true
required: true,
},
recipientEmail: { type: String, required: true },
recipientType: {
type: String,
enum: ['client', 'vendor'],
required: false
},
recipientType: { type: String, enum: ['client', 'vendor'], required: false },
recipient: {
type: Schema.Types.ObjectId,
refPath: 'recipientType',
required: false
required: false,
},
fromEmail: { type: String, required: true, immutable: true },
messageId: { type: String, required: false },
@ -42,11 +34,11 @@ const emailMessageSchema = new Schema(
state: {
type: { type: String, required: true, default: 'queued' },
progress: { type: Number, required: false, default: 0 },
message: { type: String, required: false }
message: { type: String, required: false },
},
read: { type: Boolean, required: true, default: false },
sentAt: { type: Date, required: false },
readAt: { type: Date, required: false }
readAt: { type: Date, required: false },
},
{ timestamps: true, suppressReservedKeysWarning: true }
);
@ -55,14 +47,11 @@ emailMessageSchema.index({
name: 'text',
recipientEmail: 'text',
fromEmail: 'text',
objectType: 'text'
objectType: 'text',
});
emailMessageSchema.virtual('id').get(function () {
return this._id;
});
emailMessageSchema.set('toJSON', { virtuals: true });
export const emailMessageModel = mongoose.model(
'emailMessage',
emailMessageSchema
);
export const emailMessageModel = mongoose.model('emailMessage', emailMessageSchema);