All checks were successful
farmcontrol/farmcontrol-scheduler/pipeline/head This commit looks good
69 lines
1.9 KiB
JavaScript
69 lines
1.9 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import { generateId } from '../../utils.js';
|
|
|
|
const { Schema } = mongoose;
|
|
|
|
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: true
|
|
},
|
|
emailAccount: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'emailAccount',
|
|
required: true
|
|
},
|
|
recipientEmail: { type: String, required: true },
|
|
recipientType: {
|
|
type: String,
|
|
enum: ['client', 'vendor'],
|
|
required: false
|
|
},
|
|
recipient: {
|
|
type: Schema.Types.ObjectId,
|
|
refPath: 'recipientType',
|
|
required: false
|
|
},
|
|
fromEmail: { type: String, required: true, immutable: true },
|
|
messageId: { type: String, required: false },
|
|
subject: { type: String, required: false, default: '' },
|
|
content: { type: String, required: false, default: '' },
|
|
attachments: [{ type: Schema.Types.ObjectId, ref: 'file', required: false }],
|
|
state: {
|
|
type: { type: String, required: true, default: 'queued' },
|
|
progress: { type: Number, required: false, default: 0 },
|
|
message: { type: String, required: false }
|
|
},
|
|
read: { type: Boolean, required: true, default: false },
|
|
sentAt: { type: Date, required: false },
|
|
readAt: { type: Date, required: false }
|
|
},
|
|
{ timestamps: true, suppressReservedKeysWarning: true }
|
|
);
|
|
|
|
emailMessageSchema.index({
|
|
name: 'text',
|
|
recipientEmail: 'text',
|
|
fromEmail: 'text',
|
|
objectType: 'text'
|
|
});
|
|
emailMessageSchema.virtual('id').get(function () {
|
|
return this._id;
|
|
});
|
|
emailMessageSchema.set('toJSON', { virtuals: true });
|
|
|
|
export const emailMessageModel = mongoose.model(
|
|
'emailMessage',
|
|
emailMessageSchema
|
|
);
|