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 1 commit 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { manageListSubscription } from '../../handlers/manageListSubscription';

// remove .skip to run the test, be aware it does real API calls
describe('manage list subscription', () => {
Comment on lines +3 to +4
Copy link
Collaborator

Choose a reason for hiding this comment

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

What skip?

Suggested change
// remove .skip to run the test, be aware it does real API calls
describe('manage list subscription', () => {
// remove .skip to run the test, be aware it does real API calls
describe.skip('manage list subscription', () => {

const cognitoUserId = '66ae52a0-f051-7080-04a1-465b3a4f44cc';
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the proper name of this constant cognitoId, cognitoUserId or username?

const listName = 'test-webinar-1732097286071';

it('should subscriber the contact to the list', async () => {
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
it('should subscriber the contact to the list', async () => {
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
@@ -0,0 +1,40 @@
import { APIGatewayProxyResult } from 'aws-lambda';
import { acClient } from '../utils/activeCampaignClient';

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

Choose a reason for hiding this comment

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

what is the proper name of this attribute cognitoId, cognitoUserId or username?

listName: string,
action: 'subscribe' | 'unsubscribe'
): Promise<APIGatewayProxyResult> {
try {
const contactId = await acClient.getContactByCognitoId(username);
if (!contactId) {
return {
statusCode: 404,
body: JSON.stringify({ message: 'Contact not found' }),
};
}

const listId = await acClient.getListIdByName(listName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if the listId does not exist?


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
Expand Up @@ -111,6 +111,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
Loading