payload-plugin-newsletter 0.25.7 → 0.25.9

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.
@@ -227,6 +227,7 @@ var init_broadcast2 = __esm({
227
227
  throw new Error(`Broadcast API error: ${response.status} - ${error}`);
228
228
  }
229
229
  const broadcast = await response.json();
230
+ console.log("[BroadcastApiProvider] GET response:", broadcast);
230
231
  return this.transformBroadcastFromApi(broadcast);
231
232
  } catch (error) {
232
233
  if (error instanceof BroadcastProviderError) throw error;
@@ -2292,82 +2293,18 @@ var createBroadcastsCollection = (pluginConfig) => {
2292
2293
  async ({ doc, operation, req, previousDoc }) => {
2293
2294
  if (!hasProviders) return doc;
2294
2295
  if (operation === "create") {
2295
- try {
2296
- req.payload.logger.info("Broadcast afterChange create hook - doc info:", {
2297
- docId: doc.id,
2298
- docIdType: typeof doc.id,
2299
- hasDoc: !!doc,
2300
- operation
2301
- });
2302
- const providerConfig = await getBroadcastConfig(req, pluginConfig);
2303
- if (!providerConfig || !providerConfig.token) {
2304
- req.payload.logger.error("Broadcast provider not configured in Newsletter Settings or environment variables");
2305
- return doc;
2306
- }
2307
- const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
2308
- const provider = new BroadcastApiProvider2(providerConfig);
2309
- const subject = doc.subject || `Draft Broadcast ${(/* @__PURE__ */ new Date()).toISOString()}`;
2310
- const htmlContent = doc.contentSection?.content ? await convertToEmailSafeHtml(
2311
- await populateMediaFields(doc.contentSection.content, req.payload, pluginConfig),
2312
- {
2313
- wrapInTemplate: pluginConfig.customizations?.broadcasts?.emailPreview?.wrapInTemplate ?? true,
2314
- customWrapper: pluginConfig.customizations?.broadcasts?.emailPreview?.customWrapper,
2315
- preheader: doc.contentSection?.preheader,
2316
- subject,
2317
- documentData: doc,
2318
- customBlockConverter: pluginConfig.customizations?.broadcasts?.customBlockConverter
2319
- }
2320
- ) : "<p>Draft content - to be updated</p>";
2321
- const createData = {
2322
- name: subject,
2323
- // Use subject as name
2324
- subject,
2325
- preheader: doc.contentSection?.preheader || "",
2326
- content: htmlContent,
2327
- trackOpens: doc.settings?.trackOpens ?? true,
2328
- trackClicks: doc.settings?.trackClicks ?? true,
2329
- replyTo: doc.settings?.replyTo || providerConfig.replyTo,
2330
- audienceIds: doc.audienceIds?.map((a) => a.audienceId) || []
2331
- };
2332
- req.payload.logger.info("Creating broadcast in provider with minimal data to establish association", {
2333
- subject: createData.subject,
2334
- hasActualContent: !!doc.contentSection?.content
2335
- });
2336
- const providerBroadcast = await provider.create(createData);
2337
- req.payload.logger.info("Provider broadcast created:", {
2338
- providerBroadcastId: providerBroadcast.id,
2339
- providerBroadcastIdType: typeof providerBroadcast.id,
2340
- docId: doc.id,
2341
- docIdType: typeof doc.id
2342
- });
2343
- req.payload.logger.info(`Broadcast ${doc.id} created in provider with ID ${providerBroadcast.id}`);
2344
- return {
2345
- ...doc,
2346
- providerId: providerBroadcast.id,
2347
- externalId: providerBroadcast.id,
2348
- // Include externalId in return value
2349
- providerData: providerBroadcast.providerData
2350
- };
2351
- } catch (error) {
2352
- req.payload.logger.error("Failed to create broadcast in provider during initial creation:");
2353
- if (error instanceof Error) {
2354
- req.payload.logger.error("Error details:", {
2355
- message: error.message,
2356
- stack: error.stack,
2357
- name: error.name
2358
- });
2359
- } else {
2360
- req.payload.logger.error("Raw error:", error);
2361
- }
2362
- return doc;
2363
- }
2296
+ req.payload.logger.info("Broadcast created in Payload, provider sync will happen on first update with content");
2297
+ return doc;
2364
2298
  }
2365
2299
  if (operation === "update") {
2366
2300
  req.payload.logger.info("Broadcast afterChange update hook triggered", {
2367
2301
  operation,
2368
2302
  hasProviderId: !!doc.providerId,
2303
+ hasExternalId: !!doc.externalId,
2369
2304
  sendStatus: doc.sendStatus,
2370
- publishStatus: doc._status
2305
+ publishStatus: doc._status,
2306
+ hasSubject: !!doc.subject,
2307
+ hasContent: !!doc.contentSection?.content
2371
2308
  });
2372
2309
  try {
2373
2310
  const providerConfig = await getBroadcastConfig(req, pluginConfig);
@@ -2377,8 +2314,40 @@ var createBroadcastsCollection = (pluginConfig) => {
2377
2314
  }
2378
2315
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
2379
2316
  const provider = new BroadcastApiProvider2(providerConfig);
2317
+ if (!doc.providerId && !doc.externalId && doc.subject && doc.contentSection?.content) {
2318
+ req.payload.logger.info("Creating broadcast in provider on first update with content");
2319
+ const htmlContent = await convertToEmailSafeHtml(
2320
+ await populateMediaFields(doc.contentSection.content, req.payload, pluginConfig),
2321
+ {
2322
+ wrapInTemplate: pluginConfig.customizations?.broadcasts?.emailPreview?.wrapInTemplate ?? true,
2323
+ customWrapper: pluginConfig.customizations?.broadcasts?.emailPreview?.customWrapper,
2324
+ preheader: doc.contentSection?.preheader,
2325
+ subject: doc.subject,
2326
+ documentData: doc,
2327
+ customBlockConverter: pluginConfig.customizations?.broadcasts?.customBlockConverter
2328
+ }
2329
+ );
2330
+ const createData = {
2331
+ name: doc.subject,
2332
+ subject: doc.subject,
2333
+ preheader: doc.contentSection?.preheader || "",
2334
+ content: htmlContent,
2335
+ trackOpens: doc.settings?.trackOpens ?? true,
2336
+ trackClicks: doc.settings?.trackClicks ?? true,
2337
+ replyTo: doc.settings?.replyTo || providerConfig.replyTo,
2338
+ audienceIds: doc.audienceIds?.map((a) => a.audienceId) || []
2339
+ };
2340
+ const providerBroadcast = await provider.create(createData);
2341
+ req.payload.logger.info(`Broadcast ${doc.id} created in provider with ID ${providerBroadcast.id}`);
2342
+ return {
2343
+ ...doc,
2344
+ providerId: providerBroadcast.id,
2345
+ externalId: providerBroadcast.id,
2346
+ providerData: providerBroadcast.providerData
2347
+ };
2348
+ }
2380
2349
  if (!doc.providerId) {
2381
- req.payload.logger.warn(`Broadcast ${doc.id} has no providerId - provider sync skipped. This shouldn't happen with immediate creation.`);
2350
+ req.payload.logger.info(`Broadcast ${doc.id} has no providerId and insufficient content for creation - skipping sync`);
2382
2351
  return doc;
2383
2352
  }
2384
2353
  if (doc.providerId) {