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-1614] Add bulk import of contacts #1273

Merged
merged 23 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4984a2a
sync user wip
petros-double-test1 Dec 5, 2024
33802ef
sync user
petros-double-test1 Dec 5, 2024
c432ad4
changeset
petros-double-test1 Dec 5, 2024
fd01f78
Merge branch 'main' into dev-2017
MarcoPonchia Dec 6, 2024
5865dc1
Update .changeset/cuddly-cats-retire.md
tommaso1 Dec 6, 2024
b1b8180
Update packages/active-campaign-client/src/index.ts
tommaso1 Dec 6, 2024
6ec2fc2
Update packages/active-campaign-client/src/index.ts
tommaso1 Dec 6, 2024
b102553
Update packages/active-campaign-client/src/helpers/resyncUser.ts
tommaso1 Dec 6, 2024
3a7569d
pr comments
petros-double-test1 Dec 6, 2024
941f3a7
bulk add contact
petros-double-test1 Dec 10, 2024
cdbf4cc
bulk add contact
petros-double-test1 Dec 10, 2024
210f144
resync user
petros-double-test1 Dec 10, 2024
53f3ea1
changeset
petros-double-test1 Dec 10, 2024
49f2320
Update packages/active-campaign-client/src/clients/activeCampaignClie…
tommaso1 Dec 16, 2024
29e7407
Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts
tommaso1 Dec 16, 2024
2e93e4c
pr changes
petros-double-test1 Dec 16, 2024
997e3ad
Merge branch 'dev-1614' of github.com:pagopa/developer-portal into de…
petros-double-test1 Dec 16, 2024
e1949e6
Update packages/active-campaign-client/src/helpers/fetchSubscribedWeb…
tommaso1 Dec 17, 2024
a082a7b
Update packages/active-campaign-client/.env.example
tommaso1 Dec 17, 2024
696fab5
Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts
tommaso1 Dec 17, 2024
2874681
pr changes
petros-double-test1 Dec 18, 2024
3808be8
Merge remote-tracking branch 'origin/main' into dev-1614
petros-double-test1 Dec 18, 2024
f3dd9a4
Merge branch 'main' into dev-1614
MarcoPonchia Dec 19, 2024
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/cuddly-cats-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"active-campaign-client": minor
---

Add resync user handler
Copy link
Collaborator

Choose a reason for hiding this comment

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

This changeset file is duplicated from previous PR should be deleted

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
tommaso1 marked this conversation as resolved.
Show resolved Hide resolved
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
68 changes: 68 additions & 0 deletions packages/active-campaign-client/src/handlers/resyncUserHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { deleteContact } from '../helpers/deleteContact';
import { getUserFromCognitoByUsername } from '../helpers/getUserFromCognito';
import { fetchSubscribedWebinarsFromDynamo } from '../helpers/fetchSubscribedWebinarsFromDynamo';
import { addContact } from '../helpers/addContact';
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda';
import { addContactToList } from '../helpers/manageListSubscription';
import { queueEventParser } from '../helpers/queueEventParser';
import { acClient } from '../clients/activeCampaignClient';
import { bulkAddContactToList } from '../helpers/bulkAddContactsToLists';

export async function resyncUserHandler(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
try {
const queueEvent = queueEventParser(event);
const cognitoId = queueEvent.detail.additionalEventData.sub;
const deletionResult = await deleteContact(cognitoId);
if (deletionResult.statusCode != 200 && deletionResult.statusCode != 404) {
// eslint-disable-next-line functional/no-throw-statements
throw new Error('Error adding contact');
}

const user = await getUserFromCognitoByUsername(cognitoId);

if (!user) {
console.log(`User: ${cognitoId} not present on Cognito, sync done.`);
tommaso1 marked this conversation as resolved.
Show resolved Hide resolved
return {
statusCode: 200,
body: JSON.stringify({
message: 'User not present on Cognito, sync done.',
}),
};
}

const userWebinarsSubscriptions = await fetchSubscribedWebinarsFromDynamo(
cognitoId
);

const webinarIds = JSON.parse(userWebinarsSubscriptions.body)
.map(
(webinar: { readonly webinarId: { readonly S: string } }) =>
webinar?.webinarId?.S
)
.filter(Boolean);

console.log('Webinar IDs:', webinarIds); // TODO: Remove after testing

const webinarListIds = await Promise.all(
webinarIds.map(async (webinarId: string) => {
const listId = await acClient.getListIdByName(webinarId);
return listId;
})
MarcoPonchia marked this conversation as resolved.
Show resolved Hide resolved
);

const res = await bulkAddContactToList([user], [webinarListIds]);


return {
statusCode: 200,
body: JSON.stringify({ message: 'User resynced' }),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ message: error }),
};
}
}
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,30 @@
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);
marcobottaro marked this conversation as resolved.
Show resolved Hide resolved
return {
statusCode: 200,
body: JSON.stringify(response.Items),
};
} catch (error) {
console.error('Error querying items by username:', error);
tommaso1 marked this conversation as resolved.
Show resolved Hide resolved
return {
statusCode: 500,
body: JSON.stringify({ message: 'Internal server error' }),
};
}
}
16 changes: 11 additions & 5 deletions packages/active-campaign-client/src/helpers/getUserFromCognito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import { QueueEvent } from '../types/queueEvent';
import { listUsersCommandOutputToUser } from './listUsersCommandOutputToUser';

export async function getUserFromCognito(queueEvent: QueueEvent) {
const username = queueEvent.detail.additionalEventData.sub;
const user = await getUserFromCognitoByUsername(username);
if (!user) {
// eslint-disable-next-line functional/no-throw-statements
throw new Error('User not found');
}
return user;
}

export async function getUserFromCognitoByUsername(username: string) {
const command = new ListUsersCommand({
UserPoolId: process.env.COGNITO_USER_POOL_ID,
Filter: `username = "${queueEvent.detail.additionalEventData.sub}"`,
Filter: `username = "${username}"`,
});
const listUsersCommandOutput = await cognitoClient.send(command);
const user = listUsersCommandOutputToUser(listUsersCommandOutput);
if (!user) {
// eslint-disable-next-line functional/no-throw-statements
throw new Error('User not found');
}
console.log('User:', JSON.stringify(user, null, 2)); // TODO: Remove after testing
return user;
}
7 changes: 7 additions & 0 deletions packages/active-campaign-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { SQSEvent } from 'aws-lambda';
import { sqsQueueHandler } from './handlers/sqsQueueHandler';
import { resyncUserHandler } from './handlers/resyncUserHandler';

export async function sqsQueue(event: {
readonly Records: SQSEvent['Records'];
}) {
return await sqsQueueHandler(event);
}

export async function resyncQueue(event: {
readonly Records: SQSEvent['Records'];
}) {
return await resyncUserHandler(event);
}
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 }[];
}[];
};
Loading