Skip to content

Commit

Permalink
[DEV-2026] Refactor Active Campaign package (#1252)
Browse files Browse the repository at this point in the history
* refactor

* refactor

* Removed axios, removed .env except than for the test, create new main handler to execute function in aws lambda

* Update packages/active-campaign-client/jest.config.ts

---------

Co-authored-by: t <[email protected]>
Co-authored-by: Marco Ponchia <[email protected]>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent ba37f09 commit e725bb1
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 123 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-trains-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"active-campaign-client": patch
---

Removed axios, removed .env except than for the test, create new main handler to execute function in aws lambda
16 changes: 4 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/active-campaign-client/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import * as dotenv from 'dotenv';

// Load environment variables from .env file
dotenv.config({ path: '.env' });

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
Expand Down
3 changes: 1 addition & 2 deletions packages/active-campaign-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
"@eslint/eslintrc": "^2.1.4",
"@eslint/js": "^8.57.1",
"eslint": "^8.57.1",
"dotenv": "16.4.5",
"eslint-config-custom": "^0.1.0"
},
"dependencies": {
"@types/aws-lambda": "^8.10.126",
"@types/jest": "^29.5.1",
"aws-lambda": "^1.0.7",
"axios": "^1.7.7",
"dotenv": "16.4.5",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.0.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handler } from '../../handlers/addContact';
import { addContact } from '../../handlers/addContact';
import { SQSEvent } from 'aws-lambda';

