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/dist/index.js CHANGED
@@ -2881,6 +2881,29 @@ async function requireAdmin(req, config) {
2881
2881
  };
2882
2882
  }
2883
2883
 
2884
+ // src/utils/getBroadcastConfig.ts
2885
+ async function getBroadcastConfig(req, pluginConfig) {
2886
+ try {
2887
+ const settings = await req.payload.findGlobal({
2888
+ slug: pluginConfig.settingsSlug || "newsletter-settings",
2889
+ req
2890
+ });
2891
+ if (settings?.provider === "broadcast" && settings?.broadcastSettings) {
2892
+ return {
2893
+ apiUrl: settings.broadcastSettings.apiUrl || pluginConfig.providers?.broadcast?.apiUrl || "",
2894
+ token: settings.broadcastSettings.token || pluginConfig.providers?.broadcast?.token || "",
2895
+ fromAddress: settings.fromAddress || pluginConfig.providers?.broadcast?.fromAddress || "",
2896
+ fromName: settings.fromName || pluginConfig.providers?.broadcast?.fromName || "",
2897
+ replyTo: settings.replyTo || pluginConfig.providers?.broadcast?.replyTo
2898
+ };
2899
+ }
2900
+ return pluginConfig.providers?.broadcast || null;
2901
+ } catch (error) {
2902
+ req.payload.logger.error("Failed to get broadcast config from settings:", error);
2903
+ return pluginConfig.providers?.broadcast || null;
2904
+ }
2905
+ }
2906
+
2884
2907
  // src/endpoints/broadcasts/send.ts
2885
2908
  var createSendBroadcastEndpoint = (config, collectionSlug) => {
2886
2909
  return {
@@ -2922,11 +2945,11 @@ var createSendBroadcastEndpoint = (config, collectionSlug) => {
2922
2945
  error: "Broadcast not found or not synced with provider"
2923
2946
  }, { status: 404 });
2924
2947
  }
