Skip to content

Commit

Permalink
[DEV-1614] Add bulk import of contacts (#1273)
Browse files Browse the repository at this point in the history
* sync user wip

* sync user

* changeset

* Update .changeset/cuddly-cats-retire.md

Co-authored-by: Marco Ponchia <[email protected]>

* Update packages/active-campaign-client/src/index.ts

Co-authored-by: Marco Ponchia <[email protected]>

* Update packages/active-campaign-client/src/index.ts

Co-authored-by: Marco Ponchia <[email protected]>

* Update packages/active-campaign-client/src/helpers/resyncUser.ts

Co-authored-by: Marco Ponchia <[email protected]>

* pr comments

* bulk add contact

* bulk add contact

* resync user

* changeset

* Update packages/active-campaign-client/src/clients/activeCampaignClient.ts

Co-authored-by: Marco Ponchia <[email protected]>

* Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts

Co-authored-by: Marco Ponchia <[email protected]>

* pr changes

* Update packages/active-campaign-client/src/helpers/fetchSubscribedWebinarsFromDynamo.ts

Co-authored-by: Marco Ponchia <[email protected]>

* Update packages/active-campaign-client/.env.example

Co-authored-by: marcobottaro <[email protected]>

* Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts

Co-authored-by: Marco Ponchia <[email protected]>

* pr changes

---------

Co-authored-by: t <[email protected]>
Co-authored-by: Marco Ponchia <[email protected]>
Co-authored-by: marcobottaro <[email protected]>
  • Loading branch information
4 people authored Dec 19, 2024
1 parent 2345af9 commit 4e4933d
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/loud-otters-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"active-campaign-client": patch
---

Add bulk import of contacts
1 change: 1 addition & 0 deletions packages/active-campaign-client/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ COGNITO_USER_ID=66ae52a0-f051-7080-04a1-465b3a4f44cc
LIST_NAME=test-webinar-1732097286071
AC_BASE_URL_PARAM='/ac/base_url'
AC_API_KEY_PARAM='/ac/api_key'
TEST_AC_LIST_ID=28
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { bulkAddContactToList } from '../../helpers/bulkAddContactsToLists';
import { User } from '../../types/user';

const user: User = {
username: '466e0280-9061-7007-c3e0-beb6be672f68',
email: `test@example${new Date().getTime()}e.com`,
given_name: 'Giovanni',
family_name: 'Doe',
'custom:mailinglist_accepted': 'true',
'custom:company_type': 'Test Co',
'custom:job_role': 'Developer',
};

describe.skip('Active campaign integration contact flow', () => {
it('should bulk add contacts to a list', async () => {
const response = await bulkAddContactToList(
[user],
[[Number(process.env.TEST_AC_LIST_ID)]]
);
expect(response.statusCode).toBe(200);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm';
import { ContactPayload } from '../types/contactPayload';
import { ListPayload } from '../types/listPayload';
import { ListStatusPayload } from '../types/listStatusPayload';
import { BulkAddContactPayload } from '../types/bulkAddContactPayload';

async function getParameter(
paramName: string,
Expand Down Expand Up @@ -43,7 +44,11 @@ export class ActiveCampaignClient {
private async makeRequest<T>(
method: string,
path: string,
data?: ContactPayload | ListPayload | ListStatusPayload,
data?:
| ContactPayload
| ListPayload
| ListStatusPayload
| BulkAddContactPayload,
params?: Record<string, string>
): Promise<T> {
const [apiKey, baseUrl] = await Promise.all([
Expand Down Expand Up @@ -137,6 +142,29 @@ export class ActiveCampaignClient {
return this.makeRequest('DELETE', `/api/3/lists/${id}`);
}

async bulkAddContactToList(
contacts: readonly (ContactPayload & {
readonly listIds: readonly number[];
})[]
) {
const body = {
contacts: contacts.map((payload) => ({
email: payload.contact.email,
first_name: payload.contact.firstName,
last_name: payload.contact.lastName,
phone: payload.contact.phone,
customer_acct_name: payload.contact.lastName,
fields: payload.contact.fieldValues.map((field) => ({
id: Number(field.field),
value: field.value,
})),
subscribe: payload.listIds.map((listId) => ({ listid: listId })),
})),
};

return this.makeRequest('POST', `/api/3/import/bulk_import`, body);
}

async addContactToList(contactId: string, listId: number) {
return this.makeRequest('POST', `/api/3/contactLists`, {
contactList: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { APIGatewayProxyResult } from 'aws-lambda';
import { acClient } from '../clients/activeCampaignClient';
import { ContactPayload } from '../types/contactPayload';
import { User } from '../types/user';

export async function bulkAddContactToList(
users: readonly User[],
listIds: readonly (readonly number[])[]
): Promise<APIGatewayProxyResult> {
try {
// Transform to AC payload
const acPayload: readonly (ContactPayload & {
readonly listIds: readonly number[];
})[] = users.map((user, index) => ({
contact: {
email: user.email,
firstName: user.given_name,
lastName: user.family_name,
phone: `cognito:${user.username}`,
fieldValues: [
{
field: '2',
value: user['custom:company_type'],
},
{
field: '1',
value: user['custom:job_role'],
},
{
field: '3',
value:
user['custom:mailinglist_accepted'] === 'true' ? 'TRUE' : 'FALSE',
},
],
},
listIds: listIds[index],
}));

const response = await acClient.bulkAddContactToList(acPayload);

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
@@ -0,0 +1,29 @@
import { DynamoDBClient, QueryCommand } from '@aws-sdk/client-dynamodb';
import { APIGatewayProxyResult } from 'aws-lambda';

export async function fetchSubscribedWebinarsFromDynamo(
username: string
): Promise<APIGatewayProxyResult> {
try {
const dynamoClient = new DynamoDBClient({ region: process.env.AWS_REGION });
const command = new QueryCommand({
TableName: process.env.DYNAMO_WEBINARS_TABLE_NAME,
KeyConditionExpression: 'username = :username',
ExpressionAttributeValues: {
':username': { S: username },
},
});

const response = await dynamoClient.send(command);
console.log('getWebinarSubscriptions', response);
return {
statusCode: 200,
body: JSON.stringify(response.Items),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ message: 'Internal server error' }),
};
}
}
11 changes: 11 additions & 0 deletions packages/active-campaign-client/src/types/bulkAddContactPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type BulkAddContactPayload = {
readonly contacts: readonly {
readonly email: string;
readonly first_name: string;
readonly last_name: string;
readonly phone: string | undefined;
readonly customer_acct_name: string;
readonly fields: readonly { readonly id: number; readonly value: string }[];
readonly subscribe: readonly { readonly listid: number }[];
}[];
};

0 comments on commit 4e4933d

Please sign in to comment.