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

View File

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