failed-email.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import type { Types } from 'mongoose';
  2. import { Schema } from 'mongoose';
  3. import { getOrCreateModel } from '../util/mongoose-utils';
  4. export interface IFailedEmail {
  5. _id: Types.ObjectId;
  6. emailConfig: {
  7. to: string;
  8. from?: string;
  9. subject?: string;
  10. text?: string;
  11. template?: string;
  12. vars?: Record<string, unknown>;
  13. };
  14. error: {
  15. message: string;
  16. code?: string;
  17. stack?: string;
  18. };
  19. transmissionMethod: 'smtp' | 'ses' | 'oauth2';
  20. attempts: number;
  21. lastAttemptAt: Date;
  22. createdAt: Date;
  23. updatedAt: Date;
  24. }
  25. const schema = new Schema<IFailedEmail>(
  26. {
  27. emailConfig: {
  28. type: Schema.Types.Mixed,
  29. required: true,
  30. },
  31. error: {
  32. message: { type: String, required: true },
  33. code: { type: String },
  34. stack: { type: String },
  35. },
  36. transmissionMethod: {
  37. type: String,
  38. enum: ['smtp', 'ses', 'oauth2'],
  39. required: true,
  40. },
  41. attempts: {
  42. type: Number,
  43. required: true,
  44. default: 3,
  45. },
  46. lastAttemptAt: {
  47. type: Date,
  48. required: true,
  49. },
  50. },
  51. {
  52. timestamps: true,
  53. },
  54. );
  55. // Index for querying failed emails by creation date
  56. schema.index({ createdAt: 1 });
  57. export const FailedEmail = getOrCreateModel<
  58. IFailedEmail,
  59. Record<string, never>
  60. >('FailedEmail', schema);