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-2073] Add sync users script #1286

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 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
d86cd54
add makeContactPayload
MarcoPonchia Dec 19, 2024
f3846c8
Add types
MarcoPonchia Dec 19, 2024
466b1a9
refactor AC client
MarcoPonchia Dec 19, 2024
7ea308c
add getListByName return type
MarcoPonchia Dec 19, 2024
ab13428
refactor getListIdByName and add error manage to sqsQueueHandler
MarcoPonchia Dec 19, 2024
d917094
Add addOrUpdateContact helper
MarcoPonchia Dec 19, 2024
06b99b1
add getNewWebinarsAndUnsubsriptionLists
MarcoPonchia Dec 19, 2024
e58012d
add addArrayOfListToContact and removeArrayOfListFromContact helpers
MarcoPonchia Dec 19, 2024
7c73575
Update resync user handlers
MarcoPonchia Dec 19, 2024
3d705b2
Fix manageError
MarcoPonchia Dec 19, 2024
7837078
Merge remote-tracking branch 'origin/dev-1614' into refactor-resync-e…
MarcoPonchia Dec 19, 2024
3d70311
Fix after merge
MarcoPonchia Dec 19, 2024
a3b8052
add changeset
MarcoPonchia Dec 19, 2024
599506e
Merge remote-tracking branch 'origin/main' into refactor-resync-error…
MarcoPonchia Dec 19, 2024
2416aba
Fix list ac client call
MarcoPonchia Dec 19, 2024
8af9e78
Merge branch 'main' into refactor-resync-error-handler
MarcoPonchia Dec 19, 2024
2477350
Refactor env var for test
MarcoPonchia Dec 19, 2024
a3d4bed
fix make payload
MarcoPonchia Dec 19, 2024
294c2b9
fix resync function
MarcoPonchia Dec 19, 2024
55eabb9
add sync user script
MarcoPonchia Dec 19, 2024
cd19ea4
add changeset
MarcoPonchia Dec 19, 2024
7d8eeef
Merge remote-tracking branch 'origin/main' into add-sync-users-script
MarcoPonchia Dec 20, 2024
62a6f6b
add env
MarcoPonchia Dec 20, 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/heavy-points-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"active-campaign-client": minor
---

Add sync users script
172 changes: 172 additions & 0 deletions packages/active-campaign-client/src/scripts/sync_all_users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// This script is only ment to be launched manually to sync all users from Cognito to ActiveCampaign
/* eslint-disable */
import { ActiveCampaignClient } from '../clients/activeCampaignClient';
import { CognitoIdentityServiceProvider } from 'aws-sdk';
import { DynamoDBClient, QueryCommand } from '@aws-sdk/client-dynamodb';
import { ContactPayload } from '../types/contactPayload';
import * as dotenv from 'dotenv';

const activeCampaignClient = new ActiveCampaignClient(
process.env.TEST_AC_BASE_URL,
process.env.TEST_AC_API_KEY
);

const userPoolId = () => process.env.COGNITO_USER_POOL_ID!;

// Initialize Cognito client
const cognito = new CognitoIdentityServiceProvider({
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
});

export async function getSubscribedWebinars(username: string): Promise<any[]> {
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);
return response.Items as any[];
} catch (error) {
console.error('Error querying items by username:', error);
return [];
}
}

// Load all users from Cognito
async function listAllUsers() {
try {
const allUsers = [];
let paginationToken = undefined;

do {
const params: any = {
UserPoolId: userPoolId(),
PaginationToken: paginationToken,
};

const response = await cognito.listUsers(params).promise();
if (response.Users) {
allUsers.push(...response.Users);
}
paginationToken = response.PaginationToken;
} while (paginationToken);

return allUsers;
} catch (error) {
console.error('Error fetching users:', error);
return [];
}
}

async function main() {
dotenv.config();
console.log(process.env);
const users = await listAllUsers();

console.log(process.env.DYNAMO_WEBINARS_TABLE_NAME);

const usersAndWebinars = [];

for (const user of users) {
if (!user.Username) {
continue;
}
const subscribedWebinars = await getSubscribedWebinars(user.Username);
usersAndWebinars.push({
user,
subscribedWebinars: subscribedWebinars
.map((webinar: any) => webinar?.webinarId?.S)
.filter(Boolean),
});
}

const allLists: any = await activeCampaignClient.getLists();

const webinarIdByName = allLists.lists.reduce((acc: any, list: any) => {
acc[list.name] = Number(list.id);
return acc;
}, {});

const emails = new Set();
// remove duplicates on emails
const uniqueUsersAndWebinars = usersAndWebinars.filter((userAndWebinars) => {
const email = userAndWebinars?.user?.Attributes?.find(
(attr: any) => attr.Name === 'email'
)?.Value;
if (emails.has(email)) {
return false;
}
emails.add(email);
return true;
});

// TODO: limit uniqueUsersAndWebinars to 10 remove after testing
const limitedUniqueUsersAndWebinars = uniqueUsersAndWebinars
.filter((userAndWebinars) => userAndWebinars.subscribedWebinars.length > 0)
.slice(0, 10);

const acPayload = limitedUniqueUsersAndWebinars.map(
(userAndWebinars, index) => {
const attributes = userAndWebinars.user.Attributes;
return {
contact: {
email: attributes?.find((attr: any) => attr.Name === 'email')?.Value,
firstName: attributes?.find((attr: any) => attr.Name === 'given_name')
?.Value,
lastName: attributes?.find((attr: any) => attr.Name === 'family_name')
?.Value,
phone: `cognito:${userAndWebinars.user.Username}`,
fieldValues: [
{
field: '2',
value: attributes?.find(
(attr: any) => attr.Name === 'custom:company_type'
)?.Value,
},
{
field: '1',
value: attributes?.find(
(attr: any) => attr.Name === 'custom:job_role'
)?.Value,
},
{
field: '3',
value:
attributes?.find(
(attr: any) => attr.Name === 'custom:mailinglist_accepted'
)?.Value === 'true'
? 'TRUE'
: 'FALSE',
},
],
},
listIds: userAndWebinars.subscribedWebinars
.map((webinar: any) => webinarIdByName[webinar])
.filter(Boolean) as unknown as number[],
};
}
) as unknown as readonly (ContactPayload & {
readonly listIds: readonly number[];
})[];

console.log(acPayload.filter((user: any) => user.listIds.length !== 0));

const response = await activeCampaignClient.bulkAddContactToList(acPayload);

console.log(response);
}

// Execute the main function
main().catch((error) => {
console.error('Error executing sync:', error);
process.exit(1);
});
Loading