Add email recipient schema to email message structure
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
- Introduced a new `emailRecipientSchema` to encapsulate recipient details, including email, type, and associated objects. - Updated the `emailMessageSchema` to utilize the new `recipients` field, enhancing the management of multiple recipients. - Modified indexing to support text search on recipient emails and ensure unique pixel IDs for recipients. - Improved overall schema structure for better clarity and maintainability.
This commit is contained in:
parent
ea80bfb217
commit
bd6038241f
@ -3,6 +3,32 @@ import { generateId } from '../../utils.js';
|
|||||||
|
|
||||||
const { Schema } = mongoose;
|
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(
|
const emailMessageSchema = new Schema(
|
||||||
{
|
{
|
||||||
_reference: { type: String, default: () => generateId()() },
|
_reference: { type: String, default: () => generateId()() },
|
||||||
@ -20,36 +46,29 @@ const emailMessageSchema = new Schema(
|
|||||||
ref: 'emailAccount',
|
ref: 'emailAccount',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
recipientEmail: { type: String, required: true },
|
recipients: { type: [emailRecipientSchema], required: true, default: [] },
|
||||||
recipientType: { type: String, enum: ['client', 'vendor'], required: false },
|
|
||||||
recipient: {
|
|
||||||
type: Schema.Types.ObjectId,
|
|
||||||
refPath: 'recipientType',
|
|
||||||
required: false,
|
|
||||||
},
|
|
||||||
fromEmail: { type: String, required: true, immutable: true },
|
fromEmail: { type: String, required: true, immutable: true },
|
||||||
messageId: { type: String, required: false },
|
|
||||||
subject: { type: String, required: false, default: '' },
|
subject: { type: String, required: false, default: '' },
|
||||||
content: { type: String, required: false, default: '' },
|
content: { type: String, required: false, default: '' },
|
||||||
attachments: [{ type: Schema.Types.ObjectId, ref: 'file', required: false }],
|
|
||||||
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 },
|
|
||||||
sentAt: { type: Date, required: false },
|
sentAt: { type: Date, required: false },
|
||||||
readAt: { type: Date, required: false },
|
firstReadAt: { type: Date, required: false },
|
||||||
|
lastReadAt: { type: Date, required: false },
|
||||||
},
|
},
|
||||||
{ timestamps: true, suppressReservedKeysWarning: true }
|
{ timestamps: true, suppressReservedKeysWarning: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
emailMessageSchema.index({
|
emailMessageSchema.index({
|
||||||
name: 'text',
|
name: 'text',
|
||||||
recipientEmail: 'text',
|
|
||||||
fromEmail: 'text',
|
fromEmail: 'text',
|
||||||
objectType: 'text',
|
objectType: 'text',
|
||||||
|
'recipients.recipientEmail': 'text',
|
||||||
});
|
});
|
||||||
|
emailMessageSchema.index({ 'recipients.pixelId': 1 }, { unique: true, sparse: true });
|
||||||
emailMessageSchema.virtual('id').get(function () {
|
emailMessageSchema.virtual('id').get(function () {
|
||||||
return this._id;
|
return this._id;
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user