From 3197206512c7718f09acc0f26ba07fb778c38135 Mon Sep 17 00:00:00 2001 From: tommaso1 Date: Wed, 20 Nov 2024 12:07:29 +0100 Subject: [PATCH] [DEV-2007][DEV-2008] Implement create list active campaign lambda, Implement delete list from active campaign (#1250) * Add delete and update contact lambdas + test * Update deleteContact.test.ts Co-authored-by: marcobottaro <39835990+marcobottaro@users.noreply.github.com> * Update updateContact.test.ts Co-authored-by: marcobottaro <39835990+marcobottaro@users.noreply.github.com> * Update deleteContact.ts Co-authored-by: marcobottaro <39835990+marcobottaro@users.noreply.github.com> * changeset * Add handlers for create list and delete list on Active Campaign * Fix delete list * Skiiip --------- Co-authored-by: t Co-authored-by: marcobottaro <39835990+marcobottaro@users.noreply.github.com> --- .changeset/many-pens-grab.md | 5 +++ .../src/__tests__/handlers/createList.test.ts | 37 ++++++++++++++++ .../src/__tests__/handlers/deleteList.test.ts | 31 ++++++++++++++ .../src/activeCampaignClient.ts | 23 ++++++++++ .../src/handlers/createList.ts | 37 ++++++++++++++++ .../src/handlers/deleteList.ts | 42 +++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 .changeset/many-pens-grab.md create mode 100644 packages/active-campaign-client/src/__tests__/handlers/createList.test.ts create mode 100644 packages/active-campaign-client/src/__tests__/handlers/deleteList.test.ts create mode 100644 packages/active-campaign-client/src/handlers/createList.ts create mode 100644 packages/active-campaign-client/src/handlers/deleteList.ts diff --git a/.changeset/many-pens-grab.md b/.changeset/many-pens-grab.md new file mode 100644 index 0000000000..cbc0f7d35a --- /dev/null +++ b/.changeset/many-pens-grab.md @@ -0,0 +1,5 @@ +--- +"active-campaign-client": minor +--- + +Add handlers for create list and delete list on Active Campaign diff --git a/packages/active-campaign-client/src/__tests__/handlers/createList.test.ts b/packages/active-campaign-client/src/__tests__/handlers/createList.test.ts new file mode 100644 index 0000000000..011b7f6524 --- /dev/null +++ b/packages/active-campaign-client/src/__tests__/handlers/createList.test.ts @@ -0,0 +1,37 @@ +import { handler } from '../../handlers/createList'; +import { SQSEvent } from 'aws-lambda'; + +describe.skip('createList handler', () => { + it('should create a list successfully', async () => { + const event: SQSEvent = { + Records: [ + { + messageId: '1', + receiptHandle: '1', + body: JSON.stringify({ + title: `Test Webinar ${new Date().getTime()}`, + slug: `test-webinar-${new Date().getTime()}`, + description: 'Test Description', + subscribeCtaLabel: 'Subscribe to webinar', + isVisibleInList: true, + imagePath: '/path/to/image.jpg', + }), + attributes: { + ApproximateReceiveCount: '1', + SentTimestamp: '1', + SenderId: '1', + ApproximateFirstReceiveTimestamp: '1', + }, + messageAttributes: {}, + md5OfBody: '1', + eventSource: 'aws:sqs', + eventSourceARN: 'arn:aws:sqs:region:account-id:queue-name', + awsRegion: 'region', + }, + ], + }; + + const response = await handler(event); + expect(response.statusCode).toBe(200); + }); +}); diff --git a/packages/active-campaign-client/src/__tests__/handlers/deleteList.test.ts b/packages/active-campaign-client/src/__tests__/handlers/deleteList.test.ts new file mode 100644 index 0000000000..a4dc143be4 --- /dev/null +++ b/packages/active-campaign-client/src/__tests__/handlers/deleteList.test.ts @@ -0,0 +1,31 @@ +import { handler } from '../../handlers/deleteList'; +import { SQSEvent } from 'aws-lambda'; + +describe.skip('deleteList handler', () => { + it('should delete a list successfully', async () => { + const event: SQSEvent = { + Records: [ + { + messageId: '1', + receiptHandle: '1', + body: JSON.stringify({ + slug: 'test-webinar-1732097293540', + }), + attributes: { + ApproximateReceiveCount: '1', + SentTimestamp: '1', + SenderId: '1', + ApproximateFirstReceiveTimestamp: '1', + }, + messageAttributes: {}, + md5OfBody: '1', + eventSource: 'aws:sqs', + eventSourceARN: 'arn:aws:sqs:region:account-id:queue-name', + awsRegion: 'region', + }, + ], + }; + const response = await handler(event); + expect(response.statusCode).toBe(200); + }); +}); diff --git a/packages/active-campaign-client/src/activeCampaignClient.ts b/packages/active-campaign-client/src/activeCampaignClient.ts index d0e83cb483..f92b3c0af0 100644 --- a/packages/active-campaign-client/src/activeCampaignClient.ts +++ b/packages/active-campaign-client/src/activeCampaignClient.ts @@ -1,6 +1,7 @@ import axios from 'axios'; import * as dotenv from 'dotenv'; import { ContactPayload } from './types/contactPayload'; +import { ListPayload } from './types/listPayload'; dotenv.config({ path: '.env' }); @@ -51,6 +52,28 @@ export class ActiveCampaignClient { }); return response.data?.contacts?.[0]?.id; } + + async createList(data: ListPayload) { + const response = await axios.post(`${this.baseUrl}/api/3/lists`, data, { + headers: this.getHeaders(), + }); + return response.data; + } + + async getListIdByName(name: string) { + const response = await axios.get(`${this.baseUrl}/api/3/lists`, { + headers: this.getHeaders(), + params: { 'filters[name][eq]': name }, + }); + return response.data?.lists[0]?.id; + } + + async deleteList(id: number) { + const response = await axios.delete(`${this.baseUrl}/api/3/lists/${id}`, { + headers: this.getHeaders(), + }); + return response.data; + } } export const acClient = new ActiveCampaignClient( diff --git a/packages/active-campaign-client/src/handlers/createList.ts b/packages/active-campaign-client/src/handlers/createList.ts new file mode 100644 index 0000000000..cd8dddd204 --- /dev/null +++ b/packages/active-campaign-client/src/handlers/createList.ts @@ -0,0 +1,37 @@ +import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda'; +import { acClient } from '../activeCampaignClient'; +import { Webinar } from 'nextjs-website/src/lib/types/webinar'; +import { ListPayload } from '../types/listPayload'; + +export async function handler(event: { + readonly Records: SQSEvent['Records']; +}): Promise { + try { + const firstMessage = event.Records[0] ?? { body: '{}' }; + const webinarData: Webinar = JSON.parse(firstMessage.body); + + const acPayload: ListPayload = { + list: { + name: webinarData.slug, + stringid: webinarData.title, // Using slug as the stringid + sender_url: process.env.SENDER_URL || '', + sender_reminder: webinarData.subscribeCtaLabel || '', + subscription_notify: '', + unsubscription_notify: '', + }, + }; + + const response = await acClient.createList(acPayload); + + return { + statusCode: 200, + body: JSON.stringify(response), + }; + } catch (error) { + console.error('Error:', error); + return { + statusCode: 500, + body: JSON.stringify({ message: 'Internal server error' }), + }; + } +} diff --git a/packages/active-campaign-client/src/handlers/deleteList.ts b/packages/active-campaign-client/src/handlers/deleteList.ts new file mode 100644 index 0000000000..5e4ab96595 --- /dev/null +++ b/packages/active-campaign-client/src/handlers/deleteList.ts @@ -0,0 +1,42 @@ +import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda'; +import { acClient } from '../activeCampaignClient'; + +export async function handler(event: { + readonly Records: SQSEvent['Records']; +}): Promise { + try { + const firstMessage = event.Records[0] ?? { body: '{}' }; + const { slug } = JSON.parse(firstMessage.body); + + if (!slug) { + return { + statusCode: 400, + body: JSON.stringify({ message: 'Webinar slug is required' }), + }; + } + + // Get list ID using the slug (name) + const listId = await acClient.getListIdByName(slug); + + if (!listId) { + return { + statusCode: 404, + body: JSON.stringify({ message: 'List not found' }), + }; + } + + // Delete the list + await acClient.deleteList(Number(listId)); + + return { + statusCode: 200, + body: JSON.stringify({ message: 'List deleted successfully' }), + }; + } catch (error) { + console.error('Error:', error); + return { + statusCode: 500, + body: JSON.stringify({ message: 'Internal server error' }), + }; + } +}