@resolveio/server-lib 22.2.54 → 22.2.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,9 @@ import { WebSocketManager } from './websocket.manager';
6
6
  interface SendEmailOptions {
7
7
  correlationId?: string;
8
8
  meta?: Record<string, any>;
9
+ waitForCompletion?: boolean;
10
+ waitForCompletionTimeoutMs?: number;
11
+ waitForCompletionPollMs?: number;
9
12
  }
10
13
  export declare class AWS {
11
14
  private _s3;
@@ -69,6 +72,7 @@ export declare class MethodManager {
69
72
  getCircularReplacer(): (key: any, value: any) => any;
70
73
  private finalizeEmailHistoryStorage;
71
74
  private tryMergeEmailOccurrence;
75
+ private waitForEmailCompletion;
72
76
  sendEmail(sendTo: string, subject: string, text?: string | null, html?: string | null, attachments?: any[] | null, send_from?: string | null, reply_to?: string | null, force_ses?: boolean, local_override?: boolean, options?: SendEmailOptions): Promise<true | EmailHistoryModel>;
73
77
  getAWS(): AWS;
74
78
  readFile(fileName: any): string;
@@ -473,6 +473,7 @@ var MethodManager = /** @class */ (function () {
473
473
  };
474
474
  MethodManager.prototype.initialize = function (websocketManager, monitorManagerFunction, isWorkersEnabled, isWorkerInstance) {
475
475
  var _this = this;
476
+ var _a, _b;
476
477
  this._websocketManager = websocketManager;
477
478
  this._monitorManagerFunction = monitorManagerFunction;
478
479
  this._isWorkersEnabled = isWorkersEnabled;
@@ -550,7 +551,12 @@ var MethodManager = /** @class */ (function () {
550
551
  });
551
552
  }); });
552
553
  this._aws = new AWS();
553
- var sesRegion = process.env.AWS_SES_REGION || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';
554
+ var sesRegion = process.env.AWS_SES_REGION
555
+ || ((_a = this.serverConfig) === null || _a === void 0 ? void 0 : _a.AWS_SES_REGION)
556
+ || ((_b = this.serverConfig) === null || _b === void 0 ? void 0 : _b.AWSSES_REGION)
557
+ || process.env.AWS_REGION
558
+ || process.env.AWS_DEFAULT_REGION
559
+ || 'us-east-1';
554
560
  var sesCredentials = process.env.AWS_ACCESS_KEY && process.env.AWS_SECRET_ACCESS_KEY ? {
555
561
  accessKeyId: process.env.AWS_ACCESS_KEY,
556
562
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
@@ -1700,6 +1706,62 @@ var MethodManager = /** @class */ (function () {
1700
1706
  });
1701
1707
  });
1702
1708
  };
