payload-plugin-newsletter 0.16.0 → 0.16.2

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.
@@ -1149,6 +1149,29 @@ function wrapInEmailTemplate(content, preheader) {
1149
1149
  </html>`;
1150
1150
  }
1151
1151
 
1152
+ // src/utils/getBroadcastConfig.ts
1153
+ async function getBroadcastConfig(req, pluginConfig) {
1154
+ try {
1155
+ const settings = await req.payload.findGlobal({
1156
+ slug: pluginConfig.settingsSlug || "newsletter-settings",
1157
+ req
1158
+ });
1159
+ if (settings?.provider === "broadcast" && settings?.broadcastSettings) {
1160
+ return {
1161
+ apiUrl: settings.broadcastSettings.apiUrl || pluginConfig.providers?.broadcast?.apiUrl || "",
1162
+ token: settings.broadcastSettings.token || pluginConfig.providers?.broadcast?.token || "",
1163
+ fromAddress: settings.fromAddress || pluginConfig.providers?.broadcast?.fromAddress || "",
1164
+ fromName: settings.fromName || pluginConfig.providers?.broadcast?.fromName || "",
1165
+ replyTo: settings.replyTo || pluginConfig.providers?.broadcast?.replyTo
1166
+ };
1167
+ }
1168
+ return pluginConfig.providers?.broadcast || null;
1169
+ } catch (error) {
1170
+ req.payload.logger.error("Failed to get broadcast config from settings:", error);
1171
+ return pluginConfig.providers?.broadcast || null;
1172
+ }
1173
+ }
1174
+
1152
1175
  // src/collections/Broadcasts.ts
1153
1176
  var createBroadcastsCollection = (pluginConfig) => {
1154
1177
  const hasProviders = !!(pluginConfig.providers?.broadcast || pluginConfig.providers?.resend);
@@ -1389,9 +1412,9 @@ var createBroadcastsCollection = (pluginConfig) => {
1389
1412
  async ({ doc, operation, req }) => {
1390
1413
  if (!hasProviders || operation !== "create") return doc;
1391
1414
  try {
1392
- const providerConfig = pluginConfig.providers?.broadcast;
1393
- if (!providerConfig) {
1394
- req.payload.logger.error("Broadcast provider not configured");
1415
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
1416
+ if (!providerConfig || !providerConfig.token) {
1417
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
1395
1418
  return doc;
1396
1419
  }
1397
1420
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -1426,6 +1449,51 @@ var createBroadcastsCollection = (pluginConfig) => {
1426
1449
  req.payload.logger.error("Failed to create broadcast in provider:", error);
1427
1450
  return doc;
1428
1451
  }
1452
+ },
1453
+ // Hook to send when published
1454
+ async ({ doc, operation, previousDoc, req }) => {
1455
+ if (operation !== "update") return doc;
1456
+ const wasUnpublished = !previousDoc?._status || previousDoc._status === "draft";
1457
+ const isNowPublished = doc._status === "published";
1458
+ if (wasUnpublished && isNowPublished && doc.providerId) {
1459
+ if (doc.status === "sent" || doc.status === "sending") {
1460
+ return doc;
1461
+ }
1462
+ try {
1463
+ const broadcastConfig = await getBroadcastConfig(req, pluginConfig);
1464
+ const resendConfig = pluginConfig.providers?.resend;
1465
+ if (!broadcastConfig && !resendConfig) {
1466
+ req.payload.logger.error("No provider configured for sending in Newsletter Settings or environment variables");
1467
+ return doc;
1468
+ }
1469
+ if (broadcastConfig && broadcastConfig.token) {
1470
+ const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
1471
+ const provider = new BroadcastApiProvider2(broadcastConfig);
1472
+ await provider.send(doc.providerId);
1473
+ }
1474
+ await req.payload.update({
1475
+ collection: "broadcasts",
1476
+ id: doc.id,
1477
+ data: {
1478
+ status: "sending" /* SENDING */,
1479
+ sentAt: (/* @__PURE__ */ new Date()).toISOString()
1480
+ },
1481
+ req
1482
+ });
1483
+ req.payload.logger.info(`Broadcast ${doc.id} sent successfully`);
1484
+ } catch (error) {
1485
+ req.payload.logger.error(`Failed to send broadcast ${doc.id}:`, error);
1486
+ await req.payload.update({
1487
+ collection: "broadcasts",
1488
+ id: doc.id,
1489
+ data: {
1490
+ status: "failed" /* FAILED */
1491
+ },
1492
+ req
1493
+ });
1494
+ }
1495
+ }
1496
+ return doc;
1429
1497
  }
1430
1498
  ],
1431
1499
  // Sync updates with provider
@@ -1433,9 +1501,9 @@ var createBroadcastsCollection = (pluginConfig) => {
1433
1501
  async ({ data, originalDoc, operation, req }) => {
1434
1502
  if (!hasProviders || !originalDoc?.providerId || operation !== "update") return data;
1435
1503
  try {
1436
- const providerConfig = pluginConfig.providers?.broadcast;
1437
- if (!providerConfig) {
1438
- req.payload.logger.error("Broadcast provider not configured");
1504
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
1505
+ if (!providerConfig || !providerConfig.token) {
1506
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
1439
1507
  return data;
1440
1508
  }
1441
1509
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -1479,9 +1547,9 @@ var createBroadcastsCollection = (pluginConfig) => {
1479
1547
  async ({ doc, req }) => {
1480
1548
  if (!hasProviders || !doc?.providerId) return doc;
1481
1549
  try {
1482
- const providerConfig = pluginConfig.providers?.broadcast;
1483
- if (!providerConfig) {
1484
- req.payload.logger.error("Broadcast provider not configured");
1550
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
1551
+ if (!providerConfig || !providerConfig.token) {
1552
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
1485
1553
  return doc;
1486
1554
  }
1487
1555
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -1494,48 +1562,6 @@ var createBroadcastsCollection = (pluginConfig) => {
1494
1562
  req.payload.logger.error("Failed to delete broadcast from provider:", error);
1495
1563
  }
1496
1564
  return doc;
1497
- },
1498
- // Hook to send when published
1499
- async ({ doc, req }) => {
1500
- if (doc._status === "published" && doc.providerId) {
1501
- if (doc.status === "sent" || doc.status === "sending") {
1502
- return doc;
1503
- }
1504
- try {
1505
- const broadcastConfig = pluginConfig.providers?.broadcast;
1506
- const resendConfig = pluginConfig.providers?.resend;
1507
- if (!broadcastConfig && !resendConfig) {
1508
- req.payload.logger.error("No provider configured for sending");
1509
- return doc;
1510
- }
1511
- if (broadcastConfig) {
1512
- const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
1513
- const provider = new BroadcastApiProvider2(broadcastConfig);
1514
- await provider.send(doc.providerId);
1515
- }
1516
- await req.payload.update({
1517
- collection: "broadcasts",
1518
- id: doc.id,
1519
- data: {
1520
- status: "sending" /* SENDING */,
1521
- sentAt: (/* @__PURE__ */ new Date()).toISOString()
1522
- },
1523
- req
1524
- });
1525
- req.payload.logger.info(`Broadcast ${doc.id} sent successfully`);
1526
- } catch (error) {
1527
- req.payload.logger.error(`Failed to send broadcast ${doc.id}:`, error);
1528
- await req.payload.update({
1529
- collection: "broadcasts",
1530
- id: doc.id,
1531
- data: {
1532
- status: "failed" /* FAILED */
1533
- },
1534
- req
1535
- });
1536
- }
1537
- }
1538
- return doc;
1539
1565
  }
1540
1566
  ]
1541
1567
  }