Skip to content

Commit

Permalink
[DEV-2016] Connect cognito event to active campaign (#1253)
Browse files Browse the repository at this point in the history
* connect add contant to AC

* update and delete

* Add test and refactor handlers

* Refactor get user from cognito

* Fix active campaing client

* Update packages/active-campaign-client/src/__tests__/helpers/deleteContact.test.ts

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

* Update packages/active-campaign-client/src/__tests__/helpers/updateContact.test.ts

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

* Update packages/active-campaign-client/src/__tests__/helpers/listUsersCommandOutputToUser.test.ts

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

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

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

* Unify AC integration tests

* Merge tests

* Add changeset

---------

Co-authored-by: t <[email protected]>
Co-authored-by: tommaso1 <[email protected]>
Co-authored-by: marcobottaro <[email protected]>
  • Loading branch information
4 people authored Nov 27, 2024
1 parent 7fe96a6 commit c6a3f41
Show file tree
Hide file tree
Showing 26 changed files with 1,500 additions and 261 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-wombats-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"active-campaign-client": minor
---

Refactor active campaign pacakge and create index file
1,155 changes: 1,153 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/active-campaign-client/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
AC_BASE_URL=your_account_url
AC_API_KEY=your_api_key
SENDER_URL=localhost:3000
AWS_REGION="region"
AWS_USER_POOL_ID="region_DFWF81fRa"
6 changes: 4 additions & 2 deletions packages/active-campaign-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"description": "Implements ActiveCampaign API to add, update and delete Accounts and to add and update lists",
"scripts": {
"lint": "eslint src",
"test": "jest"
"test": "jest",
"build": "tsc"
},
"devDependencies": {
"@aws-sdk/client-cognito-identity-provider": "^3.696.0",
"@eslint/eslintrc": "^2.1.4",
"@eslint/js": "^8.57.1",
"eslint": "^8.57.1",
"dotenv": "16.4.5",
"eslint": "^8.57.1",
"eslint-config-custom": "^0.1.0"
},
"dependencies": {
Expand Down
43 changes: 43 additions & 0 deletions packages/active-campaign-client/src/__tests__/contact_flow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { addContact } from '../helpers/addContact';
import { deleteContact } from '../helpers/deleteContact';
import { updateContact } from '../helpers/updateContact';
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',
};

// remove .skip to run the test, be aware it does a real API call so it will create, update and delete a contact in the active campaign account
// NB: this test is not idempotent and it must run in this order
describe.skip('Active campaign integration contact flow', () => {
it('should create a contact successfully', async () => {
const response = await addContact(user);
expect(response.statusCode).toBe(200);
});

it('should update a contact successfully', async () => {
const updatedUser: User = {
...user,
email: `test@example${new Date().getTime()}e.com`,
given_name: 'new given_name',
family_name: 'new family_name',
'custom:mailinglist_accepted': 'true',
'custom:company_type': 'new company_type',
'custom:job_role': 'new job_role',
};

const response = await updateContact(updatedUser);
expect(response.statusCode).toBe(200);
});

it('should delete a contact successfully', async () => {
const response = await deleteContact(user.username);
expect(response.statusCode).toBe(200);
});
});

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createList } from '../../handlers/createList';
import { createList } from '../../helpers/createList';
import { SQSEvent } from 'aws-lambda';

describe.skip('createList handler', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deleteList } from '../../handlers/deleteList';
import { deleteList } from '../../helpers/deleteList';
import { SQSEvent } from 'aws-lambda';

describe.skip('deleteList handler', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { ListUsersCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
import { listUsersCommandOutputToUser } from '../../helpers/listUsersCommandOutputToUser';
import { User } from '../../types/user';

const listUsersCommandOutput: ListUsersCommandOutput = {
$metadata: {
httpStatusCode: 200,
requestId: 'c67ec280-799a-40d6-b398-2a2b31aefbbd',
attempts: 1,
totalRetryDelay: 0,
},
Users: [
{
Attributes: [
{
Name: 'email',
Value: '[email protected]',
},
{
Name: 'email_verified',
Value: 'true',
},
{
Name: 'family_name',
Value: 'Doe',
},
{
Name: 'given_name',
Value: 'Giovanni',
},
{
Name: 'custom:mailinglist_accepted',
Value: 'true',
},
{
Name: 'custom:user_preferences',
Value:
'{"subscribedWebinarSlugs":["comunicazioni-a-valore-legale","nuove-api-io","live","cooming-soon","test","always-live","PagoPA-multe"]}',
},
{
Name: 'custom:company_type',
Value: 'gestore-di-pubblico-servizio',
},
{
Name: 'custom:job_role',
Value: 'Developer',
},
{
Name: 'custom:privacy_accepted',
Value: 'true',
},
{
Name: 'sub',
Value: 'c67ec280-799a-40d6-b398-2a2b31aefbbd',
},
],
Enabled: true,
UserStatus: 'CONFIRMED',
Username: 'c67ec280-799a-40d6-b398-2a2b31aefbbd',
},
],
};

describe('addContact handler', () => {
it('should properly convert ListUsersCommandOutput to User', async () => {
const user = listUsersCommandOutputToUser(listUsersCommandOutput);
const expectedUser: User = {
username: 'c67ec280-799a-40d6-b398-2a2b31aefbbd',
email: '[email protected]',
given_name: 'Giovanni',
family_name: 'Doe',
'custom:mailinglist_accepted': 'true',
'custom:company_type': 'gestore-di-pubblico-servizio',
'custom:job_role': 'Developer',
};
expect(user).toBeDefined();
Object.keys(expectedUser).forEach((value) => {
const key = value as keyof User;
expect(user![key]).toBe(expectedUser[key]);

Check warning on line 79 in packages/active-campaign-client/src/__tests__/helpers/listUsersCommandOutputToUser.test.ts

View workflow job for this annotation

GitHub Actions / Update CHANGELOG

Forbidden non-null assertion
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ActiveCampaignClient {
private async makeRequest<T>(
method: string,
path: string,
data?: any,
data?: ContactPayload | ListPayload,
params?: Record<string, string>
): Promise<T> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -114,8 +114,6 @@ export class ActiveCampaignClient {
}

export const acClient = new ActiveCampaignClient(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
process.env.AC_BASE_URL!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
process.env.AC_API_KEY!
process.env.AC_BASE_URL || '',
process.env.AC_API_KEY || ''
);
5 changes: 5 additions & 0 deletions packages/active-campaign-client/src/clients/cognitoClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';

export const cognitoClient = new CognitoIdentityProviderClient({
region: process.env.AWS_REGION,
});
52 changes: 0 additions & 52 deletions packages/active-campaign-client/src/handlers/addContact.ts

This file was deleted.

Loading

0 comments on commit c6a3f41

Please sign in to comment.