1709
+ MethodManager.prototype.waitForEmailCompletion = function (emailHistory, options) {
1710
+ return __awaiter(this, void 0, void 0, function () {
1711
+ var timeoutMs, pollMs, deadline, latest;
1712
+ return __generator(this, function (_a) {
1713
+ switch (_a.label) {
1714
+ case 0:
1715
+ timeoutMs = Math.max(1000, Number(options === null || options === void 0 ? void 0 : options.waitForCompletionTimeoutMs) || 30000);
1716
+ pollMs = Math.max(100, Number(options === null || options === void 0 ? void 0 : options.waitForCompletionPollMs) || 250);
1717
+ deadline = Date.now() + timeoutMs;
1718
+ _a.label = 1;
1719
+ case 1:
1720
+ if (!(Date.now() <= deadline)) return [3 /*break*/, 4];
1721
+ return [4 /*yield*/, email_history_collection_1.EmailHistories.findOne({ _id: emailHistory._id }, {
1722
+ projection: {
1723
+ status: 1,
1724
+ error: 1,
1725
+ date: 1,
1726
+ id_user: 1,
1727
+ user: 1,
1728
+ email: 1,
1729
+ subject: 1,
1730
+ text: 1,
1731
+ html: 1,
1732
+ attachments: 1,
1733
+ send_from: 1,
1734
+ reply_to: 1,
1735
+ force_ses: 1,
1736
+ correlationId: 1,
1737
+ meta: 1,
1738
+ occurrences: 1,
1739
+ processingAt: 1,
1740
+ completedAt: 1
1741
+ }
1742
+ })];
1743
+ case 2:
1744
+ latest = _a.sent();
1745
+ if (!latest) {
1746
+ return [2 /*return*/, __assign(__assign({}, emailHistory), { status: 'completed', error: '', completedAt: new Date() })];
1747
+ }
1748
+ if (latest.status === 'completed') {
1749
+ return [2 /*return*/, latest];
1750
+ }
1751
+ if (latest.status === 'failed') {
1752
+ throw new Error(latest.error || "Email failed to send: ".concat(latest.subject));
1753
+ }
1754
+ // eslint-disable-next-line no-restricted-syntax
1755
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, pollMs); })];
1756
+ case 3:
1757
+ // eslint-disable-next-line no-restricted-syntax
1758
+ _a.sent();
1759
+ return [3 /*break*/, 1];
1760
+ case 4: throw new Error("Email send timed out waiting for completion: ".concat(emailHistory.subject));
1761
+ }
1762
+ });
1763
+ });
1764
+ };
1703
1765
  MethodManager.prototype.sendEmail = function (sendTo_1, subject_1, text_1, html_1, attachments_1, send_from_1, reply_to_1) {
1704
1766
  return __awaiter(this, arguments, void 0, function (sendTo, subject, text, html, attachments, send_from, reply_to, force_ses, local_override, options) {
1705
1767
  var normalizedSubject, correlationId, finalSubject, groupByCorrelationId, normalizedText, normalizedHtml, occurrence, allowGrouping, normalizedAttachments, merged, emailHistory, _a, aggregatedText, aggregatedHtml, history_1, err_7;
@@ -1725,9 +1787,9 @@ var MethodManager = /** @class */ (function () {
1725
1787
  ? createEmailOccurrence(normalizedSubject, normalizedText, normalizedHtml, options === null || options === void 0 ? void 0 : options.meta)
1726
1788
  : null;
1727
1789
  allowGrouping = groupByCorrelationId;
1728
- if (!sendTo) return [3 /*break*/, 9];
1790
+ if (!sendTo) return [3 /*break*/, 11];
1729
1791
  if (!(resolveio_server_app_1.ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200' ||
1730
- local_override)) return [3 /*break*/, 7];
1792
+ local_override)) return [3 /*break*/, 9];
1731
1793
  normalizedAttachments = [];
1732
1794
  if (Array.isArray(attachments)) {
1733
1795
  normalizedAttachments = attachments.slice();
@@ -1792,23 +1854,26 @@ var MethodManager = /** @class */ (function () {
1792
1854
  }
1793
1855
  _b.label = 3;
1794
1856
  case 3:
1795
- _b.trys.push([3, 5, , 6]);
1857
+ _b.trys.push([3, 7, , 8]);
1796
1858
  return [4 /*yield*/, email_history_collection_1.EmailHistories.insertOne(emailHistory)];
1797
1859
  case 4:
1798
1860
  history_1 = _b.sent();
1799
- return [2 /*return*/, history_1];
1800
- case 5:
1861
+ if (!(options === null || options === void 0 ? void 0 : options.waitForCompletion)) return [3 /*break*/, 6];
1862
+ return [4 /*yield*/, this.waitForEmailCompletion(history_1, options)];
1863
+ case 5: return [2 /*return*/, _b.sent()];
1864
+ case 6: return [2 /*return*/, history_1];
1865
+ case 7:
1801
1866
  err_7 = _b.sent();
1802
1867
  console.error('Failed to queue email:', err_7);
1803
1868
  err_7.message = "Failed to queue email: ".concat(err_7.message);
1804
1869
  throw err_7;
1805
- case 6: return [3 /*break*/, 8];
1806
- case 7:
1870
+ case 8: return [3 /*break*/, 10];
1871
+ case 9:
1807
1872
  console.log('Send email', sendTo, finalSubject, normalizedText, normalizedHtml, attachments, send_from, force_ses, correlationId);
1808
1873
  return [2 /*return*/, true];
1809
- case 8: return [3 /*break*/, 10];
1810
- case 9: return [2 /*return*/, true];
1811
- case 10: return [2 /*return*/];
1874
+ case 10: return [3 /*break*/, 12];
1875
+ case 11: return [2 /*return*/, true];
1876
+ case 12: return [2 /*return*/];
1812
1877
  }
1813
1878
  });
1814
1879
  });