// remove .skip to run the test, be aware it does a real API call so it will create a contact in the active campaign account
Expand All @@ -12,6 +12,7 @@ describe.skip('addContact handler', () => {
body: JSON.stringify({
username: `test@example${new Date().getTime()}e.com`,
firstName: 'Giovanni',
cognitoId: '466e0280-9061-7007-c3e0-beb6be672f68',
lastName: 'Doe',
company: 'Test Co',
role: 'Developer',
Expand All @@ -32,7 +33,7 @@ describe.skip('addContact handler', () => {
],
};

const response = await handler(event);
const response = await addContact(event);
expect(response.statusCode).toBe(200);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handler } from '../../handlers/createList';
import { createList } from '../../handlers/createList';
import { SQSEvent } from 'aws-lambda';

describe.skip('createList handler', () => {
Expand Down Expand Up @@ -31,7 +31,7 @@ describe.skip('createList handler', () => {
],
};

const response = await handler(event);
const response = await createList(event);
expect(response.statusCode).toBe(200);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handler } from '../../handlers/deleteContact';
import { deleteContact } from '../../handlers/deleteContact';
import { SQSEvent } from 'aws-lambda';

// remove .skip to run the test, be aware it does a real API call so it will create a contact in the active campaign account
Expand All @@ -11,7 +11,7 @@ describe.skip('deleteContact handler', () => {
receiptHandle: '1',
body: JSON.stringify({
// Replace this with the existing email of the contact you want to delete, otherwise the test will fail
username: `[email protected]`,
cognitoId: `466e0280-9061-7007-c3e0-beb6be672f68`,
}),
attributes: {
ApproximateReceiveCount: '1',
Expand All @@ -28,7 +28,7 @@ describe.skip('deleteContact handler', () => {
],
};

const response = await handler(event);
const response = await deleteContact(event);
expect(response.statusCode).toBe(200);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handler } from '../../handlers/deleteList';
import { deleteList } from '../../handlers/deleteList';
import { SQSEvent } from 'aws-lambda';

describe.skip('deleteList handler', () => {
Expand All @@ -25,7 +25,7 @@ describe.skip('deleteList handler', () => {
},
],
};
const response = await handler(event);
const response = await deleteList(event);
expect(response.statusCode).toBe(200);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handler } from '../../handlers/updateContact';
import { updateContact } from '../../handlers/updateContact';
import { SQSEvent } from 'aws-lambda';

// remove .skip to run the test, be aware it does a real API call so it will create a contact in the active campaign account
Expand All @@ -16,6 +16,7 @@ describe.skip('updateContact handler', () => {
company: 'Test Co',
role: 'Developer',
mailinglistAccepted: true,
cognitoId: '466e0280-9061-7007-c3e0-beb6be672f68',
}),
attributes: {
ApproximateReceiveCount: '1',
Expand All @@ -32,7 +33,7 @@ describe.skip('updateContact handler', () => {
],
};

const response = await handler(event);
const response = await updateContact(event);
expect(response.statusCode).toBe(200);
});
});
82 changes: 0 additions & 82 deletions packages/active-campaign-client/src/activeCampaignClient.ts

This file was deleted.

8 changes: 5 additions & 3 deletions packages/active-campaign-client/src/handlers/addContact.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda';
import { acClient } from '../activeCampaignClient';
import { acClient } from '../utils/activeCampaignClient';
import { SignUpUserData } from 'nextjs-website/src/lib/types/sign-up';
import { ContactPayload } from '../types/contactPayload';

export async function handler(event: {
export async function addContact(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
try {
const firstMessage = event.Records[0] ?? { body: '{}' };
// Parse request body
const userData: SignUpUserData = JSON.parse(firstMessage.body);
const userData: SignUpUserData & { readonly cognitoId: string } =
JSON.parse(firstMessage.body);

// Transform to AC payload
const acPayload: ContactPayload = {
contact: {
email: userData.username,
firstName: userData.firstName,
lastName: userData.lastName,
phone: `cognito:${userData.cognitoId}`,
fieldValues: [
{
field: '2',
Expand Down
8 changes: 4 additions & 4 deletions packages/active-campaign-client/src/handlers/createList.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda';
import { acClient } from '../activeCampaignClient';
import { Webinar } from 'nextjs-website/src/lib/types/webinar';
import { acClient } from '../utils/activeCampaignClient';
import { ListPayload } from '../types/listPayload';
import { WebinarPayload } from '../types/webinarPayload';

export async function handler(event: {
export async function createList(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
try {
const firstMessage = event.Records[0] ?? { body: '{}' };
const webinarData: Webinar = JSON.parse(firstMessage.body);
const webinarData: WebinarPayload = JSON.parse(firstMessage.body);

const acPayload: ListPayload = {
list: {
Expand Down
8 changes: 4 additions & 4 deletions packages/active-campaign-client/src/handlers/deleteContact.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda';
import { acClient } from '../activeCampaignClient';
import { acClient } from '../utils/activeCampaignClient';

export async function handler(event: {
export async function deleteContact(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
try {
const firstMessage = event.Records[0] ?? { body: '{}' };
// Parse request body
const { username } = JSON.parse(firstMessage.body);
const { cognitoId } = JSON.parse(firstMessage.body);

const contactId = await acClient.getContactByEmail(username);
const contactId = await acClient.getContactByCognitoId(cognitoId);

if (!contactId) {
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/active-campaign-client/src/handlers/deleteList.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda';
import { acClient } from '../activeCampaignClient';
import { acClient } from '../utils/activeCampaignClient';

export async function handler(event: {
export async function deleteList(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
try {
Expand Down
6 changes: 3 additions & 3 deletions packages/active-campaign-client/src/handlers/updateContact.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda';
import { acClient } from '../activeCampaignClient';
import { acClient } from '../utils/activeCampaignClient';
import { ContactPayload } from '../types/contactPayload';

export async function handler(event: {
export async function updateContact(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
try {
Expand All @@ -17,7 +17,7 @@ export async function handler(event: {
};
}

const contactId = await acClient.getContactByEmail(userData.username);
const contactId = await acClient.getContactByCognitoId(userData.cognitoId);

if (!contactId) {
return {
Expand Down
12 changes: 12 additions & 0 deletions packages/active-campaign-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda';
import { addContact } from './handlers/addContact';

Check warning on line 2 in packages/active-campaign-client/src/index.ts

View workflow job for this annotation

GitHub Actions / Update CHANGELOG

'addContact' is defined but never used

export async function handler(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
console.log('Event:', event);
return {
statusCode: 200,
body: JSON.stringify(event),
};
}
5 changes: 5 additions & 0 deletions packages/active-campaign-client/src/types/webinarPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type WebinarPayload = {
readonly slug: string;
readonly title: string;
readonly subscribeCtaLabel: string;
};
Loading

0 comments on commit e725bb1

Please sign in to comment.