Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-1983][DEV-1986] Add function to subscribe and unsubscribe contact from active campaign list #1257

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-parrots-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"active-campaign-client": minor
---

Add function to subscribe and upsubscribe contact from active campaign list
2 changes: 2 additions & 0 deletions packages/active-campaign-client/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ AC_API_KEY=your_api_key
SENDER_URL=localhost:3000
AWS_REGION="region"
AWS_USER_POOL_ID="region_DFWF81fRa"
COGNITO_USER_ID = 66ae52a0-f051-7080-04a1-465b3a4f44cc
LIST_NAME = test-webinar-1732097286071
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// remove .skip to run the test, be aware it does real API calls
import { manageListSubscription } from '../../helpers/manageListSubscription';

describe.skip('manage list subscription', () => {
const cognitoUserId = process.env.COGNITO_USER_ID || '';
const listName = process.env.LIST_NAME || '';

it('should subscribe the contact to the list', async () => {
const result = await manageListSubscription(
cognitoUserId,
listName,
'subscribe'
);

expect(result.statusCode).toBe(200);
});

it('should unsubscribe the contact from the list', async () => {
const result = await manageListSubscription(
cognitoUserId,
listName,
'unsubscribe'
);
expect(result.statusCode).toBe(200);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as https from 'https';
import { ContactPayload } from '../types/contactPayload';
import { ListPayload } from '../types/listPayload';
import { ListStatusPayload } from '../types/listStatusPayload';

export class ActiveCampaignClient {
private readonly baseUrl: string;
Expand All @@ -22,7 +23,7 @@ export class ActiveCampaignClient {
private async makeRequest<T>(
method: string,
path: string,
data?: ContactPayload | ListPayload,
data?: ContactPayload | ListPayload | ListStatusPayload,
params?: Record<string, string>
): Promise<T> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -111,6 +112,26 @@ export class ActiveCampaignClient {
async deleteList(id: number) {
return this.makeRequest('DELETE', `/api/3/lists/${id}`);
}

async addContactToList(contactId: string, listId: number) {
return this.makeRequest('POST', `/api/3/contactLists`, {
contactList: {
contact: contactId,
list: listId,
status: 1, // subscribe
},
});
}

async removeContactFromList(contactId: string, listId: number) {
return this.makeRequest('POST', `/api/3/contactLists`, {
contactList: {
contact: contactId,
list: listId,
status: 2, // unsubscrbe
},
});
}
}

export const acClient = new ActiveCampaignClient(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { APIGatewayProxyResult } from 'aws-lambda';
import { acClient } from '../clients/activeCampaignClient';

export async function manageListSubscription(
cognitoUserId: string,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cognitoUserId: string,
cognitoId: string,

listName: string,
action: 'subscribe' | 'unsubscribe'
): Promise<APIGatewayProxyResult> {
try {
const contactId = await acClient.getContactByCognitoId(cognitoUserId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const contactId = await acClient.getContactByCognitoId(cognitoUserId);
const contactId = await acClient.getContactByCognitoId(cognitoId);

if (!contactId) {
return {
statusCode: 404,
body: JSON.stringify({ message: 'Contact not found' }),
};
}

const listId = await acClient.getListIdByName(listName);

if (action == 'subscribe') {
const response = await acClient.addContactToList(contactId, listId);
return {
statusCode: 200,
body: JSON.stringify(response),
};
} else {
const response = await acClient.removeContactFromList(contactId, listId);
return {
statusCode: 200,
body: JSON.stringify(response),
};
}
} catch (error) {
console.error('Error:', error);
return {
statusCode: 500,
body: JSON.stringify({ message: 'Internal server error' }),
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type ListStatusPayload = {
readonly contactList: {
readonly list: string;
readonly list: number;
readonly contact: string;
readonly status: string;
readonly status: number;
};
};