2925
- const providerConfig = config.providers?.broadcast;
2926
- if (!providerConfig) {
2948
+ const providerConfig = await getBroadcastConfig(req, config);
2949
+ if (!providerConfig || !providerConfig.token) {
2927
2950
  return Response.json({
2928
2951
  success: false,
2929
- error: "Broadcast provider not configured"
2952
+ error: "Broadcast provider not configured in Newsletter Settings or environment variables"
2930
2953
  }, { status: 500 });
2931
2954
  }
2932
2955
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -4339,9 +4362,9 @@ var createBroadcastsCollection = (pluginConfig) => {
4339
4362
  async ({ doc, operation, req }) => {
4340
4363
  if (!hasProviders || operation !== "create") return doc;
4341
4364
  try {
4342
- const providerConfig = pluginConfig.providers?.broadcast;
4343
- if (!providerConfig) {
4344
- req.payload.logger.error("Broadcast provider not configured");
4365
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
4366
+ if (!providerConfig || !providerConfig.token) {
4367
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
4345
4368
  return doc;
4346
4369
  }
4347
4370
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -4376,6 +4399,51 @@ var createBroadcastsCollection = (pluginConfig) => {
4376
4399
  req.payload.logger.error("Failed to create broadcast in provider:", error);
4377
4400
  return doc;
4378
4401
  }
4402
+ },
4403
+ // Hook to send when published
4404
+ async ({ doc, operation, previousDoc, req }) => {
4405
+ if (operation !== "update") return doc;
4406
+ const wasUnpublished = !previousDoc?._status || previousDoc._status === "draft";
4407
+ const isNowPublished = doc._status === "published";
4408
+ if (wasUnpublished && isNowPublished && doc.providerId) {
4409
+ if (doc.status === "sent" || doc.status === "sending") {
4410
+ return doc;
4411
+ }
4412
+ try {
4413
+ const broadcastConfig = await getBroadcastConfig(req, pluginConfig);
4414
+ const resendConfig = pluginConfig.providers?.resend;
4415
+ if (!broadcastConfig && !resendConfig) {
4416
+ req.payload.logger.error("No provider configured for sending in Newsletter Settings or environment variables");
4417
+ return doc;
4418
+ }
4419
+ if (broadcastConfig && broadcastConfig.token) {
4420
+ const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
4421
+ const provider = new BroadcastApiProvider2(broadcastConfig);
4422
+ await provider.send(doc.providerId);
4423
+ }
4424
+ await req.payload.update({
4425
+ collection: "broadcasts",
4426
+ id: doc.id,
4427
+ data: {
4428
+ status: "sending" /* SENDING */,
4429
+ sentAt: (/* @__PURE__ */ new Date()).toISOString()
4430
+ },
4431
+ req
4432
+ });
4433
+ req.payload.logger.info(`Broadcast ${doc.id} sent successfully`);
4434
+ } catch (error) {
4435
+ req.payload.logger.error(`Failed to send broadcast ${doc.id}:`, error);
4436
+ await req.payload.update({
4437
+ collection: "broadcasts",
4438
+ id: doc.id,
4439
+ data: {
4440
+ status: "failed" /* FAILED */
4441
+ },
4442
+ req
4443
+ });
4444
+ }
4445
+ }
4446
+ return doc;
4379
4447
  }
4380
4448
  ],
4381
4449
  // Sync updates with provider
@@ -4383,9 +4451,9 @@ var createBroadcastsCollection = (pluginConfig) => {
4383
4451
  async ({ data, originalDoc, operation, req }) => {
4384
4452
  if (!hasProviders || !originalDoc?.providerId || operation !== "update") return data;
4385
4453
  try {
4386
- const providerConfig = pluginConfig.providers?.broadcast;
4387
- if (!providerConfig) {
4388
- req.payload.logger.error("Broadcast provider not configured");
4454
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
4455
+ if (!providerConfig || !providerConfig.token) {
4456
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
4389
4457
  return data;
4390
4458
  }
4391
4459
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -4429,9 +4497,9 @@ var createBroadcastsCollection = (pluginConfig) => {
4429
4497
  async ({ doc, req }) => {
4430
4498
  if (!hasProviders || !doc?.providerId) return doc;
4431
4499
  try {
4432
- const providerConfig = pluginConfig.providers?.broadcast;
4433
- if (!providerConfig) {
4434
- req.payload.logger.error("Broadcast provider not configured");
4500
+ const providerConfig = await getBroadcastConfig(req, pluginConfig);
4501
+ if (!providerConfig || !providerConfig.token) {
4502
+ req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
4435
4503
  return doc;
4436
4504
  }
4437
4505
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -4444,48 +4512,6 @@ var createBroadcastsCollection = (pluginConfig) => {
4444
4512
  req.payload.logger.error("Failed to delete broadcast from provider:", error);
4445
4513
  }
4446
4514
  return doc;
4447
- },
4448
- // Hook to send when published
4449
- async ({ doc, req }) => {
4450
- if (doc._status === "published" && doc.providerId) {
4451
- if (doc.status === "sent" || doc.status === "sending") {
4452
- return doc;
4453
- }
4454
- try {
4455
- const broadcastConfig = pluginConfig.providers?.broadcast;
4456
- const resendConfig = pluginConfig.providers?.resend;
4457
- if (!broadcastConfig && !resendConfig) {
4458
- req.payload.logger.error("No provider configured for sending");
4459
- return doc;
4460
- }
4461
- if (broadcastConfig) {
4462
- const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
4463
- const provider = new BroadcastApiProvider2(broadcastConfig);
4464
- await provider.send(doc.providerId);
4465
- }
4466
- await req.payload.update({
4467
- collection: "broadcasts",
4468
- id: doc.id,
4469
- data: {
4470
- status: "sending" /* SENDING */,
4471
- sentAt: (/* @__PURE__ */ new Date()).toISOString()
4472
- },
4473
- req
4474
- });
4475
- req.payload.logger.info(`Broadcast ${doc.id} sent successfully`);
4476
- } catch (error) {
4477
- req.payload.logger.error(`Failed to send broadcast ${doc.id}:`, error);
4478
- await req.payload.update({
4479
- collection: "broadcasts",
4480
- id: doc.id,
4481
- data: {
4482
- status: "failed" /* FAILED */
4483
- },
4484
- req
4485
- });
4486
- }
4487
- }
4488
- return doc;
4489
4515
  }
4490
4516
  ]
4491
4517
  }