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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ ## [0.16.2] - 2025-07-27
2
+
3
+ ### Fixed
4
+ - **Configuration Consistency** - Fixed broadcast operations to read from Newsletter Settings collection
5
+ - Broadcast create/update/delete operations now check Newsletter Settings first before falling back to env vars
6
+ - This matches the behavior of email operations (magic links, welcome emails)
7
+ - Resolves issue where broadcasts failed when env vars were missing despite settings being configured
8
+ - Added `getBroadcastConfig` utility for consistent configuration retrieval
9
+
10
+ ### Added
11
+ - **Configuration Utilities** - New utilities for consistent provider configuration
12
+ - `getBroadcastConfig` - Gets Broadcast provider config from settings or env vars
13
+ - `getResendConfig` - Gets Resend provider config from settings or env vars
14
+ - Both utilities handle errors gracefully with fallback to env vars
15
+
16
+ ## [0.16.1] - 2025-07-27
17
+
18
+ ### Fixed
19
+ - **Critical Bug Fix** - Fixed afterChange hook placement for sending broadcasts
20
+ - The send hook was incorrectly placed in the `afterDelete` array instead of `afterChange`
21
+ - This prevented broadcasts from being sent when published
22
+ - Publishing broadcasts now correctly triggers the send functionality
23
+
1
24
  ## [0.16.0] - 2025-07-27
2
25
 
3
26
  ### Changed
