payload-plugin-newsletter 0.17.3 → 0.18.0

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.
@@ -1612,7 +1612,87 @@ var createTestBroadcastEndpoint = (config, collectionSlug) => {
1612
1612
  };
1613
1613
 
1614
1614
  // src/endpoints/broadcasts/preview.ts
1615
- var createBroadcastPreviewEndpoint = (config, collectionSlug) => {
1615
+ async function populateMediaFields(content, payload, config) {
1616
+ if (!content || typeof content !== "object") return content;
1617
+ if (content.root?.children) {
1618
+ for (const child of content.root.children) {
1619
+ await populateBlockMediaFields(child, payload, config);
1620
+ }
1621
+ }
1622
+ return content;
1623
+ }
1624
+ async function populateBlockMediaFields(node, payload, config) {
1625
+ if (node.type === "block" && node.fields) {
1626
+ const blockType = node.fields.blockType || node.fields.blockName;
1627
+ const customBlocks = config.customizations?.broadcasts?.customBlocks || [];
1628
+ const blockConfig = customBlocks.find((b) => b.slug === blockType);
1629
+ if (blockConfig && blockConfig.fields) {
1630
+ for (const field of blockConfig.fields) {
1631
+ if (field.type === "upload" && field.relationTo && node.fields[field.name]) {
1632
+ const fieldValue = node.fields[field.name];
1633
+ if (typeof fieldValue === "string" && fieldValue.match(/^[a-f0-9]{24}$/i)) {
1634
+ try {
1635
+ const media = await payload.findByID({
1636
+ collection: field.relationTo,
1637
+ id: fieldValue,
1638
+ depth: 0
1639
+ });
1640
+ if (media) {
1641
+ node.fields[field.name] = media;
1642
+ payload.logger?.info(`Populated ${field.name} for block ${blockType}:`, {
1643
+ mediaId: fieldValue,
1644
+ mediaUrl: media.url,
1645
+ filename: media.filename
1646
+ });
1647
+ }
1648
+ } catch (error) {
1649
+ payload.logger?.error(`Failed to populate ${field.name} for block ${blockType}:`, error);
1650
+ }
1651
+ }
1652
+ }
1653
+ if (field.type === "array" && field.fields) {
1654
+ const arrayValue = node.fields[field.name];
1655
+ if (Array.isArray(arrayValue)) {
1656
+ for (const arrayItem of arrayValue) {
1657
+ if (arrayItem && typeof arrayItem === "object") {
1658
+ for (const arrayField of field.fields) {
1659
+ if (arrayField.type === "upload" && arrayField.relationTo && arrayItem[arrayField.name]) {
1660
+ const arrayFieldValue = arrayItem[arrayField.name];
1661
+ if (typeof arrayFieldValue === "string" && arrayFieldValue.match(/^[a-f0-9]{24}$/i)) {
1662
+ try {
1663
+ const media = await payload.findByID({
1664
+ collection: arrayField.relationTo,
1665
+ id: arrayFieldValue,
1666
+ depth: 0
1667
+ });
1668
+ if (media) {
1669
+ arrayItem[arrayField.name] = media;
1670
+ payload.logger?.info(`Populated array ${arrayField.name} for block ${blockType}:`, {
1671
+ mediaId: arrayFieldValue,
1672
+ mediaUrl: media.url,
1673
+ filename: media.filename
1674
+ });
1675
+ }
1676
+ } catch (error) {
1677
+ payload.logger?.error(`Failed to populate array ${arrayField.name} for block ${blockType}:`, error);
1678
+ }
1679
+ }
1680
+ }
1681
+ }
1682
+ }
1683
+ }
1684
+ }
1685
+ }
1686
+ }
1687
+ }
1688
+ }
1689
+ if (node.children) {
1690
+ for (const child of node.children) {
1691
+ await populateBlockMediaFields(child, payload, config);
1692
+ }
1693
+ }
1694
+ }
1695
+ var createBroadcastPreviewEndpoint = (config, _collectionSlug) => {
1616
1696
  return {
1617
1697
  path: "/preview",
1618
1698
  method: "post",
@@ -1627,7 +1707,9 @@ var createBroadcastPreviewEndpoint = (config, collectionSlug) => {
1627
1707
  }, { status: 400 });
1628
1708
  }
1629
1709
  const mediaUrl = req.payload.config.serverURL ? `${req.payload.config.serverURL}/api/media` : "/api/media";
1630
- const htmlContent = await convertToEmailSafeHtml(content, {
1710
+ req.payload.logger?.info("Populating media fields for email preview...");
1711
+ const populatedContent = await populateMediaFields(content, req.payload, config);
1712
+ const htmlContent = await convertToEmailSafeHtml(populatedContent, {
1631
1713
  wrapInTemplate: true,
1632
1714
  preheader,
1633
1715
  mediaUrl,
@@ -1926,8 +2008,9 @@ var createBroadcastsCollection = (pluginConfig) => {
1926
2008
  }
1927
2009
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
1928
2010
  const provider = new BroadcastApiProvider2(providerConfig);
1929
- req.payload.logger.info("Converting content to HTML...");
1930
- const htmlContent = await convertToEmailSafeHtml(doc.contentSection?.content, {
2011
+ req.payload.logger.info("Populating media fields and converting content to HTML...");
2012
+ const populatedContent = await populateMediaFields(doc.contentSection?.content, req.payload, pluginConfig);
2013
+ const htmlContent = await convertToEmailSafeHtml(populatedContent, {
1931
2014
  customBlockConverter: pluginConfig.customizations?.broadcasts?.customBlockConverter
1932
2015
  });
1933
2016
  if (!htmlContent || htmlContent.trim() === "") {
@@ -2026,7 +2109,8 @@ var createBroadcastsCollection = (pluginConfig) => {
2026
2109
  return doc;
2027
2110
  }
2028
2111
  req.payload.logger.info("Creating broadcast in provider (deferred from initial create)...");
2029
- const htmlContent = await convertToEmailSafeHtml(doc.contentSection?.content, {
2112
+ const populatedContent = await populateMediaFields(doc.contentSection?.content, req.payload, pluginConfig);
2113
+ const htmlContent = await convertToEmailSafeHtml(populatedContent, {
2030
2114
  customBlockConverter: pluginConfig.customizations?.broadcasts?.customBlockConverter
2031
2115
  });
2032
2116
  if (!htmlContent || htmlContent.trim() === "") {
@@ -2087,7 +2171,8 @@ var createBroadcastsCollection = (pluginConfig) => {
2087
2171
  updates.preheader = doc.contentSection?.preheader;
2088
2172
  }
2089
2173
  if (JSON.stringify(doc.contentSection?.content) !== JSON.stringify(previousDoc?.contentSection?.content)) {
2090
- updates.content = await convertToEmailSafeHtml(doc.contentSection?.content, {
2174
+ const populatedContent = await populateMediaFields(doc.contentSection?.content, req.payload, pluginConfig);
2175
+ updates.content = await convertToEmailSafeHtml(populatedContent, {
2091
2176
  customBlockConverter: pluginConfig.customizations?.broadcasts?.customBlockConverter
2092
2177
  });
2093
2178
  }