Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
This commit introduces several utility functions in the `utils.js` file, including `getSchemaTypeRef`, `forEachSchemaPath`, and `collectIdsAtPath`, to streamline schema reference handling. Additionally, it refactors the `modelHasRef` and `getFieldsByRef` functions to utilize these new utilities, improving code readability and maintainability. The email message schema is updated to support multiple recipients with enhanced attachment handling, ensuring better management of email content and references. Tests are added to validate the new functionality, reinforcing the integrity of email message processing.
78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import { generateId } from '../../utils.js';
|
|
|
|
const { Schema } = mongoose;
|
|
|
|
const emailRecipientSchema = new Schema(
|
|
{
|
|
recipientEmail: { type: String, required: true },
|
|
recipientType: { type: String, enum: ['client', 'vendor'], required: false },
|
|
recipient: {
|
|
type: Schema.Types.ObjectId,
|
|
refPath: 'recipients.recipientType',
|
|
required: false,
|
|
},
|
|
objectType: { type: String, required: false },
|
|
object: {
|
|
type: Schema.Types.ObjectId,
|
|
refPath: 'recipients.objectType',
|
|
required: false,
|
|
},
|
|
pixelId: { type: String, required: true },
|
|
messageId: { type: String, required: false },
|
|
attachments: [{ type: Schema.Types.ObjectId, ref: 'file', required: false }],
|
|
read: { type: Boolean, required: true, default: false },
|
|
sent: { type: Boolean, required: true, default: false },
|
|
sentAt: { type: Date, required: false },
|
|
readAt: { type: Date, required: false },
|
|
},
|
|
{ _id: true }
|
|
);
|
|
|
|
const emailMessageSchema = new Schema(
|
|
{
|
|
_reference: { type: String, default: () => generateId()() },
|
|
name: { type: String, required: true },
|
|
emailTemplate: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'emailTemplate',
|
|
required: true,
|
|
},
|
|
objectType: { type: String, required: true },
|
|
object: { type: Schema.Types.ObjectId, refPath: 'objectType', required: false },
|
|
objects: [{ type: Schema.Types.ObjectId, refPath: 'objectType', required: false }],
|
|
emailAccount: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'emailAccount',
|
|
required: true,
|
|
},
|
|
recipients: { type: [emailRecipientSchema], required: true, default: [] },
|
|
fromEmail: { type: String, required: true, immutable: true },
|
|
subject: { type: String, required: false, default: '' },
|
|
content: { type: String, required: false, default: '' },
|
|
state: {
|
|
type: { type: String, required: true, default: 'queued' },
|
|
progress: { type: Number, required: false, default: 0 },
|
|
message: { type: String, required: false },
|
|
},
|
|
sentAt: { type: Date, required: false },
|
|
firstReadAt: { type: Date, required: false },
|
|
lastReadAt: { type: Date, required: false },
|
|
},
|
|
{ timestamps: true, suppressReservedKeysWarning: true }
|
|
);
|
|
|
|
emailMessageSchema.index({
|
|
name: 'text',
|
|
fromEmail: 'text',
|
|
objectType: 'text',
|
|
'recipients.recipientEmail': 'text',
|
|
});
|
|
emailMessageSchema.index({ 'recipients.pixelId': 1 }, { unique: true, sparse: true });
|
|
emailMessageSchema.virtual('id').get(function () {
|
|
return this._id;
|
|
});
|
|
emailMessageSchema.set('toJSON', { virtuals: true });
|
|
|
|
export const emailMessageModel = mongoose.model('emailMessage', emailMessageSchema);
|