@@ -1162,6 +1162,29 @@ function wrapInEmailTemplate(content, preheader) {
1162
1162
  </html>`;
1163
1163
  }
1164
1164
 
1165
+ // src/utils/getBroadcastConfig.ts
1166
+ async function getBroadcastConfig(req, pluginConfig) {
1167
+ try {
1168
+ const settings = await req.payload.findGlobal({
1169
+ slug: pluginConfig.settingsSlug || "newsletter-settings",
1170
+ req
1171
+ });
1172
+ if (settings?.provider === "broadcast" && settings?.broadcastSettings) {
1173
+ return {
1174
+ apiUrl: settings.broadcastSettings.apiUrl || pluginConfig.providers?.broadcast?.apiUrl || "",
1175
+ token: settings.broadcastSettings.token || pluginConfig.providers?.broadcast?.token || "",
1176
+ fromAddress: settings.fromAddress || pluginConfig.providers?.broadcast?.fromAddress || "",
1177
+ fromName: settings.fromName || pluginConfig.providers?.broadcast?.fromName || "",
1178
+ replyTo: settings.replyTo || pluginConfig.providers?.broadcast?.replyTo
1179
+ };
1180
+ }
1181
+ return pluginConfig.providers?.broadcast || null;
1182
+ } catch (error) {
1183
+ req.payload.logger.error("Failed to get broadcast config from settings:", error);
1184
+ return pluginConfig.providers?.broadcast || null;
1185
+ }
1186
+ }
1187
+
1165
1188
  // src/collections/Broadcasts.ts
1166
1189
  var createBroadcastsCollection = (pluginConfig) => {
1167
1190
  const hasProviders = !!(pluginConfig.providers?.broadcast || pluginConfig.providers?.resend);
@@ -1402,9 +1425,9 @@ var createBroadcastsCollection = (pluginConfig) => {
1402
1425
  async ({ doc, operation, req }) => {
1403
1426
  if (!hasProviders || operation !== "create") return doc;
1404
1427
  try {
1405
- const providerConfig = pluginConfig.providers?.broadcast;
1406
- if (!providerConfig) {
1407
- req.payload.logger.error("Broadcast provider not configured");
1428
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
1429
+ if (!providerConfig || !providerConfig.token) {
1430
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
1408
1431
  return doc;
1409
1432
  }
1410
1433
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -1439,6 +1462,51 @@ var createBroadcastsCollection = (pluginConfig) => {
1439
1462
  req.payload.logger.error("Failed to create broadcast in provider:", error);
1440
1463
  return doc;
1441
1464
  }
1465
+ },
1466
+ // Hook to send when published
1467
+ async ({ doc, operation, previousDoc, req }) => {
1468
+ if (operation !== "update") return doc;
1469
+ const wasUnpublished = !previousDoc?._status || previousDoc._status === "draft";
1470
+ const isNowPublished = doc._status === "published";
1471
+ if (wasUnpublished && isNowPublished && doc.providerId) {
1472
+ if (doc.status === "sent" || doc.status === "sending") {
1473
+ return doc;
1474
+ }
1475
+ try {
1476
+ const broadcastConfig = await getBroadcastConfig(req, pluginConfig);
1477
+ const resendConfig = pluginConfig.providers?.resend;
1478
+ if (!broadcastConfig && !resendConfig) {
1479
+ req.payload.logger.error("No provider configured for sending in Newsletter Settings or environment variables");
1480
+ return doc;
1481
+ }
1482
+ if (broadcastConfig && broadcastConfig.token) {
1483
+ const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
1484
+ const provider = new BroadcastApiProvider2(broadcastConfig);
1485
+ await provider.send(doc.providerId);
1486
+ }
1487
+ await req.payload.update({
1488
+ collection: "broadcasts",
1489
+ id: doc.id,
1490
+ data: {
1491
+ status: "sending" /* SENDING */,
1492
+ sentAt: (/* @__PURE__ */ new Date()).toISOString()
1493
+ },
1494
+ req
1495
+ });
1496
+ req.payload.logger.info(`Broadcast ${doc.id} sent successfully`);
1497
+ } catch (error) {
1498
+ req.payload.logger.error(`Failed to send broadcast ${doc.id}:`, error);
1499
+ await req.payload.update({
1500
+ collection: "broadcasts",
1501
+ id: doc.id,
1502
+ data: {
1503
+ status: "failed" /* FAILED */
1504
+ },
1505
+ req
1506
+ });
1507
+ }
1508
+ }
1509
+ return doc;
1442
1510
  }
1443
1511
  ],
1444
1512
  // Sync updates with provider
@@ -1446,9 +1514,9 @@ var createBroadcastsCollection = (pluginConfig) => {
1446
1514
  async ({ data, originalDoc, operation, req }) => {
1447
1515
  if (!hasProviders || !originalDoc?.providerId || operation !== "update") return data;
1448
1516
  try {
1449
- const providerConfig = pluginConfig.providers?.broadcast;
1450
- if (!providerConfig) {
1451
- req.payload.logger.error("Broadcast provider not configured");
1517
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
1518
+ if (!providerConfig || !providerConfig.token) {
1519
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
1452
1520
  return data;
1453
1521
  }
1454
1522
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -1492,9 +1560,9 @@ var createBroadcastsCollection = (pluginConfig) => {
1492
1560
  async ({ doc, req }) => {
1493
1561
  if (!hasProviders || !doc?.providerId) return doc;
1494
1562
  try {
1495
- const providerConfig = pluginConfig.providers?.broadcast;
1496
- if (!providerConfig) {
1497
- req.payload.logger.error("Broadcast provider not configured");
1563
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
1564
+ if (!providerConfig || !providerConfig.token) {
1565
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
1498
1566
  return doc;
1499
1567
  }
1500
1568
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -1507,48 +1575,6 @@ var createBroadcastsCollection = (pluginConfig) => {
1507
1575
  req.payload.logger.error("Failed to delete broadcast from provider:", error);
1508
1576
  }
1509
1577
  return doc;
1510
- },
1511
- // Hook to send when published
1512
- async ({ doc, req }) => {
1513
- if (doc._status === "published" && doc.providerId) {
1514
- if (doc.status === "sent" || doc.status === "sending") {
1515
- return doc;
1516
- }
1517
- try {
1518
- const broadcastConfig = pluginConfig.providers?.broadcast;
1519
- const resendConfig = pluginConfig.providers?.resend;
1520
- if (!broadcastConfig && !resendConfig) {
1521
- req.payload.logger.error("No provider configured for sending");
1522
- return doc;
1523
- }
1524
- if (broadcastConfig) {
1525
- const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
1526
- const provider = new BroadcastApiProvider2(broadcastConfig);
1527
- await provider.send(doc.providerId);
1528
- }
1529
- await req.payload.update({
1530
- collection: "broadcasts",
1531
- id: doc.id,
1532
- data: {
1533
- status: "sending" /* SENDING */,
1534
- sentAt: (/* @__PURE__ */ new Date()).toISOString()
1535
- },
1536
- req
1537
- });
1538
- req.payload.logger.info(`Broadcast ${doc.id} sent successfully`);
1539
- } catch (error) {
1540
- req.payload.logger.error(`Failed to send broadcast ${doc.id}:`, error);
1541
- await req.payload.update({
1542
- collection: "broadcasts",
1543
- id: doc.id,
1544
- data: {
1545
- status: "failed" /* FAILED */
1546
- },
1547
- req
1548
- });
1549
- }
1550
- }
1551
- return doc;
1552
1578
  }
1553
1579
  ]
1554
1580
  }