payload-plugin-newsletter 0.6.0 → 0.6.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.cjs CHANGED
@@ -837,20 +837,22 @@ var BroadcastProvider = class {
837
837
  }
838
838
  async addContact(contact) {
839
839
  try {
840
- const response = await fetch(`${this.apiUrl}/api/v1/contacts`, {
840
+ const [firstName, ...lastNameParts] = (contact.name || "").split(" ");
841
+ const lastName = lastNameParts.join(" ");
842
+ const response = await fetch(`${this.apiUrl}/api/v1/subscribers.json`, {
841
843
  method: "POST",
842
844
  headers: {
843
845
  "Authorization": `Bearer ${this.token}`,
844
846
  "Content-Type": "application/json"
845
847
  },
846
848
  body: JSON.stringify({
847
- email: contact.email,
848
- name: contact.name,
849
- status: contact.subscriptionStatus === "active" ? "subscribed" : "unsubscribed",
850
- metadata: {
851
- locale: contact.locale,
852
- source: contact.source,
853
- ...contact.utmParameters
849
+ subscriber: {
850
+ email: contact.email,
851
+ first_name: firstName || void 0,
852
+ last_name: lastName || void 0,
853
+ tags: [`lang:${contact.locale || "en"}`],
854
+ is_active: contact.subscriptionStatus === "active",
855
+ source: contact.source
854
856
  }
855
857
  })
856
858
  });
@@ -869,7 +871,7 @@ var BroadcastProvider = class {
869
871
  async updateContact(contact) {
870
872
  try {
871
873
  const searchResponse = await fetch(
872
- `${this.apiUrl}/api/v1/contacts?email=${encodeURIComponent(contact.email)}`,
874
+ `${this.apiUrl}/api/v1/subscribers/find.json?email=${encodeURIComponent(contact.email)}`,
873
875
  {
874
876
  headers: {
875
877
  "Authorization": `Bearer ${this.token}`
@@ -880,26 +882,27 @@ var BroadcastProvider = class {
880
882
  await this.addContact(contact);
881
883
  return;
882
884
  }
883
- const contacts = await searchResponse.json();
884
- const existingContact = contacts.data?.[0];
885
- if (!existingContact) {
885
+ const existingContact = await searchResponse.json();
886
+ if (!existingContact || !existingContact.id) {
886
887
  await this.addContact(contact);
887
888
  return;
888
889
  }
889
- const response = await fetch(`${this.apiUrl}/api/v1/contacts/${existingContact.id}`, {
890
- method: "PUT",
890
+ const [firstName, ...lastNameParts] = (contact.name || "").split(" ");
891
+ const lastName = lastNameParts.join(" ");
892
+ const response = await fetch(`${this.apiUrl}/api/v1/subscribers.json`, {
893
+ method: "PATCH",
891
894
  headers: {
892
895
  "Authorization": `Bearer ${this.token}`,
893
896
  "Content-Type": "application/json"
894
897
  },
895
898
  body: JSON.stringify({
896
899
  email: contact.email,
897
- name: contact.name,
898
- status: contact.subscriptionStatus === "active" ? "subscribed" : "unsubscribed",
899
- metadata: {
900
- locale: contact.locale,
901
- source: contact.source,
902
- ...contact.utmParameters
900
+ subscriber: {
901
+ first_name: firstName || void 0,
902
+ last_name: lastName || void 0,
903
+ tags: [`lang:${contact.locale || "en"}`],
904
+ is_active: contact.subscriptionStatus === "active",
905
+ source: contact.source
903
906
  }
904
907
  })
905
908
  });
@@ -918,7 +921,7 @@ var BroadcastProvider = class {
918
921
  async removeContact(email) {
919
922
  try {
920
923
  const searchResponse = await fetch(
921
- `${this.apiUrl}/api/v1/contacts?email=${encodeURIComponent(email)}`,
924
+ `${this.apiUrl}/api/v1/subscribers/find.json?email=${encodeURIComponent(email)}`,
922
925
  {
923
926
  headers: {
924
927
  "Authorization": `Bearer ${this.token}`
@@ -928,16 +931,17 @@ var BroadcastProvider = class {
928
931
  if (!searchResponse.ok) {
929
932
  return;
930
933
  }
931
- const contacts = await searchResponse.json();
932
- const contact = contacts.data?.[0];
933
- if (!contact) {
934
+ const contact = await searchResponse.json();
935
+ if (!contact || !contact.id) {
934
936
  return;
935
937
  }
936
- const response = await fetch(`${this.apiUrl}/api/v1/contacts/${contact.id}`, {
937
- method: "DELETE",
938
+ const response = await fetch(`${this.apiUrl}/api/v1/subscribers/deactivate.json`, {
939
+ method: "POST",
938
940
  headers: {
939
- "Authorization": `Bearer ${this.token}`
940
- }
941
+ "Authorization": `Bearer ${this.token}`,
942
+ "Content-Type": "application/json"
943
+ },
944
+ body: JSON.stringify({ email })
941
945
  });
942
946
  if (!response.ok) {
943
947
  const error = await response.text();