payload-plugin-newsletter 0.16.1 → 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));
@@ -4387,13 +4410,13 @@ var createBroadcastsCollection = (pluginConfig) => {
4387
4410
  return doc;
4388
4411
  }
4389
4412
  try {
4390
- const broadcastConfig = pluginConfig.providers?.broadcast;
4413
+ const broadcastConfig = await getBroadcastConfig(req, pluginConfig);
4391
4414
  const resendConfig = pluginConfig.providers?.resend;
4392
4415
  if (!broadcastConfig && !resendConfig) {
4393
- req.payload.logger.error("No provider configured for sending");
4416
+ req.payload.logger.error("No provider configured for sending in Newsletter Settings or environment variables");
4394
4417
  return doc;
4395
4418
  }
4396
- if (broadcastConfig) {
4419
+ if (broadcastConfig && broadcastConfig.token) {
4397
4420
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
4398
4421
  const provider = new BroadcastApiProvider2(broadcastConfig);
4399
4422
  await provider.send(doc.providerId);
@@ -4428,9 +4451,9 @@ var createBroadcastsCollection = (pluginConfig) => {
4428
4451
  async ({ data, originalDoc, operation, req }) => {
4429
4452
  if (!hasProviders || !originalDoc?.providerId || operation !== "update") return data;
4430
4453
  try {
4431
- const providerConfig = pluginConfig.providers?.broadcast;
4432
- if (!providerConfig) {
4433
- 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");
4434
4457
  return data;
4435
4458
  }
4436
4459
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
@@ -4474,9 +4497,9 @@ var createBroadcastsCollection = (pluginConfig) => {
4474
4497
  async ({ doc, req }) => {
4475
4498
  if (!hasProviders || !doc?.providerId) return doc;
4476
4499
  try {
4477
- const providerConfig = pluginConfig.providers?.broadcast;
4478
- if (!providerConfig) {
4479
- 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");
4480
4503
  return doc;
4481
4504
  }
4482
4505
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));