payload-plugin-newsletter 0.4.5 → 0.6.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.
package/dist/index.js CHANGED
@@ -320,40 +320,17 @@ var createSubscribersCollection = (pluginConfig) => {
320
320
  return subscribersCollection;
321
321
  };
322
322
 
323
- // src/collections/NewsletterSettings.ts
324
- var createNewsletterSettingsCollection = (pluginConfig) => {
323
+ // src/globals/NewsletterSettings.ts
324
+ var createNewsletterSettingsGlobal = (pluginConfig) => {
325
325
  const slug = pluginConfig.settingsSlug || "newsletter-settings";
326
326
  return {
327
327
  slug,
328
- labels: {
329
- singular: "Newsletter Setting",
330
- plural: "Newsletter Settings"
331
- },
328
+ label: "Newsletter Settings",
332
329
  admin: {
333
- useAsTitle: "name",
334
- defaultColumns: ["name", "provider", "active", "updatedAt"],
335
330
  group: "Newsletter",
336
331
  description: "Configure email provider settings and templates"
337
332
  },
338
333
  fields: [
339
- {
340
- name: "name",
341
- type: "text",
342
- label: "Configuration Name",
343
- required: true,
344
- admin: {
345
- description: 'A descriptive name for this configuration (e.g., "Production", "Development", "Marketing Emails")'
346
- }
347
- },
348
- {
349
- name: "active",
350
- type: "checkbox",
351
- label: "Active",
352
- defaultValue: false,
353
- admin: {
354
- description: "Only one configuration can be active at a time"
355
- }
356
- },
357
334
  {
358
335
  type: "tabs",
359
336
  tabs: [
@@ -613,53 +590,16 @@ var createNewsletterSettingsCollection = (pluginConfig) => {
613
590
  ],
614
591
  hooks: {
615
592
  beforeChange: [
616
- async ({ data, req, operation }) => {
593
+ async ({ data, req }) => {
617
594
  if (!req.user || req.user.collection !== "users") {
618
595
  throw new Error("Only administrators can modify newsletter settings");
619
596
  }
620
- if (data?.active && operation !== "create") {
621
- await req.payload.update({
622
- collection: slug,
623
- where: {
624
- id: {
625
- not_equals: data.id
626
- }
627
- },
628
- data: {
629
- active: false
630
- }
631
- // Keep overrideAccess: true for admin operations after verification
632
- });
633
- }
634
- if (operation === "create" && data?.active) {
635
- const existingActive = await req.payload.find({
636
- collection: slug,
637
- where: {
638
- active: {
639
- equals: true
640
- }
641
- }
642
- // Keep overrideAccess: true for admin operations
643
- });
644
- if (existingActive.docs.length > 0) {
645
- for (const doc of existingActive.docs) {
646
- await req.payload.update({
647
- collection: slug,
648
- id: doc.id,
649
- data: {
650
- active: false
651
- }
652
- // Keep overrideAccess: true for admin operations
653
- });
654
- }
655
- }
656
- }
657
597
  return data;
658
598
  }
659
599
  ],
660
600
  afterChange: [
661
601
  async ({ doc, req }) => {
662
- if (req.payload.newsletterEmailService && doc.active) {
602
+ if (req.payload.newsletterEmailService) {
663
603
  try {
664
604
  console.warn("Newsletter settings updated, reinitializing service...");
665
605
  } catch {
@@ -672,11 +612,8 @@ var createNewsletterSettingsCollection = (pluginConfig) => {
672
612
  access: {
673
613
  read: () => true,
674
614
  // Settings can be read publicly for validation
675
- create: adminOnly(pluginConfig),
676
- update: adminOnly(pluginConfig),
677
- delete: adminOnly(pluginConfig)
678
- },
679
- timestamps: true
615
+ update: adminOnly(pluginConfig)
616
+ }
680
617
  };
681
618
  };
682
619
 
@@ -1156,18 +1093,11 @@ var createSubscribeEndpoint = (config) => {
1156
1093
  errors: validation.errors
1157
1094
  });
1158
1095
  }
1159
- const settingsResult = await req.payload.find({
1160
- collection: config.settingsSlug || "newsletter-settings",
1161
- where: {
1162
- active: {
1163
- equals: true
1164
- }
1165
- },
1166
- limit: 1,
1096
+ const settings = await req.payload.findGlobal({
1097
+ slug: config.settingsSlug || "newsletter-settings",
1167
1098
  overrideAccess: false
1168
1099
  // No user context for public endpoint
1169
1100
  });
1170
- const settings = settingsResult.docs[0];
1171
1101
  const allowedDomains = settings?.subscriptionSettings?.allowedDomains?.map((d) => d.domain) || [];
1172
1102
  if (!isDomainAllowed(trimmedEmail, allowedDomains)) {
1173
1103
  return res.status(400).json({
@@ -1872,8 +1802,8 @@ var newsletterPlugin = (pluginConfig) => (incomingConfig) => {
1872
1802
  return incomingConfig;
1873
1803
  }
1874
1804
  const subscribersCollection = createSubscribersCollection(config);
1875
- const settingsCollection = createNewsletterSettingsCollection(config);
1876
- let collections = [...incomingConfig.collections || [], subscribersCollection, settingsCollection];
1805
+ const settingsGlobal = createNewsletterSettingsGlobal(config);
1806
+ let collections = [...incomingConfig.collections || [], subscribersCollection];
1877
1807
  if (config.features?.newsletterScheduling?.enabled) {
1878
1808
  const targetCollections = config.features.newsletterScheduling.collections || "articles";
1879
1809
  const collectionsToExtend = Array.isArray(targetCollections) ? targetCollections : [targetCollections];
@@ -1896,7 +1826,8 @@ var newsletterPlugin = (pluginConfig) => (incomingConfig) => {
1896
1826
  ...incomingConfig,
1897
1827
  collections,
1898
1828
  globals: [
1899
- ...incomingConfig.globals || []
1829
+ ...incomingConfig.globals || [],
1830
+ settingsGlobal
1900
1831
  ],
1901
1832
  endpoints: [
1902
1833
  ...incomingConfig.endpoints || [],
@@ -1904,16 +1835,9 @@ var newsletterPlugin = (pluginConfig) => (incomingConfig) => {
1904
1835
  ],
1905
1836
  onInit: async (payload) => {
1906
1837
  try {
1907
- const settingsResult = await payload.find({
1908
- collection: config.settingsSlug || "newsletter-settings",
1909
- where: {
1910
- active: {
1911
- equals: true
1912
- }
1913
- },
1914
- limit: 1
1838
+ const settings = await payload.findGlobal({
1839
+ slug: config.settingsSlug || "newsletter-settings"
1915
1840
  });
1916
- const settings = settingsResult.docs[0];
1917
1841
  let emailServiceConfig;
1918
1842
  if (settings) {
1919
1843
  emailServiceConfig = {