-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEV-1614] Add bulk import of contacts (#1273)
* 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
1 parent
2345af9
commit 4e4933d
Showing
7 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"active-campaign-client": patch | ||
--- | ||
|
||
Add bulk import of contacts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/active-campaign-client/src/__tests__/helpers/bulkAddContactToList.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/active-campaign-client/src/helpers/bulkAddContactsToLists.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }), | ||
}; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/active-campaign-client/src/helpers/fetchSubscribedWebinarsFromDynamo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
packages/active-campaign-client/src/types/bulkAddContactPayload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }[]; | ||
}[]; | ||
}; |